forked from bobrippling/ps4bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PS4Bot.py
985 lines (812 loc) · 36.8 KB
/
PS4Bot.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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
from collections import defaultdict
import datetime
import random
import sys
import re
import traceback
from Functional import find
from Bot import Bot
from SlackPostedMessage import SlackPostedMessage
from PS4Game import Game, GameStates
from PS4Formatting import format_user, format_user_padding, when_str, number_emojis, generate_table
from PS4Config import PLAY_TIME, GAME_FOLLOWON_TIME
from PS4Parsing import parse_time, deserialise_time, parse_game_initiation, \
pretty_mode, parse_stats_request, date_with_year
from PS4History import PS4History, Keys
from PS4GameCategory import vote_message, Stats, channel_statmap, suggest_teams
DIALECT = ["here", "hew", "areet"]
BIG_GAME_REGEX = re.compile(".*(big|large|medium|huge|hueg|massive|medium|micro|mini|biggest) game.*")
SAVE_FILE = "ps4-games.txt"
class UserOption:
mute = "mute"
class StatRequest:
stats = "stats"
elo = "elo"
# command => (show-in-usage, handler)
PS4Bot_commands = {
# args given are self, message, rest
"nar": (True, lambda self, *args: self.maybe_cancel_game(*args)),
"games": (True, lambda self, *args: self.show_games()),
"flyin": (True, lambda self, *args: self.join_or_bail(*args)),
"bail": (True, lambda self, *args: self.join_or_bail(*args, bail = True)),
"scuttle": (True, lambda self, *args: self.maybe_scuttle_game(*args)),
StatRequest.stats: (True, lambda self, *args: self.handle_stats_request(*args)),
StatRequest.elo: (True, lambda self, *args: self.handle_stats_request(*args, type=StatRequest.elo)),
"credits": (True, lambda self, *args: self.send_credits(*args)),
"topradge": (False, lambda self, *args: self.handle_stats_request(*args)),
"thanks": (False, lambda self, *args: self.send_thanks_reply(*args)),
"ta": (False, lambda self, *args: self.send_thanks_reply(*args)),
"cheers": (False, lambda self, *args: self.send_thanks_reply(*args)),
}
def replace_dict(str, dict):
for k in dict:
str = str.replace("%" + k, dict[k])
return str
def plural(int):
return "" if int == 1 else "s"
class LatestStats:
def __init__(self, timestamp = None, year = None):
self.timestamp = timestamp
self.year = year
class PS4Bot(Bot):
def __init__(self, slackconnection, botname):
Bot.__init__(self, slackconnection, botname)
self.icon_emoji = ":video_game:"
self.games = []
self.user_options = defaultdict(set) # name => set([flag1, flag2...])
self.history = PS4History(negative_stats = set([Stats.scrub]))
self.latest_stats_table = defaultdict(LatestStats) # channel => LatestStats
self.load()
def load(self):
try:
with open(SAVE_FILE, "r") as f:
for line in iter(f.readline, ""):
line = line.rstrip("\n")
if len(line) == 0:
continue
if line[:6] == "stats ":
tokens = line.split()
self.latest_stats_table[tokens[1]] = LatestStats(
tokens[2],
date_with_year(int(tokens[3])) if len(tokens) > 3 else None)
continue
if line[:5] == "user ":
tokens = line.split()
self.user_options[tokens[1]] = set(tokens[2:])
continue
tokens = line.split(" ", 8)
if len(tokens) != 9:
print "invalid line \"{}\"".format(line)
continue
str_when, channel, creator, str_state, \
str_players, str_max_player_count, str_play_time, \
mode, description = tokens
timestamp_str = f.readline()
if timestamp_str == "":
print "early EOF"
break
timestamp_str = timestamp_str.rstrip("\n")
msg_channel = f.readline()
if msg_channel == "":
print "early EOF"
break
msg_channel = msg_channel.rstrip("\n")
extra_text = []
line = f.readline()
while line != "":
line = line.rstrip("\n")
if len(line) == 0:
break
extra_text.append(line)
line = f.readline()
when = deserialise_time(str_when)
try:
max_player_count = int(str_max_player_count)
except ValueError:
print "invalid max player count \"{}\"".format(str_max_player_count)
continue
try:
play_time = int(str_play_time)
except ValueError:
print "invalid play_time \"{}\"".format(str_play_time)
continue
try:
state = int(str_state)
except ValueError:
print "invalid state \"{}\"".format(str_state)
continue
players = str_players.split(",")
message = SlackPostedMessage(msg_channel, timestamp_str, "\n".join(extra_text))
g = self.new_game(when, description, channel, creator, \
message, max_player_count, play_time, \
mode if mode != "None" else None, state)
for p in players:
if len(p):
g.add_player(p)
except IOError:
pass
def save(self):
try:
with open(SAVE_FILE, "w") as f:
for g in self.games:
print >>f, "{} {} {} {} {} {} {} {} {}".format(
when_str(g.when),
g.channel,
g.creator,
g.state,
",".join(g.players),
g.max_player_count,
g.play_time,
g.mode or "None",
g.description)
msg = g.message
print >>f, "{}\n{}\n{}".format(msg.timestamp, msg.channel, msg.text)
print >>f, ""
for channel, latest in self.latest_stats_table.iteritems():
print >>f, "stats {} {} {}".format(channel, latest.timestamp, latest.year.year if latest.year else "")
for user, options in self.user_options.iteritems():
if len(options):
print >>f, "user {} {}".format(user, " ".join(options))
except IOError as e:
print >>sys.stderr, "exception saving state: {}".format(e)
self.history.save()
def game_occuring_at(self, when):
for game in self.games:
if game.contains(when):
return game
return None
def game_overlapping(self, when, play_time, ignoring = None):
when_end = when + datetime.timedelta(minutes = play_time)
for game in self.games:
if game == ignoring:
continue
if game.contains(when) or game.contains(when_end, start_overlap = False):
return game
return None
def game_straight_after(self, previous, threshold):
threshold_delta = datetime.timedelta(minutes = threshold)
for game in self.games:
endtime = previous.endtime()
if endtime <= game.when < endtime + threshold_delta:
return game
return None
def games_created_by(self, user):
return filter(lambda g: g.creator == user, self.games)
def games_in_channel(self, channel):
return filter(lambda g: g.channel == channel, self.games)
def new_game(
self,
when, desc, channel, creator, msg,
max_players, play_time, mode,
state = GameStates.scheduled
):
g = Game(when, desc, channel, creator, msg, max_players, play_time, mode, state)
self.games.append(g)
self.history.add_game(g)
return g
def history_sync(self):
# push player updates to history
updated = False
for g in self.games:
h = self.history.find_game(g.message.timestamp)
if not h:
continue
h.players = g.players[:]
updated = True
if updated:
self.history.save()
def load_banter(self, type, replacements = {}, for_user = None, in_channel = None):
is_champ = False
if for_user:
if not in_channel:
raise TypeError
year = self.latest_stats_table[in_channel].year
user_ranking = self.history.user_ranking(in_channel, year = year)
try:
is_champ = user_ranking.index(for_user) < 3
except ValueError:
pass
searching_type = type
if is_champ:
searching_type = type + "-champ"
try:
msgs = []
with open("ps4-banter.txt", "r") as f:
while True:
line = f.readline()
if line == "":
break
line = line.rstrip("\n")
if len(line) == 0 or line[0] == "#":
continue
tokens = line.split(":", 1)
if len(tokens) != 2:
print >>sys.stderr, "invalid banter line %s" % line
continue
if tokens[0] != searching_type:
continue
msg = tokens[1].strip()
msgs.append(msg)
if len(msgs):
r = random.randint(0, len(msgs) - 1)
return replace_dict(msgs[r], replacements)
except IOError as e:
print >>sys.stderr, "exception loading banter: {}".format(e)
if type == "joined":
return "welcome to the game"
if type == "created":
return "game registered, gg"
if type == "kickoff":
return "match kickoff is now"
if type == "dialect":
return "areet"
if type == "thanked":
return "nee bother"
if type == "follow-on":
return ""
return "?"
def maybe_new_game(self, user, channel, rest):
"""
Attempts to create a new game from freeform text
Returns True on parse success (even if game creation failed)
"""
parsed = parse_game_initiation(rest, channel)
if not parsed:
return False
when, desc, max_player_count, play_time, mode = parsed
if len(desc) == 0:
desc = "big game"
game = self.game_overlapping(when, play_time)
if game:
self.send_duplicate_game_message(game)
return True
banter = self.load_banter(
"created",
{ "s": format_user(user) },
for_user = user,
in_channel = channel)
message = Game.create_message(banter, desc, when, max_player_count, mode, channel)
posted_message = self.send_message(message)
game = self.new_game(when, desc, channel, user, \
posted_message, max_player_count, play_time, mode)
self.add_user_to_game(user, game, subtle_message = True)
return True
def chronological_games(self):
def cmp_games(a, b):
if a.when > b.when:
return 1
if a.when < b.when:
return -1
return 0
return sorted(self.games, cmp_games)
def show_games(self):
if len(self.games) == 0:
self.send_message(":warning: no games, are people actually doing work??")
return
self.send_message("{0} game{1}:\n{2}".format(
len(self.games),
plural(len(self.games)),
"\n".join([g.pretty() for g in self.chronological_games()])
))
def update_game_message(self, game, subtle_addition = None):
if not game.message:
return
players = ""
if len(game.players):
players = "\nplayers: {} `{}`".format(game.pretty_players(), len(game.players))
newtext = game.message.text + players + ("\nlatest news: " + subtle_addition if subtle_addition else "")
self.update_message(newtext, original_message = game.message)
def join_or_bail(self, message, rest, bail = False):
if len(rest) == 0:
channel_games = self.games_in_channel(message.channel)
if len(channel_games) == 1:
game = channel_games[0]
else:
self.send_not1_games_message(channel_games)
return
else:
try:
when = parse_time(rest)
except ValueError:
self.send_message(":warning: howay! `flyin/join/flyout/bail <game-time>`")
return
game = self.game_occuring_at(when)
if not game:
self.send_game_not_found(when, message.user)
return
if bail:
self.remove_user_from_game(message.user, game)
else:
self.add_user_to_game(message.user, game)
def add_user_to_game(self, user, game, subtle_message = False):
if len(game.players) >= game.max_player_count:
banter = ":warning: game's full, rip {0}".format(format_user(user))
if subtle_message:
self.update_game_message(game, banter)
else:
self.send_message(banter)
return
if not game.add_player(user):
banter = "you're already in the '{}' game {}".format(game.description, format_user(user))
else:
banter = self.load_banter(
"joined",
{ "s": format_user(user), "d": game.description },
for_user = user,
in_channel = game.channel)
self.history_sync()
if not subtle_message:
self.send_message(banter)
self.update_game_message(game, banter if subtle_message else None)
self.save()
def remove_user_from_game(self, user, game, subtle_message = False):
if game.remove_player(user):
banter = ":candle: {}".format(format_user(user))
else:
if subtle_message:
# don't say anything - they're silently removing their failed join-attmept-emoji
return
banter = ":warning: you're not in the {} game {} (\"{}\")".format(
when_str(game.when),
format_user(user),
game.description)
self.history_sync()
if not subtle_message:
self.send_message(banter)
self.update_game_message(game, banter if subtle_message else None)
self.save()
def send_game_not_found(self, when, user):
if random.randint(0, 1) == 0:
self.send_message(":warning: {0}, there isnae game at {1}".format(format_user(user), when_str(when)))
else:
self.send_message(":warning: scrubadubdub, there's no game at {}".format(when_str(when)))
def send_scuttle_usage(self):
self.send_message(":warning: scrubadubdub, try something like \"scuttle 16:00 to 3:30pm\"")
def send_duplicate_game_message(self, game):
self.send_message(":warning: there's already a {} game at {}: {}. rip :candle:".format(
game.channel,
when_str(game.when),
game.description))
def send_not1_games_message(self, channel_games):
if len(channel_games) > 1:
msg = ":warning: there's {} games in this channel, which do you mean?".format(
len(channel_games))
else:
msg = ":warning: there's no games in this channel to cancel"
self.send_message(msg)
def send_too_many_owned_games_message(self, owned_games, verb):
if len(owned_games) == 0:
self.send_message(":warning: scrubadubdub, you've got no games to {}".format(verb))
return
self.send_message(":warning: scrubadubdub, which of your {} game{} do you want to {}?".format(
len(owned_games),
plural(len(owned_games)),
verb))
def maybe_cancel_game(self, message, rest):
user = message.user
if len(rest) == 0:
user_games = self.games_created_by(message.user)
if len(user_games) == 1:
game = user_games[0]
else:
self.send_too_many_owned_games_message(user_games, "cancel")
return
else:
try:
when = parse_time(rest)
except ValueError:
self.send_message(":warning: scrubadubdub, when's this game you want to cancel?".format(rest))
return
game = self.game_occuring_at(when)
if not game:
self.send_game_not_found(when, user)
return
if game.creator != user:
self.send_message(":warning: scrubadubdub, only {} can cancel the {} {}".format(
format_user(game.creator), when_str(game.when), game.description))
return
self.games = filter(lambda g: g != game, self.games)
rip_players = game.pretty_players(with_creator = False)
rip_players_message = " (just burn some time on kimble instead {})".format(rip_players) \
if len(rip_players) else ""
self.send_message(":candle: {}'s {} ({}) has been flown out by {}{}".format(
game.channel,
game.description,
when_str(game.when),
format_user(user),
rip_players_message))
newtext = game.message.text + "\n:warning: Cancelled :warning: :candle::candle:"
self.update_message(newtext, original_message = game.message)
self.history.cancel_game(game)
self.save()
def maybe_scuttle_game(self, message, rest):
tokens = rest.split(" ")
if len(tokens) >= 3 and tokens[-2] == "to":
str_to = tokens[-1]
str_from = " ".join(tokens[:-2])
elif len(tokens) == 1:
str_from = None
str_to = tokens[0]
else:
self.send_scuttle_usage()
return
try:
when_desc = None
when_to = parse_time(str_to)
except ValueError:
self.send_scuttle_usage()
return
try:
when_from = parse_time(str_from) if str_from else None
except ValueError:
when_desc = str_from
if when_desc:
game_to_move = None
for g in self.games:
if g.description == when_desc:
if game_to_move:
self.send_message(
":warning: scrubadubdub - there's multiple games called \"{}\"".format(
when_desc))
return
game_to_move = g
elif when_from:
# we've been given an explicit game to move
game_to_move = self.game_occuring_at(when_from)
if not game_to_move:
self.send_game_not_found(when_from, message.user)
return
else:
# no explicit game to move, if the user has just one, move it
created_games = self.games_created_by(message.user)
if len(created_games) != 1:
self.send_too_many_owned_games_message(created_games, "scuttle")
return
game_to_move = created_games[0]
if game_to_move.creator != message.user:
self.send_message(":warning: scrubadubdub, only {} can scuttle the {} {}".format(
format_user(game_to_move.creator),
when_str(game_to_move.when),
game_to_move.description))
return
game_in_slot = self.game_overlapping(when_to, game_to_move.play_time, ignoring = game_to_move)
if game_in_slot:
self.send_duplicate_game_message(game_in_slot)
return
old_when = game_to_move.when
banter = self.load_banter(
"created",
{ "s": format_user(message.user) },
for_user = message.user,
in_channel = game_to_move.channel)
game_to_move.update_when(when_to, banter)
self.update_game_message(game_to_move, "moved by {} to {}".format(
format_user(message.user), when_str(when_to)))
pretty_players = game_to_move.pretty_players(with_creator = False)
self.send_message(":alarm_clock: {}{} moved from {} to {} by {}".format(
pretty_players + " - " if len(pretty_players) else "",
game_to_move.description,
when_str(old_when),
when_str(when_to),
format_user(message.user)))
self.save()
def send_dialect_reply(self, message):
reply = self.load_banter(
"dialect",
{ "u": format_user(message.user) },
for_user = message.user,
in_channel = message.channel.name)
self.send_message(reply)
def send_thanks_reply(self, message, rest):
reply = self.load_banter(
"thanked",
{ "s": format_user(message.user) },
for_user = message.user,
in_channel = message.channel.name)
self.send_message(reply)
def send_credits(self, message, rest):
self.send_message(
"\n\n:film_projector: Credits :clapper:" +
"\n-------------------" +
"\n:toilet: Barely functional codebase: <@rpilling>" +
"\n:bee: Codebase fluffer: <@jpearce>" +
"\n:ship: Boom Operator: <@danallsop>" +
"\n:survival-steve: Localisation: <@sjob>" +
"\n:movie_camera: Cinematographer: <@danallsop>" +
"\n:muscle: More localisation: <@morchard>" +
"\n:scroll: Banter: <@danallsop>" +
"\n:javascript: More banter: <@craigayre>" +
"\n:mantelpiece_clock: Scheduling advisor: <@eclennett>")
def update_stats_table(self, channel, stats, \
force_new = False, anchor_message = True, last_updated_user_stat = None):
"""
This method is responsible for taking the stats dictionary and converting it to a
table, with prettified headers and sorted rows.
"""
tables = [] # [(mode, table), ...]
for mode in stats:
modestats = stats[mode]
allstats = set()
for v in modestats.values():
allstats.update(v.keys())
allstats = list(allstats)
allstats.sort()
if Keys.game_wins in allstats:
# ensure relative ordering
special_keys = [Keys.game_wins, Keys.played, Keys.winratio]
allstats = filter(lambda s: s not in special_keys, allstats)
allstats.append(Keys.game_wins)
allstats.append(Keys.played)
allstats.append(Keys.winratio)
stats_to_ignore = list(self.history.negative_stats)
stats_to_ignore.append(Keys.game_wins)
stats_to_ignore.append(Keys.played)
stats_to_ignore.append(Keys.winratio)
relevant_stats = filter(lambda s: s not in stats_to_ignore, allstats)
if len(relevant_stats) == 1:
# no need to show the game_wins field
allstats.remove(Keys.game_wins)
def stat_for_user(user_stats):
user, users_stats = user_stats
def get_stat_value(stat):
value = users_stats[stat]
# maybe highlight the value, if it was the latest
if last_updated_user_stat:
last_u, last_s = last_updated_user_stat
if user == last_u and stat == last_s:
return "[{}]".format(value)
return value
if UserOption.mute in self.user_options[user]:
# we won't be @ing this user
user_name = user
user_padding = 0
else:
padding_for_slackat = -2
user_name = format_user(user)
user_padding = format_user_padding(user) + padding_for_slackat
return [(user_padding, user_name)] \
+ map(get_stat_value, allstats)
def stats_sort_key(stats):
# sort on the last statistic
return stats[len(stats) - 1]
header = ["Player"] + map(Stats.pretty, allstats)
stats_per_user = map(stat_for_user, modestats.iteritems())
stats_per_user.sort(key = stats_sort_key, reverse = True)
table = generate_table(header, stats_per_user)
tables.append((mode, table))
if len(tables) == 0:
self.send_message(":warning: no stats for \"{}\"".format(channel))
return
if len(tables) > 1:
def mode_and_table_to_str(mode_and_table):
mode = pretty_mode(mode_and_table[0])
table = mode_and_table[1]
return "{}\n{}".format(mode or "Normal", table)
tables_message_str = "\n".join(map(mode_and_table_to_str, tables))
else:
# no need for mode string
tables_message_str = tables[0][1]
if not force_new and channel in self.latest_stats_table:
# update the table instead
table_msg = None
now = datetime.datetime.today().strftime("%Y-%m-%d %H:%M:%S")
self.update_message(
"{}\n:stopwatch: Last updated {}{}".format(
tables_message_str,
now,
" (last updated stat in `[brackets]`)" if last_updated_user_stat else ""),
original_timestamp = self.latest_stats_table[channel].timestamp,
original_channel = channel)
else:
table_msg = self.send_message(tables_message_str)
if table_msg and anchor_message:
self.latest_stats_table[channel].timestamp = table_msg.timestamp
# type: "basic" | "elo"
def handle_stats_request(self, message, rest, type=StatRequest.stats):
anchor_message = True
channel_name = None
year = None
k_factor = None
history_length = None
if len(rest):
parsed = parse_stats_request(rest)
if not parsed:
self.send_message(":warning: ere {}: \"{} [year] [channel]\"".format(
type, format_user(message.user)))
return
channel_name, year, k_factor, history_length = parsed
anchor_message = (channel_name is None or channel_name == message.channel.name) \
and (year is None or year.year == datetime.date.today().year)
if not channel_name:
channel_name = message.channel.name
if type == StatRequest.elo:
rankings = self.history.summary_elo(
channel_name,
year = year,
k_factor = k_factor
)
if history_length:
ranking_values = map(lambda ranking: [ranking.getName(), ranking.games_played, ranking.getFormattedRanking(), ranking.getHistory(history_length)], rankings.values())
ranking_values.sort(key=lambda x: (x[1] > 10, x[2]), reverse=True)
table = generate_table(['Player', 'Games Played', 'Ranking', 'Form'], ranking_values)
else:
ranking_values = map(lambda ranking: [ranking.getName(), ranking.games_played, ranking.getFormattedRanking()], rankings.values())
ranking_values.sort(key=lambda x: (x[1] > 10, x[2]), reverse=True)
table = generate_table(['Player', 'Games Played', 'Ranking'], ranking_values)
self.send_message(table)
else:
stats = self.history.summary_stats(channel_name, year = year)
self.update_stats_table(channel_name, stats, force_new = True, anchor_message = anchor_message)
self.latest_stats_table[channel_name].year = year
def handle_command(self, message, command, rest):
if len(command.strip()) == 0 and len(rest) == 0:
self.send_dialect_reply(message)
return
if command.lower() in PS4Bot_commands:
PS4Bot_commands[command.lower()][1](self, message, rest)
return
# attempt to parse a big game, if unsuccessful, show usage:
if not self.maybe_new_game(message.user, message.channel.name, command + " " + rest):
self.send_message((
":warning: Hew {}, here's what I listen to: `{} {}`, " +
"or try adding a :+1: to a game invite (or typing `+:+1:` as a response)."
).format(
format_user(message.user),
self.botname,
"/".join(command for command, (show, _) in PS4Bot_commands.iteritems() if show)
))
def update_game_states(self):
now = datetime.datetime.today()
fiveminutes = datetime.timedelta(minutes = 5)
twelvehours = datetime.timedelta(hours = 12)
for g in self.games:
if g.state == GameStates.scheduled:
if now > g.when - fiveminutes:
g.state = GameStates.active
elif g.state == GameStates.active:
if now > g.endtime():
g.state = GameStates.finished
elif g.state == GameStates.finished:
timestamp_date = datetime.datetime.fromtimestamp(float(g.message.timestamp))
if now - timestamp_date > twelvehours:
g.state = GameStates.dead
def handle_imminent_games(self):
scheduled_games = filter(lambda g: g.state == GameStates.scheduled, self.games)
active_games = filter(lambda g: g.state == GameStates.active, self.games)
self.update_game_states()
# keep games until end-of-day (to allow late entrants, etc)
self.games = filter(lambda g: g.state != GameStates.dead, self.games)
imminent_games = filter(lambda g: g.state == GameStates.active, scheduled_games)
just_finished_games = filter(lambda g: g.state == GameStates.finished, active_games)
for g in imminent_games:
if len(g.players) == 0:
banter = "big game ({0}) about to kick off at {1}, no one wants to play?".format(
g.description, when_str(g.when))
else:
banter = self.load_banter("kickoff", {
"s": g.pretty_players(),
"t": when_str(g.when),
"d": g.description,
})
suggested_teams = suggest_teams(g)
if suggested_teams:
banter += "\n{}".format(suggested_teams)
nextgame = self.game_straight_after(g, threshold = GAME_FOLLOWON_TIME)
if nextgame:
nextgame_banter = self.load_banter("follow-on", {
"s": format_user(nextgame.creator),
"d": nextgame.description,
"c": nextgame.channel,
})
banter += "\n({})".format(nextgame_banter)
self.send_message(banter, to_channel = g.channel)
for g in just_finished_games:
msg = vote_message(g)
if msg:
self.update_game_message(g, msg)
def teardown(self):
self.save()
def timeout(self):
self.handle_imminent_games()
self.save()
def handle_game_reaction(self, game, reacting_user, emoji, removed):
now = datetime.datetime.today()
join_emojis = ["+1", "thumbsup", "plus1" "heavy_plus_sign"]
if emoji in join_emojis:
if now < game.endtime():
if removed:
self.remove_user_from_game(reacting_user, game, subtle_message = True)
else:
self.add_user_to_game(reacting_user, game, subtle_message = True)
else:
self.update_game_message(game, "game's over {}, can't {}".format(
format_user(reacting_user), "flyout" if removed else "flyin"))
def maybe_register_emoji_number_stat(self, gametime, emoji, from_user, removed):
historic_game = self.history.find_game(gametime)
if not historic_game:
return None, None
try:
index = number_emojis.index(emoji)
except ValueError:
return None, None
try:
user = historic_game.players[index]
except IndexError:
return None, None
if self.history.register_stat(gametime, user, from_user, removed, Stats.scrub):
return Stats.scrub, user
return None, None
def maybe_record_stat(self, gametime, channel, user, emoji, removed):
recorded = False
target_user = user
if emoji in number_emojis:
stat, target_user = self.maybe_register_emoji_number_stat(gametime, emoji, user, removed)
recorded = stat != None
statmap = channel_statmap(channel)
if statmap and emoji in statmap:
stat = statmap[emoji]
recorded = self.history.register_stat(gametime, user, user, removed, stat)
if recorded and channel in self.latest_stats_table:
stats = self.history.summary_stats(channel, year = self.latest_stats_table[channel].year)
self.update_stats_table(channel, stats, last_updated_user_stat = (target_user, stat))
def maybe_record_useroption(self, reaction, removed, reacting_user):
channel = reaction.channel.name
if channel not in self.latest_stats_table:
return
ts = self.latest_stats_table[channel].timestamp
if reaction.original_msg_time != ts:
return
emoji = reaction.emoji
# found a reaction on the latest stats table
option_emojis = ["mute", "sound"]
if emoji not in option_emojis:
return
# if it's just the removal of a sound emoji, ignore (must explicitly mute)
if removed and emoji == "sound":
return
should_mute = (emoji == "mute")
if removed:
should_mute ^= 1
if should_mute:
self.user_options[reacting_user].add(UserOption.mute)
else:
try:
self.user_options[reacting_user].remove(UserOption.mute)
except KeyError:
pass
stats = self.history.summary_stats(channel, year = self.latest_stats_table[channel].year)
self.update_stats_table(channel, stats)
self.save()
def handle_reaction(self, reaction, removed = False):
emoji = reaction.emoji
msg_when = reaction.original_msg_time
reacting_user = self.lookup_user(reaction.reacting_user)
game = find(lambda g: g.message.timestamp == msg_when, self.games)
if game:
self.handle_game_reaction(game, reacting_user, emoji, removed)
self.maybe_record_stat(msg_when, reaction.channel.name, reacting_user, emoji, removed)
self.maybe_record_useroption(reaction, removed, reacting_user)
def handle_unreaction(self, reaction):
self.handle_reaction(reaction, removed = True)
def is_message_for_me(self, msg):
return msg.lower() == self.botname
def handle_edit(self, edit):
self.handle_message(edit.toMessage())
def handle_message(self, message):
tokens = message.text.split()
if len(tokens) < 1 or not self.is_message_for_me(tokens[0]):
biggame = BIG_GAME_REGEX.match(message.text, re.IGNORECASE)
if biggame:
self.send_message("... did someone mention a {} game?".format(biggame.groups(0)[0]))
return
try:
# lookup message.user
message.user = self.lookup_user(message.user)
# ignore leading dialect bits, e.g. "ps4bot here hew big game" --> "ps4bot big game"
while len(tokens) > 1 and tokens[1] in DIALECT:
tokens = [tokens[0]] + tokens[2:]
self.handle_command(message, tokens[1] if len(tokens) > 1 else "", " ".join(tokens[2:]))
except Exception as e:
self.send_message(":rotating_light: {}'s massive computer membrane has ruptured".format(self.botname))
traceback.print_exc()
raise e