def new_game(bot, update): """Handler for the /new command""" chat_id = update.message.chat_id if update.message.chat.type == 'private': help(bot, update) else: if update.message.chat_id in gm.remind_dict: for user in gm.remind_dict[update.message.chat_id]: send_async( bot, user, text=_("A new game has been started in {title}").format( title=update.message.chat.title)) del gm.remind_dict[update.message.chat_id] game = gm.new_game(update.message.chat) game.owner = update.message.from_user send_async(bot, chat_id, text=_("Created a new game! Join the game with /join " "and start the game with /start")) if botan: botan.track(update.message, 'New games')
def kill_game(bot, update): """Handler for the /kill command""" chat = update.message.chat user = update.message.from_user games = gm.chatid_games.get(chat.id) if update.message.chat.type == 'private': help(bot, update) return if not games: send_async(bot, chat.id, text=_("There is no running game in this chat.")) return game = games[-1] if user.id in game.owner or user.id in get_admin_ids(bot, chat.id): try: gm.end_game(chat, user) send_async(bot, chat.id, text=__("Game ended!", multi=game.translate)) except NoGameInChatError: send_async(bot, chat.id, text=_("The game is not started yet. " "Join the game with /join and start the game with /start"), reply_to_message_id=update.message.message_id) else: send_async(bot, chat.id, text=_("Only the game creator ({name}) and admin can do that.") .format(name=game.starter.first_name), reply_to_message_id=update.message.message_id)
def new_game(bot, update): """Handler for the /new command""" chat_id = update.message.chat_id if update.message.chat.type == 'private': help(bot, update) else: game = gm.new_game(update.message.chat) game.owner = update.message.from_user send_async(bot, chat_id, text=_("Created a new game! Join the game with /join " "and start the game with /start")) if botan: botan.track(update.message, 'New games')
def add_ai(bot, update): """ Handler for the /add_ai command """ chat_id = update.message.chat_id if update.message.chat.type == 'private': help(bot, update) else: try: game = gm.chatid_games[chat_id][-1] if not game.open: send_async(bot, chat_id, text="The lobby is closed") return else: Player(game, None, ai=True) send_async(bot, chat_id, text="Added computer player", reply_to_message_id=update.message.message_id) except (KeyError, IndexError): send_async(bot, chat_id, text="No game is running at the moment. " "Create a new game with /new", reply_to_message_id=update.message.message_id)
def join_game(bot, update): """Handler for the /join command""" chat = update.message.chat if update.message.chat.type == 'private': help(bot, update) return try: gm.join_game(update.message.from_user, chat) except LobbyClosedError: send_async(bot, chat.id, text=_("The lobby is closed")) except NoGameInChatError: send_async(bot, chat.id, text=_("No game is running at the moment. " "Create a new game with /new"), reply_to_message_id=update.message.message_id) except AlreadyJoinedError: send_async(bot, chat.id, 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"), reply_to_message_id=update.message.message_id)
def start_game(bot, update, args): """Handler for the /start command""" if update.message.chat.type != 'private': chat = update.message.chat try: game = gm.chatid_games[chat.id][-1] except (KeyError, IndexError): send_async(bot, chat.id, text=_("There is no game running in this chat. Create " "a new one with /new")) return if game.started: send_async(bot, chat.id, text=_("The game has already started")) elif len(game.players) < 2: send_async(bot, chat.id, text=_("At least two players must /join the game " "before you can start it")) else: game.play_card(game.last_card) game.started = True first_message = (__( "First player: {name}\n" "Use /close to stop people from joining the game.\n" "Enable multi-translations with /enable_translations", multi=game.translate).format( name=display_name(game.current_player.user))) @run_async def send_first(): """Send the first card and player""" bot.sendSticker(chat.id, sticker=c.STICKERS[str(game.last_card)], timeout=TIMEOUT) bot.sendMessage(chat.id, text=first_message, timeout=TIMEOUT) send_first() elif len(args) and args[0] == 'select': players = gm.userid_players[update.message.from_user.id] groups = list() for player in players: title = player.game.chat.title if player is gm.userid_current[update.message.from_user.id]: title = '- %s -' % player.game.chat.title groups.append([ InlineKeyboardButton(text=title, callback_data=str(player.game.chat.id)) ]) send_async(bot, update.message.chat_id, text=_('Please select the group you want to play in.'), reply_markup=InlineKeyboardMarkup(groups)) else: help(bot, update)