From aee310ec9ca8ea841fe1e6799ee58364d9fd3de1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannes=20H=C3=B6ke?= Date: Fri, 20 May 2016 18:34:27 +0200 Subject: [PATCH] handle empty decks on player join --- bot.py | 17 ++++++++++++++++- player.py | 13 ++++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/bot.py b/bot.py index ea4f180..b1dd093 100644 --- a/bot.py +++ b/bot.py @@ -30,6 +30,9 @@ from telegram.ext import Updater, InlineQueryHandler, \ from telegram.ext.dispatcher import run_async from telegram.utils.botan import Botan +from flufl.i18n import registry +from flufl.i18n import PackageStrategy + from game_manager import GameManager from credentials import TOKEN, BOTAN_TOKEN from start_bot import start_bot @@ -41,6 +44,7 @@ import card as c from errors import (NoGameInChatError, LobbyClosedError, AlreadyJoinedError, NotEnoughPlayersError, DeckEmptyError) from database import db_session +import i18n TIMEOUT = 2.5 @@ -49,6 +53,10 @@ logging.basicConfig( level=logging.DEBUG) logger = logging.getLogger(__name__) +strategy = PackageStrategy('uno', i18n) +application = registry.register(strategy) +_ = application._ + gm = GameManager() u = Updater(token=TOKEN, workers=32) dp = u.dispatcher @@ -162,6 +170,13 @@ def join_game(bot, update): text="You already joined the game. Start the game " "with /start", reply_to_message_id=update.message.message_id) + + except DeckEmptyError: + send_async(bot, chat.id, + text="There are not enough cards left in the deck for new " + "players to join.", + reply_to_message_id=update.message.message_id) + else: send_async(bot, chat.id, text="Joined the game", @@ -439,7 +454,7 @@ def skip_player(bot, update): def help(bot, update): """Handler for the /help command""" - send_async(bot, update.message.chat_id, text=help_text, + send_async(bot, update.message.chat_id, text=_(help_text), parse_mode=ParseMode.HTML, disable_web_page_preview=True) diff --git a/player.py b/player.py index a9b37bf..904ce76 100644 --- a/player.py +++ b/player.py @@ -22,6 +22,7 @@ import logging from datetime import datetime import card as c +from errors import DeckEmptyError class Player(object): @@ -38,6 +39,15 @@ class Player(object): self.user = user self.logger = logging.getLogger(__name__) + try: + for i in range(7): + self.cards.append(self.game.deck.draw()) + except DeckEmptyError: + for card in self.cards: + self.game.deck.dismiss(card) + + raise + # Check if this player is the first player in this game. if game.current_player: self.next = game.current_player @@ -49,9 +59,6 @@ class Player(object): self._prev = self game.current_player = self - for i in range(7): - self.cards.append(self.game.deck.draw()) - self.bluffing = False self.drew = False self.anti_cheat = 0