# -*- coding: utf-8 -*- # Copyright (c) 2017 Tomek Wójcik # # 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.players =============================================== This module contains the APIv1 players views. """ from flask import abort, current_app, jsonify import sqlalchemy as sa import six from q3stats.web_app.blueprints.api_v1 import api_v1_blueprint from q3stats.lib import queries, stats from q3stats.models import Game, Score def _process_weapons(weapons): """Returns processed weapon stats.""" result = {} for weapon, weapon_stats in six.iteritems(weapons): result[weapon] = dict(weapon_stats) result[weapon]['accuracy'] = stats.weapon_accuracy( weapon, weapon_stats ) return result def _process_powerups(powerups): """Returns processed powerup stats.""" result = {} for powerup, powerup_stats in six.iteritems(powerups): result[powerup] = [ powerup_stats[0], stats.powerup_time(powerup_stats[1]) ] return result @api_v1_blueprint.route('/players/') def get_api_v1_players(): """Returns list of known players.""" players_query = sa.sql.expression.select([Score.__table__.c.player]).\ distinct(Score.__table__.c.player).\ order_by(Score.__table__.c.player.asc()) players = [x.player for x in current_app.db_session.execute(players_query)] return jsonify({"players": players}) @api_v1_blueprint.route('/players//game/') def get_api_v1_player_game(player, game_uuid): """Returns data for the Player Game view.""" game = current_app.db_session.query(Game).\ filter_by(uuid=game_uuid).\ first() if not game: abort(404) score = current_app.db_session.query(Score).\ filter_by(game_id=game.id).\ filter_by(player=player).\ first() if not score: abort(404) result = { "game": game_uuid, "player": player, "map": game.map, "items": score.items, "weapons": _process_weapons(score.weapons), "powerups": _process_powerups(score.powerups) } return jsonify(result)