コード例 #1
0
def report(form: ReportForm) -> bool:
    try:
        db().get_lock('deck_id:{id}'.format(id=form.entry))
        db().get_lock('deck_id:{id}'.format(id=form.opponent))

        for m in match.get_matches(form):
            if int(form.opponent) == m.opponent_deck_id:
                form.errors[
                    'result'] = 'This match was reported as You {game_wins}–{game_losses} {opponent} {date}'.format(
                        game_wins=m.game_wins,
                        game_losses=m.game_losses,
                        opponent=m.opponent,
                        date=dtutil.display_date(m.date))
                return False

        counts = deck.count_matches(form.entry, form.opponent)
        if counts[int(form.entry)] >= 5:
            form.errors['entry'] = 'You already have 5 matches reported'
            return False
        if counts[int(form.opponent)] >= 5:
            form.errors[
                'opponent'] = 'Your opponent already has 5 matches reported'
            return False
        pdbot = form.get('api_token',
                         None) == configuration.get('pdbot_api_token')
        if pdbot:
            mtgo_match_id = form.get('matchID', None)
        else:
            mtgo_match_id = None
            entry_name = deck.load_deck(int(form.entry)).person
            opp_name = deck.load_deck(int(form.opponent)).person
            fetcher.post_discord_webhook(
                configuration.get_str('league_webhook_id'),
                configuration.get_str('league_webhook_token'),
                '{entry} reported {f.entry_games}-{f.opponent_games} vs {opponent}'
                .format(f=form, entry=entry_name, opponent=opp_name))

        db().begin()
        match.insert_match(dtutil.now(), form.entry, form.entry_games,
                           form.opponent, form.opponent_games, None, None,
                           mtgo_match_id)
        db().commit()
        return True
    except LockNotAcquiredException:
        form.errors[
            'entry'] = 'Cannot report right now, somebody else is reporting a match for you or your opponent. Try again a bit later'
        return False
    finally:
        db().release_lock('deck_id:{id}'.format(id=form.opponent))
        db().release_lock('deck_id:{id}'.format(id=form.entry))
コード例 #2
0
def report(form: ReportForm) -> bool:
    try:
        db().get_lock('deck_id:{id}'.format(id=form.entry))
        db().get_lock('deck_id:{id}'.format(id=form.opponent))

        pdbot = form.get('api_token',
                         None) == configuration.get('pdbot_api_token')

        entry_deck_id = int(form.entry)
        opponent_deck_id = int(form.opponent)

        ds = {
            d.id: d
            for d in deck.load_decks(
                f'd.id IN ({entry_deck_id}, {opponent_deck_id})')
        }
        entry_deck = ds.get(entry_deck_id)
        opponent_deck = ds.get(opponent_deck_id)

        if not entry_deck:
            form.errors[
                'entry'] = 'This deck does not appear to exist. Please try again.'
            return False

        if not opponent_deck:
            form.errors[
                'opponent'] = 'This deck does not appear to exist. Please try again.'
            return False

        if not pdbot:
            if entry_deck.retired:
                form.errors[
                    'entry'] = 'Your deck is retired, you cannot report results for it. If you need to do this, contact a mod on the Discord.'
                return False
            if opponent_deck.retired:
                form.errors[
                    'opponent'] = "Your opponent's deck is retired, you cannot report results against it. If you need to do this, please contact a mod on the Discord."
                return False

        for m in match.load_matches_by_deck(form):
            if int(form.opponent) == m.opponent_deck_id:
                form.errors[
                    'result'] = 'This match was reported as You {game_wins}–{game_losses} {opponent} {date}'.format(
                        game_wins=m.game_wins,
                        game_losses=m.game_losses,
                        opponent=m.opponent,
                        date=dtutil.display_date(m.date))
                return False

        counts = deck.count_matches(form.entry, form.opponent)
        if counts[int(form.entry)] >= 5:
            form.errors['entry'] = 'You already have 5 matches reported'
            return False
        if counts[int(form.opponent)] >= 5:
            form.errors[
                'opponent'] = 'Your opponent already has 5 matches reported'
            return False

        if pdbot:
            mtgo_match_id = form.get('matchID', None)
        else:
            mtgo_match_id = None
        match.insert_match(dtutil.now(), form.entry, form.entry_games,
                           form.opponent, form.opponent_games, None, None,
                           mtgo_match_id)
        if not pdbot:
            if configuration.get('league_webhook_id') and configuration.get(
                    'league_webhook_token'):
                fetch_tools.post_discord_webhook(
                    configuration.get_str('league_webhook_id'),
                    configuration.get_str('league_webhook_token'),
                    '{entry} reported {f.entry_games}-{f.opponent_games} vs {opponent}'
                    .format(f=form,
                            entry=entry_deck.person,
                            opponent=opponent_deck.person))
            else:
                logger.warning(
                    'Not posting manual report to discord because not configured.'
                )
        return True
    except LockNotAcquiredException:
        form.errors[
            'entry'] = 'Cannot report right now, somebody else is reporting a match for you or your opponent. Try again a bit later'
        return False
    finally:
        db().release_lock('deck_id:{id}'.format(id=form.opponent))
        db().release_lock('deck_id:{id}'.format(id=form.entry))