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
This commit is contained in:
ChillerDragon 2022-11-15 10:33:05 +01:00
parent fa08043968
commit 3a219f406b
4 changed files with 43 additions and 12 deletions

View file

@ -1,5 +1,22 @@
# TeeworldsClient
### <a name="on_input_timing"></a> #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)
```
### <a name="on_snapshot"></a> #on_snapshot(&block)
**Parameter: block [Block |[context](../classes/Context.md)|]**

View file

@ -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

View file

@ -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

View file

@ -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)