From acba9e7a5d07360135d822574a37632daab6f742 Mon Sep 17 00:00:00 2001 From: ChillerDragon Date: Sun, 13 Nov 2022 11:15:33 +0100 Subject: [PATCH] Display chat messages in server log --- lib/game_server.rb | 9 +++++++ lib/models/cl_say.rb | 52 +++++++++++++++++++++++++++++++++++++++++ lib/models/template.rb | 48 +++++++++++++++++++++++++++++++++++++ lib/teeworlds_server.rb | 5 ++++ 4 files changed, 114 insertions(+) create mode 100644 lib/models/cl_say.rb create mode 100644 lib/models/template.rb diff --git a/lib/game_server.rb b/lib/game_server.rb index e75d9cc..8e23f7a 100644 --- a/lib/game_server.rb +++ b/lib/game_server.rb @@ -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: diff --git a/lib/models/cl_say.rb b/lib/models/cl_say.rb new file mode 100644 index 0000000..ebffe25 --- /dev/null +++ b/lib/models/cl_say.rb @@ -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 diff --git a/lib/models/template.rb b/lib/models/template.rb new file mode 100644 index 0000000..fc11cbd --- /dev/null +++ b/lib/models/template.rb @@ -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 diff --git a/lib/teeworlds_server.rb b/lib/teeworlds_server.rb index 3acdc0f..f63081b 100644 --- a/lib/teeworlds_server.rb +++ b/lib/teeworlds_server.rb @@ -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