예제 #1
0
def multiple_choice(update: Update,
                    context: CallbackContext) -> Union[str, int]:
    """
    Parses the corresponding response for the question mode.

    Args:
        update: The update.
        context: The context as provided by the :class:`telegram.ext.Dispatcher`.
    """
    context.user_data[CONVERSATION_KEY] = CONVERSATION_VALUE
    message = update.effective_message
    orchestra = context.bot_data[ORCHESTRA_KEY]

    if update.callback_query:
        data = update.callback_query.data
        update.callback_query.answer()

        game_settings = cast(GameSettings, context.user_data[GAME_KEY])
        game_settings.multiple_choice = data == 'True'

        try:
            message.edit_text(
                TEXTS[HINT_ATTRIBUTES],
                reply_markup=build_questions_hints_keyboard(
                    orchestra=orchestra,
                    hint=True,
                    multiple_choice=game_settings.multiple_choice,
                    exclude_members=[
                        orchestra.members[update.effective_user.id]
                    ],
                    current_selection={
                        h: True
                        for h in game_settings.hint_attributes
                    },
                ),
            )
            return HINT_ATTRIBUTES
        except RuntimeError as exc:
            if str(exc
                   ) == 'Orchestra currently has no questionable attributes.':
                message.reply_text(
                    'Es sind leider noch nicht genug AkaBlasen angemeldet, um ein '
                    'Spiel starten zu können. 😕 Bitte versuche es später erneut.'
                )
                message.delete()
                context.user_data[CONVERSATION_KEY] = False
                return ConversationHandler.END

            raise exc
    else:
        msg = message.reply_text(TEXTS[MULTIPLE_CHOICE],
                                 reply_markup=MULTIPLE_CHOICE_KEYBOARD)
        context.user_data[GAME_MESSAGE_KEY] = msg
        if GAME_KEY not in context.user_data:
            context.user_data[GAME_KEY] = GameSettings()
        return MULTIPLE_CHOICE
예제 #2
0
def question_attributes(update: Update, context: CallbackContext) -> str:
    """
    Parses the corresponding response for the question attributes.

    Args:
        update: The update.
        context: The context as provided by the :class:`telegram.ext.Dispatcher`.

    Returns:
        :attr:`QUESTION_ATTRIBUTES`: If the user is not yet done selecting
        :attr:`NUMBER_QUESTIONS`: Else.
    """
    message = update.effective_message
    orchestra = context.bot_data[ORCHESTRA_KEY]
    data = update.callback_query.data
    update.callback_query.answer()
    game_settings = cast(GameSettings, context.user_data[GAME_KEY])
    exclude_members = [orchestra.members[update.effective_user.id]]

    if data == DONE:
        current_selection = parse_questions_hints_keyboard(
            message.reply_markup)
        game_settings.question_attributes = [
            attr for attr, sl in current_selection.items() if sl
        ]

        message.edit_text(text=TEXTS[NUMBER_QUESTIONS],
                          reply_markup=NUMBER_QUESTIONS_KEYBOARD)
        return NUMBER_QUESTIONS

    if data == ALL:
        current_selection = parse_questions_hints_keyboard(
            message.reply_markup)
        current_value = all(current_selection.values())
        for entry in current_selection:
            current_selection[entry] = not current_value

        message.edit_reply_markup(reply_markup=build_questions_hints_keyboard(
            orchestra=orchestra,
            question=True,
            current_selection=current_selection,
            multiple_choice=game_settings.multiple_choice,
            allowed_hints=game_settings.hint_attributes,
            exclude_members=exclude_members,
        ))

        return QUESTION_ATTRIBUTES

    current_selection = parse_questions_hints_keyboard(message.reply_markup)
    attr, selection = data.split(' ')
    current_selection[attr] = not selection == SELECTED

    message.edit_reply_markup(reply_markup=build_questions_hints_keyboard(
        orchestra=orchestra,
        question=True,
        current_selection=current_selection,
        multiple_choice=game_settings.multiple_choice,
        allowed_hints=game_settings.hint_attributes,
        exclude_members=exclude_members,
    ))

    return QUESTION_ATTRIBUTES