126 lines
4.3 KiB
Python
126 lines
4.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Copyright (c) 2017 Tomek Wójcik <tomek@bthlabs.pl>
|
|
#
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
# in the Software without restriction, including without limitation the rights
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
# furnished to do so, subject to the following conditions:
|
|
#
|
|
# The above copyright notice and this permission notice shall be included in
|
|
# all copies or substantial portions of the Software.
|
|
#
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
# THE SOFTWARE.
|
|
#
|
|
|
|
"""
|
|
q3stats.lib.charts.player
|
|
=========================
|
|
|
|
This module contains functions for genering player charts.
|
|
"""
|
|
|
|
from collections import defaultdict
|
|
|
|
import six
|
|
|
|
from q3stats.lib import defs, queries
|
|
from q3stats.models import Game, Score
|
|
|
|
|
|
def get_player_wins_chart(session, player, agg_by='date'):
|
|
"""Returns wins chart data for *player*.
|
|
|
|
Use the *agg_by* kwargs to specify aggregation level. Valid values are
|
|
``date`` and ``map``."""
|
|
assert agg_by in ('date', 'map')
|
|
|
|
categories = []
|
|
wins_serie = []
|
|
losses_serie = []
|
|
|
|
player_sessions = queries.get_player_sessions(session, player)
|
|
if player_sessions:
|
|
scores = session.query(Score).\
|
|
join(Score.game).\
|
|
filter(Game.date.in_(player_sessions)).\
|
|
filter(Score.player == player).\
|
|
all()
|
|
|
|
if scores:
|
|
intermediary = defaultdict(lambda: [0, 0])
|
|
for score in scores:
|
|
agg = getattr(score.game, agg_by)
|
|
if score.score == score.game.fraglimit:
|
|
intermediary[agg][0] += 1
|
|
else:
|
|
intermediary[agg][1] += 1
|
|
|
|
categories = sorted(intermediary.keys())
|
|
for key in categories:
|
|
wins_serie.append(intermediary[key][0])
|
|
losses_serie.append(intermediary[key][1])
|
|
|
|
return categories, wins_serie, losses_serie
|
|
|
|
|
|
def get_player_avg_accuracy_chart(session, player, agg_by='date'):
|
|
"""Returns avg accuracy chart data for *player*.
|
|
|
|
Use the *agg_by* kwargs to specify aggregation level. Valid values are
|
|
``date`` and ``map``."""
|
|
assert agg_by in ('date', 'map')
|
|
|
|
categories = []
|
|
series = []
|
|
|
|
player_sessions = queries.get_player_sessions(session, player)
|
|
if player_sessions:
|
|
scores = session.query(Score).\
|
|
join(Score.game).\
|
|
filter(Game.date.in_(player_sessions)).\
|
|
filter(Score.player == player).\
|
|
all()
|
|
|
|
if scores:
|
|
intermediary = defaultdict(lambda: defaultdict(list))
|
|
for score in scores:
|
|
for weapon, weapon_stats in six.iteritems(score.weapons):
|
|
if weapon_stats['shots'] > 0:
|
|
agg = getattr(score.game, agg_by)
|
|
intermediary[agg][weapon].append(
|
|
weapon_stats['hits'] / float(weapon_stats['shots'])
|
|
)
|
|
|
|
categories = sorted(intermediary.keys())
|
|
|
|
weapons = list(defs.WEAPON_NAMES.keys())
|
|
weapons.remove('G')
|
|
weapons.sort()
|
|
|
|
for weapon in weapons:
|
|
serie = {
|
|
'name': defs.WEAPON_NAMES[weapon],
|
|
'data': []
|
|
}
|
|
|
|
for key in categories:
|
|
accuracies = intermediary[key][weapon]
|
|
if not accuracies:
|
|
serie['data'].append(None)
|
|
else:
|
|
serie['data'].append(
|
|
round(sum(accuracies) / len(accuracies), 4) * 100
|
|
)
|
|
|
|
series.append(serie)
|
|
|
|
return categories, series
|