From 3a219f406b92d1aa1602673ac6445dad5bf4cc7f Mon Sep 17 00:00:00 2001 From: ChillerDragon Date: Tue, 15 Nov 2022 10:33:05 +0100 Subject: [PATCH] Change context api! And add input timing msg Remove the client attribute from the Context class The block that exposes a Context class has to be called on a client anyways so also passing it to the block args is redundant. Replace it by a parsed version of the chunk which has yet to be properly named current working title is: todo_rename_this --- docs/v0.0.1/classes/TeeworldsClient.md | 17 +++++++++++++++++ lib/context.rb | 8 ++++---- lib/game_client.rb | 21 ++++++++++++++------- lib/teeworlds_client.rb | 9 ++++++++- 4 files changed, 43 insertions(+), 12 deletions(-) diff --git a/docs/v0.0.1/classes/TeeworldsClient.md b/docs/v0.0.1/classes/TeeworldsClient.md index ae8fb15..3477550 100644 --- a/docs/v0.0.1/classes/TeeworldsClient.md +++ b/docs/v0.0.1/classes/TeeworldsClient.md @@ -1,5 +1,22 @@ # TeeworldsClient +### #on_input_timing(&block) + +**Parameter: block [Block |[context](../classes/Context.md)|]** + +TODO: generated documentation + +**Example:** +```ruby +client = TeeworldsClient.new + +client.on_input_timing do |context| + # TODO: generated documentation +end + +client.connect('localhost', 8303, detach: true) +``` + ### #on_snapshot(&block) **Parameter: block [Block |[context](../classes/Context.md)|]** diff --git a/lib/context.rb b/lib/context.rb index aa896e7..3289cf5 100644 --- a/lib/context.rb +++ b/lib/context.rb @@ -1,11 +1,11 @@ # frozen_string_literal: true class Context - attr_reader :old_data, :client - attr_accessor :data + attr_reader :old_data + attr_accessor :data, :todo_rename_this - def initialize(client, keys = {}) - @client = client + def initialize(todo_rename_this, keys = {}) + @todo_rename_this = todo_rename_this # the obj holding the parsed chunk @cancle = false @old_data = keys @data = keys diff --git a/lib/game_client.rb b/lib/game_client.rb index d6d38da..bb3901f 100644 --- a/lib/game_client.rb +++ b/lib/game_client.rb @@ -2,6 +2,7 @@ require_relative 'models/player' require_relative 'models/chat_message' +require_relative 'models/input_timing' require_relative 'packer' require_relative 'context' @@ -45,7 +46,7 @@ class GameClient # are currently ignored context = Context.new( - @client, + nil, player:, chunk: ) @@ -55,6 +56,12 @@ class GameClient @players[player.id] = player end + def on_input_timing(chunk) + todo_rename_this = InputTiming.new(chunk.data[1..]) + context = Context.new(todo_rename_this, chunk:, packet:) + call_hook(:input_timing, context) + end + def on_client_drop(chunk) u = Unpacker.new(chunk.data[1..]) client_id = u.get_int @@ -62,7 +69,7 @@ class GameClient silent = u.get_int context = Context.new( - @client, + nil, player: @players[client_id], chunk:, client_id:, @@ -79,20 +86,20 @@ class GameClient end def on_connected - context = Context.new(@client) + context = Context.new(nil) return if call_hook(:connected, context).nil? @client.send_msg_start_info end def on_disconnect - call_hook(:disconnect, Context.new(@client)) + call_hook(:disconnect, Context.new(nil)) end def on_rcon_line(chunk) u = Unpacker.new(chunk.data[1..]) context = Context.new( - @client, + nil, line: u.get_string ) call_hook(:rcon_line, context) @@ -138,7 +145,7 @@ class GameClient def on_emoticon(chunk); end def on_map_change(chunk) - context = Context.new(@client, chunk:) + context = Context.new(nil, chunk:) return if call_hook(:map_change, context).nil? # ignore mapdownload at all times @@ -157,7 +164,7 @@ class GameClient data[:author] = @players[data[:client_id]] msg = ChatMesage.new(data) - context = Context.new(@client, chunk:) + context = Context.new(nil, chunk:) call_hook(:chat, context, msg) end end diff --git a/lib/teeworlds_client.rb b/lib/teeworlds_client.rb index ecb75c8..633e7ad 100644 --- a/lib/teeworlds_client.rb +++ b/lib/teeworlds_client.rb @@ -31,7 +31,8 @@ class TeeworldsClient connected: [], disconnect: [], rcon_line: [], - snapshot: [] + snapshot: [], + input_timing: [] } @thread_running = false @signal_disconnect = false @@ -93,6 +94,10 @@ class TeeworldsClient @hooks[:snapshot].push(block) end + def on_input_timing(&block) + @hooks[:input_timing].push(block) + end + def send_chat(str) @netbase.send_packet( NetChunk.create_header(vital: true, size: 4 + str.length) + @@ -363,6 +368,8 @@ class TeeworldsClient @game_client.on_rcon_line(chunk) when NETMSG_SNAP, NETMSG_SNAPSINGLE, NETMSG_SNAPEMPTY @game_client.on_snapshot(chunk) + when NETMSG_INPUTTIMING + @game_client.on_input_timing(chunk) else puts "Unsupported system msg: #{chunk.msg}" exit(1)