diff --git a/lib/game_server.rb b/lib/game_server.rb index d34c38a..935081b 100644 --- a/lib/game_server.rb +++ b/lib/game_server.rb @@ -63,7 +63,7 @@ class GameServer @server.send_game_info(packet.client, GameInfo.new.to_a) end - def on_rcon_cmd(chunk, packet) + def on_rcon_cmd(chunk, _packet) u = Unpacker.new(chunk.data[1..]) cmd = u.get_string puts "got rcon_cmd=#{cmd}" @@ -78,8 +78,9 @@ class GameServer # TODO: do something end - def disconnect_client(client, reason = nil) - @server.send_ctrl_close(client, reason) + def on_client_drop(client, reason = nil) + reason = reason.nil? ? '' : " (#{reason})" + puts "'#{client.player.name}' left the game#{reason}" end def on_tick @@ -91,8 +92,7 @@ class GameServer end timeout_ids.each do |id| - disconnect_client(@server.clients[id], 'Timeout') - @server.clients.delete(id) + @server.drop_client(@server.clients[id], 'Timeout') end end end diff --git a/lib/teeworlds_server.rb b/lib/teeworlds_server.rb index 226c3e0..3a2f484 100644 --- a/lib/teeworlds_server.rb +++ b/lib/teeworlds_server.rb @@ -16,13 +16,21 @@ require_relative 'message' require_relative 'token' class Client - attr_accessor :id, :addr, :vital_sent, :last_recv_time, :token + attr_accessor :id, :addr, :vital_sent, :last_recv_time, :token, :player def initialize(attr = {}) @id = attr[:id] @addr = attr[:addr] @vital_sent = 0 @last_recv_time = Time.now + @player = Player.new( + id: @id, + local: 0, + team: 0, + name: '(connecting)', + clan: '', + country: -1 + ) @token = attr[:token] SecurityToken.validate(@token) end @@ -208,7 +216,20 @@ class TeeworldsServer end def on_ctrl_close(packet) - puts "Client closed the connection #{packet.addr}" + reason = nil + if packet.payload[2] + u = Unpacker.new(packet.payload[1..]) + reason = u.get_string + end + drop_client(packet.client, reason) + end + + def drop_client(client, reason = nil) + send_ctrl_close(client, reason) + return if client.nil? + + @game_server.on_client_drop(client, reason) + @clients.delete(client.id) end def on_ctrl_connect(packet)