Example #1
0
def game_redirect(key):
    from overtrack_models.orm.overwatch_game_summary import OverwatchGameSummary
    try:
        OverwatchGameSummary.get(key)
    except OverwatchGameSummary.DoesNotExist:
        return redirect(url_for('apex.game.game', key=key), code=308)
    else:
        return redirect(url_for('overwatch.game.game', key=key), code=308)
Example #2
0
def game_card(key: str):
    try:
        game = OverwatchGameSummary.get(key)
    except OverwatchGameSummary.DoesNotExist:
        return 'Game does not exist', 404

    return render_template_string(
        '''
            <!DOCTYPE html>
            <html lang="en">
                <head>
                    <title>{{ title }}</title>
                    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/' + game_name + '.css') }}">
                    <style>
                        body {
                            background-color: rgba(0, 0, 0, 0);
                        }
                        .game-summary {
                            margin: 0 !important;
                        }
                    </style>
                </head>
                <body>
                    {% include 'overwatch/games_list/game_card.html' %}
                </body>
            </html>
        ''',
        title='Card',
        game=game,
        show_rank=True,
        OLDEST_SUPPORTED_GAME_VERSION=OLDEST_SUPPORTED_GAME_VERSION,

        map_thumbnail_style=map_thumbnail_style,
    )
def download_games_list() -> List[OverwatchGameSummary]:
    games = []
    for season_id in mock_user.overwatch_seasons:
        next_key = True
        while next_key:
            url = f'https://api2.overtrack.gg/overwatch/games/{GAMES_SOURCE}?season={season_id}&limit=500'
            if isinstance(next_key, str):
                url += '&last_evaluated_key=' + next_key
            logger.info(f'Downloading games list: {url}')
            r = requests.get(url)
            r.raise_for_status()
            data = r.json()

            for g in data['games']:
                g['season'] = g['season_index']
                del g['season_index']
                del g['url']
                games.append(g)
            next_key = data['last_evaluated_key']

    cached_overwatch_games = [OverwatchGameSummary(**g) for g in games]

    for g in cached_overwatch_games:
        g.user_id = mock_user.user_id

    return cached_overwatch_games
def edit_game(key: str) -> FlaskResponse:
    try:
        summary = OverwatchGameSummary.get(key)
    except OverwatchGameSummary.DoesNotExist as e:
        return 'Invalid game', 403
    if summary.user_id != session.user_id and not session.superuser:
        return 'Invalid game', 403

    summary.competitive = summary.game_type == 'competitive'
    summary.placement = summary.rank == 'placement'

    return render_template(
        'overwatch/game/edit.html',
        edit_source='games_list',
        game=summary,
    )
Example #5
0
def game(key: str):
    summary = OverwatchGameSummary.get(key)

    game = load_game(summary)
    game.timestamp = summary.time

    summary_dict = summary.asdict()
    summary_dict['key'] = (
        summary.key,
        f'https://overtrack-overwatch-games.s3.amazonaws.com/{summary.key}.json'
    )

    if check_authentication() is None and session.user.superuser:
        try:
            frames_url = urlparse(summary.frames_uri)
            signed_url = s3.generate_presigned_url('get_object',
                                                   Params={
                                                       'Bucket':
                                                       frames_url.netloc,
                                                       'Key':
                                                       frames_url.path[1:]
                                                   })
            summary_dict['frames_uri'] = (summary.frames_uri, signed_url)
        except:
            pass

    game_dict = {}
    for f in fields(game):
        if not is_dataclass(getattr(game, f.name)):
            game_dict[f.name] = getattr(game, f.name)
    game_dict['key'] = (
        summary.key,
        f'https://overtrack-overwatch-games.s3.amazonaws.com/{summary.key}.json'
    )

    return render_template(
        'overwatch/game/game.html',
        summary=summary,
        game=game,
        summary_dict=summary_dict,
        game_dict=game_dict,
        all_stats=asdict(game.stats.stats['all heroes'])
        if 'all heroes' in game.stats.stats else {},
        OLDEST_SUPPORTED_GAME_VERSION=OLDEST_SUPPORTED_GAME_VERSION,
    )
