497 lines
13 KiB
Ruby
497 lines
13 KiB
Ruby
# encoding: UTF-8
|
|
#==============================================================================
|
|
# Multiplayer Chat Channels
|
|
#------------------------------------------------------------------------------
|
|
# Channels:
|
|
# global - общий чат для всех игроков
|
|
# clan - чат только для участников текущего клана
|
|
# system - системные уведомления сервера и локальные подсказки
|
|
#==============================================================================
|
|
|
|
module MPChatChannels
|
|
CHANNELS = [
|
|
["global", "Глобальный"],
|
|
["clan", "Клановый"],
|
|
["system", "Системный"]
|
|
]
|
|
|
|
COLORS = {
|
|
"global" => [255, 213, 128],
|
|
"clan" => [120, 230, 150],
|
|
"system" => [255, 92, 92]
|
|
}
|
|
|
|
def self.normalize(channel)
|
|
ch = channel.to_s.strip.downcase
|
|
return "clan" if ch == "clan" || ch == "клан"
|
|
return "system" if ch == "system" || ch == "система"
|
|
"global"
|
|
rescue
|
|
"global"
|
|
end
|
|
|
|
def self.label(channel)
|
|
ch = normalize(channel)
|
|
row = CHANNELS.find { |pair| pair[0] == ch }
|
|
row ? row[1] : "Глобальный"
|
|
rescue
|
|
"Глобальный"
|
|
end
|
|
|
|
def self.color(channel)
|
|
rgb = COLORS[normalize(channel)] || COLORS["global"]
|
|
Color.new(rgb[0], rgb[1], rgb[2])
|
|
rescue
|
|
Color.new(255, 213, 128)
|
|
end
|
|
end
|
|
|
|
module WebSocket
|
|
def self.chat_channel_value(channel)
|
|
MPChatChannels.normalize(channel)
|
|
rescue
|
|
"global"
|
|
end
|
|
|
|
def self.legacy_chat_channel(pid, nick)
|
|
n = nick.to_s.strip.upcase
|
|
return "clan" if n == "CLAN" || n == "КЛАН"
|
|
return "system" if pid.to_i < 0 || n == "SERVER" || n == "[SERVER]" || n == "SYSTEM" || n == "СИСТЕМА"
|
|
"global"
|
|
rescue
|
|
"global"
|
|
end
|
|
|
|
def self.chat_messages(channel = nil)
|
|
@chat_messages ||= []
|
|
ch = chat_channel_value(channel || (defined?(MPChat) && MPChat.respond_to?(:current_channel) ? MPChat.current_channel : "global"))
|
|
@chat_messages.select { |m| chat_channel_value(m[5] || "global") == ch }
|
|
rescue
|
|
@chat_messages || []
|
|
end
|
|
|
|
def self.chat_clear
|
|
@chat_messages = []
|
|
@chat_ids = {}
|
|
end
|
|
|
|
def self.chat_add(msg_id, ts_ms, pid, nick, text, channel = nil)
|
|
@chat_messages ||= []
|
|
@chat_ids ||= {}
|
|
mid = msg_id.to_i
|
|
return if mid <= 0
|
|
ch = chat_channel_value(channel || legacy_chat_channel(pid, nick))
|
|
key = "#{ch}:#{mid}"
|
|
return if @chat_ids[key]
|
|
|
|
n = nick.to_s
|
|
t = text.to_s
|
|
@chat_messages << [mid, ts_ms.to_i, pid.to_i, n, t, ch]
|
|
@chat_ids[key] = true
|
|
|
|
max_log = 20
|
|
grouped = {}
|
|
@chat_messages.each do |m|
|
|
grouped[chat_channel_value(m[5] || "global")] ||= []
|
|
grouped[chat_channel_value(m[5] || "global")] << m
|
|
end
|
|
@chat_messages = []
|
|
@chat_ids = {}
|
|
grouped.each do |group_channel, list|
|
|
list.sort_by! { |m| m[0] }
|
|
list.last(max_log).each do |m|
|
|
@chat_messages << m
|
|
@chat_ids["#{group_channel}:#{m[0].to_i}"] = true
|
|
end
|
|
end
|
|
@chat_messages.sort_by! { |m| [chat_channel_value(m[5] || "global"), m[0].to_i] }
|
|
rescue
|
|
end
|
|
|
|
def self.request_chat_last(n = 20, channel = nil)
|
|
return unless @connected
|
|
if n.is_a?(String) || n.is_a?(Symbol)
|
|
channel = n
|
|
n = 20
|
|
end
|
|
max_log = 20
|
|
count = n.to_i
|
|
count = max_log if count <= 0 || count > max_log
|
|
ch = chat_channel_value(channel || (defined?(MPChat) && MPChat.respond_to?(:current_channel) ? MPChat.current_channel : "global"))
|
|
send_line("chat_last:#{ch}:#{count}")
|
|
rescue
|
|
end
|
|
|
|
def self.chat_local_system(text)
|
|
now = Time.now.to_i * 1000
|
|
chat_add(now + rand(1000), now, -1, "SYSTEM", text.to_s, "system")
|
|
rescue
|
|
end
|
|
|
|
def self.send_chat(text, channel = nil)
|
|
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?
|
|
|
|
begin
|
|
return if respond_to?(:handle_clan_chat_command) && handle_clan_chat_command(t)
|
|
rescue
|
|
end
|
|
|
|
ch = chat_channel_value(channel || (defined?(MPChat) && MPChat.respond_to?(:current_channel) ? MPChat.current_channel : "global"))
|
|
if ch == "system"
|
|
chat_local_system("Системный чат только показывает уведомления.")
|
|
elsif ch == "clan"
|
|
if respond_to?(:clan_member?) && !clan_member?
|
|
chat_local_system("Клановый чат доступен только участникам клана.")
|
|
else
|
|
send_line("chat_clan:#{t}")
|
|
end
|
|
else
|
|
send_line("chat_global:#{t}")
|
|
end
|
|
rescue
|
|
end
|
|
|
|
class << self
|
|
unless method_defined?(:mp_chat_channels_parse_without_channels)
|
|
alias mp_chat_channels_parse_without_channels parse
|
|
end
|
|
end
|
|
|
|
def self.parse(msg)
|
|
if msg.start_with?("chat_msg2:")
|
|
parts = msg.split(":", 7)
|
|
if parts.size >= 7
|
|
channel = chat_channel_value(parts[1])
|
|
msg_id = parts[2].to_i
|
|
ts_ms = parts[3].to_i
|
|
pid = parts[4].to_i
|
|
nick = unesc(parts[5].to_s)
|
|
text = parts[6].to_s
|
|
chat_add(msg_id, ts_ms, pid, nick, text, channel)
|
|
end
|
|
return
|
|
end
|
|
mp_chat_channels_parse_without_channels(msg)
|
|
end
|
|
end
|
|
|
|
module MPChat
|
|
TAB_H = 30
|
|
|
|
def self.current_channel
|
|
@chat_channel = MPChatChannels.normalize(@chat_channel || load_saved_channel || "global")
|
|
rescue
|
|
"global"
|
|
end
|
|
|
|
def self.current_channel_label
|
|
MPChatChannels.label(current_channel)
|
|
rescue
|
|
"Глобальный"
|
|
end
|
|
|
|
def self.set_channel(channel)
|
|
old = current_channel
|
|
@chat_channel = MPChatChannels.normalize(channel)
|
|
save_current_channel
|
|
if @chat_channel == "clan" && defined?(WebSocket) && WebSocket.respond_to?(:clan_member?) && !WebSocket.clan_member?
|
|
WebSocket.chat_local_system("Клановый чат доступен только участникам клана.") if WebSocket.respond_to?(:chat_local_system)
|
|
end
|
|
WebSocket.request_chat_last(20, @chat_channel) if defined?(WebSocket) && WebSocket.connected?
|
|
refresh_channel_windows
|
|
old != @chat_channel
|
|
rescue
|
|
false
|
|
end
|
|
|
|
def self.load_saved_channel
|
|
return nil unless defined?($LonaINI) && $LonaINI
|
|
value = nil
|
|
begin
|
|
value = $LonaINI["Online"]["ChatChannel"]
|
|
rescue
|
|
value = nil
|
|
end
|
|
MPChatChannels.normalize(value) unless value.nil? || value.to_s == ""
|
|
rescue
|
|
nil
|
|
end
|
|
|
|
def self.save_current_channel
|
|
return false unless defined?($LonaINI) && $LonaINI
|
|
begin
|
|
$LonaINI["Online"] ||= {}
|
|
$LonaINI["Online"]["ChatChannel"] = @chat_channel.to_s
|
|
$LonaINI.save if $LonaINI.respond_to?(:save)
|
|
true
|
|
rescue
|
|
false
|
|
end
|
|
end
|
|
|
|
def self.next_channel
|
|
current = current_channel
|
|
keys = MPChatChannels::CHANNELS.map { |pair| pair[0] }
|
|
idx = keys.index(current)
|
|
idx = 0 if idx.nil?
|
|
keys[(idx + 1) % keys.length]
|
|
rescue
|
|
"global"
|
|
end
|
|
|
|
def self.channel_hotkey
|
|
return next_channel if MPKeyboard.trigger?(0x09) # TAB
|
|
nil
|
|
rescue
|
|
nil
|
|
end
|
|
|
|
def self.refresh_channel_windows
|
|
@tabs_window.refresh if @tabs_window && !@tabs_window.disposed? rescue nil
|
|
@log_window.refresh if @log_window && !@log_window.disposed? rescue nil
|
|
@input_window.refresh if @input_window && !@input_window.disposed? rescue nil
|
|
end
|
|
|
|
def self.create_windows
|
|
dispose_windows
|
|
|
|
w = Graphics.width
|
|
h = Graphics.height
|
|
|
|
input_h = 43
|
|
tabs_h = TAB_H
|
|
log_h = h - input_h - tabs_h
|
|
log_h = [log_h, 1].max
|
|
|
|
@tabs_window = Window_MPChatTabs.new(0, 0, w, tabs_h)
|
|
@log_window = Window_MPChatLog.new(0, tabs_h, w, log_h)
|
|
@input_window = Window_MPChatInput.new(0, tabs_h + log_h, w, input_h)
|
|
|
|
@tabs_window.z = 9999 rescue nil
|
|
@log_window.z = 9999 rescue nil
|
|
@input_window.z = 9999 rescue nil
|
|
end
|
|
|
|
def self.dispose_windows
|
|
if @tabs_window
|
|
@tabs_window.dispose rescue nil
|
|
@tabs_window = nil
|
|
end
|
|
if @log_window
|
|
@log_window.dispose rescue nil
|
|
@log_window = nil
|
|
end
|
|
if @input_window
|
|
@input_window.dispose rescue nil
|
|
@input_window = nil
|
|
end
|
|
end
|
|
|
|
def self.update
|
|
if !active? && open_key_triggered?
|
|
open
|
|
return
|
|
end
|
|
|
|
return unless active?
|
|
|
|
close_requested = false
|
|
with_input_passthrough do
|
|
close_requested = cancel_key_triggered?
|
|
target_channel = close_requested ? nil : channel_hotkey
|
|
set_channel(target_channel) if target_channel
|
|
@input_window.update if @input_window && !close_requested
|
|
end
|
|
|
|
if close_requested
|
|
close
|
|
return
|
|
end
|
|
|
|
@tabs_window.refresh_if_needed if @tabs_window && @tabs_window.respond_to?(:refresh_if_needed)
|
|
@log_window.update if @log_window
|
|
|
|
if @input_window && @input_window.consume_enter?
|
|
text = @input_window.text
|
|
WebSocket.send_chat(text, current_channel)
|
|
@input_window.clear
|
|
end
|
|
|
|
@log_window.refresh_if_needed if @log_window
|
|
end
|
|
end
|
|
|
|
class Window_MPChatTabs < Window_Base
|
|
def initialize(x, y, w, h)
|
|
super(x, y, w, h)
|
|
self.opacity = 220
|
|
self.z = 9999
|
|
@last_channel = nil
|
|
refresh
|
|
end
|
|
|
|
def refresh_if_needed
|
|
refresh if @last_channel != MPChat.current_channel
|
|
rescue
|
|
end
|
|
|
|
def refresh
|
|
contents.clear
|
|
contents.font.name = MPChat.ui_font_name
|
|
contents.font.size = 18
|
|
contents.font.shadow = false
|
|
contents.font.outline = false
|
|
@last_channel = MPChat.current_channel
|
|
x = 4
|
|
MPChatChannels::CHANNELS.each do |pair|
|
|
channel = pair[0]
|
|
label = pair[1]
|
|
active = channel == @last_channel
|
|
contents.font.color = active ? MPChatChannels.color(channel) : Color.new(170, 170, 170)
|
|
text = active ? "[#{label}]" : label
|
|
w = contents.text_size(text).width + 24 rescue 120
|
|
draw_text(x, 0, w, line_height, text)
|
|
x += w + 8
|
|
end
|
|
contents.font.color = Color.new(145, 145, 145)
|
|
draw_text([x, contents.width - 190].min, 0, 190, line_height, "TAB - канал")
|
|
rescue
|
|
end
|
|
end
|
|
|
|
class Window_MPChatLog < Window_Base
|
|
def refresh_if_needed
|
|
apply_chat_font
|
|
msgs = WebSocket.chat_messages
|
|
sig = [MPChat.current_channel, msgs.size, (msgs.empty? ? 0 : msgs[-1][0]), contents.width, contents.height]
|
|
refresh if sig != @last_sig
|
|
end
|
|
|
|
def color_for_message(pid, nick, channel = nil)
|
|
ch = MPChatChannels.normalize(channel || MPChat.current_channel)
|
|
return MPChatChannels.color("system") if ch == "system" || pid.to_i < 0
|
|
return MPChatChannels.color("clan") if ch == "clan"
|
|
begin
|
|
return Color.new(183, 164, 255) if nick.to_s == NicknameInput.nickname.to_s
|
|
rescue
|
|
end
|
|
MPChatChannels.color("global")
|
|
rescue
|
|
Color.new(255, 213, 128)
|
|
end
|
|
|
|
def refresh
|
|
contents.clear
|
|
apply_chat_font
|
|
|
|
msgs = WebSocket.chat_messages
|
|
@last_sig = [MPChat.current_channel, msgs.size, (msgs.empty? ? 0 : msgs[-1][0]), contents.width, contents.height]
|
|
|
|
lh = contents.font.size + 4
|
|
row_step = [lh - 2, 1].max
|
|
max_visible = [contents.height / row_step, 1].max
|
|
draw_width = [contents.width - (CHAT_PAD_X * 2), 1].max
|
|
|
|
rows = []
|
|
msgs.each do |m|
|
|
_id, ts_ms, pid, nick, text, channel = m
|
|
color = color_for_message(pid, nick, channel)
|
|
is_server = server_message?(pid, nick) || MPChatChannels.normalize(channel) == "system"
|
|
time_text = format_chat_time(ts_ms)
|
|
nick_text = normalize_chat_text(nick)
|
|
body_text = is_server ? MPChatFormat.decode_server_newlines(text) : normalize_chat_text(text)
|
|
line = "#{time_text} #{nick_text}: #{body_text}"
|
|
wrap_chat_line(line, draw_width, color, is_server).each do |wrapped|
|
|
rows << wrapped
|
|
end
|
|
end
|
|
|
|
y = 0
|
|
rows.last(max_visible).each do |segments|
|
|
draw_colored_segments(CHAT_PAD_X, y, draw_width, lh, segments)
|
|
y += row_step
|
|
end
|
|
end
|
|
end
|
|
|
|
class Window_MPChatInput < Window_Base
|
|
def update
|
|
super
|
|
begin
|
|
frame = Graphics.frame_count
|
|
return if @last_input_frame == frame
|
|
@last_input_frame = frame
|
|
rescue
|
|
end
|
|
|
|
if MPChat.current_channel == "system"
|
|
if @text != ""
|
|
@text = ""
|
|
refresh
|
|
end
|
|
@enter = true if MPChat.submit_key_triggered?
|
|
return
|
|
end
|
|
|
|
changed = false
|
|
|
|
if MPChat.backspace_pressed?
|
|
if @text && @text.size > 0
|
|
@text.chop!
|
|
changed = true
|
|
end
|
|
end
|
|
|
|
if MPChat.submit_key_triggered?
|
|
@enter = true
|
|
changed = true
|
|
end
|
|
|
|
typed = MPChat.typed_text
|
|
if typed != ""
|
|
typed.each_char do |ch|
|
|
next if ch.nil? || ch == ""
|
|
break if @text.size >= 200
|
|
@text << ch
|
|
changed = true
|
|
end
|
|
end
|
|
|
|
refresh if changed
|
|
end
|
|
|
|
def refresh
|
|
contents.clear
|
|
contents.font.name = MPChat.ui_font_name
|
|
contents.font.size = 20
|
|
default_color = MPChatChannels.color(MPChat.current_channel)
|
|
contents.font.color = default_color
|
|
contents.font.shadow = false
|
|
contents.font.outline = false
|
|
|
|
if MPChat.current_channel == "system"
|
|
prefix = "Системный: "
|
|
visible = "только уведомления"
|
|
else
|
|
prefix = MPChat.current_channel_label + ": "
|
|
visible = @text.to_s.dup
|
|
end
|
|
|
|
draw_width = [contents.width - 8, 1].max
|
|
begin
|
|
while visible.size > 0 && contents.text_size(prefix + MPChatFormat.strip_color_codes(visible)).width > draw_width
|
|
visible = visible[1..-1] || ""
|
|
end
|
|
visible = "..." + visible if visible != @text.to_s && MPChat.current_channel != "system"
|
|
rescue
|
|
end
|
|
draw_colored_input(4, 0, draw_width, line_height, prefix, visible, default_color)
|
|
end
|
|
end
|