handle empty decks on player join

This commit is contained in:
Jannes Höke 2016-05-20 18:34:27 +02:00
parent 0dcd1f6cdc
commit aee310ec9c
2 changed files with 26 additions and 4 deletions

17
bot.py
View file

@ -30,6 +30,9 @@ from telegram.ext import Updater, InlineQueryHandler, \
from telegram.ext.dispatcher import run_async from telegram.ext.dispatcher import run_async
from telegram.utils.botan import Botan from telegram.utils.botan import Botan
from flufl.i18n import registry
from flufl.i18n import PackageStrategy
from game_manager import GameManager from game_manager import GameManager
from credentials import TOKEN, BOTAN_TOKEN from credentials import TOKEN, BOTAN_TOKEN
from start_bot import start_bot from start_bot import start_bot
@ -41,6 +44,7 @@ import card as c
from errors import (NoGameInChatError, LobbyClosedError, AlreadyJoinedError, from errors import (NoGameInChatError, LobbyClosedError, AlreadyJoinedError,
NotEnoughPlayersError, DeckEmptyError) NotEnoughPlayersError, DeckEmptyError)
from database import db_session from database import db_session
import i18n
TIMEOUT = 2.5 TIMEOUT = 2.5
@ -49,6 +53,10 @@ logging.basicConfig(
level=logging.DEBUG) level=logging.DEBUG)
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
strategy = PackageStrategy('uno', i18n)
application = registry.register(strategy)
_ = application._
gm = GameManager() gm = GameManager()
u = Updater(token=TOKEN, workers=32) u = Updater(token=TOKEN, workers=32)
dp = u.dispatcher dp = u.dispatcher
@ -162,6 +170,13 @@ def join_game(bot, update):
text="You already joined the game. Start the game " text="You already joined the game. Start the game "
"with /start", "with /start",
reply_to_message_id=update.message.message_id) 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: else:
send_async(bot, chat.id, send_async(bot, chat.id,
text="Joined the game", text="Joined the game",
@ -439,7 +454,7 @@ def skip_player(bot, update):
def help(bot, update): def help(bot, update):
"""Handler for the /help command""" """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) parse_mode=ParseMode.HTML, disable_web_page_preview=True)

View file

@ -22,6 +22,7 @@ import logging
from datetime import datetime from datetime import datetime
import card as c import card as c
from errors import DeckEmptyError
class Player(object): class Player(object):
@ -38,6 +39,15 @@ class Player(object):
self.user = user self.user = user
self.logger = logging.getLogger(__name__) 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. # Check if this player is the first player in this game.
if game.current_player: if game.current_player:
self.next = game.current_player self.next = game.current_player
@ -49,9 +59,6 @@ class Player(object):
self._prev = self self._prev = self
game.current_player = self game.current_player = self
for i in range(7):
self.cards.append(self.game.deck.draw())
self.bluffing = False self.bluffing = False
self.drew = False self.drew = False
self.anti_cheat = 0 self.anti_cheat = 0