From 875132a03a7fee4165fecf36c243facd61794099 Mon Sep 17 00:00:00 2001 From: ChillerDragon Date: Fri, 4 Nov 2022 13:22:29 +0100 Subject: [PATCH] Document more samples --- README.md | 69 +++++++++++++++++++++++++++++++++++++++++ lib/teeworlds-client.rb | 60 ++++++++++++++++++++--------------- sample.rb | 3 ++ 3 files changed, 107 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 02ee000..8372415 100644 --- a/README.md +++ b/README.md @@ -1,18 +1,87 @@ # teeworlds-client A teeworlds 0.7 client library written in ruby +## Simple chat printing client + ```ruby require_relative 'lib/teeworlds-client' client = TwClient.new(verbose: false) +# print all incoming chat messages client.hook_chat do |msg| puts "chat: #{msg}" end +# properly disconnect on ctrl+c Signal.trap('INT') do client.disconnect end +# connect to localhost and block the current thread +client.connect('localhost', 8303, detach: false) +``` + +## Print all network traffic + +```ruby +require_relative 'lib/teeworlds-client' + +client = TwClient.new(verbose: true) + +# connect to localhost and block the current thread +client.connect('localhost', 8303, detach: false) +``` + +## Detach client (do not block the main thread) + +```ruby +require_relative 'lib/teeworlds-client' + +client = TwClient.new(verbose: true) + +# connect to localhost and detach a background thread +client.connect('localhost', 8303, detach: true) + +loop do + # send a chat message every 5 seconds + sleep 5 + client.send_chat('hello friends!') +end +``` + +## Set custom skin and other player infos + +```ruby +require_relative 'lib/teeworlds-client' + +client = TwClient.new(verbose: true) + +# all keys are optional +# if not provided they will fall back to the default value +client.set_startinfo( + name: "ruby gamer", + clan: "", + country: -1, + body: "spiky", + marking: "duodonny", + decoration: "", + hands: "standard", + feet: "standard", + eyes: "standard", + custom_color_body: 0, + custom_color_marking: 0, + custom_color_decoration: 0, + custom_color_hands: 0, + custom_color_feet: 0, + custom_color_eyes: 0, + color_body: 0, + color_marking: 0, + color_decoration: 0, + color_hands: 0, + color_feet: 0, + color_eyes: 0) + +# connect to localhost and block the current thread client.connect('localhost', 8303, detach: false) ``` diff --git a/lib/teeworlds-client.rb b/lib/teeworlds-client.rb index 28bc7be..12c9d56 100644 --- a/lib/teeworlds-client.rb +++ b/lib/teeworlds-client.rb @@ -24,6 +24,29 @@ class TwClient @hooks = {} @thread_running = false @signal_disconnect = false + @start_info = { + name: "ruby gamer", + clan: "", + country: -1, + body: "spiky", + marking: "duodonny", + decoration: "", + hands: "standard", + feet: "standard", + eyes: "standard", + custom_color_body: 0, + custom_color_marking: 0, + custom_color_decoration: 0, + custom_color_hands: 0, + custom_color_feet: 0, + custom_color_eyes: 0, + color_body: 0, + color_marking: 0, + color_decoration: 0, + color_hands: 0, + color_feet: 0, + color_eyes: 0 + } end def hook_chat(&block) @@ -78,6 +101,17 @@ class TwClient @signal_disconnect = true end + def set_startinfo(info) + info.each do |key, value| + unless @start_info.key?(key) + puts "Error: invalid start info key '#{key}'" + puts " valid keys: #{@start_info.keys}" + exit 1 + end + @start_info[key] = value + end + end + private def connection_loop @@ -122,33 +156,9 @@ class TwClient end def send_msg_startinfo() - start_info = { - name: "ruby gamer", - clan: "", - country: 1, - body: "spiky", - marking: "duodonny", - decoration: "", - hands: "standard", - feet: "standard", - eyes: "standard", - custom_color_body: 0, - custom_color_marking: 0, - custom_color_decoration: 0, - custom_color_hands: 0, - custom_color_feet: 0, - custom_color_eyes: 0, - color_body: 0, - color_marking: 0, - color_decoration: 0, - color_hands: 0, - color_feet: 0, - color_eyes: 0 - } - data = [] - start_info.each do |key, value| + @start_info.each do |key, value| if value.class == String data += Packer.pack_str(value) elsif value.class == Integer diff --git a/sample.rb b/sample.rb index 5d4d2e5..5adf9cf 100755 --- a/sample.rb +++ b/sample.rb @@ -27,6 +27,9 @@ args[:port] = args[:port] || 8303 client = TwClient.new(verbose: args[:verbose]) +client.set_startinfo( + name: "ruby gamer") + client.hook_chat do |msg| puts "chat: #{msg}" end