mau_mau_bot/bot.py

125 lines
3.7 KiB
Python
Raw Normal View History

2016-02-29 08:53:59 +08:00
import logging
2016-02-29 06:57:24 +08:00
2016-02-29 08:53:59 +08:00
from telegram import Updater, InlineQueryResultPhoto, InlineQueryResultArticle
2016-02-29 06:57:24 +08:00
from game_manager import GameManager
import card as c
from credentials import TOKEN
2016-02-29 08:53:59 +08:00
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.DEBUG)
logger = logging.getLogger(__name__)
2016-02-29 06:57:24 +08:00
gm = GameManager()
u = Updater(TOKEN)
dp = u.dispatcher
2016-02-29 08:53:59 +08:00
def list_subtract(list1, list2):
list1 = list1.copy()
for x in list2:
list1.remove(x)
return list1
2016-02-29 06:57:24 +08:00
def new_game(bot, update):
chat_id = update.message.chat_id
link = gm.generate_invite_link(u.bot.getMe().username, chat_id)
bot.sendMessage(chat_id,
text="Click this link to join the game: %s" % link)
def start(bot, update, args):
if args:
gm.join_game(args[0], update.message.from_user)
else:
bot.sendMessage(update.message.chat_id,
text="Please invite me to a group and "
2016-02-29 07:00:32 +08:00
"issue the /new command there.")
2016-02-29 06:57:24 +08:00
def inline(bot, update):
if update.inline_query:
user_id = update.inline_query.from_user.id
player = gm.userid_player[user_id]
2016-02-29 08:53:59 +08:00
results = list()
playable = player.playable_cards()
if playable:
for card in playable:
results.append(
InlineQueryResultPhoto(str(card),
card.get_image_link(),
card.get_thumb_link(),
title="Play card",
description="<Card type goes here>")
)
results.append(
InlineQueryResultPhoto(
"hand",
c.IMAGE_PATTERN % 'thinking',
c.THUMB_PATTERN % 'thinking',
title="Other cards:",
description=', '.join([repr(card) for card in
list_subtract(player.cards, playable)]),
)
)
else:
results.append(
InlineQueryResultArticle(
"draw",
title="No suitable cards...",
description="Draw!",
message_text='Drawing %d card(s)' % player.game.draw_counter
)
)
results.append(
InlineQueryResultArticle(
"hand",
title="Other cards:",
description=', '.join([repr(card) for card in
list_subtract(player.cards, playable)]),
message_text='Just checking cards'
)
2016-02-29 06:57:24 +08:00
)
2016-02-29 08:53:59 +08:00
[logger.info(str(result)) for result in results]
bot.answerInlineQuery(update.inline_query.id, results, cache_time=0)
2016-02-29 06:57:24 +08:00
else:
user_id = update.chosen_inline_result.from_user.id
game = gm.userid_game[user_id]
2016-02-29 08:53:59 +08:00
player = gm.userid_player[user_id]
result_id = update.chosen_inline_result.result_id
logger.info("Selected result: " + result_id)
if result_id is 'hand':
pass
elif result_id is 'draw':
for n in range(game.draw_counter):
player.cards.append(game.deck.draw())
else:
card = c.from_str(result_id)
game.play_card(card)
player.cards.remove(card)
def error(bot, update, error):
logger.exception(error)
2016-02-29 07:00:32 +08:00
dp.addTelegramInlineHandler(inline)
dp.addTelegramCommandHandler('start', start)
dp.addTelegramCommandHandler('new', new_game)
2016-02-29 08:53:59 +08:00
dp.addErrorHandler(error)
2016-02-29 07:00:32 +08:00
u.start_polling()
2016-02-29 08:53:59 +08:00
u.idle()