-
Notifications
You must be signed in to change notification settings - Fork 34
/
balance.py
620 lines (521 loc) · 25 KB
/
balance.py
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
# minqlx - A Quake Live server administrator bot.
# Copyright (C) 2015 Mino <[email protected]>
# This file is part of minqlx.
# minqlx is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# minqlx is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with minqlx. If not, see <http://www.gnu.org/licenses/>.
import minqlx
import requests
import itertools
import threading
import random
import time
RATING_KEY = "minqlx:players:{0}:ratings:{1}" # 0 == steam_id, 1 == short gametype.
MAX_ATTEMPTS = 3
CACHE_EXPIRE = 60*10 # 10 minutes TTL.
DEFAULT_RATING = 1500
UNTRACKED_RATING = 9999
SUPPORTED_GAMETYPES = ("ad", "ca", "ctf", "dom", "ft", "tdm")
# Externally supported game types. Used by !getrating for game types the API works with.
EXT_SUPPORTED_GAMETYPES = ("ad", "ca", "ctf", "dom", "ft", "tdm", "duel", "ffa")
class balance(minqlx.Plugin):
def __init__(self):
self.add_hook("round_countdown", self.handle_round_countdown)
self.add_hook("round_start", self.handle_round_start)
self.add_hook("vote_ended", self.handle_vote_ended)
self.add_hook("player_disconnect", self.handle_player_disconnect)
self.add_hook("new_game", self.handle_new_game)
self.add_command(("setrating", "setelo"), self.cmd_setrating, 3, usage="<id> <rating>")
self.add_command(("getrating", "getelo", "elo"), self.cmd_getrating, usage="<id> [gametype]")
self.add_command(("remrating", "remelo"), self.cmd_remrating, 3, usage="<id>")
self.add_command("balance", self.cmd_balance, 1)
self.add_command(("teams", "teens"), self.cmd_teams)
self.add_command("do", self.cmd_do, 1)
self.add_command(("agree", "a"), self.cmd_agree, client_cmd_perm=0)
self.add_command(("ratings", "elos", "selo"), self.cmd_ratings)
self.ratings_lock = threading.RLock()
# Keys: steam_id - Items: {"ffa": {"elo": 123, "games": 321, "local": False}, ...}
self.ratings = {}
# Keys: steam_id - Items: {"deactivated": true/false, "ratings": {...}, "allowRating": true/false, "privacy": "public/private/anonymous/untracked"}
self.player_info = {}
# Keys: request_id - Items: (players, callback, channel)
self.requests = {}
self.request_counter = itertools.count()
self.suggested_pair = None
self.suggested_agree = [False, False]
self.in_countdown = False
self.set_cvar_once("qlx_balanceUseLocal", "1")
self.set_cvar_once("qlx_balanceUrl", "qlstats.net")
self.set_cvar_once("qlx_balanceAuto", "1")
self.set_cvar_once("qlx_balanceMinimumSuggestionDiff", "25")
self.set_cvar_once("qlx_balanceApi", "elo")
self.cache_cvars()
def cache_cvars(self):
# Store some cvar values that are used in non-game threads
self.use_local = self.get_cvar("qlx_balanceUseLocal", bool)
self.api_url = "http://{}/{}/".format(self.get_cvar("qlx_balanceUrl"), self.get_cvar("qlx_balanceApi"))
def handle_round_countdown(self, *args, **kwargs):
if all(self.suggested_agree):
# If we don't delay the switch a bit, the round countdown sound and
# text disappears for some weird reason.
@minqlx.next_frame
def f():
self.execute_suggestion()
f()
self.in_countdown = True
def handle_round_start(self, *args, **kwargs):
self.in_countdown = False
def handle_vote_ended(self, votes, vote, args, passed):
if passed == True and vote == "shuffle" and self.get_cvar("qlx_balanceAuto", bool):
gt = self.game.type_short
if gt not in SUPPORTED_GAMETYPES:
return
@minqlx.delay(3.5)
def f():
players = self.teams()
if len(players["red"] + players["blue"]) % 2 != 0:
self.msg("Teams were ^6NOT^7 balanced due to the total number of players being an odd number.")
return
players = dict([(p.steam_id, gt) for p in players["red"] + players["blue"]])
self.add_request(players, self.callback_balance, minqlx.CHAT_CHANNEL)
f()
def handle_player_disconnect(self, player, reason):
self.clean_player_data(player)
def handle_new_game(self):
self.cache_cvars()
# reset ratings cache on start
if self.game.state == "warmup":
with self.ratings_lock:
self.ratings = {}
@minqlx.thread
def clean_player_data(self, player):
for p in self.players().copy():
if p.steam_id == player.steam_id and p.id != player.id:
# there is a second client with same steam id
return
if player.steam_id in self.player_info:
del self.player_info[player.steam_id]
with self.ratings_lock:
if player.steam_id in self.ratings:
del self.ratings[player.steam_id]
@minqlx.thread
def fetch_ratings(self, players, request_id):
if not players:
return
# We don't want to modify the actual dict, so we use a copy.
players = players.copy()
# Get local ratings if present in DB.
if self.use_local:
for steam_id in players.copy():
gt = players[steam_id]
key = RATING_KEY.format(steam_id, gt)
if key in self.db:
with self.ratings_lock:
if steam_id in self.ratings:
self.ratings[steam_id][gt] = {"games": -1, "elo": int(self.db[key]), "local": True, "time": -1}
else:
self.ratings[steam_id] = {gt: {"games": -1, "elo": int(self.db[key]), "local": True, "time": -1}}
del players[steam_id]
if not players:
self.handle_ratings_fetched(request_id, requests.codes.ok)
return
attempts = 0
last_status = 0
untracked_sids = []
while attempts < MAX_ATTEMPTS:
attempts += 1
url = self.api_url + "+".join([str(sid) for sid in players])
res = requests.get(url, headers={"X-QuakeLive-Map": self.game.map})
last_status = res.status_code
if res.status_code != requests.codes.ok:
continue
js = res.json()
if "players" not in js:
last_status = -1
continue
# Fill our ratings dict with the ratings we just got.
for p in js["players"]:
sid = int(p["steamid"])
del p["steamid"]
t = time.time()
with self.ratings_lock:
if sid not in self.ratings:
self.ratings[sid] = {}
for gt in p:
p[gt]["time"] = t
p[gt]["local"] = False
self.ratings[sid][gt] = p[gt]
if self.ratings[sid][gt]["elo"] == 0 and self.ratings[sid][gt]["games"] == 0:
self.ratings[sid][gt]["elo"] = DEFAULT_RATING
if sid in players and gt == players[sid]:
# The API gave us the game type we wanted, so we remove it.
del players[sid]
# Fill the rest of the game types the API didn't return but supports.
for gt in SUPPORTED_GAMETYPES:
if gt not in self.ratings[sid]:
self.ratings[sid][gt] = {"games": -1, "elo": DEFAULT_RATING, "local": False, "time": time.time()}
# If the API didn't return all the players, we set them to the default rating.
for sid in players:
with self.ratings_lock:
if sid not in self.ratings:
self.ratings[sid] = {}
self.ratings[sid][players[sid]] = {"games": -1, "elo": DEFAULT_RATING, "local": False, "time": time.time()}
# Setting ratings for untracked players.
if "untracked" in js:
untracked_sids = list(map( lambda sid: int(sid), js["untracked"]))
for gt in SUPPORTED_GAMETYPES:
for sid in untracked_sids:
with self.ratings_lock:
if sid not in self.ratings:
self.ratings[sid] = {}
self.ratings[sid][gt] = {"games": -1, "elo": UNTRACKED_RATING, "local": False, "time": time.time()}
# Saving player info
try:
for player, data in js["playerinfo"].items():
sid = int(player)
self.player_info[sid] = js["playerinfo"][player]
self.player_info[sid]["time"] = time.time()
except KeyError:
pass
break
if attempts == MAX_ATTEMPTS:
self.handle_ratings_fetched(request_id, last_status)
return
self.handle_ratings_fetched(request_id, requests.codes.ok)
@minqlx.next_frame
def handle_ratings_fetched(self, request_id, status_code):
players, callback, channel, args = self.requests[request_id]
del self.requests[request_id]
if status_code != requests.codes.ok:
# TODO: Put a couple of known errors here for more detailed feedback.
channel.reply("ERROR {}: Failed to fetch ratings.".format(status_code))
else:
callback(players, channel, *args)
def add_request(self, players, callback, channel, *args):
req = next(self.request_counter)
self.requests[req] = players.copy(), callback, channel, args
# Only start a new thread if we need to make an API request.
if self.remove_cached(players):
self.fetch_ratings(players, req)
else:
# All players were cached, so we tell it to go ahead and call the callbacks.
self.handle_ratings_fetched(req, requests.codes.ok)
def remove_cached(self, players):
with self.ratings_lock:
for sid in players.copy():
gt = players[sid]
if sid in self.ratings and gt in self.ratings[sid]:
t = self.ratings[sid][gt]["time"]
if t == -1 or time.time() < t + CACHE_EXPIRE:
del players[sid]
return players
def cmd_getrating(self, player, msg, channel):
if len(msg) == 1:
sid = player.steam_id
else:
try:
sid = int(msg[1])
target_player = None
if 0 <= sid < 64:
target_player = self.player(sid)
sid = target_player.steam_id
except ValueError:
player.tell("Invalid ID. Use either a client ID or a SteamID64.")
return minqlx.RET_STOP_ALL
except minqlx.NonexistentPlayerError:
player.tell("Invalid client ID. Use either a client ID or a SteamID64.")
return minqlx.RET_STOP_ALL
if len(msg) > 2:
if msg[2].lower() in EXT_SUPPORTED_GAMETYPES:
gt = msg[2].lower()
else:
player.tell("Invalid gametype. Supported gametypes: {}"
.format(", ".join(EXT_SUPPORTED_GAMETYPES)))
return minqlx.RET_STOP_ALL
else:
gt = self.game.type_short
if gt not in EXT_SUPPORTED_GAMETYPES:
player.tell("This game mode is not supported by the balance plugin.")
return minqlx.RET_STOP_ALL
self.add_request({sid: gt}, self.callback_getrating, channel, gt)
def callback_getrating(self, players, channel, gametype):
sid = next(iter(players))
player = self.player(sid)
if player:
name = player.name
else:
name = sid
channel.reply("{} has a rating of ^6{}^7 in {}.".format(name, self.ratings[sid][gametype]["elo"], gametype.upper()))
def cmd_setrating(self, player, msg, channel):
if len(msg) < 3:
return minqlx.RET_USAGE
try:
sid = int(msg[1])
target_player = None
if 0 <= sid < 64:
target_player = self.player(sid)
sid = target_player.steam_id
except ValueError:
player.tell("Invalid ID. Use either a client ID or a SteamID64.")
return minqlx.RET_STOP_ALL
except minqlx.NonexistentPlayerError:
player.tell("Invalid client ID. Use either a client ID or a SteamID64.")
return minqlx.RET_STOP_ALL
try:
rating = int(msg[2])
except ValueError:
player.tell("Invalid rating.")
return minqlx.RET_STOP_ALL
if target_player:
name = target_player.name
else:
name = sid
gt = self.game.type_short
self.db[RATING_KEY.format(sid, gt)] = rating
# If we have the player cached, set the rating.
with self.ratings_lock:
if sid in self.ratings and gt in self.ratings[sid]:
self.ratings[sid][gt]["elo"] = rating
self.ratings[sid][gt]["local"] = True
self.ratings[sid][gt]["time"] = -1
channel.reply("{}'s {} rating has been set to ^6{}^7.".format(name, gt.upper(), rating))
def cmd_remrating(self, player, msg, channel):
if len(msg) < 2:
return minqlx.RET_USAGE
try:
sid = int(msg[1])
target_player = None
if 0 <= sid < 64:
target_player = self.player(sid)
sid = target_player.steam_id
except ValueError:
player.tell("Invalid ID. Use either a client ID or a SteamID64.")
return minqlx.RET_STOP_ALL
except minqlx.NonexistentPlayerError:
player.tell("Invalid client ID. Use either a client ID or a SteamID64.")
return minqlx.RET_STOP_ALL
if target_player:
name = target_player.name
else:
name = sid
gt = self.game.type_short
del self.db[RATING_KEY.format(sid, gt)]
# If we have the player cached, remove the game type.
with self.ratings_lock:
if sid in self.ratings and gt in self.ratings[sid]:
del self.ratings[sid][gt]
channel.reply("{}'s locally set {} rating has been deleted.".format(name, gt.upper()))
def cmd_balance(self, player, msg, channel):
gt = self.game.type_short
if gt not in SUPPORTED_GAMETYPES:
player.tell("This game mode is not supported by the balance plugin.")
return minqlx.RET_STOP_ALL
teams = self.teams()
if len(teams["red"] + teams["blue"]) % 2 != 0:
player.tell("The total number of players should be an even number.")
return minqlx.RET_STOP_ALL
players = dict([(p.steam_id, gt) for p in teams["red"] + teams["blue"]])
self.add_request(players, self.callback_balance, minqlx.CHAT_CHANNEL)
def callback_balance(self, players, channel):
# We check if people joined while we were requesting ratings and get them if someone did.
teams = self.teams()
current = teams["red"] + teams["blue"]
gt = self.game.type_short
for p in current:
if p.steam_id not in players:
d = dict([(p.steam_id, gt) for p in current])
self.add_request(d, self.callback_balance, channel)
return
# Start out by evening out the number of players on each team.
diff = len(teams["red"]) - len(teams["blue"])
if abs(diff) > 1:
if diff > 0:
for i in range(diff - 1):
p = teams["red"].pop()
p.put("blue")
teams["blue"].append(p)
elif diff < 0:
for i in range(abs(diff) - 1):
p = teams["blue"].pop()
p.put("red")
teams["red"].append(p)
# Start shuffling by looping through our suggestion function until
# there are no more switches that can be done to improve teams.
switch = self.suggest_switch(teams, gt)
if switch:
while switch:
p1 = switch[0][0]
p2 = switch[0][1]
self.switch(p1, p2)
teams["blue"].append(p1)
teams["red"].append(p2)
teams["blue"].remove(p2)
teams["red"].remove(p1)
switch = self.suggest_switch(teams, gt)
avg_red = self.team_average(teams["red"], gt)
avg_blue = self.team_average(teams["blue"], gt)
diff_rounded = abs(round(avg_red) - round(avg_blue)) # Round individual averages.
if round(avg_red) > round(avg_blue):
self.msg("^1{} ^7vs ^4{}^7 - DIFFERENCE: ^1{}"
.format(round(avg_red), round(avg_blue), diff_rounded))
elif round(avg_red) < round(avg_blue):
self.msg("^1{} ^7vs ^4{}^7 - DIFFERENCE: ^4{}"
.format(round(avg_red), round(avg_blue), diff_rounded))
else:
self.msg("^1{} ^7vs ^4{}^7 - Holy shit!"
.format(round(avg_red), round(avg_blue)))
else:
channel.reply("Teams are good! Nothing to balance.")
return True
def cmd_teams(self, player, msg, channel):
gt = self.game.type_short
if gt not in SUPPORTED_GAMETYPES:
player.tell("This game mode is not supported by the balance plugin.")
return minqlx.RET_STOP_ALL
teams = self.teams()
if len(teams["red"]) != len(teams["blue"]):
player.tell("Both teams should have the same number of players.")
return minqlx.RET_STOP_ALL
teams = dict([(p.steam_id, gt) for p in teams["red"] + teams["blue"]])
self.add_request(teams, self.callback_teams, channel)
def callback_teams(self, players, channel):
# We check if people joined while we were requesting ratings and get them if someone did.
teams = self.teams()
current = teams["red"] + teams["blue"]
gt = self.game.type_short
for p in current:
if p.steam_id not in players:
d = dict([(p.steam_id, gt) for p in current])
self.add_request(d, self.callback_teams, channel)
return
avg_red = self.team_average(teams["red"], gt)
avg_blue = self.team_average(teams["blue"], gt)
switch = self.suggest_switch(teams, gt)
diff_rounded = abs(round(avg_red) - round(avg_blue)) # Round individual averages.
if round(avg_red) > round(avg_blue):
channel.reply("^1{} ^7vs ^4{}^7 - DIFFERENCE: ^1{}"
.format(round(avg_red), round(avg_blue), diff_rounded))
elif round(avg_red) < round(avg_blue):
channel.reply("^1{} ^7vs ^4{}^7 - DIFFERENCE: ^4{}"
.format(round(avg_red), round(avg_blue), diff_rounded))
else:
channel.reply("^1{} ^7vs ^4{}^7 - Holy shit!"
.format(round(avg_red), round(avg_blue)))
minimum_suggestion_diff = self.get_cvar("qlx_balanceMinimumSuggestionDiff", float)
if switch and switch[1] >= minimum_suggestion_diff:
channel.reply("SUGGESTION: switch ^6{}^7 with ^6{}^7. Mentioned players can type !a to agree."
.format(switch[0][0].clean_name, switch[0][1].clean_name))
if not self.suggested_pair or self.suggested_pair[0] != switch[0][0] or self.suggested_pair[1] != switch[0][1]:
self.suggested_pair = (switch[0][0], switch[0][1])
self.suggested_agree = [False, False]
else:
i = random.randint(0, 99)
if not i:
channel.reply("Teens look ^6good!")
else:
channel.reply("Teams look good!")
self.suggested_pair = None
return True
def cmd_do(self, player, msg, channel):
"""Forces a suggested switch to be done."""
if self.suggested_pair:
self.execute_suggestion()
def cmd_agree(self, player, msg, channel):
"""After the bot suggests a switch, players in question can use this to agree to the switch."""
if self.suggested_pair and not all(self.suggested_agree):
p1, p2 = self.suggested_pair
if p1 == player:
self.suggested_agree[0] = True
elif p2 == player:
self.suggested_agree[1] = True
if all(self.suggested_agree):
# If the game's in progress and we're not in the round countdown, wait for next round.
if self.game.state == "in_progress" and not self.in_countdown:
self.msg("The switch will be executed at the start of next round.")
return
# Otherwise, switch right away.
self.execute_suggestion()
def cmd_ratings(self, player, msg, channel):
gt = self.game.type_short
if gt not in EXT_SUPPORTED_GAMETYPES:
player.tell("This game mode is not supported by the balance plugin.")
return minqlx.RET_STOP_ALL
players = dict([(p.steam_id, gt) for p in self.players()])
self.add_request(players, self.callback_ratings, channel)
def callback_ratings(self, players, channel):
# We check if people joined while we were requesting ratings and get them if someone did.
teams = self.teams()
current = self.players()
gt = self.game.type_short
for p in current:
if p.steam_id not in players:
d = dict([(p.steam_id, gt) for p in current])
self.add_request(d, self.callback_ratings, channel)
return
if teams["free"]:
free_sorted = sorted(teams["free"], key=lambda x: self.ratings[x.steam_id][gt]["elo"], reverse=True)
free = ", ".join(["{}: ^6{}^7".format(p.clean_name, self.ratings[p.steam_id][gt]["elo"]) for p in free_sorted])
channel.reply(free)
if teams["red"]:
red_sorted = sorted(teams["red"], key=lambda x: self.ratings[x.steam_id][gt]["elo"], reverse=True)
red = ", ".join(["{}: ^1{}^7".format(p.clean_name, self.ratings[p.steam_id][gt]["elo"]) for p in red_sorted])
channel.reply(red)
if teams["blue"]:
blue_sorted = sorted(teams["blue"], key=lambda x: self.ratings[x.steam_id][gt]["elo"], reverse=True)
blue = ", ".join(["{}: ^4{}^7".format(p.clean_name, self.ratings[p.steam_id][gt]["elo"]) for p in blue_sorted])
channel.reply(blue)
if teams["spectator"]:
spec_sorted = sorted(teams["spectator"], key=lambda x: self.ratings[x.steam_id][gt]["elo"], reverse=True)
spec = ", ".join(["{}: {}".format(p.clean_name, self.ratings[p.steam_id][gt]["elo"]) for p in spec_sorted])
channel.reply(spec)
def suggest_switch(self, teams, gametype):
"""Suggest a switch based on average team ratings."""
avg_red = self.team_average(teams["red"], gametype)
avg_blue = self.team_average(teams["blue"], gametype)
cur_diff = abs(avg_red - avg_blue)
min_diff = 999999
best_pair = None
for red_p in teams["red"]:
for blue_p in teams["blue"]:
r = teams["red"].copy()
b = teams["blue"].copy()
b.append(red_p)
r.remove(red_p)
r.append(blue_p)
b.remove(blue_p)
avg_red = self.team_average(r, gametype)
avg_blue = self.team_average(b, gametype)
diff = abs(avg_red - avg_blue)
if diff < min_diff:
min_diff = diff
best_pair = (red_p, blue_p)
if min_diff < cur_diff:
return (best_pair, cur_diff - min_diff)
else:
return None
def team_average(self, team, gametype):
"""Calculates the average rating of a team."""
avg = 0
if team:
for p in team:
avg += self.ratings[p.steam_id][gametype]["elo"]
avg /= len(team)
return avg
def execute_suggestion(self):
p1, p2 = self.suggested_pair
try:
p1.update()
p2.update()
except minqlx.NonexistentPlayerError:
return
if p1.team != "spectator" and p2.team != "spectator":
self.switch(self.suggested_pair[0], self.suggested_pair[1])
self.suggested_pair = None
self.suggested_agree = [False, False]