From a5d24cb2c6abacb688b6d3cb587badf3846fec6d Mon Sep 17 00:00:00 2001 From: ChillerDragon Date: Sun, 17 Sep 2023 19:50:09 +0200 Subject: [PATCH] Add on_tick hook --- docs/README.md | 2 ++ docs/classes/TeeworldsClient.md | 13 +++++++++++++ lib/game_client.rb | 4 ++++ lib/teeworlds_client.rb | 6 ++++++ 4 files changed, 25 insertions(+) diff --git a/docs/README.md b/docs/README.md index 1d2d667..db90f74 100644 --- a/docs/README.md +++ b/docs/README.md @@ -82,3 +82,5 @@ Version v0.0.4 [#send_enter_game](classes/TeeworldsClient.md#send_enter_game) [#send_input(input = {})](classes/TeeworldsClient.md#send_input) + +[#on_tick(&block)](classes/TeeworldsClient.md#on_tick) diff --git a/docs/classes/TeeworldsClient.md b/docs/classes/TeeworldsClient.md index 47a43a7..7b41b8e 100644 --- a/docs/classes/TeeworldsClient.md +++ b/docs/classes/TeeworldsClient.md @@ -632,3 +632,16 @@ loop do prev_weapon: 0) end ``` +### #on_tick(&block) + +**Parameter: TODO** + +**Example:** +```ruby +client = TeeworldsClient.new + +# TODO: generated documentation +client.on_tick(&block) + +client.connect('localhost', 8303, detach: false) +``` diff --git a/lib/game_client.rb b/lib/game_client.rb index 0d6a96c..c029513 100644 --- a/lib/game_client.rb +++ b/lib/game_client.rb @@ -38,6 +38,10 @@ class GameClient context end + def on_tick + call_hook(:tick, nil) + end + def on_auth_on return if call_hook(:auth_on, Context.new(nil)).nil? diff --git a/lib/teeworlds_client.rb b/lib/teeworlds_client.rb index 7b1b71b..c4fc4e8 100644 --- a/lib/teeworlds_client.rb +++ b/lib/teeworlds_client.rb @@ -40,6 +40,7 @@ class TeeworldsClient auth_off: [], rcon_cmd_add: [], rcon_cmd_rem: [], + tick: [], maplist_entry_add: [], maplist_entry_rem: [] } @@ -76,6 +77,10 @@ class TeeworldsClient @rcon_authed end + def on_tick(&block) + @hooks[:tick].push(block) + end + def on_auth_on(&block) @hooks[:auth_on].push(block) end @@ -466,6 +471,7 @@ class TeeworldsClient puts 'retrying connection ...' end end + @game_client.on_tick return unless pck data = pck.first