forked from d0tcc/Secret-Blue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainController.py
763 lines (673 loc) · 35.5 KB
/
MainController.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = "Julian Schrittwieser"
import json
import logging as log
import random
import re
from random import randrange
from time import sleep
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import (Updater, CommandHandler, CallbackQueryHandler)
import Commands
from Constants.Cards import playerSets
from Constants.Config import TOKEN, STATS
from Boardgamebox.Game import Game
from Boardgamebox.Player import Player
import GamesController
import datetime
# Enable logging
log.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=log.INFO,
filename='../logs/logging.log')
logger = log.getLogger(__name__)
def initialize_testdata():
# Sample game for quicker tests
testgame = Game(-1001113216265, 15771023)
GamesController.games[-1001113216265] = testgame
players = [Player("Александр", 320853702), Player("Gustav", 305333239), Player("Rene", 318940765), Player("Susi", 290308460), Player("Renate", 312027975)]
for player in players:
testgame.add_player(player.uid, player)
##
#
# Beginning of round
#
##
def start_round(bot, game):
log.info('start_round called')
if game.board.state.chosen_president is None:
game.board.state.nominated_president = game.player_sequence[game.board.state.player_counter]
else:
game.board.state.nominated_president = game.board.state.chosen_president
game.board.state.chosen_president = None
bot.send_message(game.cid,
"The next presidential canditate is %s.\n%s, please nominate a Chancellor in our private chat!" % (
game.board.state.nominated_president.name, game.board.state.nominated_president.name))
choose_chancellor(bot, game)
# --> nominate_chosen_chancellor --> vote --> handle_voting --> count_votes --> voting_aftermath --> draw_policies
# --> choose_policy --> pass_two_policies --> choose_policy --> enact_policy --> start_round
def choose_chancellor(bot, game):
log.info('choose_chancellor called')
strcid = str(game.cid)
pres_uid = 0
chan_uid = 0
btns = []
if game.board.state.president is not None:
pres_uid = game.board.state.president.uid
if game.board.state.chancellor is not None:
chan_uid = game.board.state.chancellor.uid
for uid in game.playerlist:
# If there are only five players left in the
# game, only the last elected Chancellor is
# ineligible to be Chancellor Candidate; the
# last President may be nominated.
if len(game.player_sequence) > 5:
if uid != game.board.state.nominated_president.uid and game.playerlist[
uid].is_dead == False and uid != pres_uid and uid != chan_uid:
name = game.playerlist[uid].name
btns.append([InlineKeyboardButton(name, callback_data=strcid + "_chan_" + str(uid))])
else:
if uid != game.board.state.nominated_president.uid and game.playerlist[
uid].is_dead == False and uid != chan_uid:
name = game.playerlist[uid].name
btns.append([InlineKeyboardButton(name, callback_data=strcid + "_chan_" + str(uid))])
chancellorMarkup = InlineKeyboardMarkup(btns)
bot.send_message(game.board.state.nominated_president.uid, game.board.print_board())
bot.send_message(game.board.state.nominated_president.uid, 'Please nominate your chancellor!',
reply_markup=chancellorMarkup)
def nominate_chosen_chancellor(bot, update):
log.info('nominate_chosen_chancellor called')
log.info(GamesController.games.keys())
callback = update.callback_query
regex = re.search("(-[0-9]*)_chan_([0-9]*)", callback.data)
cid = int(regex.group(1))
chosen_uid = int(regex.group(2))
try:
game = GamesController.games.get(cid, None)
log.info(game)
log.info(game.board)
game.board.state.nominated_chancellor = game.playerlist[chosen_uid]
log.info("President %s (%d) nominated %s (%d)" % (
game.board.state.nominated_president.name, game.board.state.nominated_president.uid,
game.board.state.nominated_chancellor.name, game.board.state.nominated_chancellor.uid))
bot.edit_message_text("You nominated %s as Chancellor!" % game.board.state.nominated_chancellor.name,
callback.from_user.id, callback.message.message_id)
bot.send_message(game.cid,
"President %s nominated %s as Chancellor. Please vote now!" % (
game.board.state.nominated_president.name, game.board.state.nominated_chancellor.name))
vote(bot, game)
except AttributeError as e:
log.error("nominate_chosen_chancellor: Game or board should not be None! Eror: " + str(e))
except Exception as e:
log.error("Unknown error: " + str(e))
def vote(bot, game):
log.info('vote called')
#When voting starts we start the counter to see later with the vote/calltovote command we can see who voted.
game.dateinitvote = datetime.datetime.now()
strcid = str(game.cid)
btns = [[InlineKeyboardButton("Ja", callback_data=strcid + "_Ja"),
InlineKeyboardButton("Nein", callback_data=strcid + "_Nein")]]
voteMarkup = InlineKeyboardMarkup(btns)
for uid in game.playerlist:
if not game.playerlist[uid].is_dead:
if game.playerlist[uid] is not game.board.state.nominated_president:
# the nominated president already got the board before nominating a chancellor
bot.send_message(uid, game.board.print_board())
bot.send_message(uid,
"Do you want to elect President %s and Chancellor %s?" % (
game.board.state.nominated_president.name, game.board.state.nominated_chancellor.name),
reply_markup=voteMarkup)
def handle_voting(bot, update):
callback = update.callback_query
log.info('handle_voting called: %s' % callback.data)
regex = re.search("(-[0-9]*)_(.*)", callback.data)
cid = int(regex.group(1))
answer = regex.group(2)
try:
game = GamesController.games[cid]
uid = callback.from_user.id
bot.edit_message_text("Thank you for your vote: %s to a President %s and a Chancellor %s" % (
answer, game.board.state.nominated_president.name, game.board.state.nominated_chancellor.name), uid,
callback.message.message_id)
log.info("Player %s (%d) voted %s" % (callback.from_user.first_name, uid, answer))
if uid not in game.board.state.last_votes:
game.board.state.last_votes[uid] = answer
if len(game.board.state.last_votes) == len(game.player_sequence):
count_votes(bot, game)
except:
log.error("handle_voting: Game or board should not be None!")
def count_votes(bot, game):
log.info('count_votes called')
# Voted Ended
game.dateinitvote = None
voting_text = ""
voting_success = False
for player in game.player_sequence:
if game.board.state.last_votes[player.uid] == "Ja":
voting_text += game.playerlist[player.uid].name + " voted Ja!\n"
elif game.board.state.last_votes[player.uid] == "Nein":
voting_text += game.playerlist[player.uid].name + " voted Nein!\n"
if list(game.board.state.last_votes.values()).count("Ja") > (
len(game.player_sequence) / 2): # because player_sequence doesnt include dead
# VOTING WAS SUCCESSFUL
log.info("Voting successful")
voting_text += "Hail President %s! Hail Chancellor %s!" % (
game.board.state.nominated_president.name, game.board.state.nominated_chancellor.name)
game.board.state.chancellor = game.board.state.nominated_chancellor
game.board.state.president = game.board.state.nominated_president
game.board.state.nominated_president = None
game.board.state.nominated_chancellor = None
voting_success = True
bot.send_message(game.cid, voting_text)
voting_aftermath(bot, game, voting_success)
else:
log.info("Voting failed")
voting_text += "The people didn't like the two candidates!"
game.board.state.nominated_president = None
game.board.state.nominated_chancellor = None
game.board.state.failed_votes += 1
bot.send_message(game.cid, voting_text)
if game.board.state.failed_votes == 3:
do_anarchy(bot, game)
else:
voting_aftermath(bot, game, voting_success)
def voting_aftermath(bot, game, voting_success):
log.info('voting_aftermath called')
game.board.state.last_votes = {}
if voting_success:
if game.board.state.fascist_track >= 3 and game.board.state.chancellor.role == "Hitler":
# fascists win, because Hitler was elected as chancellor after 3 fascist policies
game.board.state.game_endcode = -2
end_game(bot, game, game.board.state.game_endcode)
elif game.board.state.fascist_track >= 3 and game.board.state.chancellor.role != "Hitler" and game.board.state.chancellor not in game.board.state.not_hitlers:
game.board.state.not_hitlers.append(game.board.state.chancellor)
draw_policies(bot, game)
else:
# voting was successful and Hitler was not nominated as chancellor after 3 fascist policies
draw_policies(bot, game)
else:
bot.send_message(game.cid, game.board.print_board())
start_next_round(bot, game)
def draw_policies(bot, game):
log.info('draw_policies called')
strcid = str(game.cid)
game.board.state.veto_refused = False
# shuffle discard pile with rest if rest < 3
shuffle_policy_pile(bot, game)
btns = []
for i in range(3):
game.board.state.drawn_policies.append(game.board.policies.pop(0))
for policy in game.board.state.drawn_policies:
btns.append([InlineKeyboardButton(policy, callback_data=strcid + "_" + policy)])
choosePolicyMarkup = InlineKeyboardMarkup(btns)
bot.send_message(game.board.state.president.uid,
"You drew the following 3 policies. Which one do you want to discard?",
reply_markup=choosePolicyMarkup)
def choose_policy(bot, update):
log.info('choose_policy called')
callback = update.callback_query
regex = re.search("(-[0-9]*)_(.*)", callback.data)
cid = int(regex.group(1))
answer = regex.group(2)
try:
game = GamesController.games[cid]
strcid = str(game.cid)
uid = callback.from_user.id
if len(game.board.state.drawn_policies) == 3:
log.info("Player %s (%d) discarded %s" % (callback.from_user.first_name, uid, answer))
bot.edit_message_text("The policy %s will be discarded!" % answer, uid,
callback.message.message_id)
# remove policy from drawn cards and add to discard pile, pass the other two policies
for i in range(3):
if game.board.state.drawn_policies[i] == answer:
game.board.discards.append(game.board.state.drawn_policies.pop(i))
break
pass_two_policies(bot, game)
elif len(game.board.state.drawn_policies) == 2:
if answer == "veto":
log.info("Player %s (%d) suggested a veto" % (callback.from_user.first_name, uid))
bot.edit_message_text("You suggested a Veto to President %s" % game.board.state.president.name, uid,
callback.message.message_id)
bot.send_message(game.cid,
"Chancellor %s suggested a Veto to President %s." % (
game.board.state.chancellor.name, game.board.state.president.name))
btns = [[InlineKeyboardButton("Veto! (accept suggestion)", callback_data=strcid + "_yesveto")],
[InlineKeyboardButton("No Veto! (refuse suggestion)", callback_data=strcid + "_noveto")]]
vetoMarkup = InlineKeyboardMarkup(btns)
bot.send_message(game.board.state.president.uid,
"Chancellor %s suggested a Veto to you. Do you want to veto (discard) these cards?" % game.board.state.chancellor.name,
reply_markup=vetoMarkup)
else:
log.info("Player %s (%d) chose a %s policy" % (callback.from_user.first_name, uid, answer))
bot.edit_message_text("The policy %s will be enacted!" % answer, uid,
callback.message.message_id)
# remove policy from drawn cards and enact, discard the other card
for i in range(2):
if game.board.state.drawn_policies[i] == answer:
game.board.state.drawn_policies.pop(i)
break
game.board.discards.append(game.board.state.drawn_policies.pop(0))
assert len(game.board.state.drawn_policies) == 0
enact_policy(bot, game, answer, False)
else:
log.error("choose_policy: drawn_policies should be 3 or 2, but was " + str(
len(game.board.state.drawn_policies)))
except:
log.error("choose_policy: Game or board should not be None!")
def pass_two_policies(bot, game):
log.info('pass_two_policies called')
strcid = str(game.cid)
btns = []
for policy in game.board.state.drawn_policies:
btns.append([InlineKeyboardButton(policy, callback_data=strcid + "_" + policy)])
if game.board.state.fascist_track == 5 and not game.board.state.veto_refused:
btns.append([InlineKeyboardButton("Veto", callback_data=strcid + "_veto")])
choosePolicyMarkup = InlineKeyboardMarkup(btns)
bot.send_message(game.cid,
"President %s gave two policies to Chancellor %s." % (
game.board.state.president.name, game.board.state.chancellor.name))
bot.send_message(game.board.state.chancellor.uid,
"President %s gave you the following 2 policies. Which one do you want to enact? You can also use your Veto power." % game.board.state.president.name,
reply_markup=choosePolicyMarkup)
elif game.board.state.veto_refused:
choosePolicyMarkup = InlineKeyboardMarkup(btns)
bot.send_message(game.board.state.chancellor.uid,
"President %s refused your Veto. Now you have to choose. Which one do you want to enact?" % game.board.state.president.name,
reply_markup=choosePolicyMarkup)
elif game.board.state.fascist_track < 5:
choosePolicyMarkup = InlineKeyboardMarkup(btns)
bot.send_message(game.board.state.chancellor.uid,
"President %s gave you the following 2 policies. Which one do you want to enact?" % game.board.state.president.name,
reply_markup=choosePolicyMarkup)
def enact_policy(bot, game, policy, anarchy):
log.info('enact_policy called')
if policy == "liberal":
game.board.state.liberal_track += 1
elif policy == "fascist":
game.board.state.fascist_track += 1
game.board.state.failed_votes = 0 # reset counter
if not anarchy:
bot.send_message(game.cid,
"President %s and Chancellor %s enacted a %s policy!" % (
game.board.state.president.name, game.board.state.chancellor.name, policy))
else:
bot.send_message(game.cid,
"The top most policy was enacted: %s" % policy)
sleep(3)
bot.send_message(game.cid, game.board.print_board())
# end of round
if game.board.state.liberal_track == 5:
game.board.state.game_endcode = 1
end_game(bot, game, game.board.state.game_endcode) # liberals win with 5 liberal policies
if game.board.state.fascist_track == 6:
game.board.state.game_endcode = -1
end_game(bot, game, game.board.state.game_endcode) # fascists win with 6 fascist policies
sleep(3)
# End of legislative session, shuffle if necessary
shuffle_policy_pile(bot, game)
if not anarchy:
if policy == "fascist":
action = game.board.fascist_track_actions[game.board.state.fascist_track - 1]
if action is None and game.board.state.fascist_track == 6:
pass
elif action == None:
start_next_round(bot, game)
elif action == "policy":
bot.send_message(game.cid,
"Presidential Power enabled: Policy Peek " + u"\U0001F52E" + "\nPresident %s now knows the next three policies on "
"the pile. The President may share "
"(or lie about!) the results of their "
"investigation at their discretion." % game.board.state.president.name)
action_policy(bot, game)
elif action == "kill":
bot.send_message(game.cid,
"Presidential Power enabled: Execution " + u"\U0001F5E1" + "\nPresident %s has to kill one person. You can "
"discuss the decision now but the "
"President has the final say." % game.board.state.president.name)
action_kill(bot, game)
elif action == "inspect":
bot.send_message(game.cid,
"Presidential Power enabled: Investigate Loyalty " + u"\U0001F50E" + "\nPresident %s may see the party membership of one "
"player. The President may share "
"(or lie about!) the results of their "
"investigation at their discretion." % game.board.state.president.name)
action_inspect(bot, game)
elif action == "choose":
bot.send_message(game.cid,
"Presidential Power enabled: Call Special Election " + u"\U0001F454" + "\nPresident %s gets to choose the next presidential "
"candidate. Afterwards the order resumes "
"back to normal." % game.board.state.president.name)
action_choose(bot, game)
else:
start_next_round(bot, game)
else:
start_next_round(bot, game)
def choose_veto(bot, update):
log.info('choose_veto called')
callback = update.callback_query
regex = re.search("(-[0-9]*)_(.*)", callback.data)
cid = int(regex.group(1))
answer = regex.group(2)
try:
game = GamesController.games[cid]
uid = callback.from_user.id
if answer == "yesveto":
log.info("Player %s (%d) accepted the veto" % (callback.from_user.first_name, uid))
bot.edit_message_text("You accepted the Veto!", uid, callback.message.message_id)
bot.send_message(game.cid,
"President %s accepted Chancellor %s's Veto. No policy was enacted but this counts as a failed election." % (
game.board.state.president.name, game.board.state.chancellor.name))
game.board.discards += game.board.state.drawn_policies
game.board.state.drawn_policies = []
game.board.state.failed_votes += 1
if game.board.state.failed_votes == 3:
do_anarchy(bot, game)
else:
bot.send_message(game.cid, game.board.print_board())
start_next_round(bot, game)
elif answer == "noveto":
log.info("Player %s (%d) declined the veto" % (callback.from_user.first_name, uid))
game.board.state.veto_refused = True
bot.edit_message_text("You refused the Veto!", uid, callback.message.message_id)
bot.send_message(game.cid,
"President %s refused Chancellor %s's Veto. The Chancellor now has to choose a policy!" % (
game.board.state.president.name, game.board.state.chancellor.name))
pass_two_policies(bot, game)
else:
log.error("choose_veto: Callback data can either be \"veto\" or \"noveto\", but not %s" % answer)
except:
log.error("choose_veto: Game or board should not be None!")
def do_anarchy(bot, game):
log.info('do_anarchy called')
bot.send_message(game.cid, game.board.print_board())
bot.send_message(game.cid, "ANARCHY!!")
game.board.state.president = None
game.board.state.chancellor = None
top_policy = game.board.policies.pop(0)
game.board.state.last_votes = {}
enact_policy(bot, game, top_policy, True)
def action_policy(bot, game):
log.info('action_policy called')
topPolicies = ""
# shuffle discard pile with rest if rest < 3
shuffle_policy_pile(bot, game)
for i in range(3):
topPolicies += game.board.policies[i] + "\n"
bot.send_message(game.board.state.president.uid,
"The top three polices are (top most first):\n%s\nYou may lie about this." % topPolicies)
start_next_round(bot, game)
def action_kill(bot, game):
log.info('action_kill called')
strcid = str(game.cid)
btns = []
for uid in game.playerlist:
if uid != game.board.state.president.uid and game.playerlist[uid].is_dead == False:
name = game.playerlist[uid].name
btns.append([InlineKeyboardButton(name, callback_data=strcid + "_kill_" + str(uid))])
killMarkup = InlineKeyboardMarkup(btns)
bot.send_message(game.board.state.president.uid, game.board.print_board())
bot.send_message(game.board.state.president.uid,
'You have to kill one person. You can discuss your decision with the others. Choose wisely!',
reply_markup=killMarkup)
def choose_kill(bot, update):
log.info('choose_kill called')
callback = update.callback_query
regex = re.search("(-[0-9]*)_kill_(.*)", callback.data)
cid = int(regex.group(1))
answer = int(regex.group(2))
try:
game = GamesController.games[cid]
chosen = game.playerlist[answer]
chosen.is_dead = True
if game.player_sequence.index(chosen) <= game.board.state.player_counter:
game.board.state.player_counter -= 1
game.player_sequence.remove(chosen)
game.board.state.dead += 1
log.info("Player %s (%d) killed %s (%d)" % (
callback.from_user.first_name, callback.from_user.id, chosen.name, chosen.uid))
bot.edit_message_text("You killed %s!" % chosen.name, callback.from_user.id, callback.message.message_id)
if chosen.role == "Hitler":
bot.send_message(game.cid, "President " + game.board.state.president.name + " killed " + chosen.name + ". ")
end_game(bot, game, 2)
else:
bot.send_message(game.cid,
"President %s killed %s who was not Hitler. %s, you are dead now and are not allowed to talk anymore!" % (
game.board.state.president.name, chosen.name, chosen.name))
bot.send_message(game.cid, game.board.print_board())
start_next_round(bot, game)
except:
log.error("choose_kill: Game or board should not be None!")
def action_choose(bot, game):
log.info('action_choose called')
strcid = str(game.cid)
btns = []
for uid in game.playerlist:
if uid != game.board.state.president.uid and game.playerlist[uid].is_dead == False:
name = game.playerlist[uid].name
btns.append([InlineKeyboardButton(name, callback_data=strcid + "_choo_" + str(uid))])
inspectMarkup = InlineKeyboardMarkup(btns)
bot.send_message(game.board.state.president.uid, game.board.print_board())
bot.send_message(game.board.state.president.uid,
'You get to choose the next presidential candidate. Afterwards the order resumes back to normal. Choose wisely!',
reply_markup=inspectMarkup)
def choose_choose(bot, update):
log.info('choose_choose called')
callback = update.callback_query
regex = re.search("(-[0-9]*)_choo_(.*)", callback.data)
cid = int(regex.group(1))
answer = int(regex.group(2))
try:
game = GamesController.games[cid]
chosen = game.playerlist[answer]
game.board.state.chosen_president = chosen
log.info(
"Player %s (%d) chose %s (%d) as next president" % (
callback.from_user.first_name, callback.from_user.id, chosen.name, chosen.uid))
bot.edit_message_text("You chose %s as the next president!" % chosen.name, callback.from_user.id,
callback.message.message_id)
bot.send_message(game.cid,
"President %s chose %s as the next president." % (
game.board.state.president.name, chosen.name))
start_next_round(bot, game)
except:
log.error("choose_choose: Game or board should not be None!")
def action_inspect(bot, game):
log.info('action_inspect called')
strcid = str(game.cid)
btns = []
for uid in game.playerlist:
if uid != game.board.state.president.uid and game.playerlist[uid].is_dead == False:
name = game.playerlist[uid].name
btns.append([InlineKeyboardButton(name, callback_data=strcid + "_insp_" + str(uid))])
inspectMarkup = InlineKeyboardMarkup(btns)
bot.send_message(game.board.state.president.uid, game.board.print_board())
bot.send_message(game.board.state.president.uid,
'You may see the party membership of one player. Which do you want to know? Choose wisely!',
reply_markup=inspectMarkup)
def choose_inspect(bot, update):
log.info('choose_inspect called')
callback = update.callback_query
regex = re.search("(-[0-9]*)_insp_(.*)", callback.data)
cid = int(regex.group(1))
answer = int(regex.group(2))
try:
game = GamesController.games[cid]
chosen = game.playerlist[answer]
log.info(
"Player %s (%d) inspects %s (%d)'s party membership (%s)" % (
callback.from_user.first_name, callback.from_user.id, chosen.name, chosen.uid,
chosen.party))
bot.edit_message_text("The party membership of %s is %s" % (chosen.name, chosen.party),
callback.from_user.id,
callback.message.message_id)
bot.send_message(game.cid, "President %s inspected %s." % (game.board.state.president.name, chosen.name))
start_next_round(bot, game)
except:
log.error("choose_inspect: Game or board should not be None!")
def start_next_round(bot, game):
log.info('start_next_round called')
# start next round if there is no winner (or /cancel)
if game.board.state.game_endcode == 0:
# start new round
sleep(5)
# if there is no special elected president in between
if game.board.state.chosen_president is None:
increment_player_counter(game)
start_round(bot, game)
##
#
# End of round
#
##
def end_game(bot, game, game_endcode):
log.info('end_game called')
##
# game_endcode:
# -2 fascists win by electing Hitler as chancellor
# -1 fascists win with 6 fascist policies
# 0 not ended
# 1 liberals win with 5 liberal policies
# 2 liberals win by killing Hitler
# 99 game cancelled
#
with open(STATS, 'r') as f:
stats = json.load(f)
if game_endcode == 99:
if GamesController.games[game.cid].board is not None:
bot.send_message(game.cid,
"Game cancelled!\n\n%s" % game.print_roles())
# bot.send_message(ADMIN, "Game of Secret Hitler canceled in group %d" % game.cid)
stats['cancelled'] = stats['cancelled'] + 1
else:
bot.send_message(game.cid, "Game cancelled!")
else:
if game_endcode == -2:
bot.send_message(game.cid,
"Game over! The fascists win by electing Hitler as Chancellor!\n\n%s" % game.print_roles())
stats['fascwin_hitler'] = stats['fascwin_hitler'] + 1
if game_endcode == -1:
bot.send_message(game.cid,
"Game over! The fascists win by enacting 6 fascist policies!\n\n%s" % game.print_roles())
stats['fascwin_policies'] = stats['fascwin_policies'] + 1
if game_endcode == 1:
bot.send_message(game.cid,
"Game over! The liberals win by enacting 5 liberal policies!\n\n%s" % game.print_roles())
stats['libwin_policies'] = stats['libwin_policies'] + 1
if game_endcode == 2:
bot.send_message(game.cid,
"Game over! The liberals win by killing Hitler!\n\n%s" % game.print_roles())
stats['libwin_kill'] = stats['libwin_kill'] + 1
# bot.send_message(ADMIN, "Game of Secret Hitler ended in group %d" % game.cid)
with open(STATS, 'w') as f:
json.dump(stats, f)
del GamesController.games[game.cid]
def inform_players(bot, game, cid, player_number):
log.info('inform_players called')
bot.send_message(cid,
"Let's start the game with %d players!\n%s\nGo to your private chat and look at your secret role!" % (
player_number, print_player_info(player_number)))
available_roles = list(playerSets[player_number]["roles"]) # copy not reference because we need it again later
for uid in game.playerlist:
random_index = randrange(len(available_roles))
role = available_roles.pop(random_index)
party = get_membership(role)
game.playerlist[uid].role = role
game.playerlist[uid].party = party
bot.send_message(uid, "Your secret role is: %s\nYour party membership is: %s" % (role, party))
def print_player_info(player_number):
if player_number == 5:
return "There are 3 Liberals, 1 Fascist and Hitler. Hitler knows who the Fascist is."
elif player_number == 6:
return "There are 4 Liberals, 1 Fascist and Hitler. Hitler knows who the Fascist is."
elif player_number == 7:
return "There are 4 Liberals, 2 Fascist and Hitler. Hitler doesn't know who the Fascists are."
elif player_number == 8:
return "There are 5 Liberals, 2 Fascist and Hitler. Hitler doesn't know who the Fascists are."
elif player_number == 9:
return "There are 5 Liberals, 3 Fascist and Hitler. Hitler doesn't know who the Fascists are."
elif player_number == 10:
return "There are 6 Liberals, 3 Fascist and Hitler. Hitler doesn't know who the Fascists are."
def inform_fascists(bot, game, player_number):
log.info('inform_fascists called')
for uid in game.playerlist:
role = game.playerlist[uid].role
if role == "Fascist":
fascists = game.get_fascists()
if player_number > 6:
fstring = ""
for f in fascists:
if f.uid != uid:
fstring += f.name + ", "
fstring = fstring[:-2]
bot.send_message(uid, "Your fellow fascists are: %s" % fstring)
hitler = game.get_hitler()
bot.send_message(uid, "Hitler is: %s" % hitler.name)
elif role == "Hitler":
if player_number <= 6:
fascists = game.get_fascists()
bot.send_message(uid, "Your fellow fascist is: %s" % fascists[0].name)
elif role == "Liberal":
pass
else:
log.error("inform_fascists: can\'t handle the role %s" % role)
def get_membership(role):
log.info('get_membership called')
if role == "Fascist" or role == "Hitler":
return "fascist"
elif role == "Liberal":
return "liberal"
else:
return None
def increment_player_counter(game):
log.info('increment_player_counter called')
if game.board.state.player_counter < len(game.player_sequence) - 1:
game.board.state.player_counter += 1
else:
game.board.state.player_counter = 0
def shuffle_policy_pile(bot, game):
log.info('shuffle_policy_pile called')
if len(game.board.policies) < 3:
game.board.discards += game.board.policies
game.board.policies = random.sample(game.board.discards, len(game.board.discards))
game.board.discards = []
bot.send_message(game.cid,
"There were not enough cards left on the policy pile so I shuffled the rest with the discard pile!")
def error(bot, update, error):
logger.warning('Update "%s" caused error "%s"' % (update, error))
def main():
GamesController.init() #Call only once
#initialize_testdata()
updater = Updater(TOKEN)
# Get the dispatcher to register handlers
dp = updater.dispatcher
# on different commands - answer in Telegram
dp.add_handler(CommandHandler("start", Commands.command_start))
dp.add_handler(CommandHandler("help", Commands.command_help))
dp.add_handler(CommandHandler("board", Commands.command_board))
dp.add_handler(CommandHandler("rules", Commands.command_rules))
dp.add_handler(CommandHandler("ping", Commands.command_ping))
dp.add_handler(CommandHandler("symbols", Commands.command_symbols))
dp.add_handler(CommandHandler("stats", Commands.command_stats))
dp.add_handler(CommandHandler("newgame", Commands.command_newgame))
dp.add_handler(CommandHandler("startgame", Commands.command_startgame))
dp.add_handler(CommandHandler("cancelgame", Commands.command_cancelgame))
dp.add_handler(CommandHandler("join", Commands.command_join))
dp.add_handler(CommandHandler("votes", Commands.command_votes))
dp.add_handler(CommandHandler("calltovote", Commands.command_calltovote))
dp.add_handler(CallbackQueryHandler(pattern="(-[0-9]*)_chan_(.*)", callback=nominate_chosen_chancellor))
dp.add_handler(CallbackQueryHandler(pattern="(-[0-9]*)_insp_(.*)", callback=choose_inspect))
dp.add_handler(CallbackQueryHandler(pattern="(-[0-9]*)_choo_(.*)", callback=choose_choose))
dp.add_handler(CallbackQueryHandler(pattern="(-[0-9]*)_kill_(.*)", callback=choose_kill))
dp.add_handler(CallbackQueryHandler(pattern="(-[0-9]*)_(yesveto|noveto)", callback=choose_veto))
dp.add_handler(CallbackQueryHandler(pattern="(-[0-9]*)_(liberal|fascist|veto)", callback=choose_policy))
dp.add_handler(CallbackQueryHandler(pattern="(-[0-9]*)_(Ja|Nein)", callback=handle_voting))
# log all errors
dp.add_error_handler(error)
# Start the Bot
updater.start_polling()
# Run the bot until the you presses Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT. This should be used most of the time, since
# start_polling() is non-blocking and will stop the bot gracefully.
updater.idle()
if __name__ == '__main__':
main()