Example #6
0
def game(key: str):
    try:
        summary = OverwatchGameSummary.get(key)
    except OverwatchGameSummary.DoesNotExist:
        return 'Game does not exist', 404

    if summary.player_name and summary.result != 'UNKNOWN':
        title = f'{summary.player_name}\'s {summary.result} on {summary.map}'
    elif summary.player_name:
        title = f'{summary.player_name}\'s game on {summary.map}'
    else:
        title = f'{key.split("/", 1)[0]}\'s game on {summary.map}'

    if not summary.game_version or summary.game_version < OLDEST_SUPPORTED_GAME_VERSION or 'legacy' in request.args:
        return render_template(
            'overwatch/game/legacy_game.html',
            title=title,
            legacy_base=LEGACY_URL,
            legacy_scripts=legacy_scripts,
            legacy_stylesheet=legacy_stylesheet,
        )

    if not summary.viewable and not(check_authentication() is None and session.superuser):
        return 'Please subscribe to view game details', 403

    if summary.game_type == 'custom' and (check_authentication() is not None or (not session.superuser and session.user_id != summary.user_id)):
        return 'Custom games are restricted to being viewed by their owner', 403

    game, metadata = load_game(summary)
    game.timestamp = summary.time

    if game.teams.owner and game.result != 'UNKNOWN':
        title = f'{game.teams.owner.name}\'s {game.result} on {game.map.name}'
    elif game.teams.owner:
        title = f'{game.teams.owner.name}\'s game on {game.map.name}'

    dev_info = get_dev_info(summary, game, metadata)

    imagehash = hashlib.md5(str((game.result, game.start_sr, game.end_sr)).encode()).hexdigest()

    try:
        tfs = game.teamfights
        stat_totals = {
            'eliminations.during_fights': len(tfs.eliminations_during_fights),
            'deaths.during_fights': len(tfs.eliminations_during_fights) + len(tfs.suicides_during_fights),
            'killfeed_assists.during_fights': len(tfs.killfeed_assists_during_fights),
            'first_elims': len(tfs.first_bloods),
            'first_deaths': len(tfs.first_bloods),
            'eliminations.outside_fights': len(tfs.eliminations_outside_fights),
            'deaths.outside_fights': len(tfs.eliminations_outside_fights) + len(tfs.suicides_outside_fights),
            'killfeed_assists.outside_fights': len(tfs.killfeed_assists_outside_fights),
            'fight_starts_missed': len(tfs.teamfights),
            'times_staggered': len(tfs.teamfights),
        }
    except AttributeError:
        stat_totals = {}

    return render_template(
        'overwatch/game/game.html',

        title=title,
        meta=Meta(
            title=title,
            image_url=url_for('overwatch.game.game_card_png', key=key, _external=True) + f'?_cachebust={imagehash}',
            twitter_image_url=url_for('overwatch.game.game_card_png', key=key, _external=True) + f'?height=190&_cachebust={imagehash}',
            summary_large_image=True,
            colour=COLOURS.get(game.result, 'gray')
        ),

        # show_stats=show_stats,
        stat_totals=stat_totals,
        # get_top_heroes=get_top_heroes,
        # get_hero_color=get_hero_color,
        # get_hero_image=get_hero_image,
        # process_stat=process_stat,

        summary=summary,
        game=game,

        show_edit=check_authentication() is None and (summary.user_id == session.user_id or session.superuser),

        dev_info=dev_info,

        OLDEST_SUPPORTED_GAME_VERSION=OLDEST_SUPPORTED_GAME_VERSION,
    )
Example #7
0
def edit_game():
    logger.info(f'Updating game: {request.form}')
    try:
        summary = OverwatchGameSummary.get(request.form['key'])
    except OverwatchGameSummary.DoesNotExist:
        return 'Game does not exist', 404

    logger.info(f'Loaded game: {summary}')

    if summary.user_id != session.user_id and not session.superuser:
        user = session.user
        logger.error(f'Rejecting edit for unowned game')
        return 'Objection!', 403

    if 'delete' in request.form:
        logger.warning(f'Deleting {summary.key!r}')
        summary.delete()
        return redirect(url_for('overwatch.games_list.games_list'), code=303)

    summary.edited = True

    summary.start_sr = int(request.form['start-sr']) if request.form['start-sr'] else None
    summary.end_sr = int(request.form['end-sr']) if request.form['end-sr'] else None
    logger.info(f'Got SR {summary.start_sr} -> {summary.end_sr} from form')

    if request.form['game-type'] == 'quickplay':
        summary.game_type = 'quickplay'
    elif request.form['game-type'] == 'competitive':
        summary.game_type = 'competitive'
    elif request.form['game-type'] == 'competitive-placement':
        summary.game_type = 'competitive'
        summary.rank = 'placement'
        logger.info(f'Derived rank={summary.rank!r} from form')
    logger.info(f'Got game_type={summary.game_type!r} from form')

    if request.form['result'] == 'auto':
        if summary.game_type == 'competitive' and summary.start_sr and summary.end_sr:
            if summary.end_sr > summary.start_sr:
                summary.result = 'WIN'
            elif summary.end_sr < summary.start_sr:
                summary.result = 'LOSS'
            else:
                summary.result = 'DRAW'
        else:
            summary.result = 'UNKNOWN'
        logger.info(f'Derived result={summary.result!r} from SR')
    elif request.form['result'].upper() in ['WIN', 'LOSS', 'DRAW', 'UNKNOWN']:
        summary.result = request.form['result'].upper()
        logger.info(f'Got result={summary.result!r} from form')

    logger.info(f'Saving game: {summary}')
    summary.save()

    game, metadata = load_game(summary)
    game.start_sr = summary.start_sr
    game.end_sr = summary.end_sr
    game.result = summary.result
    game.placement = summary.rank == 'placement'
    if summary.game_type == 'competitive':
        game.mode = Mode('Competitive Play')
        game.competitive = True
    else:
        game.mode = Mode('Quick Play')
        game.competitive = False

    metadata['edited'] = 'True'
    s3.put_object(
        Bucket=GAMES_BUCKET,
        Key=game.key + '.json',
        Body=json.dumps(referenced_typedload.dump(game), indent=1).encode(),
        ACL='public-read',
        ContentType='application/json',
        Metadata=metadata
    )

    if request.form['source'] == 'games_list':
        return redirect(url_for('overwatch.games_list.games_list'), code=303)
    else:
        return redirect(url_for('overwatch.game.game', key=summary.key), code=303)