Add --verbose-snap to sample client

This commit is contained in:
ChillerDragon 2023-10-22 08:19:50 +02:00
parent f6c57a31e5
commit 42d7fda502
5 changed files with 66 additions and 28 deletions

View file

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

View file

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

View file

@ -1,5 +1,24 @@
# TeeworldsClient
### <a name="initialize"></a> #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)
```
### <a name="on_disconnect"></a> #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!')
```
### <a name="initialize"></a> #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)
```
### <a name="rcon_authed"></a> #rcon_authed? -> Boolean
Returns true if the client is currently rcon authenticated.

View file

@ -76,6 +76,7 @@ end
class SnapshotUnpacker
def initialize(client)
@client = client
@verbose = client.verbose_snap
end
def unpack_ddnet_item(u, notes)

View file

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