Beispiel #1
0
def get_elo():
    # This method will be refactored.
    # pylint: disable=too-many-locals

    _, cursor = util.connect_to_db()

    events = util.translate_events(cursor)

    player_elo = collections.defaultdict(lambda: DEFAULT_ELO)
    participated = collections.defaultdict(int)
    for event in sorted(events.values(), key=lambda event: event['date']):
        teams = event['teams']
        team_elo = {}
        for i, team in enumerate(teams):
            total = 0
            for player in team['squad']:
                total += player_elo[player]
                participated[player] += 1
            team_elo[i] = total // len(team['squad'])

        team_elo_diff = {i: 0 for i, _ in enumerate(teams)}
        for i, _ in enumerate(teams):
            for j in range(i + 1, len(teams)):
                win_odds = (1 /
                            (1 + 10**((team_elo[j] - team_elo[i]) / ELO_DIFF)))
                if teams[i]['result'] == teams[j]['result']:
                    actual_res = 0.5
                elif event['event_type'] == 'match':
                    actual_res = float(teams[i]['result'] > teams[j]['result'])
                else:
                    assert event['event_type'] == 'tournament'
                    actual_res = float(teams[i]['result'] < teams[j]['result'])

                coef = int(ELO_MULTIPLIER * (actual_res - win_odds))
                team_elo_diff[i] += coef
                team_elo_diff[j] -= coef

        for i, team in enumerate(teams):
            for player in team['squad']:
                player_elo[player] += team_elo_diff[i]

        # LOGGER.error('--ELO DIFF--')
        # for i, team in enumerate(teams):
        #     LOGGER.error(
        #         '%s %s : %s',
        #         team_elo[i], team['result'], team_elo_diff[i],
        #     )
        # LOGGER.error('==ELO DIFF==')

    response = {
        player: {
            'participated': played,
            'elo': player_elo[player],
        }
        for player, played in participated.items()
    }

    return flask.jsonify({'items': response})
Beispiel #2
0
def get_events():
    _, cursor = util.connect_to_db()

    events = util.translate_events(cursor)

    items = {
        util.make_event_key(event['date'], event['event_number']): event
        for event in events.values()
    }

    return flask.jsonify({'items': items}), 200
Beispiel #3
0
def get_total_stats():
    _, cursor = util.connect_to_db()
    events = util.translate_and_sort_events(cursor)

    total = total_stat.build_total_stat(events)

    response = {
        'total': total,
        'streaks': total_stat.build_streaks(events, total.keys()),
        'goals': total_stat.build_goals(events),
    }

    return flask.jsonify(response), 200
Beispiel #4
0
def add_event():
    connection, cursor = util.connect_to_db()
    payload = flask.request.get_json()

    try:
        data = validate_and_prepare_data(cursor, payload)
    except s.ValidationError as exc:
        return flask.jsonify({'errors': exc.messages}), 422

    flatten_event_to_db(connection, cursor, data)

    event_id = util.make_event_key(data['date'], data['event_number'])
    return flask.jsonify({'id': event_id}), 201
Beispiel #5
0
def get_personal_stats(hero):
    _, cursor = util.connect_to_db()

    events = util.translate_and_sort_events(cursor)

    result_summary = shared.build_result_summary(events)
    if hero not in result_summary:
        return flask.jsonify({'errors': f"Player '{hero}' doesn't exist"}), 404

    response = {
        'elo': personal_stat.build_persisted_elo(events, hero),
        'teammates': personal_stat.build_stat_teammates(events, hero),
        'rivals': personal_stat.build_stat_rivals(events, hero),
        'result_summary': result_summary[hero],
    }

    return flask.jsonify(response), 200