forked from sekret666/mafiayoxlama
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmafia_bot_public.py
More file actions
1802 lines (1595 loc) · 85.8 KB
/
mafia_bot_public.py
File metadata and controls
1802 lines (1595 loc) · 85.8 KB
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
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3.4
### The Mafia Bot's token is: [REDACTED]
### The TCoD Mafia chat id is: [REDACTED]
### The Private Testing chat id is: [REDACTED]
###SETUP
from telegram.ext import *
from telegram import *
from random import shuffle
from random import choice
from random import randint
import logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',level=logging.INFO)
updater = Updater(token='1453674021:AAG9nHdnmj8tpqId6B2PHlL3tNqQ-KxFyGM') #TOKEN REDACTED
dispatcher = updater.dispatcher
###FRIEND LIST
friend_dict = {}
#FRIEND IDS REDACTED
###ROLES
class Role:
def __init__(self,name='Unknown',alignment='Innocent',info='Unknown', has_night_action = False, has_day_action = False, number_of_targets = 0, priority = 0,
sends_mafia_kill = False, can_self_target = True,shots = -1):
self.name = name
self.alignment = alignment
self.info = info
self.has_night_action = has_night_action
self.has_day_action = has_day_action
self.number_of_targets = number_of_targets
self.priority = priority
self.sends_mafia_kill = sends_mafia_kill
self.can_self_target = can_self_target
self.shots = shots
def dayPower(self,bot,update,player):
global group_id
if self.name == "Horgie":
string = "ATTENTION: " + player.name + " is confirmed to be INNOCENT!\n"
bonus_string = choice(["I mean, they like to knitting in their spare time and everything!",
"They didn't mean to bother anyone!",
"What kind of monster would ever accuse this poor thing of being mafia?",
"How could they be anything but?",
"Look at them. They couldn't hurt a fly!"])
string += bonus_string
bot.sendMessage(chat_id=group_id, text=string)
##Create roles
role_database = {}
role_database['Vanilla'] = Role(name='Vanilla',
alignment='Innocent',
info = "You are a Vanilla Innocent!\nYou have no special powers.\nYou win when all the mafiosi are dead.",
has_night_action = False,
has_day_action = False,
number_of_targets = 0,
priority = 0,
sends_mafia_kill = False)
role_database['Cop'] = Role(name='Cop',
alignment='Innocent',
info = "You are a Cop!\nDuring the night, you may inspect a player to learn their alignment.\nYou win when all the mafiosi are dead.",
has_night_action = True,
has_day_action = False,
number_of_targets = 1,
priority = 0,
sends_mafia_kill = False)
role_database['Rolecop'] = Role(name='Rolecop',
alignment='Innocent',
info = "You are a Rolecop!\nDuring the night, you may inspect a player to learn their role. " \
"Note that this will NOT tell you their alignment (so a powerless role would show up as Vanilla regardless of alignment, for example).\n" \
"You win when all the mafiosi are dead.",
has_night_action = True,
has_day_action = False,
number_of_targets = 1,
priority = 0,
sends_mafia_kill = False)
role_database['Oracle'] = Role(name='Oracle',
alignment='Innocent',
info = "You are an Oracle!\nDuring the night, you may target two players. The first player's alignment is revealed to the second player. " \
"You cannot target yourself, however.\nYou win when all the mafiosi are dead.",
has_night_action = True,
has_day_action = False,
number_of_targets = 2,
priority = 0,
sends_mafia_kill = False,
can_self_target = False)
role_database['Doctor'] = Role(name='Doctor',
alignment='Innocent',
info = "You are a Doctor!\nDuring the night, you may heal another player to prevent them from dying that night. "
"However, if the target is healed by several sources at once, they overdose and die!\n"
"You win when all the mafiosi are dead.",
has_night_action = True,
has_day_action = False,
number_of_targets = 1,
priority = 3,
sends_mafia_kill = False,
can_self_target = False)
role_database['Roleblocker'] = Role(name='Roleblocker',
alignment='Innocent',
info = "You are a Roleblocker!\nDuring the night, you may roleblock a player, negating their powers.\nYou win when all the mafiosi are dead.",
has_night_action = True,
has_day_action = False,
number_of_targets = 1,
priority = 10,
sends_mafia_kill = False,
can_self_target = False)
role_database['Vigilante'] = Role(name='Vigilante',
alignment='Innocent',
info = "You are a Vigilante!\nDuring the night, you may kill a player.\nYou win when all the mafiosi are dead.",
has_night_action = True,
has_day_action = False,
number_of_targets = 1,
priority = 5,
sends_mafia_kill = False)
role_database['Bulletproof'] = Role(name='Bulletproof',
alignment='Innocent',
info = "You are a Bulletproof Innocent!\nYou can survive a kill at night, but only once.\nYou win when all the mafiosi are dead.",
has_night_action = False,
has_day_action = False,
number_of_targets = 0,
priority = 5,
sends_mafia_kill = False,
shots = 1)
role_database['Horgie'] = Role(name='Horgie',
alignment='Innocent',
info = "You are a Horgie!\nDuring the day, you may activate your power to have the bot publicly confirm your innocence.\nYou win when all the mafiosi are dead.",
has_night_action = False,
has_day_action = True,
number_of_targets = 0,
priority = 5,
sends_mafia_kill = False)
role_database['Miller'] = Role(name='Miller',
alignment='Innocent',
info = "You are a Miller!\nAlthough you are Innocent, investigative roles will see you as Mafia. How unfair!\nYou win when all the mafiosi are dead.",
has_night_action = False,
has_day_action = False,
number_of_targets = 0,
priority = 11,
sends_mafia_kill = False)
role_database['Tracker'] = Role(name='Tracker',
alignment='Innocent',
info = "You are a Tracker!\nDuring the night, you may track a player to learn who their targets were that night (if any).\nYou win when all the mafiosi are dead.",
has_night_action = True,
has_day_action = False,
number_of_targets = 1,
priority = 0,
sends_mafia_kill = False)
role_database['Watcher'] = Role(name='Watcher',
alignment='Innocent',
info = "You are a Watcher!\nDuring the night, you may watch a player to learn who targeted them that night (if anyone).\nYou win when all the mafiosi are dead.",
has_night_action = True,
has_day_action = False,
number_of_targets = 1,
priority = 0,
sends_mafia_kill = False)
###
role_database['Alien'] = Role(name='Alien',
alignment='Alien',
info = "You are an Alien!\nYou can survive a kill at night, but only once. If you do, you become activated.\nYou win if you get lynched while you are activated.",
has_night_action = False,
has_day_action = False,
number_of_targets = 0,
priority = 0,
sends_mafia_kill = False)
role_database['Serial Killer'] = Role(name='Serial Killer',
alignment='Serial Killer',
info = "You are a Serial Killer!\nDuring the night, you may kill a player. Also, you can survive a kill at night, but only once.\nYou win when everybody else is dead.",
has_night_action = True,
has_day_action = False,
number_of_targets = 1,
priority = 5,
sends_mafia_kill = False)
###
role_database['Mafioso'] = Role(name='Vanilla',
alignment='Mafia',
info = "You are a Mafioso!\nYou may communicate privately with other mafiosi. During the night, you (or another mafia member) may kill a player.\n"
"You win when all the innocents are dead.",
has_night_action = False,
has_day_action = False,
number_of_targets = 0,
priority = 0,
sends_mafia_kill = True)
role_database['Godfather'] = Role(name='Godfather',
alignment='Mafia',
info = "You are a Mafia Godfather!\nDuring the night, you (or another mafia member) may kill a player.\n"
"Also, even though you are Mafia, investigative roles will see you as Innocent. How tricksy!\nYou win when all the innocents are dead.",
has_night_action = False,
has_day_action = False,
number_of_targets = 0,
priority = 11,
sends_mafia_kill = True)
role_database['Mafia Doctor'] = Role(name='Doctor',
alignment='Mafia',
info = "You are a Mafia Doctor!\nYou may communicate privately with other mafiosi. During the night, you (or another mafia member) may kill a player.\n" \
"Also, during the night, you may heal another player to prevent them from dying that night. "
"However, if the target is healed by several sources at once, they overdose and die!\n"
"You win when all the innocents are dead.",
has_night_action = True,
has_day_action = False,
number_of_targets = 1,
priority = 3,
sends_mafia_kill = True,
can_self_target = False)
role_database['Mafia Roleblocker'] = Role(name='Roleblocker',
alignment='Mafia',
info = "You are a Mafia Roleblocker!\nYou may communicate privately with other mafiosi. During the night, you (or another mafia member) may kill a player.\n" \
"Also, during the night, you may roleblock a player, negating their powers.\n"
"You win when all the innocents are dead.",
has_night_action = True,
has_day_action = False,
number_of_targets = 1,
priority = 10,
sends_mafia_kill = True,
can_self_target = False)
role_database['Mafia Rolecop'] = Role(name='Rolecop',
alignment='Mafia',
info = "You are a Mafia Rolecop!\nYou may communicate privately with other mafiosi. During the night, you (or another mafia member) may kill a player.\n" \
"Also, during the night, you may inspect a player to learn their role.\n"
"You win when all the innocents are dead.",
has_night_action = True,
has_day_action = False,
number_of_targets = 1,
priority = 0,
sends_mafia_kill = True)
role_database['Mafia Bulletproof'] = Role(name='Bulletproof',
alignment='Mafia',
info = "You are a Bulletproof Mafioso!\nYou can survive a kill at night, but only once.\nYou win when all the innocents are dead.",
has_night_action = False,
has_day_action = False,
number_of_targets = 0,
priority = 5,
sends_mafia_kill = True,
shots = 1)
###DEFINE PLAYER CLASS
class Player:
def __init__(self,name='Unknown',id=0,alignment='Innocent',role=role_database['Vanilla'],chat_id = 0):
self.name = name
self.id = id
self.alignment = alignment #'Innocent' or 'Mafia'
self.role = role
self.chat_id = chat_id
self.status = 'Alive' #or 'Dead'
self.effects = []
self.targets = []
self.targeted_by = []
self.action_used = False
self.shots = self.role.shots
self.vote_targets = []
self.is_abstaining = False
self.votes = 0
self.subpriority = 0
def removeTempEffects(self): #Remove temporary effects
for temp_effect in ['blocked','dying','healed']:
while temp_effect in self.effects:
self.effects.remove(temp_effect)
def __eq__(self,other):
if self.id == other.id:
return(True)
else:
return(False)
def sentMafiaKill(self):
global mafia_kill
if mafia_kill[0] and self == mafia_kill[2]:
return(True)
else:
return(False)
def flip(self):
if not (self.alignment == 'Innocent' or self.alignment == 'Mafia'):
return('neither Innocent nor Mafia')
else:
return(self.alignment)
###DEFINE BEHIND-THE-SCENES FUNCTIONS
def clearGame(): #Cleanup after game is finished
global group_id
global player_dict
global dead_players
global mafia_ids
global host_id
global phase
global day_count
global abstain_votes
global mafia_kill
global abort_confirmation
global tiebreaker
global roles
group_id = 0 #Default group ID redacted
player_dict = {}
dead_players = []
mafia_ids = []
host_id = 0
phase = 'off' #off, startup, day, night
day_count = 0
abstain_votes = 0
mafia_kill = [False,None,None] #Sent bool, victim, killer
abort_confirmation = False
tiebreaker = Player()
roles = []
def playerIsValid(bot,update,user_id, #Checks if player is allowed to input a command
allow_off = False,
allow_startup = False,
allow_day = False,
allow_night = False,
allow_nonplayer = False,
only_host = False,
only_private = False,
allow_dead = False):
global phase
global player_dict
global mafia_kill
global host_id
if not allow_off and phase == 'off':
bot.sendMessage(chat_id=update.message.chat_id, text="No game exists yet!",reply_to_message_id=update.message.message_id)
return(False)
elif not allow_startup and phase == 'startup':
bot.sendMessage(chat_id=update.message.chat_id, text="The game hasn't started yet!",reply_to_message_id=update.message.message_id)
return(False)
elif not allow_nonplayer and not user_id in player_dict:
bot.sendMessage(chat_id=update.message.chat_id, text="You are not a player!",reply_to_message_id=update.message.message_id)
return(False)
elif only_host and not user_id == host_id:
bot.sendMessage(chat_id=update.message.chat_id, text="Only the host can do that!",reply_to_message_id=update.message.message_id)
return(False)
elif allow_dead == False and user_id in player_dict and player_dict[user_id].status == 'Dead':
bot.sendMessage(chat_id=update.message.chat_id, text="You are dead!",reply_to_message_id=update.message.message_id)
return(False)
elif only_private and not update.message.chat.type == 'private':
bot.sendMessage(chat_id=update.message.chat_id, text="Message me privately to do that!",reply_to_message_id=update.message.message_id)
return(False)
elif not allow_day and phase == 'day':
bot.sendMessage(chat_id=update.message.chat_id, text="You can't do that during the day phase!",reply_to_message_id=update.message.message_id)
return(False)
elif not allow_night and phase == 'night':
bot.sendMessage(chat_id=update.message.chat_id, text="You can't do that during the night phase!",reply_to_message_id=update.message.message_id)
return(False)
else:
return(True)
def offerTargets(bot,update,command): ##Prompt the user to pick a target for /action. Provides a handy keyboard too
global player_dict
target_keyboard = []
for target_id in player_dict:
target = player_dict[target_id]
target_keyboard.append([command + " " + target.name])
reply_markup = ReplyKeyboardMarkup(keyboard=target_keyboard,resize_keyboard=True,one_time_keyboard=True,selective=True)
bot.sendMessage(chat_id=update.message.chat_id, text="Choose a player.", reply_markup=reply_markup,reply_to_message_id=update.message.message_id)
def findTarget(bot,update,args): ##Checks if target is an existing player
global player_dict
target_name = ' '.join(args)
for i in player_dict:
if player_dict[i].name.upper() == target_name.upper():
if player_dict[i].status == 'Alive':
return([True,player_dict[i]])
else:
return([False,None])
def setTarget(player,target): #Sets targeting
player.targets.append(target)
target.targeted_by.append(player)
def inspect(target): #Returns inspection results
if (target.alignment == 'Innocent' and not 'misleading' in target.effects) \
or (target.alignment == 'Mafia' and 'misleading' in target.effects) \
or (target.alignment == 'Alien' and not 'activated' in target.effects):
return('Innocent')
elif (target.alignment == 'Mafia' and not 'misleading' in target.effects) \
or (target.alignment == 'Innocent' and 'misleading' in target.effects) \
or (target.alignment == 'Alien' and 'activated' in target.effects):
return('Mafia')
else:
return('neither Innocent nor Mafia')
def sendNightMessage(bot,update):
global player_dict
global day_count
for player in player_dict.values():
if player.status == 'Alive':
string = "NIGHT " + str(day_count) + " begins!\n"
if player.role.has_night_action:
string += "Type /action or /target to use your night action, or pass on it with /pass.\n"
if player.role.sends_mafia_kill:
string += "Type /kill to send in the mafia kill, or just /pass. Be sure to discuss this with the other mafiosi, if any.\n"
elif not player.role.has_night_action:
string += "You have nothing to do at night, so just wait for everyone else to send in their actions."
bot.sendMessage(chat_id=player.chat_id, text=string)
def sendDayMessage(bot,update):
global player_dict
global day_count
for player in player_dict.values():
if player.status == 'Alive':
string = "DAY " + str(day_count) + " begins!\n"
if player.role.has_day_action:
string += "Type /action or /target to use your day action.\n"
string += "Type /vote or /lynch to lynch a player.\nType /abstain if you want to lynch no one.\nType /retract to change your vote."
bot.sendMessage(chat_id=player.chat_id, text=string)
def sendDeathMessage(bot,victim):
##Send personal message too
message = "YOU HAVE DIED!\n"
bonus_string = choice(["I'm sorry, pal. But you can still hang around and shitpost if you want!",
"That's too bad. But you can stick around and see how the game goes!",
"Ouch. But you did your best and that's what really matters!",
"Oh my. Well, stuff like this happens, right?",
"I hope you don't feel too bad about it, hehe!",
"Ah well. At least it's fun, right?"])
message += bonus_string
bot.sendMessage(chat_id=victim.chat_id, text=message)
def waitingOn(player):
global mafia_kill
if player.status == 'Alive':
##If not a mafia killer
if player.role.sends_mafia_kill == False:
if player.role.has_night_action and not player.action_used:
return(True)
else:
return(False)
##If a mafia killer
else:
if player.sentMafiaKill() or player.action_used or (mafia_kill[0] and not player.role.has_night_action):
return(False)
else:
return(True)
else:
return(False)
def checkIfNightFinished(bot,update):
global test_mode
global player_dict
for player in player_dict.values():
if waitingOn(player):
break
else:
if not test_mode:
resolveNightActions(bot,update) #If we're waiting on no one, resolve the night actions.
else:
bot.sendMessage(chat_id=group_id, text="The night actions have now been sent in, but in TEST MODE, "
"the night phase still needs to be advanced manually.")
def resolveNightKill():
global mafia_kill
victim = mafia_kill[1]
killer = mafia_kill[2]
if not 'blocked' in killer.effects:
victim.effects.append('dying')
def resolveNightActions(bot,update):
global phase
global group_id
global player_dict
global mafia_kill
global day_count
global dead_players
phase = 'day'
##Deal with multiple roleblockers
for player in player_dict.values():
if player.role.name == 'Roleblocker' or player.role.name == 'Mafia Roleblocker':
for target in player.targets:
target.subpriority = -1
#Sort resolution list by subpriority, then priority
resolution_list = sorted(player_dict.values(), key=lambda x: x.subpriority, reverse=True)
resolution_list = sorted(resolution_list, key=lambda x: x.role.priority, reverse=True)
##Resolve other powers
nightkill_resolved = False
for player in resolution_list:
role = player.role
##Nightkill
if role.priority < 5 and nightkill_resolved == False: #Nightkill resolves at priority 5
if mafia_kill[0] == False:
nightkill_resolved = True
else:
resolveNightKill()
nightkill_resolved = True
##Roles
if role.name == 'Roleblocker':
if len(player.targets) > 0:
if not 'blocked' in player.effects:
player.targets[0].effects.append('blocked')
if role.name == 'Bulletproof' or role.name == 'Serial Killer' and day_count == 0:
player.effects.append('bulletproof')
if (role.name == 'Miller' or role.name == 'Godfather') and not 'misleading' in player.effects:
player.effects.append('misleading')
if role.name == 'Vigilante' or role.name == 'Serial Killer':
if len(player.targets) > 0:
if not 'blocked' in player.effects:
player.targets[0].effects.append('dying')
if role.name == 'Doctor':
if len(player.targets) > 0:
if not 'blocked' in player.effects:
player.targets[0].effects.append('healed')
if role.name == 'Cop':
if len(player.targets) > 0:
if not 'blocked' in player.effects:
target = player.targets[0]
result = inspect(target)
string = "You investigate " + target.name + " and discover that they are " + result + "."
if result == 'Innocent':
bonus_string = choice(["","","",
"\n(Phew! That's a relief, isn't it?)",
"\n(Well, at least in the context of this game. Wahaha!)"])
elif result == 'Mafia':
bonus_string = choice(["","","",
"\n(Ack! So they're one of the bad guys!)",
"\n(Oh boy! Now that's not a very nice thing to be, is it!)"])
else:
bonus_string = choice(["\n(Huh? Y-you're not telling me it's...!)",
"\n(Are you thinking what I'm thinking...?)",
"\n(What!? But... what could that mean!?)"])
string += bonus_string
bot.sendMessage(chat_id=player.chat_id, text=string)
else:
bot.sendMessage(chat_id=player.chat_id, text="Your investigation turned up no result.")
if role.name == 'Rolecop':
if len(player.targets) > 0:
if not 'blocked' in player.effects:
target = player.targets[0]
string = "You investigate " + target.name + " and discover that they are a " + target.role.name + "."
bot.sendMessage(chat_id=player.chat_id, text=string)
else:
bot.sendMessage(chat_id=player.chat_id, text="Your investigation turned up no result.")
if role.name == 'Oracle':
if len(player.targets) == 2:
if not 'blocked' in player.effects:
dream = player.targets[0]
dreamer = player.targets[1]
result = inspect(dream)
string = "Last night, you had a dream about " + dream.name + " and saw that they are " + result + "."
bot.sendMessage(chat_id=dreamer.chat_id, text=string)
if role.name == 'Tracker':
if len(player.targets) > 0:
target = player.targets[0]
if not 'blocked' in player.effects:
if len(target.targets) == 0 and (mafia_kill[0] == False or not mafia_kill[2] == target):
string = "You tracked " + target.name + ", but they didn't visit anyone last night."
else:
string = "You tracked " + target.name + " and saw that they targeted the following players last night:\n"
for person in target.targets:
string += person.name + "\n"
if mafia_kill[0] == True and mafia_kill[2] == target:
string += mafia_kill[1].name
bot.sendMessage(chat_id=player.chat_id, text=string)
else:
bot.sendMessage(chat_id=player.chat_id, text="Your tracking turned up no result.")
if role.name == 'Watcher':
if len(player.targets) > 0:
target = player.targets[0]
if not 'blocked' in player.effects:
visitor_list = []
##Add players who target
for visitor in target.targeted_by:
if not visitor == player and not visitor in visitor_list:
visitor_list.append(visitor)
##Also add mafia kill
if mafia_kill[0] == True and mafia_kill[1] == target:
visitor_list.append(mafia_kill[2])
if len(visitor_list) == 0:
string = "You watched " + target.name + ", but no one visited them last night."
else:
shuffle(visitor_list)
string = "You watched " + target.name + " and saw that the following players targeted them last night:\n"
for visitor in visitor_list:
string += visitor.name + "\n"
bot.sendMessage(chat_id=player.chat_id, text=string)
else:
bot.sendMessage(chat_id=player.chat_id, text="Your watching turned up no result.")
##If nightkill still not resolved, do it now
if player == resolution_list[-1] and nightkill_resolved == False:
if mafia_kill[0] == False:
nightkill_resolved = True
else:
resolveNightKill()
nightkill_resolved = True
##Resolve effects
deaths = []
for player in resolution_list:
##Check if inactive alien:
if player.alignment == 'Alien' and 'dying' in player.effects and not 'activated' in player.effects:
player.effects.remove('dying')
player.effects.append('activated')
bonus_string = choice(["Someone tried to kill you... but this wasn't even your final form! You're now an activated Alien!",
"Heh heh heh... Fools... Someone tried to kill you, but it only made you stronger - you are now activated.",
"Someone tried to kill you - but instead it unleashed your true power! You are now activated!"])
bot.sendMessage(chat_id=player.chat_id, text=bonus_string)
##Check for protections
if 'healed' in player.effects:
if player.effects.count('healed') > 1:
player.effects.append('dying')
else:
while 'dying' in player.effects:
player.effects.remove('dying')
else:
while ('bulletproof' in player.effects) and ('dying' in player.effects):
player.effects.remove('bulletproof')
player.effects.remove('dying')
bonus_string = choice(["Oh my. Someone attacked you, and you lost your bulletproofing.",
"Looks like you've made enemies. Someone's attack destroyed your bulletproofing!",
"Someone tried to kill you! But you held up your sign saying 'Bulletproof' and scared them off. Next time you won't be so lucky."])
bot.sendMessage(chat_id=player.chat_id, text=bonus_string)
##If still dying, then dead
if 'dying' in player.effects:
player.status = 'Dead'
deaths.append(player)
#Begin the day phase
day_count += 1
string = "It is now DAY " + str(day_count) + ".\n"
if len(deaths) == 0:
string += "No one has died.\n"
else:
for victim in deaths:
string += victim.name + " has died. They were " + victim.flip() +".\n"
dead_players.append(victim)
sendDeathMessage(bot,victim)
##Bonus string
bonus_string = ''
if len(deaths) > 1:
bonus_string = choice(["(Wow, that's a lot of death.)",
"(Oh my. Plenty of murder to go around today, huh.)",
"(So much carnage!)"])
string += bonus_string
bot.sendMessage(chat_id=group_id, text=string)
##Cleanup
for player in resolution_list:
player.targets.clear()
player.targeted_by.clear()
player.removeTempEffects()
player.action_used = False
mafia_kill = [False,None,None]
if not checkWinConditions(bot,update):
sendDayMessage(bot,update)
bot.sendMessage(chat_id=group_id, text="You may now discuss and vote to lynch.")
if day_count == 1:
with open('PW-AA OST- 13 - Search - Opening 2001.mp3', 'rb') as song:
bot.send_audio(chat_id=group_id, audio=song,duration = 108,title = "Day " + str(day_count))
def checkWinConditions(bot,update):
global test_mode
global player_dict
global group_id
if test_mode:
return
number_of_players = 0
number_of_innocents = 0
number_of_mafiosi = 0
number_of_serial_killers = 0
number_of_aliens = 0
alien_win = False
alien_winner_id = 0
for player in player_dict.values():
if player.status == 'Alive':
number_of_players += 1
if player.alignment == 'Alien':
number_of_aliens += 1
if 'winning' in player.effects:
alien_win = True
alien_winner_id = player.id
elif player.alignment == 'Serial Killer':
number_of_serial_killers += 1
elif player.alignment == 'Innocent':
number_of_innocents += 1
elif player.alignment == 'Mafia':
number_of_mafiosi += 1
if ((number_of_mafiosi == number_of_players) #Mafia win
or (number_of_mafiosi == 0 and number_of_serial_killers == 0) #Innocent win
or (number_of_players == number_of_serial_killers and number_of_serial_killers == 1) #Serial Killer win
or alien_win): #Alien win
if alien_win:
alien_name = player_dict[alien_winner_id].name
string = "The alien has played you all! " + alien_name.upper() + " WINS!\n\n"
elif number_of_players == 0:
string = "Everyone has died! EMERIC THE MAFIA BOT WINS!\n\n"
elif number_of_players == number_of_aliens:
string = "The alien is a failure and everyone else has died! EMERIC THE MAFIA BOT WINS!\n\n"
elif number_of_players == number_of_serial_killers and number_of_serial_killers == 1:
##Find the serial killer
name = 'No one'
for player in player_dict.values():
if player.status == 'Alive' and player.alignment == 'Serial Killer':
name = player.name
break
string = "The Serial Killer has murdered everyone!\n" + name.upper() + " WINS!\n\n"
elif number_of_mafiosi == 0 and number_of_serial_killers == 0:
string = "All threats to the Innocents have been wiped out! TOWN WINS!\n\n"
elif number_of_mafiosi == number_of_players:
string = "The town has been wiped out! MAFIA WINS!\n\n"
###Finish
string += "The setup was:\n"
for player in player_dict.values():
string += player.name + ": " + player.role.name + " (" + player.alignment + ")\n"
string += "\nThanks for playing!"
bot.sendMessage(chat_id=group_id, text=string)
##Send personal messages too
for player in player_dict.values():
string = "The game has now ended. Thanks for playing, " + player.name + "!"
bot.sendMessage(chat_id=player.chat_id, text=string)
clearGame()
return(True)
else:
return(False)
###DEFINE COMMAND HANDLERS
def help_command(bot,update): #Help
helptext = "Here are the commands you can use with the mafia bot.\n" \
"\nGAME SETUP COMMANDS:\n" \
"/start: Gives a welcome message and shows the most recent updates.\n" \
"/create: Create a new mafia game.\n" \
"/join: Join the game during startup.\n" \
"/playerlist: See a list of all players in the game.\n" \
"/add: Add roles to the game's setup. Type /add with no argument to see a list of available roles.\n" \
"/rolelist: Show all roles in the current setup.\n" \
"/randomize [x] [y] [z]: Create a random setup with [x] players, [y] of whom are mafia, where one in [z] roles are power roles.\n" \
"/resetroles: Reset the game's setup.\n" \
"/ready: Begin playing the mafia game (use this once everybody has joined). Only the host can use this command. \n" \
"\nPLAYER COMMANDS:\n" \
"/role: Receive information about your role.\n" \
"/lynch or /vote [name]: Vote to lynch a player (during the day). Type /lynch with no argument to bring up a keyboard.\n" \
"/abstain: Vote to lynch no one.\n" \
"/retract: Retract your vote.\n" \
"/votelist: See how many lynch votes each player has.\n" \
"/action or /target [name]: Use your role's power, if applicable. Type /action with no argument to bring up a keyboard.\n" \
"/kill [name]: Nightkill a player. Only the mafia can use this, and only one of them each night.\n" \
"/undo: Undo your action or nightkill.\n" \
"/pass: Pass on using your night action/nightkill.\n" \
"\nHOST-ONLY COMMANDS:\n" \
"/advance: Advance day phase to night phase, or night phase to day phase. " \
"(Note that the night phase also advances automatically when all actions are sent in.)\n" \
"/remind: Sends reminders to all players who haven't yet submitted their night actions.\n" \
"/abort: Abort the current game."
bot.sendMessage(chat_id=update.message.chat_id, text=helptext,reply_to_message_id=update.message.message_id)
help_handler = CommandHandler('help', help_command)
dispatcher.add_handler(help_handler)
###
def start(bot,update): #Start message
bot.sendMessage(chat_id=update.message.chat_id, text="Welcome! I'm Emeric, the TCoD Mafia Bot. "
"To create a new game, type /create in the group you want to play in. "
"Use /help to see what commands are available. Have fun, and don't forget to tell MD about any bugs you find!\n\n"
"RECENT UPDATES:\n"
"[*] Please try to avoid sending messages to Emeric while he is sleeping, or he'll panic when he wakes up.\n"
"[*] I've rewritten the way night actions work. THE MAFIA CAN NO LONGER SEND IN A NIGHTKILL AND A NIGHT ACTION AT THE SAME TIME. "
"As a result, there are only three commands: /action, /kill, and /undo. "
"The old /passkill and /undokill commands have been removed. "
"This is a significant change, so be on the lookout for strange behavior.")
start_handler = CommandHandler('start', start)
dispatcher.add_handler(start_handler)
###
def create(bot, update): #Creates a new game
global host_id
global phase
global group_id
global test_mode
if not phase == 'off':
bot.sendMessage(chat_id=update.message.chat_id, text="A game already exists.",reply_to_message_id=update.message.message_id)
elif update.message.chat.type == 'private' or update.message.chat.type == 'channel':
bot.sendMessage(chat_id=update.message.chat_id, text="Type /create in the group where you want to play. Be sure to invite me to it first!",
reply_to_message_id=update.message.message_id)
else:
host_id = update.message.from_user.id
host_name = update.message.from_user.first_name
group_id = update.message.chat_id
print('Group ID: ' + str(group_id)) #Testing
phase = 'startup'
bot.sendMessage(chat_id=group_id, text="A new game has been created by " + host_name + "!\n"
"Message /join to the bot to join.\n"
"Type /playerlist to see who has joined so far.\n\n" +
host_name + ", choose roles for the setup using /add.\nUse /rolelist to show the current setup and /resetroles to clear it.\n"
"Use /randomize to generate a random setup.\n\n" +
host_name + " must type /ready when everyone has joined to begin the game.")
if test_mode:
bot.sendMessage(chat_id=group_id, text="NOTE: The bot has been set to TEST MODE, meaning games will never actually end, "
"even if win conditions are met. If you see this, you're not supposed to. Use /testmode to turn it off.")
create_handler = CommandHandler('create', create)
dispatcher.add_handler(create_handler)
###
def testmode(bot,update): #Turn on test mode
global test_mode
global group_id
test_mode = not test_mode
if phase == 'off':
chat_id = update.message.chat_id
else:
chat_id = group_id
bot.sendMessage(chat_id=chat_id, text="TEST MODE has now been set to " + str(test_mode) + ".")
testmode_handler = CommandHandler('testmode', testmode)
dispatcher.add_handler(testmode_handler)
###
def join(bot, update): #Lets a player join during startup
global player_dict
global group_id
if phase == 'off':
bot.sendMessage(chat_id=update.message.chat_id, text="There is no game to join.",reply_to_message_id=update.message.message_id)
elif update.message.from_user.id in player_dict:
bot.sendMessage(chat_id=update.message.chat_id, text="You have already joined.",reply_to_message_id=update.message.message_id)
elif not phase == 'startup':
bot.sendMessage(chat_id=update.message.chat_id, text="Sorry, the game has already started. Join the next one!",
reply_to_message_id=update.message.message_id)
elif not update.message.chat.type == 'private':
bot.sendMessage(chat_id=update.message.chat_id, text="To /join, please message me privately.",
reply_to_message_id=update.message.message_id)
else:
##Add the player
new_user = update.message.from_user
new_player = Player(name=new_user.first_name,id=new_user.id,chat_id=update.message.chat_id)
player_dict[new_user.id] = new_player
##Make announcement
name = new_user.first_name
print(name + ": " + str(new_user.id))
string = name + " has joined the game!\n"
bonus_string = choice(["Watch out for that one.",
"Take good care of " + name + "!",
"You do want this egg, don't you?",
"That's going to spice things up, surely!",
"Oh boy!",
"Make them feel welcome.",
"I don't know who they are, but they sound attractive.",
"Amazing! They're a Fully Trained Pokémon!"])
if new_user.id in friend_dict:
if friend_dict[new_user.id] == 'MD':
bonus_string = choice(["I am duty-bound to describe him as 'handsome'.",
"As if we haven't seen enough of each other already. Hmph!",
"Oh, it's *you*. Hrrm.",
"\"Emeric Eggert Bot, you were named after two of the bravest men I ever knew...\"",
"You... you made me what I am..."])
elif friend_dict[new_user.id] == 'Flora':
bonus_string = choice(["Time for a friendly game of subs vs. doms!",
"What!? No, *I'm* the real Emeric, I swear! The other one's the robot!",
"You haven't seen the Flora Bot around, by any chance?",
"Welp, I guess we know who the mafia is already.",
"Watch out for those stompy boots!"])
elif friend_dict[new_user.id] == 'hope':
bonus_string = choice(['Ah, excellent. That means I get to put a Vigilante in the game.',
"You're Vanilla Townie. AGAIN. Ha!",
"What's the matter? Eat your burgers, hope.",
"WEBECCA! ~dramatic music plays~",
"That Mafia",
"No one with naturally wavy hair can be that bad.",
"I'm sorry, but \"Vocaloid\" is not an alignment in this game."])
elif friend_dict[new_user.id] == 'ZM':
bonus_string = choice(['meme too danks',
"Praise the Sun!",
"Me too thanks",
"Greetings.",
"The feast of souls begins now!",
"( ͡° ͜ʖ ͡°)",
"Heh, Greetings."])
elif friend_dict[new_user.id] == 'VM':
bonus_string = choice(['Time for a friendly game of Snakes and Ladders!',
"I have had it with these goddamn snakes on this goddamn plane!",
"This game will now include a third faction, the Furries.",
"Ha! Now it's MY turn to host the games!",
"Life has many mafiosi, long boy!"])
elif friend_dict[new_user.id] == 'Butterfree':
bonus_string = choice(["If you run into the Leppa Bot, tell them I said hi.",
"Time for a friendly game of Íslendinga-Mafia!",
"Eftirlýstur síðan 2015 fyrir morð.",
"(You know, my parents would have named me Eggert if it wasn't for the fact that they didn't want to name me that. True story.)",
"Computer scientists always make me feel woefully inadequate."])
elif friend_dict[new_user.id] == 'Eifie':
bonus_string = choice(["Some people bug me, but here's a person who DE-bugs me. Hehe, get it!?",
"Nya ha! I sure do love mafia and ripping thumbs off!",
"(Please don't look at my code, please don't look at my code...)",
"The Messenger of Truth and Ideals, Eifie Girl, enters!",
"Eep!"])
elif friend_dict[new_user.id] == 'Jack':
bonus_string = choice(["Don't give him all your prize like fools!",
"Bendytoots...!?!",
'"I choose violence."',
"Ass right there, freezehole!",
"You know nothing, Jack Snow."])
elif friend_dict[new_user.id] == 'Murkrow':
bonus_string = choice(["He's a mathioso. Hehe, get it!?",
"By which I mean, joined the mathia game.",
"I feel like \"True Neutral\" could go either way here, really.",
"He's Innocent up to isomorphism."])
elif friend_dict[new_user.id] == 'Alex':
bonus_string = choice(["Or is it Karousever now? I get confused.",
"The artist formerly known as Jake.",
"He has learned well... Don't underestimate him just because he's new!",
"(No, I am not going to make the obvious joke. Too easy.)"])
elif friend_dict[new_user.id] == 'Walker':
bonus_string = choice(["Go on, ask about complex numbers!",
"xoxoxor~",
"Boy am I glad that Walker is in there, and that they're the vigilante and that I'm out here, and -"])
elif friend_dict[new_user.id] == 'Faorzia':
bonus_string = choice(["Even a sheltered bean can be deadly...",
"!!! lowkey cover enabled!"])
elif friend_dict[new_user.id] == 'SS':
bonus_string = choice(["Fallen Innocents... You will be avenged!!",
"Mafia! The time of your reckoning is at hand!",
"The light of justice shining bright!",
"Sword of Justice!!"])
elif friend_dict[new_user.id] == 'ILS':
bonus_string = choice(["This astropolitician will defeat you using space governance!",
"I suppose political science really primes you for this sort of game.",
"Obviously aligned with the Squirtles faction.",
"Playing mafia is basically the same as studying UK politics, right?"])
elif friend_dict[new_user.id] == 'ILS':
bonus_string = choice(["This astropolitician will defeat you using space governance!",
"I suppose political science really primes you for this sort of game.",
"Obviously aligned with the Squirtles faction.",
"Playing mafia is basically the same as studying UK politics, right?"])
elif friend_dict[new_user.id] == 'Stryke':
bonus_string = choice(["Wow! I can't believe the entire mafia crew has come to Telecod!",
"Let's we go!",
"Stryke one! Stryke two! Aaaand you're out."])
string += bonus_string
bot.sendMessage(chat_id=group_id, text=string)