From 42d7fda502a12f9d1cf13a417102b524f4908699 Mon Sep 17 00:00:00 2001 From: ChillerDragon Date: Sun, 22 Oct 2023 08:19:50 +0200 Subject: [PATCH] Add --verbose-snap to sample client --- client_sample.rb | 51 +++++++++++++++++++++++++++------ docs/README.md | 4 +-- docs/classes/TeeworldsClient.md | 35 +++++++++++----------- lib/snapshot/unpacker.rb | 1 + lib/teeworlds_client.rb | 3 +- 5 files changed, 66 insertions(+), 28 deletions(-) diff --git a/client_sample.rb b/client_sample.rb index 76dc964..796001c 100755 --- a/client_sample.rb +++ b/client_sample.rb @@ -3,19 +3,52 @@ require_relative 'lib/teeworlds_client' -args = { verbose: false, ip: nil, port: nil } +args = { + verbose: false, + verbose_snap: false, + ip: nil, + port: nil +} + +verbose_level = 0 + +def show_help + puts 'usage: client_sample.rb [OPTIONS..] [host] [port]' + puts 'options:' + puts ' --help|-h show this help' + puts ' --verbose|-v verbose output' + puts ' --verbose-snap|-s verbose snap item output' + puts 'example:' + puts ' client_sample.rb --verbose localhost 8303' + puts ' client_sample.rb -s' + puts ' client_sample.rb -vv ger.ddnet.org 8307' + exit(0) +end ARGV.each do |arg| if ['--help', '-h'].include?(arg) - puts 'usage: teeworlds.rb [OPTIONS..] [host] [port]' - puts 'options:' - puts ' --help|-h show this help' - puts ' --verbose|-v verbose output' - puts 'example:' - puts ' teeworlds.rb --verbose localhost 8303' - exit(0) + show_help elsif ['--verbose', '-v'].include?(arg) args[:verbose] = true + elsif ['--verbose-snap', '-s'].include?(arg) + args[:verbose_snap] = true + elsif arg[0] == '-' && arg[1] != '-' + # flags + arg[1..].chars.each do |flag| + case flag + when 'v' + verbose_level += 1 + args[:verbose] = true + args[:verbose_snap] = true if verbose_level > 1 + when 'h' + show_help + when 's' + args[:verbose_snap] = true + else + puts "Error: unknown flag '#{flag}'" + exit(1) + end + end elsif args[:ip].nil? args[:ip] = arg elsif args[:port].nil? @@ -26,7 +59,7 @@ end args[:ip] = args[:ip] || '127.0.0.1' args[:port] = args[:port] || 8303 -client = TeeworldsClient.new(verbose: args[:verbose]) +client = TeeworldsClient.new(args) client.on_chat do |_, msg| puts "[chat] #{msg}" diff --git a/docs/README.md b/docs/README.md index 7bb753d..f57e20b 100644 --- a/docs/README.md +++ b/docs/README.md @@ -17,6 +17,8 @@ Version v0.0.6 ### [TeeworldsClient](classes/TeeworldsClient.md) +[#initialize(options = {})](classes/TeeworldsClient.md#initialize) + [#on_disconnect(&block)](classes/TeeworldsClient.md#on_disconnect) [#on_connected(&block)](classes/TeeworldsClient.md#on_connected) @@ -51,8 +53,6 @@ Version v0.0.6 [#send_chat(str)](classes/TeeworldsClient.md#send_chat) -[#initialize(options = {})](classes/TeeworldsClient.md#initialize) - [#rcon_authed? -> Boolean](classes/TeeworldsClient.md#rcon_authed) [#send_ctrl_close](classes/TeeworldsClient.md#send_ctrl_close) diff --git a/docs/classes/TeeworldsClient.md b/docs/classes/TeeworldsClient.md index 7b41b8e..96c2ef7 100644 --- a/docs/classes/TeeworldsClient.md +++ b/docs/classes/TeeworldsClient.md @@ -1,5 +1,24 @@ # TeeworldsClient +### #initialize(options = {}) + +**Parameter: Hash** + +Available keys: +- `:verbose [Boolean]` enables verbose output. +- `:verbose_snap [Boolean]` enables verbose output specific to the snap message. +- `:config [String]` path to autoexec.cfg file. As of right now only those commands are supported: + + `password [yourpassword]` will be sent on connect + + `echo [message]` prints a message + + `quit` quits the client + +**Example:** +```ruby +client = TeeworldsClient.new(verbose: true, config: "autoexec.cfg") + +client.connect('localhost', 8303, detach: false) +``` + ### #on_disconnect(&block) **Parameter: block [Block |[context](../classes/Context.md)|]** @@ -376,23 +395,7 @@ client.connect('localhost', 8303, detach: false) client.send_chat('hello world!') ``` -### #initialize(options = {}) -**Parameter: Hash** - -Available keys: -- `:verbose [Boolean]` enables verbose output. -- `:config [String]` path to autoexec.cfg file. As of right now only those commands are supported: - + `password [yourpassword]` will be sent on connect - + `echo [message]` prints a message - + `quit` quits the client - -**Example:** -```ruby -client = TeeworldsClient.new(verbose: true, config: "autoexec.cfg") - -client.connect('localhost', 8303, detach: false) -``` ### #rcon_authed? -> Boolean Returns true if the client is currently rcon authenticated. diff --git a/lib/snapshot/unpacker.rb b/lib/snapshot/unpacker.rb index c8c0c5c..b95e1d5 100644 --- a/lib/snapshot/unpacker.rb +++ b/lib/snapshot/unpacker.rb @@ -76,6 +76,7 @@ end class SnapshotUnpacker def initialize(client) @client = client + @verbose = client.verbose_snap end def unpack_ddnet_item(u, notes) diff --git a/lib/teeworlds_client.rb b/lib/teeworlds_client.rb index c4fc4e8..af020b4 100644 --- a/lib/teeworlds_client.rb +++ b/lib/teeworlds_client.rb @@ -16,11 +16,12 @@ require_relative 'game_client' require_relative 'config' class TeeworldsClient - attr_reader :state, :hooks, :game_client + attr_reader :state, :hooks, :game_client, :verbose_snap attr_accessor :rcon_authed, :local_client_id def initialize(options = {}) @verbose = options[:verbose] || false + @verbose_snap = options[:verbose_snap] || false @state = NET_CONNSTATE_OFFLINE @ip = 'localhost' @port = 8303