1138 lines
27 KiB
Ruby
1138 lines
27 KiB
Ruby
# encoding: UTF-8
|
|
#==============================================================================
|
|
# Multiplayer Chat (T / E) + server-backed history (last 20)
|
|
#------------------------------------------------------------------------------
|
|
# Protocol (TCP lines):
|
|
# client -> chat:<text>
|
|
# client -> chat_last:<n>
|
|
# server -> chat_msg:<msg_id>:<ts_ms>:<player_id>:<nick>:<text>
|
|
# Notes:
|
|
# - <text> can contain ':' (client splits with limit)
|
|
# - nick uses WebSocket.esc/unesc (':' -> ';')
|
|
#==============================================================================
|
|
|
|
module WebSocket
|
|
# -------------------------
|
|
# Chat state
|
|
# -------------------------
|
|
@chat_messages = [] # [id, ts_ms, pid, nick, text]
|
|
@chat_ids = {}
|
|
|
|
def self.chat_messages
|
|
@chat_messages ||= []
|
|
end
|
|
|
|
def self.chat_clear
|
|
@chat_messages = []
|
|
@chat_ids = {}
|
|
end
|
|
|
|
def self.chat_add(msg_id, ts_ms, pid, nick, text)
|
|
@chat_messages ||= []
|
|
@chat_ids ||= {}
|
|
mid = msg_id.to_i
|
|
return if mid <= 0
|
|
return if @chat_ids[mid]
|
|
|
|
n = nick.to_s
|
|
t = text.to_s
|
|
@chat_messages << [mid, ts_ms.to_i, pid.to_i, n, t]
|
|
@chat_ids[mid] = true
|
|
|
|
# keep last 20 by msg_id
|
|
@chat_messages.sort_by! { |m| m[0] }
|
|
max_log = 20 # Print (задаем макс строк логирования)
|
|
if @chat_messages.size > max_log # Print 20
|
|
removed = @chat_messages[0, @chat_messages.size - max_log] # Print 20
|
|
removed.each { |m| @chat_ids.delete(m[0]) }
|
|
@chat_messages = @chat_messages.last(max_log) # Print 20
|
|
end
|
|
end
|
|
|
|
def self.chat_time_text(ts_ms)
|
|
raw = ts_ms.to_i
|
|
return "" if raw <= 0
|
|
Time.at(raw / 1000.0).strftime("%d.%m %H:%M")
|
|
rescue
|
|
""
|
|
end
|
|
|
|
def self.request_chat_last(n = 20) # Print 20
|
|
return unless @connected
|
|
max_log = 20 # Print (добавили макс значение)
|
|
n = n.to_i
|
|
n = max_log if n <= 0 # Print 20
|
|
n = max_log if n > max_log # Print 20
|
|
send_line("chat_last:#{n}")
|
|
end
|
|
|
|
def self.send_chat(text)
|
|
return unless @connected
|
|
t = text.to_s
|
|
t = t.gsub(/\r\n?/, " ").gsub("\n", " ")
|
|
t = t.gsub("\0", "")
|
|
t = t.strip
|
|
t = t[0, 200]
|
|
return if t.empty?
|
|
send_line("chat:#{t}")
|
|
end
|
|
|
|
# -------------------------
|
|
# Extend parser without editing core file
|
|
# -------------------------
|
|
class << self
|
|
alias mp_parse_without_chat parse
|
|
end
|
|
|
|
def self.parse(msg)
|
|
if msg.start_with?("chat_msg:")
|
|
parts = msg.split(":", 6)
|
|
if parts.size >= 6
|
|
msg_id = parts[1].to_i
|
|
ts_ms = parts[2].to_i
|
|
pid = parts[3].to_i
|
|
nick = unesc(parts[4].to_s)
|
|
text = parts[5].to_s
|
|
chat_add(msg_id, ts_ms, pid, nick, text)
|
|
end
|
|
return
|
|
end
|
|
mp_parse_without_chat(msg)
|
|
end
|
|
end
|
|
|
|
#==============================================================================
|
|
# Ruby 3 / mkxp-z keyboard helpers
|
|
# - avoid direct user32 calls
|
|
# - prefer mkxp-z Input.UTF8String for layout-aware text input
|
|
#==============================================================================
|
|
module MPKeyboard
|
|
VK_TO_SYMBOL = {
|
|
0x08 => :BACK,
|
|
0x09 => :TAB,
|
|
0x0D => :RETURN,
|
|
0x10 => :SHIFT,
|
|
0x11 => :CTRL,
|
|
0x12 => :ALT,
|
|
0x1B => :ESCAPE,
|
|
0x20 => :SPACE,
|
|
0x70 => :F1,
|
|
0x71 => :F2,
|
|
0x72 => :F3,
|
|
0x73 => :F4,
|
|
0x74 => :F5,
|
|
0x75 => :F6,
|
|
0x76 => :F7,
|
|
0x77 => :F8,
|
|
0x78 => :F9,
|
|
0x79 => :F10,
|
|
0x7A => :F11,
|
|
0x7B => :F12
|
|
}
|
|
|
|
LETTER_SYMBOLS = {}
|
|
('A'..'Z').each { |ch| LETTER_SYMBOLS[ch.ord] = ch.to_sym }
|
|
|
|
DIGIT_SYMBOLS = {
|
|
0x30 => :'0', 0x31 => :'1', 0x32 => :'2', 0x33 => :'3', 0x34 => :'4',
|
|
0x35 => :'5', 0x36 => :'6', 0x37 => :'7', 0x38 => :'8', 0x39 => :'9'
|
|
}
|
|
|
|
OEM_SYMBOLS = {
|
|
0xBA => :OEM_1,
|
|
0xBB => :OEM_PLUS,
|
|
0xBC => :OEM_COMMA,
|
|
0xBD => :OEM_MINUS,
|
|
0xBE => :OEM_PERIOD,
|
|
0xBF => :OEM_2,
|
|
0xC0 => :OEM_3,
|
|
0xDB => :OEM_4,
|
|
0xDC => :OEM_5,
|
|
0xDD => :OEM_6,
|
|
0xDE => :OEM_7
|
|
}
|
|
|
|
def self.symbol_for(vk)
|
|
VK_TO_SYMBOL[vk] || LETTER_SYMBOLS[vk] || DIGIT_SYMBOLS[vk] || OEM_SYMBOLS[vk]
|
|
end
|
|
|
|
def self.query(method_name, vk)
|
|
result = false
|
|
begin
|
|
result = Input.send(method_name, vk)
|
|
return !!result
|
|
rescue
|
|
end
|
|
|
|
sym = symbol_for(vk)
|
|
if !sym.nil?
|
|
begin
|
|
result = Input.send(method_name, sym)
|
|
return !!result
|
|
rescue
|
|
end
|
|
end
|
|
|
|
false
|
|
end
|
|
|
|
def self.trigger?(vk)
|
|
query(:trigger?, vk.to_i)
|
|
end
|
|
|
|
def self.press?(vk)
|
|
query(:press?, vk.to_i)
|
|
end
|
|
end
|
|
|
|
module MPTextInput
|
|
VK_BACK = 0x08
|
|
VK_RETURN = 0x0D
|
|
VK_ESCAPE = 0x1B
|
|
VK_CTRL = 0x11
|
|
VK_SHIFT = 0x10
|
|
VK_ALT = 0x12
|
|
INITIAL_REPEAT_DELAY = 22
|
|
REPEAT_INTERVAL = 3
|
|
|
|
# punctuation
|
|
VK_OEM_1 = 0xBA
|
|
VK_OEM_PLUS = 0xBB
|
|
VK_OEM_COMMA = 0xBC
|
|
VK_OEM_MINUS = 0xBD
|
|
VK_OEM_PERIOD = 0xBE
|
|
VK_OEM_2 = 0xBF
|
|
VK_OEM_3 = 0xC0
|
|
VK_OEM_4 = 0xDB
|
|
VK_OEM_5 = 0xDC
|
|
VK_OEM_6 = 0xDD
|
|
VK_OEM_7 = 0xDE
|
|
|
|
KEYS = (
|
|
(0x30..0x39).to_a +
|
|
(0x41..0x5A).to_a +
|
|
[0x20, VK_OEM_1, VK_OEM_PLUS, VK_OEM_COMMA, VK_OEM_MINUS, VK_OEM_PERIOD,
|
|
VK_OEM_2, VK_OEM_3, VK_OEM_4, VK_OEM_5, VK_OEM_6, VK_OEM_7]
|
|
)
|
|
|
|
ASCII_SHIFT_MAP = {
|
|
'1' => '!', '2' => '@', '3' => '#', '4' => '$', '5' => '%',
|
|
'6' => '^', '7' => '&', '8' => '*', '9' => '(', '0' => ')',
|
|
'-' => '_', '=' => '+', '[' => '{', ']' => '}', '\\' => '|',
|
|
';' => ':', "'" => '"', ',' => '<', '.' => '>', '/' => '?', '`' => '~'
|
|
}
|
|
|
|
ASCII_BASE_MAP = {
|
|
VK_OEM_1 => ';',
|
|
VK_OEM_PLUS => '=',
|
|
VK_OEM_COMMA => ',',
|
|
VK_OEM_MINUS => '-',
|
|
VK_OEM_PERIOD => '.',
|
|
VK_OEM_2 => '/',
|
|
VK_OEM_3 => '`',
|
|
VK_OEM_4 => '[',
|
|
VK_OEM_5 => '\\',
|
|
VK_OEM_6 => ']',
|
|
VK_OEM_7 => "'",
|
|
0x20 => ' '
|
|
}
|
|
|
|
def self.native_available?
|
|
Input.respond_to?(:UTF8String)
|
|
rescue
|
|
false
|
|
end
|
|
|
|
def self.sanitize_text(text)
|
|
value = text.to_s
|
|
begin
|
|
value = value.encode('UTF-8', 'UTF-8', :invalid => :replace, :undef => :replace, :replace => '')
|
|
rescue
|
|
value = value.to_s
|
|
end
|
|
value = value.gsub(/
|
|
|
|
|
| |