146 lines
4.7 KiB
Python
146 lines
4.7 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.web_app.blueprints.api_v1.views.charts
|
|
==============================================
|
|
|
|
This module contains the APIv1 chart views.
|
|
"""
|
|
|
|
from collections import defaultdict
|
|
import datetime
|
|
|
|
from flask import abort, current_app, jsonify
|
|
import sqlalchemy as sa
|
|
|
|
from q3stats.web_app.blueprints.api_v1 import api_v1_blueprint
|
|
from q3stats.lib import charts, defs
|
|
from q3stats.models import Score, Game
|
|
from q3stats.lib import queries
|
|
|
|
|
|
@api_v1_blueprint.route('/charts/day/<day>', methods=['GET'])
|
|
def get_api_v1_charts_day(day):
|
|
"""Returns day chart data for *day*."""
|
|
try:
|
|
date = datetime.datetime.strptime(day, defs.DATE_FORMAT)
|
|
except (TypeError, ValueError):
|
|
abort(400)
|
|
|
|
maps, scores = charts.get_day_chart(date, current_app.db_session)
|
|
return jsonify({
|
|
'day': date.strftime(defs.DATE_FORMAT), 'maps': maps, 'scores': scores
|
|
})
|
|
|
|
|
|
@api_v1_blueprint.route('/charts/player/<player>/game/<game_uuid>',
|
|
methods=['GET'])
|
|
def get_api_v1_charts_player_game(player, game_uuid):
|
|
"""Returns player charts data for *player* and *game_uuid*."""
|
|
score = current_app.db_session.query(Score).\
|
|
join(Score.game).\
|
|
filter(Game.uuid == game_uuid).\
|
|
filter(Score.player == player).\
|
|
first()
|
|
|
|
if not score:
|
|
abort(404)
|
|
|
|
result = {
|
|
'score': [
|
|
['Frags', score.kills],
|
|
['Deaths', score.deaths],
|
|
['Suicides', score.suicides],
|
|
],
|
|
'damage': [
|
|
['Taken', score.damage_taken],
|
|
['Given', score.damage_given],
|
|
],
|
|
'totals': [
|
|
['Armor', score.total_armor],
|
|
['Health', score.total_health],
|
|
]
|
|
}
|
|
|
|
return jsonify(result)
|
|
|
|
|
|
@api_v1_blueprint.route('/charts/player/<player>/wins/session',
|
|
methods=['GET'])
|
|
def get_api_v1_charts_player_wins_session(player):
|
|
"""Returns wins chart data aggregated by session for *player*."""
|
|
categories, wins_serie, losses_serie = charts.get_player_wins_chart(
|
|
current_app.db_session, player, agg_by='date'
|
|
)
|
|
|
|
dates = [
|
|
x.strftime(defs.DATE_FORMAT) for x in categories
|
|
]
|
|
|
|
return jsonify({
|
|
'dates': dates,
|
|
'wins': wins_serie,
|
|
'losses': losses_serie
|
|
})
|
|
|
|
|
|
@api_v1_blueprint.route('/charts/player/<player>/wins/map',
|
|
methods=['GET'])
|
|
def get_api_v1_charts_player_wins_map(player):
|
|
"""Returns wins chart data aggregated by map for *player*."""
|
|
categories, wins_serie, losses_serie = charts.get_player_wins_chart(
|
|
current_app.db_session, player, agg_by='map'
|
|
)
|
|
|
|
return jsonify({
|
|
'maps': categories,
|
|
'wins': wins_serie,
|
|
'losses': losses_serie
|
|
})
|
|
|
|
|
|
@api_v1_blueprint.route('/charts/player/<player>/accuracy/session',
|
|
methods=['GET'])
|
|
def get_api_v1_charts_player_accuracy_session(player):
|
|
"""Returns avg accuracy chart data aggregated by session for *player*."""
|
|
categories, series = charts.get_player_avg_accuracy_chart(
|
|
current_app.db_session, player, agg_by='date'
|
|
)
|
|
|
|
dates = [
|
|
x.strftime(defs.DATE_FORMAT) for x in categories
|
|
]
|
|
|
|
return jsonify({'dates': dates, 'series': series})
|
|
|
|
|
|
@api_v1_blueprint.route('/charts/player/<player>/accuracy/map',
|
|
methods=['GET'])
|
|
def get_api_v1_charts_player_accuracy_map(player):
|
|
"""Returns avg accuracy chart data aggregated by map for *player*."""
|
|
categories, series = charts.get_player_avg_accuracy_chart(
|
|
current_app.db_session, player, agg_by='map'
|
|
)
|
|
|
|
return jsonify({'maps': categories, 'series': series})
|