-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_sample.rb
executable file
·90 lines (77 loc) · 1.92 KB
/
client_sample.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env ruby
# frozen_string_literal: true
require_relative 'lib/teeworlds_client'
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)
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?
args[:port] = arg.to_i
end
end
args[:ip] = args[:ip] || '127.0.0.1'
args[:port] = args[:port] || 8303
client = TeeworldsClient.new(args)
client.on_chat do |_, msg|
puts "[chat] #{msg}"
end
client.on_client_info do |ctx|
puts "'#{ctx.data[:player].name}' joined the game"
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
Signal.trap('INT') do
client.disconnect
exit
end
# connect and detach thread
client.connect(args[:ip], args[:port], detach: true)
loop do
msg = $stdin.gets.chomp
client.send_chat(msg)
end