Exemple #1
0
    def accept_button(self, message: Message, user: User, bot: TeleBot, competitor: Competitor):
        opponent_user: User
        opponent, opponent_user = get_opponent_and_opponent_user(competitor)
        if not opponent or not opponent_user:
            return teardown_challenge(
                competitor,
                message,
                user,
                bot,
                'challenge_confirm_cannot_find_opponent_msg' if not opponent else 'challenge_confirm_cannot_fin_opponents_user_msg'
            )

        opponent.change_status(COMPETITOR_STATUS.CHALLENGE_STARTER)
        competitor.change_status(COMPETITOR_STATUS.CHALLENGE_RECEIVER)
        competitor.latest_challenge_received_at = None

        n = datetime.now(tz=timezone('Europe/Kiev'))
        opponent.challenge_started_at = n
        competitor.challenge_started_at = n

        opponent.challenge_remainder_sent = False
        competitor.challenge_remainder_sent = False

        competitor.challenges_dismissed_in_a_row = 0

        opponent.save()
        competitor.save()

        config = get_config()
        if not smwae_check(
            opponent_user.user_id,
            get_translation_for('challenge_confirm_challenge_accepted_opponent_msg').format(
                f'<a href="tg://user?id={user.user_id}">{competitor.name}</a>',
                config.time_to_play_challenge
            ),
            opponent_user,
            reply_markup=get_menu_keyboard(status=opponent.status),
            parse_mode='html'
        ):
            return teardown_challenge(
                competitor,
                message,
                user,
                bot,
                'error_bot_blocked_by_opponent_challenge_canceled_msg'
            )
        opponent_user.reload()
        opponent_user.dismiss_confirmed = False
        opponent_user.states.append('MenuState')
        if len(opponent_user.states) > STATES_HISTORY_LEN:
            del opponent_user.states[0]
        opponent_user.save()

        user.dismiss_confirmed = False
        user.save()
        bot.send_message(
            user.user_id,
            get_translation_for('challenge_confirm_challenge_accepted_competitor_msg').format(
                f'<a href="tg://user?id={opponent_user.user_id}">{opponent.name}</a>',  # TODO
                config.time_to_play_challenge
            ),
            parse_mode='html'
        )

        LogsSheet.glog(
            get_translation_for('gsheet_log_player_accepted_challenge_from_player').format(competitor.name, opponent.name)
        )

        return RET.GO_TO_STATE, 'MenuState', message, user
Exemple #2
0
def teardown_challenge(competitor: Competitor,
                       message,
                       user: User,
                       bot: TeleBot,
                       cause_key,
                       canceled_by_bot=True,
                       opponent: Competitor = None,
                       opponent_msg_key=None,
                       no_glog=False):
    competitor.in_challenge_with = None
    competitor.change_status(competitor.previous_status)
    competitor.previous_status = None
    competitor.latest_challenge_received_at = None
    competitor.save()

    if opponent:
        opponent_user = User.objects(associated_with=opponent).first()

        opponent.in_challenge_with = None
        opponent.change_status(opponent.previous_status)
        opponent.previous_status = None
        opponent.latest_challenge_received_at = None
        opponent.save()

        if canceled_by_bot and not no_glog:
            LogsSheet.glog(
                get_translation_for('gsheet_log_challenge_canceled').format(
                    competitor.name, opponent.name))

        if opponent_user:

            opponent_user.dismiss_confirmed = False
            opponent_user.states.append('MenuState')
            if len(opponent_user.states) > STATES_HISTORY_LEN:
                del opponent_user.states[0]
            opponent_user.save()

            if opponent_msg_key:
                if canceled_by_bot:
                    bot.send_message(
                        opponent_user.user_id,
                        f'{get_translation_for(opponent_msg_key).format(competitor.name)}.\n{get_translation_for("challenge_confirm_challenge_canceled_by_bot_msg")}',
                        reply_markup=get_menu_keyboard(status=opponent.status),
                        parse_mode='html')
                else:
                    bot.send_message(
                        opponent_user.user_id,
                        f'{get_translation_for(opponent_msg_key).format(competitor.name)}',
                        reply_markup=get_menu_keyboard(status=opponent.status),
                        parse_mode='html')
    else:
        if canceled_by_bot and not no_glog:
            LogsSheet.glog(
                get_translation_for(
                    'gsheet_log_challenge_canceled_no_opponent').format(
                        competitor.name))

    user.dismiss_confirmed = False
    user.save()

    if cause_key is not None:
        if canceled_by_bot:
            bot.send_message(
                user.user_id,
                f'{get_translation_for(cause_key)}.\n{get_translation_for("challenge_confirm_challenge_canceled_by_bot_msg")}'
            )
        else:
            bot.send_message(user.user_id, f'{get_translation_for(cause_key)}')
    return RET.GO_TO_STATE, 'MenuState', message, user
