Пример #1
0
def process_result(bot, update, job_queue):
    """
    Handler for chosen inline results.
    Checks the players actions and acts accordingly.
    """
    try:
        user = update.chosen_inline_result.from_user
        player = gm.userid_current[user.id]
        game = player.game
        result_id = update.chosen_inline_result.result_id
        chat = game.chat
    except (KeyError, AttributeError):
        return

    logger.debug("Selected result: " + result_id)

    result_id, anti_cheat = result_id.split(':')
    last_anti_cheat = player.anti_cheat
    player.anti_cheat += 1

    if result_id in ('hand', 'gameinfo', 'nogame'):
        return
    elif result_id.startswith('mode_'):
        # First 5 characters are 'mode_', the rest is the gamemode.
        mode = result_id[5:]
        game.set_mode(mode)
        logger.info("Gamemode changed to {mode}".format(mode=mode))
        send_async(bot,
                   chat.id,
                   text=__("Gamemode changed to {mode}".format(mode=mode)))
        return
    elif len(result_id) == 36:  # UUID result
        return
    elif int(anti_cheat) != last_anti_cheat:
        send_async(bot,
                   chat.id,
                   text=__("Cheat attempt by {name}",
                           multi=game.translate).format(
                               name=display_name(player.user)))
        return
    elif result_id == 'call_bluff':
        reset_waiting_time(bot, player)
        do_call_bluff(bot, player)
    elif result_id == 'draw':
        reset_waiting_time(bot, player)
        do_draw(bot, player)
    elif result_id == 'pass':
        game.turn()
    elif result_id in c.COLORS:
        game.choose_color(result_id)
    else:
        reset_waiting_time(bot, player)
        do_play_card(bot, player, result_id)

    if game_is_running(game):
        send_async(bot,
                   chat.id,
                   text=__("Next player: {name}", multi=game.translate).format(
                       name=display_name(game.current_player.user)))
        start_player_countdown(bot, game, job_queue)
Пример #2
0
def play_next(game, bot, chat, job_queue):
    if game_is_running(game):
        nextplayer_message = (__(
            "Next player: {name}", multi=game.translate).format(
                name=display_name(game.current_player.user)))
        choice = [[
            InlineKeyboardButton(text=_("Make your choice!"),
                                 switch_inline_query_current_chat='')
        ]]
        send_async(bot,
                   chat.id,
                   text=nextplayer_message,
                   reply_markup=InlineKeyboardMarkup(choice))
        start_player_countdown(bot, game, job_queue)

        if game.current_player.user.id == bot.id:
            playBot(game.current_player, bot, chat, game, job_queue)
Пример #3
0
def process_auto(bot, game, job_queue):
    start_player = game.current_player

    if not game.choosing_color:
        playable = game.current_player.playable_cards()
        if len(playable) == 0 and game.draw_counter == 0:
            time.sleep(1)
            send_async(bot,
                       game.chat.id,
                       text='Drawing 1 card for {name}'.format(
                           name=display_name(game.current_player.user)))
            do_draw(bot, game.current_player)
            time.sleep(1)
            send_async(bot,
                       game.chat.id,
                       text=__(
                           "Next player: {name}", multi=game.translate).format(
                               name=display_name(game.current_player.user)))
            start_player_countdown(bot, game, job_queue)
            if (start_player != game.current_player):
                process_auto(bot, game, job_queue)
Пример #4
0
def start_game(bot, update, args, job_queue):
    """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) < MIN_PLAYERS:
            send_async(
                bot,
                chat.id,
                text=__(
                    "At least {minplayers} players must /join the game "
                    "before you can start it").format(minplayers=MIN_PLAYERS))

        else:
            # Set winning score
            player_num = len(game.players)
            if player_num == 2:
                game.win_score = SCORE_DUEL
            else:
                game.win_score = ADDITIONAL_POINT * player_num
            # Starting a game
            game.start()

            for player in game.players:
                player.draw_first_hand()

            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()
            start_player_countdown(bot, game, job_queue)

    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_handler(bot, update)