teeworlds_network/client_sample.rb

52 lines
1.2 KiB
Ruby
Raw Normal View History

2022-11-01 13:25:56 +00:00
#!/usr/bin/env ruby
2022-11-05 16:48:47 +00:00
# frozen_string_literal: true
2022-11-01 13:25:56 +00:00
require_relative 'lib/teeworlds_client'
2022-11-01 13:25:56 +00:00
2022-11-05 16:19:05 +00:00
args = { verbose: false, ip: nil, port: nil }
2022-11-01 13:25:56 +00:00
ARGV.each do |arg|
2022-11-05 16:19:05 +00:00
if ['--help', '-h'].include?(arg)
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'
2022-11-01 13:25:56 +00:00
exit(0)
2022-11-05 16:19:05 +00:00
elsif ['--verbose', '-v'].include?(arg)
2022-11-01 13:25:56 +00:00
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
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
2022-11-05 10:59:36 +00:00
client.on_client_info do |ctx|
puts "'#{ctx.data[:player].name}' joined the game"
2022-11-05 10:59:36 +00:00
end
client.on_client_drop do |ctx|
unless ctx.data[:silent]
reason = ctx.data[:reason] ? " (#{ctx.data[:reason]})" : ''
puts "'#{ctx.data[:player].name}' left the game#{reason}"
end
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: false)