From 0a04af1cb6577adc709e98b3d6f314ba13ee7326 Mon Sep 17 00:00:00 2001 From: ChillerDragon Date: Wed, 9 Nov 2022 16:46:04 +0100 Subject: [PATCH] Parse client info on the server side --- lib/game_server.rb | 22 ++++++++++++++++++++++ lib/teeworlds_server.rb | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 lib/game_server.rb diff --git a/lib/game_server.rb b/lib/game_server.rb new file mode 100644 index 0000000..6ad543b --- /dev/null +++ b/lib/game_server.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +class GameServer + attr_accessor :pred_game_tick, :ack_game_tick + + def initialize(server) + @server = server + @ack_game_tick = -1 + @pred_game_tick = 0 + end + + def on_info(chunk) + u = Unpacker.new(chunk.data[1..]) + net_version = u.get_string + password = u.get_string + client_version = u.get_int + puts "vers=#{net_version} vers=#{client_version} pass=#{password}" + + # TODO: respond with map info + # here tho? Check tw code when to send map info + end +end diff --git a/lib/teeworlds_server.rb b/lib/teeworlds_server.rb index ed3aae7..90cc0f7 100755 --- a/lib/teeworlds_server.rb +++ b/lib/teeworlds_server.rb @@ -11,12 +11,14 @@ require_relative 'chunk' require_relative 'net_base' require_relative 'net_addr' require_relative 'packer' +require_relative 'game_server' class TeeworldsServer def initialize(options = {}) @verbose = options[:verbose] || false @ip = '127.0.0.1' @port = 8303 + @game_server = GameServer.new(self) end def run(ip, port) @@ -38,8 +40,34 @@ class TeeworldsServer end end - def on_client_packet(_packet) - puts 'got client packet' + def on_system_chunk(chunk) + puts "got system chunk: #{chunk}" + end + + def process_chunk(chunk) + unless chunk.sys + on_system_chunk(chunk) + return + end + puts "proccess chunk with msg: #{chunk.msg}" + case chunk.msg + when NETMSG_INFO + @game_server.on_info(chunk) + else + puts "Unsupported system msg: #{chunk.msg}" + exit(1) + end + end + + def on_client_packet(packet) + chunks = BigChungusTheChunkGetter.get_chunks(packet.payload) + chunks.each do |chunk| + if chunk.flags_vital && !chunk.flags_resend + @netbase.ack = (@netbase.ack + 1) % NET_MAX_SEQUENCE + puts "got ack: #{@netbase.ack}" if @verbose + end + process_chunk(chunk) + end end def on_ctrl_message(packet) @@ -79,6 +107,7 @@ class TeeworldsServer def on_ctrl_connect(packet) puts "Got connect from #{packet.addr}" + @netbase.send_packet([NET_CTRLMSG_ACCEPT], 0, control: true, addr: packet.addr) end def on_packet(packet)