-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
568 lines (513 loc) · 19 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
import logging
import os
import telegram.bot
from telegram import (InlineKeyboardButton, InlineKeyboardMarkup,
InlineQueryResultArticle, InputTextMessageContent,
ParseMode, TelegramError)
from telegram.ext import (CallbackQueryHandler, ChosenInlineResultHandler,
CommandHandler, DelayQueue, InlineQueryHandler,
Updater)
import testPostgres # TODO edit after test
from bridge import Game, Player
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
print("test test test logging with print") # TODO remove after test
# (12 May 2020) DelayQueues run forever. Might have problem in the future.
delayQueues = {} # {chatId:DelayQueue}
def get_markup():
firstRow, secondRow = [], []
firstRow.append(InlineKeyboardButton("Join!", callback_data='1'))
firstRow.append(InlineKeyboardButton("Quit...", callback_data='2'))
secondRow.append(InlineKeyboardButton("Insert AI", callback_data='3'))
secondRow.append(InlineKeyboardButton("Delete AI", callback_data='4'))
return InlineKeyboardMarkup([firstRow, secondRow])
def update_join_message(chatId, buttons=True):
game = Game.games[chatId]
joinMessage = game.joinMessage
text = "Waiting for players to join ...\nJoined players:\n"
text += '\n'.join([player.name for player in game.players])
if not buttons:
delayQueues[chatId](joinMessage.edit_text, text=text)
return
delayQueues[chatId](
joinMessage.edit_text,
text=text,
reply_markup=get_markup()
)
def start(update, context):
chatId = update.effective_chat.id
if chatId not in delayQueues:
# Official limit is 20 msg/1 min. Make it stricter here.
delayQueues[chatId] = DelayQueue(burst_limit=19, time_limit_ms=61000)
if chatId in Game.games:
game = Game.games[chatId]
delayQueues[chatId](
context.bot.send_message,
reply_to_message_id=game.joinMessage.message_id,
chat_id=chatId,
text='Game already started!'
)
return
# create new Game and store in Game.games
game = Game(chatId)
game.joinMessage = context.bot.send_message(
chat_id=chatId,
text="Waiting for players to join ...\nJoined players:",
reply_markup=get_markup(),
)
def stop(update, context):
chatId = update.effective_chat.id
if chatId not in delayQueues:
# Official limit is 20 msg/1 min. Make it stricter here.
delayQueues[chatId] = DelayQueue(burst_limit=19, time_limit_ms=61000)
if chatId not in Game.games:
delayQueues[chatId](
context.bot.send_message,
chat_id=chatId,
text='No game started!'
)
return
game = Game.games[chatId]
if game.phase == Game.JOIN_PHASE: # no need update join msg aft game starts
update_join_message(chatId, buttons=False)
game.stop()
delayQueues[chatId](
context.bot.send_message,
reply_to_message_id=game.joinMessage.message_id,
chat_id=chatId,
text='Game stopped.'
)
for player in game.players:
if player.handMessage:
delayQueues[chatId](player.handMessage.edit_text, 'Game stopped.')
del game
def help(update, context):
chatId = update.effective_chat.id
if chatId not in delayQueues:
# Official limit is 20 msg/1 min. Make it stricter here.
delayQueues[chatId] = DelayQueue(burst_limit=19, time_limit_ms=61000)
text = '[Floating bridge](https://en.wikipedia.org/wiki/Singaporean_bridge)\n'
text += 'Start game: /start\n'
text += 'Stop game: /stop\n'
text += 'Show this: /help\n\n'
text += 'For 1st timers, pm me @{} '.format(context.bot.username)
text += 'so that I can pm you.\n\n'
text += 'Type \'@{} hi\' to select bid/cards.\n'.format(
context.bot.username)
text += 'Wait for a while for bids/cards to appear.\n\n'
text += 'I can only send <20 messages/minute'
text += ", so please wait for 1 minute if I am not responding.\n"
text += "If I did not respond within 1 minute, an error might have occured.\n"
text += "Try /start to start a new game.\n\n"
text += 'Good luck and have fun!'
delayQueues[chatId](
context.bot.send_message,
chat_id=chatId,
text=text,
parse_mode=ParseMode.MARKDOWN
)
def join(update, context):
chatId = update.effective_chat.id
query = update.callback_query
user = query.from_user
game = Game.games[chatId]
if game.full():
return
joinSuccess = game.add_human(user.id, user.first_name)
# slim possibility of failure due to full of players
# (checked for fullness above and in game.add_human(...))
if not joinSuccess:
delayQueues[chatId](
context.bot.send_message,
chat_id=chatId,
text='[{}](tg://user?id={}), '.format(user.first_name, user.id) +
'quit/stop the game you joined (maybe in other chat) to join this game!',
parse_mode=ParseMode.MARKDOWN
)
return
try:
context.bot.send_message(chat_id=user.id, text='Joined game!')
except(TelegramError):
delayQueues[chatId](
context.bot.send_message,
chat_id=chatId,
text='[{}](tg://user?id={}), '.format(user.first_name, user.id) +
'please initiate a conversation with me ' +
'@{} !'.format(context.bot.username),
parse_mode=ParseMode.MARKDOWN
)
# undo the join
game.del_human(user.id)
return
if not game.full():
update_join_message(chatId)
return
start_game(update, context)
def quit(update, context):
chatId = update.effective_chat.id
query = update.callback_query
user = query.from_user
game = Game.games[chatId]
quitSuccess = game.del_human(user.id)
if not quitSuccess:
delayQueues[chatId](
context.bot.send_message,
chat_id=chatId,
text='[{}](tg://user?id={}), '.format(user.first_name, user.id) +
'you are not in the game of this chat!',
parse_mode=ParseMode.MARKDOWN
)
return
update_join_message(chatId)
def insert_AI(update, context):
chatId = update.effective_chat.id
query = update.callback_query
game = Game.games[chatId]
if game.full():
return
# does not matter if add fail due to full players
game.add_AI()
if not game.full():
update_join_message(chatId)
return
start_game(update, context)
def delete_AI(update, context):
chatId = update.effective_chat.id
query = update.callback_query
game = Game.games[chatId]
delSuccess = game.del_AI()
if not delSuccess:
delayQueues[chatId](
context.bot.send_message,
chat_id=chatId,
text='No AI in the game!'
)
return
update_join_message(chatId)
def button(update, context):
data = update.callback_query.data
chatId = update.effective_chat.id
try:
if data == '1':
join(update, context)
elif data == '2':
quit(update, context)
elif data == '3':
insert_AI(update, context)
elif data == '4':
delete_AI(update, context)
except KeyError: # chatId not in Game.games
if chatId not in delayQueues:
# Official limit is 20 msg/1 min. Make it stricter here.
delayQueues[chatId] = DelayQueue(
burst_limit=19, time_limit_ms=61000)
try:
update.callback_query.message.delete()
text = 'I was restarted recently and lost memory. '
text += 'Please ignore the unfinished games. '
text += 'Sorry for the inconvenience. '
delayQueues[chatId](
context.bot.send_message,
chat_id=chatId,
text=text
)
except TelegramError:
# "Message to delete not found": already deleted the message, no need to send lost-memory-message
pass
update.callback_query.answer()
def translate_bid(bid):
'''Returns bid in a more readable form.'''
if bid == Game.PASS:
return 'PASS'
bid = bid.replace('C', '♣️')
bid = bid.replace('D', '♦️')
bid = bid.replace('H', '❤️')
bid = bid.replace('S', '♠️')
bid = bid.replace('N', '🚫')
return bid
def translate_card(card):
'''Returns card in a more readable form.'''
if not card:
return
card = card[::-1]
card = card.replace('T', '10')
card = card.replace('C', '♣️')
card = card.replace('D', '♦️')
card = card.replace('H', '❤️')
card = card.replace('S', '♠️')
return card
def translate_hand(hand):
club, diamond, heart, spade = [], [], [], []
for card in hand:
if card[0] == 'C':
club.append(card[1])
elif card[0] == 'D':
diamond.append(card[1])
elif card[0] == 'H':
heart.append(card[1])
elif card[0] == 'S':
spade.append(card[1])
result = ''
for suit, symbol in ((club, '♣️'), (diamond, '♦️'), (heart, '❤️'), (spade, '♠️')):
line = symbol + ': ' + ' '.join(suit) + '\n'
line = line.replace('T', '10')
result += line
return result
def thumb_url_bid(bid):
if bid == Game.PASS:
return 'https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/60/apple/81/waving-white-flag_1f3f3.png'
if bid[1] == 'C':
return 'https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/60/apple/51/black-club-suit_2663.png'
if bid[1] == 'D':
return 'https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/60/apple/51/black-diamond-suit_2666.png'
if bid[1] == 'H':
return 'https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/60/apple/237/black-heart-suit_2665.png'
if bid[1] == 'S':
return 'https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/60/apple/81/black-spade-suit_2660.png'
if bid[1] == 'N':
return 'https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/60/google/241/prohibited_1f6ab.png'
def thumb_url_card(card):
if card[0] == 'C':
return 'https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/60/apple/125/black-club-suit_2663.png'
if card[0] == 'D':
return 'https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/60/apple/126/black-diamond-suit_2666.png'
if card[0] == 'H':
return 'https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/60/apple/125/black-heart-suit_2665.png'
if card[0] == 'S':
return 'https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/60/apple/126/black-spade-suit_2660.png'
def trick_text(game, next=True):
text = 'Declarer: {}\n'.format(game.declarer.name)
text += 'Bid: {}\n'.format(translate_bid(game.bid))
text += 'Partner: {}\n\n'.format(translate_card(game.partnerCard))
for i in range(len(game.players)):
text += '{} ({}): {}\n'.format(
game.players[i].name,
game.players[i].tricks,
translate_card(game.currentTrick[i])
)
if next:
player = game.activePlayer
text += '\n[{}](tg://user?id={}), '.format(player.name, player.id)
text += 'your turn to play a card!'
return text
def start_game(update, context):
chatId = update.effective_chat.id
update_join_message(chatId, buttons=False)
delayQueues[chatId](
context.bot.send_message,
chat_id=chatId,
text='Game starts now!'
)
game = Game.games[chatId]
game.start()
for player in game.players:
if not player.isAI:
player.handMessage = context.bot.send_message(
chat_id=player.id,
text='Your Hand:\n'+translate_hand(player.hand)
)
request_bid(chatId, context)
def request_bid(chatId, context):
if chatId not in Game.games: # everyone passed, game stopped
delayQueues[chatId](
context.bot.send_message,
chat_id=chatId,
text='Everyone passed! Game ended.'
)
return
game = Game.games[chatId]
player = game.activePlayer
if game.phase == Game.CALL_PHASE:
request_partner(chatId, context)
return
if player.isAI:
bid = player.make_bid()
delayQueues[chatId](
context.bot.send_message,
chat_id=chatId,
text='{}: {}'.format(player.name, translate_bid(bid))
)
request_bid(chatId, context)
return
text = 'Current Bid: {}\n'.format(translate_bid(game.bid))
if not game.declarer:
text += 'Bidder: {}\n'.format(None)
else:
text += 'Bidder: {}\n'.format(game.declarer.name)
text += '[{}](tg://user?id={}), '.format(player.name, player.id)
text += 'your turn to bid!'
delayQueues[chatId](
context.bot.send_message,
chat_id=chatId,
text=text,
parse_mode=ParseMode.MARKDOWN
)
def request_partner(chatId, context):
game = Game.games[chatId]
player = game.activePlayer
if player.isAI:
player.call_partner()
request_card(chatId, context)
return
delayQueues[chatId](
context.bot.send_message,
chat_id=chatId,
text='[{}](tg://user?id={}), '.format(player.name, player.id) +
'you won the bid! Choose your partner\'s card!',
parse_mode=ParseMode.MARKDOWN
)
def request_card(chatId, context):
game = Game.games[chatId]
if game.phase == Game.END_PHASE:
conclude_game(chatId, context)
return
player = game.activePlayer
if player is game.players[0]:
delayQueues[chatId](
context.bot.send_message,
chat_id=chatId,
text=trick_text(game),
parse_mode=ParseMode.MARKDOWN
)
if player.isAI:
card = player.play_card()
if card:
delayQueues[chatId](
context.bot.send_message,
chat_id=chatId,
text=trick_text(game, next=player is not game.players[-1]),
parse_mode=ParseMode.MARKDOWN
)
if player is game.players[-1]:
game.complete_trick()
request_card(chatId, context)
def conclude_game(chatId, context):
game = Game.games[chatId]
text = 'Congratulations!\n'
for winner in game.winners:
text += '[{}](tg://user?id={})\n'.format(winner.name, winner.id)
text += 'You won the game!'
delayQueues[chatId](
context.bot.send_message,
chat_id=chatId,
text=text,
parse_mode=ParseMode.MARKDOWN
)
for player in game.players:
if player.handMessage:
delayQueues[chatId](player.handMessage.edit_text, 'Game ended.')
game.stop()
def inline_action(update, context):
inlineQuery = update.inline_query
query = inlineQuery.query
if not query:
return
user = inlineQuery.from_user
if user.id not in Player.players:
return
player = Player.players[user.id]
game = player.game
if player is not game.activePlayer:
return
results = []
if game.phase == Game.BID_PHASE:
for bid in game.valid_bids():
results.append(InlineQueryResultArticle(
id=bid,
title=translate_bid(bid),
input_message_content=InputTextMessageContent(
translate_bid(bid)),
thumb_url=thumb_url_bid(bid)
))
elif game.phase == Game.CALL_PHASE:
# max 50 queryresults but 52 cards -> don't allow self-calling
for card in Game.deck:
if card not in player.hand:
results.append(InlineQueryResultArticle(
id=card,
title=translate_card(card),
input_message_content=InputTextMessageContent(
translate_card(card)
),
thumb_url=thumb_url_card(card)
))
elif game.phase == Game.PLAY_PHASE:
for card in player.valid_cards():
results.append(InlineQueryResultArticle(
id=card,
title=translate_card(card),
input_message_content=InputTextMessageContent(
translate_card(card)
),
thumb_url=thumb_url_card(card)
))
context.bot.answer_inline_query(
inlineQuery.id,
results,
cache_time=2,
is_personal=True
)
def action(update, context):
result = update.chosen_inline_result
playerId = result.from_user.id
player = Player.players[playerId]
game = player.game
chatId = player.game.id
if game.phase == Game.BID_PHASE:
bid = result.result_id
if not player.make_bid(bid):
delayQueues[chatId](
context.bot.send_message,
chat_id=chatId,
text='Not your turn or invalid bid!'
)
request_bid(chatId, context)
elif game.phase == Game.CALL_PHASE:
card = result.result_id
if not player.call_partner(card):
delayQueues[chatId](
context.bot.send_message,
chat_id=chatId,
text='Not your turn or invalid card!'
)
request_partner(chatId, context)
return
# start playing cards
request_card(chatId, context)
elif game.phase == Game.PLAY_PHASE:
card = result.result_id
if not player.play_card(card):
delayQueues[chatId](
context.bot.send_message,
chat_id=chatId,
text='Not your turn or invalid card!'
)
else:
delayQueues[chatId](
player.handMessage.edit_text,
translate_hand(player.hand)
)
delayQueues[chatId](
context.bot.send_message,
chat_id=chatId,
text=trick_text(game, next=player is not game.players[-1]),
parse_mode=ParseMode.MARKDOWN
)
if player is game.players[-1]:
game.complete_trick()
request_card(chatId, context)
def error(update, context):
logger.warning('\nUpdate "%s" caused error "%s"', update, context.error)
if __name__ == '__main__':
token = os.environ['TELEGRAM_TOKEN']
updater = Updater(token=token)
updater.dispatcher.add_handler(CommandHandler('start', start))
updater.dispatcher.add_handler(CommandHandler('stop', stop))
updater.dispatcher.add_handler(CommandHandler('help', help))
updater.dispatcher.add_handler(CallbackQueryHandler(button))
updater.dispatcher.add_handler(InlineQueryHandler(inline_action))
updater.dispatcher.add_error_handler(error)
updater.dispatcher.add_handler(ChosenInlineResultHandler(action))
updater.start_polling()
updater.idle()