-
Notifications
You must be signed in to change notification settings - Fork 3
/
bot.py
708 lines (599 loc) Β· 30 KB
/
bot.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
import logging
import random
import string
from datetime import datetime, time, timedelta, timezone
from telegram import (InlineKeyboardButton, InlineKeyboardMarkup,
ReplyKeyboardMarkup, ReplyKeyboardRemove)
from telegram.ext import (BaseFilter, CallbackQueryHandler, CommandHandler,
Filters, MessageHandler, Updater)
import bridge
import keyboards
# -----DEBUG-----
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
# -----DEBUG-----
updater = Updater(
token='##TOKEN##',
use_context=True
)
dispatcher = updater.dispatcher
# TODO: implement timeouts for actions
# job_queue = updater.job_queue
game_data = {}
# No. of players in a bridge game, SET TO 1 FOR DEBUGGING PURPOSES
PLAYERS = 4
class BidFilter(BaseFilter):
def filter(self, message):
return (len(message.text) == 3
and ord(message.text[0]) >= ord("1")
and ord(message.text[0]) <= ord("7")
and message.text[1] == " "
and message.text[2] in bridge.BID_SUITS
or message.text == "β Pass!"
or message.text == "βͺ")
class CardFilter(BaseFilter):
def filter(self, message):
arg_list = message.text.split()
return (message.text == "βͺ"
or len(arg_list) == 2
and (arg_list[0].isdigit()
and int(arg_list[0]) >= 2
and int(arg_list[0]) <= 10
or arg_list[0] in ["A", "K", "Q", "J"])
and arg_list[1] in bridge.CARD_SUITS)
def random_string(string_length=10):
return ''.join(random.choice(string.ascii_letters+string.digits) for i in range(string_length))
def stop_game(context):
if not "game_id" in context.chat_data:
return
game_id = context.chat_data["game_id"]
if game_data[game_id]["mode"] != "lobby":
for i in range(0, PLAYERS):
context.bot.edit_message_text(
chat_id=game_data[game_id]["players_chat_id"][i],
message_id=game_data[game_id]["hand_message_id"][i],
parse_mode="markdown",
text="π Game in _" +
game_data[game_id]["chat_title"] + "_ has ended! π"
)
context.bot.edit_message_text(
chat_id=game_data[game_id]["chat_id"],
message_id=game_data[game_id]["initial_message_id"],
parse_mode="markdown",
text="Bridge game has ended!\n\nπ Use '/start' to start a new game π"
)
del game_data[game_id]
del context.chat_data["game_id"]
def start(update, context):
# START A GAME (IN A GROUP)
if (update.effective_chat.type == "group" or update.effective_chat.type == "supergroup"):
if "game_id" in context.chat_data:
# TODO: check if the initial game message has been deleted
# if so, delete the initial gamedata and reset the game
context.bot.send_message(
chat_id=update.effective_chat.id,
reply_to_message_id=game_data[context.chat_data["game_id"]
]["initial_message_id"],
parse_mode="markdown",
text="β A game has already been started!"
)
return
game_id = random_string(string_length=10)
while game_id in game_data:
game_id = random_string(string_length=10)
keyboard = [[InlineKeyboardButton(
"βΆ Join game", url="https://t.me/sg_bridge_bot?start="+game_id)]]
initial_message = context.bot.send_message(
chat_id=update.effective_chat.id,
parse_mode="markdown",
text=f"β£β¦ *Bridge game started* β₯β \n\n_Waiting for {PLAYERS} players to join..._",
reply_markup=InlineKeyboardMarkup(keyboard)
)
game_data[game_id] = {
"initial_message_id": initial_message.message_id,
"chat_id": update.effective_chat.id,
"chat_title": update.effective_chat.title,
"players": [],
"players_chat_id": [],
"hand_message_id": [None] * PLAYERS,
"mode": "lobby", # or bid, partner, play
"turn": 0,
"bidder": -1,
"bid": -1,
"hands": bridge.generate_hands(),
"played_cards": [None] * PLAYERS,
"sets": [0] * PLAYERS,
"sets_needed": -1,
"trump_broken": False,
"first_player": False,
"current_suit": None
}
context.chat_data["game_id"] = game_id
# JOIN A GAME
elif update.effective_chat.type == "private":
if not context.args or not context.args[0] in game_data:
keyboard = [[InlineKeyboardButton(
"π₯ Choose a group", url="https://t.me/sg_bridge_bot?startgroup=_")]]
context.bot.send_message(
chat_id=update.effective_chat.id,
parse_mode="markdown",
text="Add me to a group to start playing! π",
reply_markup=InlineKeyboardMarkup(keyboard)
)
else:
game_id = context.args[0]
if update.effective_user in game_data[game_id]["players"]:
context.bot.send_message(
chat_id=update.effective_chat.id,
parse_mode="markdown",
text="β You have already joined the game in _"
+ game_data[game_id]["chat_title"] + "_!"
)
return
elif len(game_data[game_id]["players"]) == PLAYERS:
context.bot.send_message(
chat_id=update.effective_chat.id,
parse_mode="markdown",
text="β The game in _"
+ game_data[game_id]["chat_title"] + "_ is already full!"
)
return
game_data[game_id]["players"].append(update.effective_user)
game_data[game_id]["players_chat_id"].append(
update.effective_chat.id)
player_names = []
for player in game_data[game_id]["players"]:
player_names.append("π " + player.mention_markdown())
player_names = '\n'.join(player_names)
context.bot.send_message(
chat_id=update.effective_chat.id,
parse_mode="markdown",
text="β
Successfully joined game in _"
+ game_data[game_id]["chat_title"] + "_!"
)
if len(game_data[game_id]["players"]) < PLAYERS:
keyboard = [[InlineKeyboardButton(
"βΆ Join game", url="https://t.me/sg_bridge_bot?start="+game_id)]]
context.bot.edit_message_text(
chat_id=game_data[game_id]["chat_id"],
message_id=game_data[game_id]["initial_message_id"],
reply_markup=InlineKeyboardMarkup(keyboard),
parse_mode="markdown",
text="β£β¦ *Bridge game started* β₯β \n\n*Players*\n"
+ player_names
+ "\n\n_Waiting for "
+ str(PLAYERS - len(game_data[game_id]["players"]))
+ " more player(s) to join..._"
)
else:
context.bot.edit_message_text(
chat_id=game_data[game_id]["chat_id"],
message_id=game_data[game_id]["initial_message_id"],
parse_mode="markdown",
text="β£β¦ *Bridge game started* β₯β \n\n*Players*\n"
+ player_names
+ "\n\nβ
Game has begun! Check your PMs to see your cards."
)
game_data[game_id]["mode"] = "bid"
keyboard = keyboards.bid_keyboard()
for i in range(0, PLAYERS):
hand_message = context.bot.send_message(
chat_id=game_data[game_id]["players_chat_id"][i],
parse_mode="markdown",
text="π *Your hand* π\n"
+ "(for _" +
game_data[game_id]["chat_title"] + "_)\n\n"
+ str(game_data[game_id]["sets"][i])
+ " set(s) π won\n\n"
+ bridge.generate_hand_string(game_data[game_id]["hands"][i]))
game_data[game_id]["hand_message_id"][i] = hand_message.message_id
context.bot.send_message(
chat_id=game_data[game_id]["chat_id"],
parse_mode="markdown",
text=game_data[game_id]["players"][0].mention_markdown()
+ ", start the bid!",
reply_markup=ReplyKeyboardMarkup(keyboard, selective=True, one_time_keyboard=True, resize_keyboard=True))
def stop(update, context):
if (update.effective_chat.type == "group" or update.effective_chat.type == "supergroup") and "game_id" in context.chat_data:
game_id = context.chat_data["game_id"]
else:
context.bot.send_message(
chat_id=update.effective_chat.id,
parse_mode="markdown",
text="β No game has been started!\n\nUse '/start' to start a new game π"
)
return
# TODO: implement stopping of game
keyboard = [[InlineKeyboardButton("β
Stop game", callback_data="stop")],
[InlineKeyboardButton("β Cancel", callback_data="cancel")]]
reply_markup = InlineKeyboardMarkup(keyboard)
context.bot.send_message(
chat_id=update.effective_chat.id,
parse_mode="markdown",
reply_to_message_id=game_data[game_id]["initial_message_id"],
text="β Are you sure you want to stop this bridge game?",
reply_markup=reply_markup)
def inline_button(update, context):
query = update.callback_query
if query.data == "stop":
if not "game_id" in context.chat_data:
return
game_id = context.chat_data["game_id"]
context.bot.send_message(
chat_id=game_data[game_id]["chat_id"],
message_id=game_data[game_id]["initial_message_id"],
parse_mode="markdown",
text="Bridge game has ended!\n\nπ Use '/start' to start a new game π"
)
stop_game(context)
query.edit_message_text(
text="The game has been stopped successfully β", parse_mode="markdown")
elif query.data == 'cancel':
query.edit_message_text(
text="This game will continue! π", parse_mode="markdown")
def card(update, context):
if (update.effective_chat.type == "group" or update.effective_chat.type == "supergroup") and "game_id" in context.chat_data:
game_id = context.chat_data["game_id"]
else:
return
# PARTNER MODE
if (game_data[game_id]["mode"] == "partner"
and update.effective_user == game_data[game_id]["players"][game_data[game_id]["turn"]]):
partner_card = update.message.text.split()
game_data[game_id]["partner_card"] = update.message.text
for i in range(0, 4):
if partner_card[0] in game_data[game_id]["hands"][i][partner_card[1]]:
game_data[game_id]["partner"] = i
break
partner = game_data[game_id]["partner"]
bidder = game_data[game_id]["bidder"]
if partner == bidder:
context.bot.send_message(
chat_id=game_data[game_id]["players_chat_id"][partner],
parse_mode="markdown",
text="π¬ Psst... you picked *yourself* as partner π₯ !")
# This check exists to make sure that the program doesn't crash when im debugging with <4 players
elif partner < PLAYERS:
context.bot.send_message(
chat_id=game_data[game_id]["players_chat_id"][partner],
parse_mode="markdown",
text="π¬ Psst... you are *" +
game_data[game_id]["players"][bidder].full_name
+ "'s* partner π₯ !")
else:
# Either I'm debugging and the partner picked isn't a actual player (bc i have <4 players)
# or shit has gone down
context.bot.send_message(
chat_id=game_data[game_id]["players_chat_id"][game_data[game_id]["turn"]],
parse_mode="markdown",
text="β DEBUG: Actually, the partner you picked isn't playing... ("
+ str(partner) + ")")
partner = game_data[game_id]["partner"] = bidder
context.bot.send_message(
chat_id=game_data[game_id]["chat_id"],
parse_mode="markdown",
text=update.effective_user.mention_markdown() + "'s partner π₯ is *" +
update.message.text + "*",
reply_markup=ReplyKeyboardRemove())
game_data[game_id]["mode"] = "play"
if game_data[game_id]["trump_suit"] == "π«":
hand = game_data[game_id]["hands"][game_data[game_id]["bidder"]]
keyboard = keyboards.hand_keyboard(hand)
game_data[game_id]["first_player"] = game_data[game_id]["turn"]
context.bot.send_message(
chat_id=game_data[game_id]["chat_id"],
parse_mode="markdown",
text="Winning bid is *No Trump* π«:\nBidder, " +
update.effective_user.mention_markdown() + ", will start",
reply_markup=ReplyKeyboardMarkup(keyboard, selective=True, one_time_keyboard=True, resize_keyboard=True))
else:
game_data[game_id]["turn"] += 1
game_data[game_id]["turn"] %= PLAYERS
game_data[game_id]["first_player"] = game_data[game_id]["turn"]
hand = game_data[game_id]["hands"][game_data[game_id]["turn"]]
valid_suits = bridge.get_valid_suits(
hand, trump_suit=game_data[game_id]["trump_suit"])
keyboard = keyboards.hand_keyboard(hand, valid_suits)
context.bot.send_message(
chat_id=game_data[game_id]["chat_id"],
parse_mode="markdown",
text=game_data[game_id]["players"][game_data[game_id]
["turn"]].mention_markdown() + " will start",
reply_markup=ReplyKeyboardMarkup(keyboard, selective=True, one_time_keyboard=True, resize_keyboard=True))
# PLAYING MODE
elif (game_data[game_id]["mode"] == "play"
and update.effective_user == game_data[game_id]["players"][game_data[game_id]["turn"]]):
turn = game_data[game_id]["turn"]
first_player = game_data[game_id]["first_player"]
played_cards = game_data[game_id]["played_cards"]
# first_card = played_cards[first_player]
trump_suit = game_data[game_id]["trump_suit"]
trump_broken = game_data[game_id]["trump_broken"]
current_suit = game_data[game_id]["current_suit"]
current_card = update.message.text.split()
# check if play is valid
hand = game_data[game_id]["hands"][turn]
valid_suits = bridge.get_valid_suits(hand,
trump_suit=trump_suit,
current_suit=current_suit,
trump_broken=trump_broken)
if current_card[0] == 'βͺ':
keyboard = keyboards.hand_keyboard(hand, valid_suits)
context.bot.send_message(
chat_id=game_data[game_id]["chat_id"],
parse_mode="markdown",
text="β "
+ game_data[game_id]["players"][turn].mention_markdown()
+ ", that was an invalid card... Pick again!",
reply_markup=ReplyKeyboardMarkup(keyboard, selective=True, one_time_keyboard=True, resize_keyboard=True))
return
if not (current_card[1] in valid_suits
and current_card[0] in game_data[game_id]["hands"][turn][current_card[1]]):
return
game_data[game_id]["played_cards"][turn] = update.message.text
game_data[game_id]["hands"][turn][
current_card[1]].remove(current_card[0])
context.bot.edit_message_text(
chat_id=game_data[game_id]["players_chat_id"][turn],
message_id=game_data[game_id]["hand_message_id"][turn],
parse_mode="markdown",
text="π *Your hand* π\n"
+ "(for _" + game_data[game_id]["chat_title"] + "_)\n\n"
+ str(game_data[game_id]["sets"][turn]) + " set(s) π won\n\n"
+ bridge.generate_hand_string(game_data[game_id]["hands"][turn])
)
if first_player == turn:
game_data[game_id]["current_suit"] = current_suit = current_card[1]
if current_card[1] == trump_suit:
game_data[game_id]["trump_broken"] = True
trump_broken = True
game_data[game_id]["turn"] += 1
game_data[game_id]["turn"] %= PLAYERS
turn = game_data[game_id]["turn"]
played_cards_string_list = []
# for i in range(0, PLAYERS):
# played_cards_string_list.append("\n")
# if i == game_data[game_id]["turn"] and not game_data[game_id]["played_cards"][i]:
# played_cards_string_list.append("βοΈ ")
# else:
# played_cards_string_list.append("π ")
# played_cards_string_list.append(
# game_data[game_id]["players"][i].full_name)
# played_cards_string_list.append(
# " (" + str(game_data[game_id]["sets"][i]) + " π) - ")
# if game_data[game_id]["played_cards"][i]:
# played_cards_string_list.append(
# game_data[game_id]["played_cards"][i])
# else:
# played_cards_string_list.append("βͺ")
for i in range(first_player, first_player+PLAYERS):
played_cards_string_list.append("\n")
i %= PLAYERS
if i == game_data[game_id]["turn"] and not game_data[game_id]["played_cards"][i]:
played_cards_string_list.append("βοΈ ")
else:
played_cards_string_list.append("π ")
played_cards_string_list.append(
game_data[game_id]["players"][i].full_name)
played_cards_string_list.append(
" (" + str(game_data[game_id]["sets"][i]) + " π) - ")
if game_data[game_id]["played_cards"][i]:
played_cards_string_list.append(
game_data[game_id]["played_cards"][i])
else:
played_cards_string_list.append("βͺ")
played_cards_string = "".join(played_cards_string_list)
context.bot.send_message(
chat_id=game_data[game_id]["chat_id"],
parse_mode="markdown",
text="π *Bid:* "
+ game_data[game_id]["players"][game_data[game_id]
["bidder"]].full_name
+ " - " + bridge.get_bid_from_num(game_data[game_id]["bid"])
+ "\nπ₯ *Partner:* " + game_data[game_id]["partner_card"]
+ "\n" + played_cards_string,
reply_markup=ReplyKeyboardRemove())
# All players have played a card, i.e. end of set
if turn == first_player:
winner = bridge.compare_cards(
played_cards, current_suit, trump_suit=trump_suit)
game_data[game_id]["sets"][winner] += 1
game_data[game_id]["turn"] = winner
game_data[game_id]["first_player"] = winner
game_data[game_id]["current_suit"] = None
game_data[game_id]["played_cards"] = [None] * PLAYERS
# TODO: check if winner + partner has enough sets, if so declare a win
# also check if two non partners have enough sets
bidder = game_data[game_id]["bidder"]
partner = game_data[game_id]["partner"]
sets = game_data[game_id]["sets"]
sets_needed = game_data[game_id]["sets_needed"]
if partner == bidder:
bidder_sets = sets[bidder]
else:
bidder_sets = sets[bidder] + sets[partner]
if bidder_sets == sets_needed:
# TODO: clear all the hand messages, send a win message and delete game object
context.bot.send_message(
chat_id=game_data[game_id]["chat_id"],
parse_mode="markdown",
text=game_data[game_id]["players"][winner].mention_markdown()
+ ", you have won this set π !\n\n"
+ "You now have *"
+ str(game_data[game_id]["sets"][winner])
+ " set(s)* π\n\n")
if bidder == partner:
context.bot.send_message(
chat_id=game_data[game_id]["chat_id"],
parse_mode="markdown",
text="π
The bidder, "
+ game_data[game_id]["players"][bidder].mention_markdown()
+ ", has won the game alone! π
\n\n"
+ "Use '/start' to start a new game π")
else:
context.bot.send_message(
chat_id=game_data[game_id]["chat_id"],
parse_mode="markdown",
text="π
The bidder, "
+ game_data[game_id]["players"][bidder].mention_markdown()
+ ", and partner, "
+ game_data[game_id]["players"][partner].mention_markdown()
+ ", have won the game! π
\n\n"
+ "Use '/start' to start a new game π")
stop_game(context)
return
elif sum(sets) - bidder_sets == 14 - sets_needed:
winner_list = []
for i in range(0, PLAYERS):
if i != partner and i != bidder:
winner_list.append(
game_data[game_id]["players"][i].mention_markdown())
context.bot.send_message(
chat_id=game_data[game_id]["chat_id"],
parse_mode="markdown",
text=game_data[game_id]["players"][winner].mention_markdown()
+ ", you have won this set π !\n\n"
+ "You now have *"
+ str(game_data[game_id]["sets"][winner])
+ " set(s)* π\n\n")
if len(winner_list) == 3:
winner_string = (winner_list[0] + ", " + winner_list[1]
+ " and " + winner_list[2])
else:
winner_string = " and ".join(winner_list)
context.bot.send_message(
chat_id=game_data[game_id]["chat_id"],
parse_mode="markdown",
text="π
"
+ winner_string
+ " have won the game! π
\n\n"
+ "Use '/start' to start a new game π")
stop_game(context)
return
winner_hand = game_data[game_id]["hands"][winner]
valid_suits = bridge.get_valid_suits(
winner_hand, trump_suit=trump_suit, trump_broken=trump_broken)
keyboard = keyboards.hand_keyboard(winner_hand, valid_suits)
context.bot.send_message(
chat_id=game_data[game_id]["chat_id"],
parse_mode="markdown",
text=game_data[game_id]["players"][winner].mention_markdown()
+ ", you have won this set π !\n\n"
+ "You now have *"
+ str(game_data[game_id]["sets"][winner])
+ " set(s)* π\n\n"
+ "Pick a card to start the next set!",
reply_markup=ReplyKeyboardMarkup(keyboard, selective=True, one_time_keyboard=True, resize_keyboard=True))
else:
next_hand = game_data[game_id]["hands"][turn]
valid_suits = bridge.get_valid_suits(
next_hand, trump_suit=trump_suit, trump_broken=trump_broken, current_suit=current_suit)
keyboard = keyboards.hand_keyboard(next_hand, valid_suits)
context.bot.send_message(
chat_id=game_data[game_id]["chat_id"],
parse_mode="markdown",
text=game_data[game_id]["players"][turn].mention_markdown()
+ ", it's your turn βοΈ !",
reply_markup=ReplyKeyboardMarkup(keyboard, selective=True, one_time_keyboard=True, resize_keyboard=True))
def bid(update, context):
if (update.effective_chat.type == "group" or update.effective_chat.type == "supergroup") and "game_id" in context.chat_data:
game_id = context.chat_data["game_id"]
else:
return
# HANDLE OVERLAPS IN BID/CARD FILTERS
if ((game_data[game_id]["mode"] == "partner"
or game_data[game_id]["mode"] == "play")
and CardFilter.filter(None, update.message)):
card(update, context)
elif (game_data[game_id]["mode"] == "bid"
and update.effective_user == game_data[game_id]["players"][game_data[game_id]["turn"]]):
if update.message.text == "β Pass!":
context.bot.send_message(
chat_id=game_data[game_id]["chat_id"],
parse_mode="markdown",
text=update.effective_user.mention_markdown() + " *passed* β this turn",
reply_markup=ReplyKeyboardRemove())
elif update.message.text == "βͺ":
keyboard = keyboards.bid_keyboard(game_data[game_id]["bid"])
context.bot.send_message(
chat_id=game_data[game_id]["chat_id"],
parse_mode="markdown",
text=("*Current bid:* "
+ game_data[game_id]["players"]
[game_data[game_id]["bidder"]].full_name
+ " - "
+ bridge.get_bid_from_num(game_data[game_id]["bid"])
if game_data[game_id]["bidder"] + 1 else "")
+ "\n\n"
+ game_data[game_id]["players"][game_data[game_id]
["turn"]].mention_markdown()
+ ", it's your turn to bid!",
reply_markup=ReplyKeyboardMarkup(keyboard, selective=True, one_time_keyboard=True, resize_keyboard=True))
return
else:
bid = update.message.text
bid_num = bridge.get_num_from_bid(bid)
if bid_num <= game_data[game_id]["bid"] or bid_num > 34:
return
game_data[game_id]["bid"] = bid_num
game_data[game_id]["trump_suit"] = bid[2]
game_data[game_id]["sets_needed"] = int(bid[0]) + 6
game_data[game_id]["bidder"] = game_data[game_id]["turn"]
context.bot.send_message(
chat_id=game_data[game_id]["chat_id"],
parse_mode="markdown",
text=update.effective_user.mention_markdown() + " bidded *" + bid + "*",
reply_markup=ReplyKeyboardRemove())
game_data[game_id]["turn"] += 1
game_data[game_id]["turn"] %= PLAYERS
# TODO: handle 4 passes in a row
# if (game_data[game_id]["bid"] == -1
# and game_data[game_id]["bidder"] == -1
# and game_data[game_id]["turn"] == 0):
# perform wash...
# -- recommend to refactor card shuffling and issuing (aka wash) into a reusable function --
if game_data[game_id]["bidder"] == game_data[game_id]["turn"] or game_data[game_id]["bid"] == 34:
context.bot.send_message(
chat_id=game_data[game_id]["chat_id"],
parse_mode="markdown",
text=game_data[game_id]["players"][game_data[game_id]
["bidder"]].mention_markdown()
+ ", you have won the bid π of *"
+ bridge.get_bid_from_num(game_data[game_id]["bid"]) + "*!\n\n"
+ "You and your partner need a total of *"
+ str(game_data[game_id]["sets_needed"])
+ " sets* π to win",
reply_markup=ReplyKeyboardRemove())
game_data[game_id]["mode"] = "partner"
game_data[game_id]["turn"] = game_data[game_id]["bidder"]
keyboard = keyboards.partner_keyboard()
context.bot.send_message(
chat_id=game_data[game_id]["chat_id"],
parse_mode="markdown",
text=game_data[game_id]["players"][game_data[game_id]
["bidder"]].mention_markdown()
+ ", choose your partner π₯",
reply_markup=ReplyKeyboardMarkup(keyboard, selective=True, one_time_keyboard=True, resize_keyboard=True))
else:
keyboard = keyboards.bid_keyboard(game_data[game_id]["bid"])
context.bot.send_message(
chat_id=game_data[game_id]["chat_id"],
parse_mode="markdown",
text=("*Current bid:* "
+ game_data[game_id]["players"]
[game_data[game_id]["bidder"]].full_name
+ " - "
+ bridge.get_bid_from_num(game_data[game_id]["bid"])
if game_data[game_id]["bidder"] + 1 else "")
+ "\n\n"
+ game_data[game_id]["players"][game_data[game_id]
["turn"]].mention_markdown()
+ ", it's your turn to bid!",
reply_markup=ReplyKeyboardMarkup(keyboard, selective=True, one_time_keyboard=True, resize_keyboard=True))
dispatcher.add_handler(CommandHandler('start', start))
dispatcher.add_handler(CommandHandler('stop', stop))
dispatcher.add_handler(CallbackQueryHandler(inline_button))
dispatcher.add_handler(MessageHandler(Filters.text & BidFilter(), bid))
dispatcher.add_handler(MessageHandler(Filters.text & CardFilter(), card))
updater.start_polling()