Display chat messages in server log

This commit is contained in:
ChillerDragon 2022-11-13 11:15:33 +01:00
parent fc48a53bbd
commit acba9e7a5d
4 changed files with 114 additions and 0 deletions

View file

@ -5,6 +5,8 @@ require_relative 'models/server_info'
require_relative 'models/server_settings'
require_relative 'models/game_info'
require_relative 'models/start_info'
require_relative 'models/cl_say'
require_relative 'models/chat_message'
class GameServer
attr_accessor :pred_game_tick, :ack_game_tick, :map
@ -57,6 +59,13 @@ class GameServer
@server.send_ready_to_enter(packet.client)
end
def on_say(chunk, packet)
say = ClSay.new(chunk.data[1..])
author = packet.client.player
msg = ChatMesage.new(say.to_h.merge(client_id: author.id, author:))
puts msg.to_s
end
def on_enter_game(_chunk, packet)
# vanilla server responds to enter game with two packets
# first:

52
lib/models/cl_say.rb Normal file
View file

@ -0,0 +1,52 @@
# frozen_string_literal: true
require_relative '../packer'
##
# ClSay
#
# Client -> Server
class ClSay
attr_accessor :mode, :target_id, :message
def initialize(hash_or_raw)
if hash_or_raw.instance_of?(Hash)
init_hash(hash_or_raw)
else
init_raw(hash_or_raw)
end
end
def init_raw(data)
u = Unpacker.new(data)
@mode = u.get_int
@target_id = u.get_int
@message = u.get_string
end
def init_hash(attr)
@mode = attr[:mode] || 0
@target_id = attr[:target_id] || 0
@message = attr[:message] || 0
end
def to_h
{
mode: @mode,
target_id: @target_id,
message: @message
}
end
# basically to_network
# int array the client sends to the server
def to_a
Packer.pack_int(@mode) +
Packer.pack_int(@target_id) +
Packer.pack_str(@message)
end
def to_s
to_h
end
end

48
lib/models/template.rb Normal file
View file

@ -0,0 +1,48 @@
# frozen_string_literal: true
require_relative '../packer'
##
# SamplePacket
#
# Client -> Server
class SamplePacket
attr_accessor :foo, :bar
def initialize(hash_or_raw)
if hash_or_raw.instance_of?(Hash)
init_hash(hash_or_raw)
else
init_raw(hash_or_raw)
end
end
def init_raw(data)
u = Unpacker.new(data)
@foo = u.get_int
@bar = u.get_string
end
def init_hash(attr)
@foo = attr[:foo] || 0
@bar = attr[:bar] || 'sample'
end
def to_h
{
foo: @foo,
bar: @bar
}
end
# basically to_network
# int array the client sends to the server
def to_a
Packer.pack_int(@foo) +
Packer.pack_str(@bar)
end
def to_s
to_h
end
end

View file

@ -104,6 +104,7 @@ class TeeworldsServer
puts "got game chunk: #{chunk}"
case chunk.msg
when NETMSGTYPE_CL_STARTINFO then @game_server.on_start_info(chunk, packet)
when NETMSGTYPE_CL_SAY then @game_server.on_say(chunk, packet)
else
puts "Unsupported game msg: #{chunk.msg}"
exit(1)
@ -315,6 +316,10 @@ class TeeworldsServer
end
end
def get_player_by_id(id)
@clients[id]&.player
end
def tick
unless @clients.empty?
now = Time.now