From 5465fca34f0ae33e82c8718c99a885eaa93922aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannes=20H=C3=B6ke?= Date: Mon, 29 Feb 2016 01:53:59 +0100 Subject: [PATCH] kinda works now --- bot.py | 89 ++++++++++++++++++++++++++++++++++---- card.py | 14 ++++++ deck.py | 7 ++- game.py | 2 +- game_manager.py | 8 +++- images/jpg/thinking.jpg | Bin 0 -> 2413 bytes images/thumb/thinking.jpg | Bin 0 -> 894 bytes player.py | 16 +++++-- 8 files changed, 119 insertions(+), 17 deletions(-) create mode 100644 images/jpg/thinking.jpg create mode 100644 images/thumb/thinking.jpg diff --git a/bot.py b/bot.py index 3f2c422..ce598ff 100644 --- a/bot.py +++ b/bot.py @@ -1,15 +1,30 @@ +import logging -from telegram import Updater, InlineQueryResultPhoto +from telegram import Updater, InlineQueryResultPhoto, InlineQueryResultArticle from game_manager import GameManager import card as c from credentials import TOKEN +logging.basicConfig( + format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', + level=logging.DEBUG) +logger = logging.getLogger(__name__) + gm = GameManager() u = Updater(TOKEN) dp = u.dispatcher +def list_subtract(list1, list2): + list1 = list1.copy() + + for x in list2: + list1.remove(x) + + return list1 + + def new_game(bot, update): chat_id = update.message.chat_id link = gm.generate_invite_link(u.bot.getMe().username, chat_id) @@ -31,23 +46,79 @@ def inline(bot, update): user_id = update.inline_query.from_user.id player = gm.userid_player[user_id] - playable = list() - for card in player.playable_cards(): - playable.append( - InlineQueryResultPhoto(str(card), - card.get_image_link(), - card.get_thumb_link()) + 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="") + ) + + 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 + ) ) - bot.answerInlineQuery(update.inline_query.id, playable) + results.append( + InlineQueryResultArticle( + "hand", + title="Other cards:", + description=', '.join([repr(card) for card in + list_subtract(player.cards, playable)]), + message_text='Just checking cards' + ) + ) + + [logger.info(str(result)) for result in results] + + bot.answerInlineQuery(update.inline_query.id, results, cache_time=0) else: user_id = update.chosen_inline_result.from_user.id game = gm.userid_game[user_id] - game.play_card(c.from_str(update.chosen_inline_result.id)) + 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) + dp.addTelegramInlineHandler(inline) dp.addTelegramCommandHandler('start', start) dp.addTelegramCommandHandler('new', new_game) +dp.addErrorHandler(error) u.start_polling() +u.idle() diff --git a/card.py b/card.py index 4e0f125..1b37ce1 100644 --- a/card.py +++ b/card.py @@ -51,8 +51,22 @@ class Card(object): return '%s_%s' % (self.color, self.value) def __repr__(self): + if self.special: + return self.special + ''' + if self.special is CHOOSE: + return "Colorchooser" + elif self.special is DRAW_FOUR: + return "Draw four" + ''' + else: + return self.color + " - " + self.value + return str(self) + def __eq__(self, other): + return str(self) == str(other) + def get_image_link(self): return IMAGE_PATTERN % str(self) diff --git a/deck.py b/deck.py index 9ae5107..01f4aa2 100644 --- a/deck.py +++ b/deck.py @@ -24,11 +24,14 @@ class Deck(object): self.shuffle() def shuffle(self): - self.cards = shuffle(self.cards) + self.logger.debug("Shuffling Deck") + shuffle(self.cards) def draw(self): try: - return self.cards.pop() + card = self.cards.pop() + self.logger.debug("Drawing card " + str(card)) + return card except IndexError: while len(self.graveyard): self.cards.append(self.graveyard.pop()) diff --git a/game.py b/game.py index 52e813a..d0cf616 100644 --- a/game.py +++ b/game.py @@ -11,7 +11,7 @@ class Game(object): """ current_player = None reversed = False - draw_counter = 0 + draw_counter = 1 choosing_color = False def __init__(self): diff --git a/game_manager.py b/game_manager.py index 819a68e..4dbead7 100644 --- a/game_manager.py +++ b/game_manager.py @@ -1,4 +1,6 @@ from uuid import uuid4 +import logging + from game import Game from player import Player @@ -13,16 +15,20 @@ class GameManager(object): self.chatid_gameid = dict() self.userid_user = dict() self.userid_player = dict() + self.logger = logging.getLogger(__name__) def generate_invite_link(self, bot_name, chat_id): - game_id = uuid4() + game_id = str(uuid4()) game = Game() + + self.logger.info("Creating new game with id " + game_id) self.gameid_game[game_id] = game self.chatid_gameid[chat_id] = game_id return LINK_PATTERN % (bot_name, game_id) def join_game(self, game_id, user): + self.logger.info("Joining game with id " + game_id) game = self.gameid_game[game_id] player = Player(game, user) self.userid_player[user.id] = player diff --git a/images/jpg/thinking.jpg b/images/jpg/thinking.jpg new file mode 100644 index 0000000000000000000000000000000000000000..237edf58abd4c454653dd54ed522ae36f84dfc38 GIT binary patch literal 2413 zcmb7Fc{r5q8h_te42GF^m=V%vEE!vlrAv!FjIoquEG2u6wd{O~Y*`8?W<;c>$Ven% zLi)~^VI)hDed!a)XYG(JX>nf8=`8?mPk_;X5d_02(bo6vhe@|8HzJ0|b6R8}LSg1OPz*kpys?2S@<` z%nWAxZ@{!b1R9BgPl^9^up6>H3J4)V00AK(001A8F(_KTzG#`pEIt81+EwRyuOB=F zMjf!W<4MRQc}L{Hmq5oTfl1d4R7?WAsXRPYmMCD$(J}-8oRPJqi}9<5yQ=2BWI6u+ zs!}U_@1wkg4SfyuSb0wJUYs2bh=+OXhX4?T08t3vl^|DaffiYt@lOAX;6SbNvU;FiMwWvL|M3Xz4T+X>Mb|BnJAIIAM%Zr?!n&ySz{x~@k2ks9v@2$G^f~(y4XfH-)lnWd?c@tZbo_kwGcW`DBy_5tlu2_|FSX&UxM)+_3d;ia zMM}o0q{p1X>$p#;1bjc^_^R4L{6qBnpl3VDA^d07c2x1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