def players_turn(update, context): """Execute a player's turn""" chat = update.effective_chat game = GameStore().get_game(chat.id) player = game.get_current_player() user_mention = html_mention(user_id=player.user_id, first_name=player.first_name) lang_id = Database().get_lang_id(chat.id) translator = Translator(lang_id=lang_id) logger.info("Player's turn: {}".format(player)) player_cards = get_cards_string(player, lang_id) # Check if player already has 21 or a BlackJack before their move. If so, automatically jump to the next player. # We need reply_text here, because we must send a new message (this is the first message for the player)! if player.has_blackjack(): text = (translator("your_cards_are") + "\n\n" + translator("got_blackjack")).format(user_mention, player.cardvalue, player_cards) update.effective_message.reply_text(text=text, parse_mode=ParseMode.HTML, reply_markup=None) next_player(update, context) elif player.cardvalue == 21: text = (translator("your_cards_are") + "\n\n" + translator("got_21")).format(user_mention, player.cardvalue, player_cards) update.effective_message.reply_text(text=text, parse_mode=ParseMode.HTML, reply_markup=None) next_player(update, context) else: text = translator("your_cards_are").format(user_mention, player.cardvalue, player_cards) update.effective_message.reply_text(text=text, parse_mode=ParseMode.HTML, reply_markup=get_game_keyboard(game.id, lang_id))
def error_handler(update, context): try: raise context.error except Unauthorized as e: # remove update.message.chat_id from conversation list logger.error("The update {} raised the following 'Unauthorized' exception: {}".format(update, e)) if e.message == "Forbidden: bot was blocked by the user": GameStore().remove_game(update.effective_chat.id) elif e.message == "Forbidden: bot was kicked from the supergroup chat" or e.message == "Forbidden: bot was kicked from the group chat": GameStore().remove_game(update.effective_chat.id) except BadRequest as e: # handle malformed requests - read more below! logger.error("The update {} raised the following 'BadRequest' exception: {}".format(update, e)) except TimedOut as e: logger.warning("The update {} timed out: {}".format(update, e)) # handle slow connection problems except NetworkError as e: # handle other connection problems logger.warning("The update {} raised the following 'NetworkError' exception: {}".format(update, e)) except ChatMigrated as e: # the chat_id of a group has changed, use e.new_chat_id instead logger.warning("The update {} raised the following 'ChatMigrated' exception: {}".format(update, e)) except TelegramError as e: # handle all other telegram related errors logger.error("The update {} caused the following TelegramError: {}".format(update, e))
def kill_game_cmd(update, context): """Kills the game for a certain chat/group""" if len(context.args) == 0: update.message.reply_text("Please provide a chat_id!") chat_id = context.args[0] # Input validation for chat_id if not re.match(r"^-?[0-9]+$", chat_id): update.message.reply_text("Sorry, the chat_id is invalid!") return chat_id = int(chat_id) try: _ = GameStore().get_game(chat_id=chat_id) except NoActiveGameException: update.message.reply_text( "Sorry, there is no running game in a chat with that ID!") return logger.info("Admin '{0}' removed game in chat '{1}'".format( update.effective_user.id, chat_id)) GameStore().remove_game(chat_id=chat_id) update.message.reply_text( "Alright, I killed the running game in '{0}'!".format(chat_id)) context.bot.send_message( chat_id=chat_id, text="The creator of this bot stopped your current game of BlackJack.")
def join_callback(update, context): """ CallbackQueryHandler callback for the 'join' inline button. Adds the executing player to the game of the specific chat """ user = update.effective_user chat = update.effective_chat lang_id = Database().get_lang_id(chat.id) translator = Translator(lang_id=lang_id) game = GameStore().get_game(chat.id) if not is_button_affiliated(update, context, game, lang_id): return try: game.add_player(user.id, user.first_name) update.effective_message.edit_text(text=translator("mp_request_join").format(game.get_player_list()), reply_markup=get_join_keyboard(game.id, lang_id)) update.callback_query.answer(translator("mp_join_callback").format(user.first_name)) # If players are full, replace join keyboard with start keyboard if len(game.players) >= game.MAX_PLAYERS: update.effective_message.edit_reply_markup(reply_markup=get_start_keyboard(lang_id)) except errors.GameAlreadyRunningException: remove_inline_keyboard(update, context) update.callback_query.answer(translator("mp_game_already_begun_callback")) except errors.MaxPlayersReachedException: update.effective_message.edit_reply_markup(reply_markup=get_start_keyboard(lang_id)) update.callback_query.answer(translator("mp_max_players_callback")) except errors.PlayerAlreadyExistingException: update.callback_query.answer(translator("mp_already_joined_callback"))
def stop_cmd(update, context): """Stops a game for a specific user""" user = update.effective_user chat = update.effective_chat lang_id = Database().get_lang_id(chat.id) translator = Translator(lang_id=lang_id) game = GameStore().get_game(chat.id) try: game.stop(user.id) update.effective_message.reply_text(translator("game_ended")) except errors.InsufficientPermissionsException: update.effective_message.reply_text( translator("mp_only_creator_can_end"))
def create_game(update, context): """Create a new game instance for the chat of the user""" user = update.effective_user chat = update.effective_chat lang_id = Database().get_lang_id(chat.id) translator = Translator(lang_id=lang_id) # Create either a singleplayer or multiplayer game if chat.type == "private": game_type = BlackJackGame.Type.SINGLEPLAYER elif chat.type == "group" or chat.type == "supergroup": game_type = BlackJackGame.Type.MULTIPLAYER_GROUP else: logger.error("Chat type '{}' not supported!".format(chat.type)) return game = BlackJackGame(gametype=game_type) game.add_player(user_id=user.id, first_name=user.first_name) GameStore().add_game(chat.id, game) # TODO currently the game starts instantly - this should change with multiplayer rooms if game.type == BlackJackGame.Type.SINGLEPLAYER: update.effective_message.reply_text(translator("game_starts_now").format("", get_cards_string(game.dealer, lang_id))) players_turn(update, context) else: text = translator("mp_request_join").format(game.get_player_list()) update.effective_message.reply_text(text=text, reply_markup=get_join_keyboard(game.id, lang_id))
def start_callback(update, context): """Starts a game that has been created already""" user = update.effective_user chat = update.effective_chat lang_id = Database().get_lang_id(chat.id) translator = Translator(lang_id=lang_id) try: game = GameStore().get_game(update.effective_chat.id) if not is_button_affiliated(update, context, game, lang_id): return except NoActiveGameException: update.callback_query.answer(translator("mp_no_created_game_callback")) remove_inline_keyboard(update, context) return try: game.start(user.id) update.callback_query.answer(translator("mp_starting_game_callback")) except errors.GameAlreadyRunningException: update.callback_query.answer( translator("mp_game_already_begun_callback")) return except errors.NotEnoughPlayersException: update.callback_query.answer( translator("mp_not_enough_players_callback")) return except errors.InsufficientPermissionsException: update.callback_query.answer( translator("mp_only_creator_start_callback").format( user.first_name)) return if game.type != BlackJackGame.Type.SINGLEPLAYER: players_are = translator("mp_players_are") for player in game.players: players_are += "👤{}\n".format(player.first_name) players_are += "\n" else: players_are = "" update.effective_message.edit_text( translator("game_starts_now").format( players_are, get_cards_string(game.dealer, lang_id))) players_turn(update, context)
def next_player(update, context): chat = update.effective_chat user = update.effective_user lang_id = Database().get_lang_id(chat.id) translator = Translator(lang_id=lang_id) game = GameStore().get_game(chat.id) try: if user.id != game.get_current_player().user_id: update.callback_query.answer(translator("mp_not_your_turn_callback").format(user.first_name)) return remove_inline_keyboard(update, context) game.next_player() except NoPlayersLeftException: # TODO merge messages update.effective_message.reply_text(translator("dealers_cards_are").format(game.dealer.cardvalue, get_cards_string(game.dealer, lang_id)), parse_mode=ParseMode.HTML) evaluation_string = generate_evaluation_string(game, lang_id) newgame_button = InlineKeyboardButton(text=translator("inline_keyboard_newgame"), callback_data="newgame") keyboard = InlineKeyboardMarkup(inline_keyboard=[[newgame_button]]) update.effective_message.reply_text(evaluation_string, reply_markup=keyboard) game.stop(-1) return players_turn(update, context)
def hit_callback(update, context): """ CallbackQueryHandler callback for the 'hit' inline button. Draws a card for you. """ user = update.effective_user chat = update.effective_chat lang_id = Database().get_lang_id(chat.id) translator = Translator(lang_id=lang_id) game = GameStore().get_game(chat.id) if not is_button_affiliated(update, context, game, lang_id): return player = game.get_current_player() user_mention = html_mention(user_id=player.user_id, first_name=player.first_name) try: if user.id != player.user_id: update.callback_query.answer(translator("mp_not_your_turn_callback").format(user.first_name)) return game.draw_card() player_cards = get_cards_string(player, lang_id) text = translator("your_cards_are").format(user_mention, player.cardvalue, player_cards) update.effective_message.edit_text(text=text, parse_mode=ParseMode.HTML, reply_markup=get_game_keyboard(game.id, lang_id)) except errors.PlayerBustedException: player_cards = get_cards_string(player, lang_id) text = (translator("your_cards_are") + "\n\n" + translator("you_busted")).format(user_mention, player.cardvalue, player_cards) update.effective_message.edit_text(text=text, parse_mode=ParseMode.HTML, reply_markup=None) next_player(update, context) except errors.PlayerGot21Exception: player_cards = get_cards_string(player, lang_id) if player.has_blackjack(): text = (translator("your_cards_are") + "\n\n" + translator("got_blackjack")).format(user_mention, player.cardvalue, player_cards) update.effective_message.edit_text(text=text, parse_mode=ParseMode.HTML, reply_markup=None) next_player(update, context) elif player.cardvalue == 21: text = (translator("your_cards_are") + "\n\n" + translator("got_21")).format(user_mention, player.cardvalue, player_cards) update.effective_message.edit_text(text=text, parse_mode=ParseMode.HTML, reply_markup=None) next_player(update, context)
def stand_callback(update, context): """ CallbackQueryHandler callback for the 'stand' inline button. Prepares round for the next player. """ chat = update.effective_chat lang_id = Database().get_lang_id(chat.id) game = GameStore().get_game(update.effective_chat.id) if not is_button_affiliated(update, context, game, lang_id): return next_player(update, context)
def stop_cmd(update, context): """Stops a game for a specific user""" user = update.effective_user chat = update.effective_chat lang_id = Database().get_lang_id(chat.id) translator = Translator(lang_id=lang_id) game = GameStore().get_game(chat.id) user_id = user.id try: if chat.type == "group" or chat.type == "supergroup": # If yes, get the chat admins admins = context.bot.get_chat_administrators(chat_id=chat.id) # if user.id in chat admin IDs, let them end the game with admin powers if user.id in [x.user.id for x in admins]: user_id = -1 game.stop(user_id) update.effective_message.reply_text(translator("game_ended")) except errors.InsufficientPermissionsException: update.effective_message.reply_text(translator("mp_only_creator_can_end"))
def wrapper(update, context, *args, **kwargs): chat = update.effective_chat lang_id = Database().get_lang_id(chat.id) translator = Translator(lang_id=lang_id) try: game = GameStore().get_game(chat.id) except NoActiveGameException: remove_inline_keyboard(update, context) update.effective_message.reply_text( translator("mp_no_created_game_callback")) return return func(update, context)
def start_cmd(update, context): """Handles messages contianing the /start command. Starts a game for a specific user""" user = update.effective_user chat = update.effective_chat lang_id = Database().get_lang_id(chat.id) translator = Translator(lang_id=lang_id) Database().add_user(user.id, user.language_code, user.first_name, user.last_name, user.username) try: GameStore().get_game(update.effective_chat.id) # TODO notify user that there is a running game already? except NoActiveGameException: # If there is no game, we create one create_game(update, context)