Exemple #3
0
    def dismiss_button(self, message: Message, user: User, bot: TeleBot, competitor: Competitor):
        defeat = False
        if not user.dismiss_confirmed:
            if competitor.previous_status == COMPETITOR_STATUS.PASSIVE:
                bot.send_message(
                    message.chat.id,
                    get_translation_for('challenge_confirm_technical_defeat')
                )
            else:
                bot.send_message(
                    message.chat.id,
                    get_translation_for('challenge_received_dismiss_confirm_msg')
                )
            user.dismiss_confirmed = True
            user.save()
            return RET.OK, None, None, None
        else:
            user.dismiss_confirmed = False
            user.save()
            if competitor.previous_status == COMPETITOR_STATUS.PASSIVE and competitor.challenges_dismissed_in_a_row >= 1:
                defeat = True

        opponent, opponent_user = get_opponent_and_opponent_user(competitor)
        if not opponent or not opponent_user:
            return teardown_challenge(
                competitor,
                message,
                user,
                bot,
                'challenge_confirm_cannot_find_opponent_msg' if not opponent else 'challenge_confirm_cannot_fin_opponents_user_msg'
            )

        opponent.in_challenge_with = None
        opponent.change_status(opponent.previous_status)
        opponent.previous_status = None
        opponent.save()

        if defeat:
            opponent.wins = opponent.wins + 1 if opponent.wins is not None else 1
            opponent.matches = opponent.matches + 1 if opponent.matches is not None else 1
            level_change = None
            if opponent.level > competitor.level:
                level_change = f'{opponent.level}->{competitor.level}'
                c = opponent.level
                opponent.level = competitor.level
                competitor.level = c
                ResultsSheet.upload_canceled_result(opponent, competitor, level_change, was_dismissed=True)
            UsersSheet.update_competitor_table_record(opponent)
            res = Result(
                player_a=opponent,
                player_a_s=opponent.name,
                player_b=competitor,
                player_b_s=competitor.name,
                result=RESULT.DISMISSED,
                canceled=True,
                date=datetime.now(tz=timezone('Europe/Kiev')),
                level_change=level_change
            )
            res.save()

            config = get_config()
            if config.group_chat_id:
                gmsg = get_translation_for('group_chat_technical_win_report_msg').format(opponent.name, competitor.name)
                if level_change:
                    gmsg += '.\n'
                    gmsg += get_translation_for('group_chat_players_level_changed').format(level_change)

                try:
                    bot.send_message(
                        config.group_chat_id,
                        gmsg,
                        parse_mode='html'
                    )
                except:
                    logger.exception('Exception occurred while sending message to group chat')

            result = Result(
                player_a=opponent,
                player_b=competitor,
                result=RESULT.A_WINS
            )
            result.save()

        opponent.save()

        competitor.in_challenge_with = None
        if defeat:
            competitor.change_status(COMPETITOR_STATUS.ACTIVE)
            competitor.challenges_dismissed_in_a_row = 0
            competitor.losses += competitor.losses + 1 if competitor.losses is not None else 1
            competitor.matches += competitor.matches + 1 if competitor.matches is not None else 1
            UsersSheet.update_competitor_table_record(competitor)
        else:
            competitor.change_status(COMPETITOR_STATUS.PASSIVE)
            competitor.challenges_dismissed_in_a_row = 1
        competitor.challenges_dismissed_total = competitor.challenges_dismissed_total + 1 if competitor.challenges_dismissed_total is not None else 1
        competitor.latest_challenge_received_at = None
        competitor.save()

        opponent_user.states.append('MenuState')
        if len(opponent_user.states) > STATES_HISTORY_LEN:
            del opponent_user.states[0]
        opponent_user.save()

        if defeat:
            smwae_check(
                opponent_user.user_id,
                get_translation_for('challenge_confirm_opponent_wins') + ' ' + str(opponent.level),
                opponent_user,
                reply_markup=get_menu_keyboard(status=opponent.status)
            )
            bot.send_message(
                user.user_id,
                get_translation_for('challenge_confirm_competitor_losses') + ' ' + str(competitor.level),
            )
        else:
            smwae_check(
                opponent_user.user_id,
                get_translation_for('challenge_confirmation_dismissed_opponent_msg'),
                opponent_user,
                reply_markup=get_menu_keyboard(status=opponent.status)
            )
            bot.send_message(
                user.user_id,
                get_translation_for('challenge_confirmation_dismissed_competitor_msg'),
            )

        LogsSheet.glog(
            get_translation_for('gsheet_log_player_dismissed_challenge_from_player').format(competitor.name, opponent.name)
        )

        return RET.GO_TO_STATE, 'MenuState', message, user