Exemple #1
0
def round_inline_handle(_bot: telegram.Bot, job_queue: JobQueue,
                        query: telegram.CallbackQuery, gm: bool,
                        game_round: Round):
    language_code = get_language(query.from_user)

    def _(x):
        return get(x, language_code)

    method = str(query.data)
    actors = game_round.get_actors()
    if method == 'round:next':
        next_count = game_round.counter + 1
        if next_count >= len(actors):
            next_count = 0
            game_round.round_counter += 1
        game_round.counter = next_count
        game_round.save()
        answer_callback_query(job_queue, query.id)
        update_round_message(job_queue, game_round, language_code)
    elif method == 'round:prev':
        prev_count = game_round.counter - 1
        if prev_count < 0:
            if game_round.round_counter <= 1:
                answer_callback_query(job_queue, query.id,
                                      _(Text.ALREADY_FIRST_TURN))
                return
            else:
                prev_count = len(actors) - 1
                game_round.round_counter -= 1
        game_round.counter = prev_count
        answer_callback_query(job_queue, query.id)
        update_round_message(job_queue,
                             game_round,
                             language_code,
                             refresh=True)
        game_round.save()
    elif method == 'round:remove':
        if not gm:
            raise NotGm()

        actors = game_round.get_actors()
        if len(actors) > 1:
            current = actors[game_round.counter % len(actors)]
            current.delete()
            answer_callback_query(job_queue, query.id)
            update_round_message(job_queue,
                                 game_round,
                                 language_code,
                                 refresh=True)
        else:
            answer_callback_query(query.id,
                                  _(Text.AT_LEAST_ONE_ACTOR),
                                  show_alert=True)
    elif method == 'round:finish':
        if not gm:
            raise NotGm()
        message: telegram.Message = query.message
        edit_message(job_queue, message.chat_id, message.message_id,
                     _(Text.ROUND_ALREADY_FINISHED))
        remove_round(job_queue, game_round.chat_id)
Exemple #2
0
def refresh_round_message(bot: telegram.Bot,
                          game_round: Round,
                          get_text,
                          refresh=False):
    reply_markup = InlineKeyboardMarkup([
        [
            InlineKeyboardButton(get_text(Text.ROUND_REMOVE),
                                 callback_data='round:remove'),
            InlineKeyboardButton(get_text(Text.ROUND_FINISH),
                                 callback_data='round:finish'),
            InlineKeyboardButton("←", callback_data='round:prev'),
            InlineKeyboardButton("→", callback_data='round:next'),
        ],
    ])

    actors = game_round.get_actors()
    if not actors:
        return
    game_round.counter = game_round.counter % len(actors)
    counter = game_round.counter
    state = ''
    if game_round.hide:
        state = '[{}]'.format(get_text(Text.HIDED_ROUND_LIST))
    round_counter = get_text(
        Text.ROUND_COUNTER).format(round_number=game_round.round_counter)
    text = '<b>{}</b> {state} #round\n\n{round_number}   [{counter}/{total}]\n\n'.format(
        get_text(Text.ROUND_INDICATOR),
        state=state,
        round_number=round_counter,
        counter=counter + 1,
        total=len(actors),
    )
    for index, actor in enumerate(actors):
        is_current = counter == index
        if is_current:
            text += '• {} ({}) ← {}\n'.format(actor.name, actor.value,
                                              get_text(Text.CURRENT))
        elif not game_round.hide:
            text += '◦ {} ({})\n'.format(actor.name, actor.value)

    if refresh:
        try:
            bot.edit_message_text(
                text,
                chat_id=game_round.chat_id,
                message_id=game_round.message_id,
                parse_mode='HTML',
                reply_markup=reply_markup,
            )
        except TelegramError:
            pass
    else:
        bot.delete_message(game_round.chat_id, game_round.message_id)
        message = bot.send_message(game_round.chat_id,
                                   text,
                                   parse_mode='HTML',
                                   reply_markup=reply_markup)
        game_round.message_id = message.message_id
        game_round.save()
Exemple #3
0
def round_inline_handle(bot: telegram.Bot, query: telegram.CallbackQuery,
                        gm: bool, game_round: Round):
    _ = partial(get_by_user, user=query.from_user)

    method = str(query.data)
    actors = game_round.get_actors()
    if method == 'round:next':
        next_count = game_round.counter + 1
        if next_count >= len(actors):
            next_count = 0
            game_round.round_counter += 1
        game_round.counter = next_count
        game_round.save()
        query.answer()
        refresh_round_message(bot, game_round, _)
    elif method == 'round:prev':
        prev_count = game_round.counter - 1
        if prev_count < 0:
            if game_round.round_counter <= 1:
                query.answer(text=_(Text.ALREADY_FIRST_TURN))
                return
            else:
                prev_count = len(actors) - 1
                game_round.round_counter -= 1
        game_round.counter = prev_count
        query.answer()
        refresh_round_message(bot, game_round, _, refresh=True)
        game_round.save()
    elif method == 'round:remove':
        if not gm:
            raise NotGm()

        actors = game_round.get_actors()
        if len(actors) > 1:
            current = actors[game_round.counter % len(actors)]
            current.delete()
            query.answer()
            refresh_round_message(bot, game_round, _, refresh=True)
        else:
            query.answer(show_alert=True, text=_(Text.AT_LEAST_ONE_ACTOR))
    elif method == 'round:finish':
        if not gm:
            raise NotGm()
        query.edit_message_text(_(Text.ROUND_ALREADY_FINISHED))
        remove_round(bot, game_round.chat_id)