kag-gather 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/config/config.sample.json +2 -0
- data/lib/gather.rb +1 -1
- data/lib/kag/gather.rb +80 -24
- data/lib/kag/server.rb +118 -0
- data/lib/kag/version.rb +1 -1
- metadata +1 -1
data/config/config.sample.json
CHANGED
data/lib/gather.rb
CHANGED
data/lib/kag/gather.rb
CHANGED
@@ -52,16 +52,7 @@ module KAG
|
|
52
52
|
|
53
53
|
match "add", method: :evt_add
|
54
54
|
def evt_add(m)
|
55
|
-
|
56
|
-
@queue[m.user.nick] = SymbolTable.new({
|
57
|
-
:user => m.user,
|
58
|
-
:channel => m.channel,
|
59
|
-
:message => m.message,
|
60
|
-
:joined_at => Time.now
|
61
|
-
})
|
62
|
-
send_channels_msg "Added #{m.user.nick} to queue (#{get_match_type_as_string}) [#{@queue.length}]"
|
63
|
-
check_for_new_match(m)
|
64
|
-
end
|
55
|
+
add_user_to_queue(m,m.user.nick)
|
65
56
|
end
|
66
57
|
|
67
58
|
match "rem", method: :evt_rem
|
@@ -86,17 +77,32 @@ module KAG
|
|
86
77
|
|
87
78
|
match "end", method: :evt_end
|
88
79
|
def evt_end(m)
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
end
|
80
|
+
match_in = get_match_in(m.user.nick)
|
81
|
+
if match_in
|
82
|
+
match_in[:end_votes] = match_in[:end_votes] + 1
|
83
|
+
evt = KAG::Config.instance[:end_vote_threshold].to_i
|
84
|
+
evt = 3 if evt < 1
|
85
|
+
if match_in[:end_votes] >= evt
|
86
|
+
end_match(match_in)
|
87
|
+
else
|
88
|
+
needed = evt - match_in[:end_votes]
|
89
|
+
m.reply "End vote started, #{needed} more votes to end match at #{match_in[:server][:key]}"
|
99
90
|
end
|
91
|
+
else
|
92
|
+
m.reply "You're not in a match, silly! Stop trying to hack me."
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def add_user_to_queue(m,nick)
|
97
|
+
unless @queue.key?(nick) or in_match(nick)
|
98
|
+
@queue[nick] = SymbolTable.new({
|
99
|
+
:user => User(nick),
|
100
|
+
:channel => m.channel,
|
101
|
+
:message => m.message,
|
102
|
+
:joined_at => Time.now
|
103
|
+
})
|
104
|
+
send_channels_msg "Added #{nick} to queue (#{get_match_type_as_string}) [#{@queue.length}]"
|
105
|
+
check_for_new_match
|
100
106
|
end
|
101
107
|
end
|
102
108
|
|
@@ -107,6 +113,17 @@ module KAG
|
|
107
113
|
end
|
108
114
|
end
|
109
115
|
|
116
|
+
def end_match(match)
|
117
|
+
if match[:server].has_rcon?
|
118
|
+
match[:server].kick_all
|
119
|
+
match[:server].disconnect
|
120
|
+
else
|
121
|
+
debug "NO RCON, so could not kick!"
|
122
|
+
end
|
123
|
+
@matches.delete(match[:server][:key])
|
124
|
+
send_channels_msg("Match at #{match[:server][:key]} finished!")
|
125
|
+
end
|
126
|
+
|
110
127
|
def remove_user_from_match(nick)
|
111
128
|
match = get_match_in(nick)
|
112
129
|
if match
|
@@ -136,7 +153,7 @@ module KAG
|
|
136
153
|
"#{ts.to_s}v#{ts.to_s} #{KAG::Config.instance[:match_type]}"
|
137
154
|
end
|
138
155
|
|
139
|
-
def check_for_new_match
|
156
|
+
def check_for_new_match
|
140
157
|
if @queue.length >= KAG::Config.instance[:match_size]
|
141
158
|
playing = []
|
142
159
|
@queue.each do |n,i|
|
@@ -181,10 +198,18 @@ module KAG
|
|
181
198
|
User(p).send(msg+" Red Team #{team2.join(", ")}") unless p.include?("player")
|
182
199
|
end
|
183
200
|
|
201
|
+
if server.has_rcon?
|
202
|
+
server.connect
|
203
|
+
server.restart_map
|
204
|
+
else
|
205
|
+
debug "Cannot restart map, no RCON!"
|
206
|
+
end
|
207
|
+
|
184
208
|
@matches[server[:key]] = {
|
185
209
|
:team1 => team1,
|
186
210
|
:team2 => team2,
|
187
|
-
:server => server
|
211
|
+
:server => server,
|
212
|
+
:end_votes => 0
|
188
213
|
}
|
189
214
|
end
|
190
215
|
end
|
@@ -246,6 +271,28 @@ module KAG
|
|
246
271
|
end
|
247
272
|
end
|
248
273
|
|
274
|
+
match /add (.+)/, method: :evt_add_admin
|
275
|
+
def evt_add_admin(m, arg)
|
276
|
+
if is_admin(m.user)
|
277
|
+
if m.channel.has_user?(arg)
|
278
|
+
add_user_to_queue(m,arg)
|
279
|
+
else
|
280
|
+
m.reply "User is not in this channel!"
|
281
|
+
end
|
282
|
+
end
|
283
|
+
end
|
284
|
+
|
285
|
+
match /is_admin (.+)/, method: :evt_am_i_admin
|
286
|
+
def evt_am_i_admin(m,nick)
|
287
|
+
u = User(nick)
|
288
|
+
if is_admin(u)
|
289
|
+
m.reply "Yes, #{nick} is an admin!"
|
290
|
+
else
|
291
|
+
m.reply "No, #{nick} is not an admin."
|
292
|
+
end
|
293
|
+
end
|
294
|
+
|
295
|
+
|
249
296
|
match "quit", method: :evt_quit
|
250
297
|
def evt_quit(m)
|
251
298
|
if is_admin(m.user)
|
@@ -253,12 +300,21 @@ module KAG
|
|
253
300
|
end
|
254
301
|
end
|
255
302
|
|
256
|
-
|
303
|
+
match "restart", method: :evt_restart
|
304
|
+
def evt_restart(m)
|
305
|
+
if is_admin(m.user)
|
306
|
+
cmd = (KAG::Config.instance[:restart_method] or "nohup sh gather.sh &")
|
307
|
+
puts cmd
|
308
|
+
pid = spawn cmd
|
309
|
+
debug "Restarting bot, new process ID is #{pid.to_s} ..."
|
310
|
+
exit
|
311
|
+
end
|
312
|
+
end
|
313
|
+
|
257
314
|
def get_team_classes(team)
|
258
315
|
classes = KAG::Config.instance[:classes]
|
259
316
|
classes.shuffle!
|
260
317
|
classes
|
261
318
|
end
|
262
|
-
|
263
319
|
end
|
264
320
|
end
|
data/lib/kag/server.rb
CHANGED
@@ -3,11 +3,129 @@ require 'symboltable'
|
|
3
3
|
require 'json'
|
4
4
|
require 'kagerator'
|
5
5
|
require 'kag/config'
|
6
|
+
require 'socket'
|
6
7
|
|
7
8
|
module KAG
|
8
9
|
class Server < SymbolTable
|
9
10
|
def info
|
10
11
|
Kagerator.server(self[:ip],self[:port])
|
11
12
|
end
|
13
|
+
|
14
|
+
def has_rcon?
|
15
|
+
self[:rcon_password] and !self[:rcon_password].empty?
|
16
|
+
end
|
17
|
+
|
18
|
+
def socket
|
19
|
+
unless self[:_socket]
|
20
|
+
self[:_socket] = TCPSocket.new(self[:ip],self[:port])
|
21
|
+
end
|
22
|
+
self[:_socket]
|
23
|
+
end
|
24
|
+
|
25
|
+
def connect
|
26
|
+
return false unless self.socket
|
27
|
+
self.socket.puts self[:rcon_password]
|
28
|
+
z = self.socket.gets
|
29
|
+
z.include?("now authenticated")
|
30
|
+
self[:_connected] = true
|
31
|
+
end
|
32
|
+
|
33
|
+
def disconnect
|
34
|
+
if self[:_socket]
|
35
|
+
self[:_socket].close
|
36
|
+
self[:_connected] = false
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def connected?
|
41
|
+
self[:_connected]
|
42
|
+
end
|
43
|
+
|
44
|
+
def _is_newline?(line)
|
45
|
+
line[10..line.length].rstrip.empty?
|
46
|
+
end
|
47
|
+
|
48
|
+
def _cycle
|
49
|
+
while (line = self.socket.gets)
|
50
|
+
puts line
|
51
|
+
break if _is_newline?(line)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def players
|
56
|
+
_command "/players"
|
57
|
+
|
58
|
+
players = []
|
59
|
+
while (line = self.socket.gets)
|
60
|
+
puts line
|
61
|
+
break if _is_newline?(line)
|
62
|
+
|
63
|
+
player = SymbolTable.new
|
64
|
+
line = line[10..line.length].strip
|
65
|
+
|
66
|
+
# get nick
|
67
|
+
sp = line.index("[")
|
68
|
+
next if sp == nil
|
69
|
+
ep = line.index("]",sp)
|
70
|
+
player[:nick] = line[(sp+1)..(ep-1)]
|
71
|
+
line = line[ep..line.length].strip
|
72
|
+
|
73
|
+
# get id
|
74
|
+
sp = line.index("(")
|
75
|
+
ep = line.index(")",sp)
|
76
|
+
player[:id] = line[(sp+4)..(ep-1)]
|
77
|
+
line = line[ep..line.length]
|
78
|
+
|
79
|
+
# get ip
|
80
|
+
sp = line.index("(")
|
81
|
+
ep = line.index(")",sp)
|
82
|
+
player[:ip] = line[(sp+4)..(ep-1)]
|
83
|
+
line = line[ep..line.length]
|
84
|
+
|
85
|
+
# get hwid
|
86
|
+
sp = line.index("(")
|
87
|
+
ep = line.index(")",sp)
|
88
|
+
player[:hwid] = line[(sp+6)..(ep-1)]
|
89
|
+
|
90
|
+
players << player
|
91
|
+
end
|
92
|
+
players
|
93
|
+
end
|
94
|
+
|
95
|
+
def kick(nick)
|
96
|
+
return false unless self.connected?
|
97
|
+
self.socket.puts "/kick #{nick}"
|
98
|
+
_cycle
|
99
|
+
end
|
100
|
+
|
101
|
+
def kick_all
|
102
|
+
ps = self.players
|
103
|
+
if ps
|
104
|
+
ps.each do |player|
|
105
|
+
_command "/kick #{player[:nick]}"
|
106
|
+
end
|
107
|
+
else
|
108
|
+
puts "No Players found on Server #{self[:ip]}!"
|
109
|
+
end
|
110
|
+
_cycle
|
111
|
+
end
|
112
|
+
|
113
|
+
def restart_map
|
114
|
+
_command "/restartmap"
|
115
|
+
_cycle
|
116
|
+
end
|
117
|
+
|
118
|
+
def next_map
|
119
|
+
_command "/nextmap"
|
120
|
+
_cycle
|
121
|
+
end
|
122
|
+
|
123
|
+
protected
|
124
|
+
|
125
|
+
def _command(cmd)
|
126
|
+
return false unless self.connected?
|
127
|
+
puts "[RCON] #{cmd.to_s}"
|
128
|
+
self.socket.puts cmd
|
129
|
+
end
|
12
130
|
end
|
13
131
|
end
|
data/lib/kag/version.rb
CHANGED