teeworlds_network/sample.rb

58 lines
1.2 KiB
Ruby
Raw Normal View History

2022-11-01 13:25:56 +00:00
#!/usr/bin/env ruby
require_relative 'lib/teeworlds-client'
args = {verbose: false, ip: nil, port: nil}
ARGV.each do |arg|
if arg == '--help' || arg == '-h'
puts "usage: teeworlds.rb [OPTIONS..] [host] [port]"
echo "options:"
echo " --help|-h show this help"
echo " --verbose|-v verbose output"
echo "example:"
echo " teeworlds.rb --verbose localhost 8303"
exit(0)
elsif arg == '--verbose' || arg == '-v'
args[:verbose] = true
elsif args[:ip].nil?
args[:ip] = arg
elsif args[:port].nil?
args[:port] = arg.to_i
end
end
args[:ip] = args[:ip] || '127.0.0.1'
args[:port] = args[:port] || 8303
2022-11-04 15:26:24 +00:00
client = TeeworldsClient.new(verbose: args[:verbose])
2022-11-01 13:25:56 +00:00
2022-11-04 12:22:29 +00:00
client.set_startinfo(
name: "ruby gamer")
2022-11-05 08:39:16 +00:00
client.on_chat do |msg|
2022-11-04 15:26:24 +00:00
puts "[chat] #{msg}"
2022-11-01 13:25:56 +00:00
end
client.on_map_change do |ctx|
puts "GOT MAP CHANGOS"
end
client.on_client_info do |ctx|
puts "player info: #{ctx.data[:player]}"
ctx.data[:player].name = "yee"
end
2022-11-04 12:07:34 +00:00
Signal.trap('INT') do
client.disconnect
end
2022-11-04 12:04:51 +00:00
# connect and detach thread
2022-11-04 11:55:01 +00:00
client.connect(args[:ip], args[:port], detach: true)
2022-11-01 13:25:56 +00:00
2022-11-04 12:04:51 +00:00
# after 2 seconds reconnect
# and block the main thread
2022-11-04 11:55:01 +00:00
sleep 2
client.connect(args[:ip], args[:port], detach: false)
2022-11-01 14:27:39 +00:00