174 lines
5.5 KiB
Ruby
174 lines
5.5 KiB
Ruby
# encoding: UTF-8
|
|
#==============================================================================
|
|
# TopChatOverlay 1.0.0
|
|
#------------------------------------------------------------------------------
|
|
# Верхний мини-чат:
|
|
# global -> белый
|
|
# clan -> светло-зелёный
|
|
# system -> НЕ показывается наверху
|
|
#
|
|
# Полный MPChat и системная вкладка не меняются.
|
|
# Работает поверх существующих multiplayer/34_chat_channels.rb и 90_hooks.rb.
|
|
#==============================================================================
|
|
|
|
module MPTopChatOverlay
|
|
VERSION = "1.0.1"
|
|
GLOBAL_COLOR = Color.new(255, 255, 255)
|
|
CLAN_COLOR = Color.new(155, 255, 175)
|
|
|
|
@seen = {}
|
|
@seen_order = []
|
|
|
|
def self.normalize_channel(channel)
|
|
if defined?(MPChatChannels) && MPChatChannels.respond_to?(:normalize)
|
|
MPChatChannels.normalize(channel)
|
|
else
|
|
ch = channel.to_s.strip.downcase
|
|
return "clan" if ch == "clan" || ch == "клан"
|
|
return "system" if ch == "system" || ch == "система"
|
|
"global"
|
|
end
|
|
rescue
|
|
"global"
|
|
end
|
|
|
|
def self.color_for(channel)
|
|
normalize_channel(channel) == "clan" ? CLAN_COLOR : GLOBAL_COLOR
|
|
rescue
|
|
GLOBAL_COLOR
|
|
end
|
|
|
|
def self.system_message?(channel, pid=nil, nick=nil)
|
|
return true if normalize_channel(channel) == "system"
|
|
n = nick.to_s.strip.upcase
|
|
return true if pid.to_i < 0
|
|
return true if n == "SERVER" || n == "[SERVER]" || n == "SYSTEM" || n == "СИСТЕМА"
|
|
false
|
|
rescue
|
|
false
|
|
end
|
|
|
|
def self.seen?(channel, msg_id)
|
|
id = msg_id.to_i
|
|
return false if id <= 0
|
|
key = "#{normalize_channel(channel)}:#{id}"
|
|
return true if @seen[key]
|
|
@seen[key] = true
|
|
@seen_order << key
|
|
if @seen_order.size > 200
|
|
old = @seen_order.shift(@seen_order.size - 200)
|
|
old.each { |k| @seen.delete(k) }
|
|
end
|
|
false
|
|
rescue
|
|
false
|
|
end
|
|
|
|
def self.push(channel, msg_id, ts_ms, pid, nick, text)
|
|
ch = normalize_channel(channel)
|
|
return false if system_message?(ch, pid, nick)
|
|
return false unless ch == "global" || ch == "clan"
|
|
return false if seen?(ch, msg_id)
|
|
return false unless defined?(MPNotify)
|
|
|
|
n = nick.to_s
|
|
n = "Player" if n.strip.empty?
|
|
t = text.to_s.gsub(/\r\n?/, " ").gsub("\n", " ").gsub("\0", "").strip
|
|
t = t[0, 200]
|
|
return false if t.empty?
|
|
|
|
time_text = ""
|
|
begin
|
|
if defined?(WebSocket) && WebSocket.respond_to?(:chat_time_text)
|
|
time_text = WebSocket.chat_time_text(ts_ms)
|
|
end
|
|
rescue
|
|
time_text = ""
|
|
end
|
|
prefix = time_text.to_s.empty? ? "" : "#{time_text} "
|
|
|
|
if MPNotify.respond_to?(:_push)
|
|
MPNotify._push({
|
|
:type => (ch == "clan" ? :topchat_clan : :topchat_global),
|
|
:channel => ch,
|
|
:text => "#{prefix}#{n}: #{t}",
|
|
:color => color_for(ch)
|
|
})
|
|
true
|
|
else
|
|
false
|
|
end
|
|
rescue => e
|
|
begin
|
|
puts "[TopChatOverlay] push error #{e.class}: #{e.message}"
|
|
rescue
|
|
end
|
|
false
|
|
end
|
|
end
|
|
|
|
# prepend is intentional here. ClanEstate may already have prepended its own
|
|
# WebSocket parser on mobile while ModScripts are still loading. Aliasing parse
|
|
# after that can capture a prepended method whose `super` re-enters this wrapper
|
|
# and causes SystemStackError. prepend+super is order-safe in both cases.
|
|
module MPTopChatOverlayWebSocketPatch
|
|
def parse(msg)
|
|
raw = msg.to_s
|
|
|
|
# Новый канальный протокол.
|
|
if raw.start_with?("chat_msg2:")
|
|
parts = raw.split(":", 7)
|
|
if parts.size >= 7
|
|
ch = MPTopChatOverlay.normalize_channel(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) rescue parts[5].to_s)
|
|
text = parts[6].to_s
|
|
MPTopChatOverlay.push(ch, msg_id, ts_ms, pid, nick, text)
|
|
end
|
|
return super(msg)
|
|
end
|
|
|
|
# Старые chat_msg всё ещё используются некоторыми серверными плагинами
|
|
# для системных сообщений. Не даём старому MPNotify вывести их наверх,
|
|
# но пропускаем дальше в историю полного чата.
|
|
if raw.start_with?("chat_msg:")
|
|
parts = raw.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) rescue parts[4].to_s)
|
|
text = parts[5].to_s
|
|
ch = begin
|
|
respond_to?(:legacy_chat_channel) ? legacy_chat_channel(pid, nick) : (pid < 0 ? "system" : "global")
|
|
rescue
|
|
pid < 0 ? "system" : "global"
|
|
end
|
|
|
|
MPTopChatOverlay.push(ch, msg_id, ts_ms, pid, nick, text)
|
|
|
|
# 90_hooks.rb сам рисует любой chat_msg белым. Обходим только его
|
|
# notify-wrapper, чтобы system не всплыл наверху и global не удвоился.
|
|
if respond_to?(:mp_parse_without_notify)
|
|
return mp_parse_without_notify(msg)
|
|
end
|
|
end
|
|
end
|
|
|
|
super(msg)
|
|
end
|
|
end
|
|
|
|
if defined?(WebSocket)
|
|
begin
|
|
sc = WebSocket.singleton_class
|
|
sc.prepend(MPTopChatOverlayWebSocketPatch) unless sc.ancestors.include?(MPTopChatOverlayWebSocketPatch)
|
|
rescue => e
|
|
puts "[TopChatOverlay] parser prepend failed #{e.class}: #{e.message}" rescue nil
|
|
end
|
|
end
|
|
|
|
puts "[TopChatOverlay] loaded v#{MPTopChatOverlay::VERSION}; global=white clan=light-green system=hidden"
|