From 56bb9531b247d272e516b5d21493ebc8b8f0f812 Mon Sep 17 00:00:00 2001 From: Ant Zucaro Date: Mon, 3 Dec 2012 21:55:22 -0500 Subject: [PATCH] Add a hashkey view similar to the player_info JSON one. This view is driven by hashkey (not player_id) so the Xonotic server can query for info using something it knows. The response value is a JSON object containing the things in the player info page minus the recent weapons and recent games stuff. The URL pattern is /hashkey/. All this is pretty alpha and is subject to change once we truly determine our needs Think of this as a proof of concept for communicating between the game server and the webapp. --- xonstat/__init__.py | 5 ++- xonstat/views/__init__.py | 2 +- xonstat/views/player.py | 73 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 2 deletions(-) diff --git a/xonstat/__init__.py b/xonstat/__init__.py index e7bfba3..5ee0e46 100644 --- a/xonstat/__init__.py +++ b/xonstat/__init__.py @@ -9,7 +9,7 @@ def main(global_config, **settings): """ This function returns a Pyramid WSGI application. """ # setup the database engine - engine = engine_from_config(settings, 'sqlalchemy.') + engine = engine_from_config(settings, 'sqlalchemy.', pool_size=5) sqlahelper.add_engine(engine) # initialize database structures @@ -40,6 +40,9 @@ def main(global_config, **settings): config.add_view(player_info, route_name="player_info", renderer="player_info.mako") config.add_view(player_info_json, route_name="player_info_json", renderer="jsonp") + config.add_route("player_hashkey_info_json", "/hashkey/{hashkey}") + config.add_view(player_hashkey_info_json, route_name="player_hashkey_info_json", renderer="jsonp") + config.add_route("player_accuracy", "/player/{id:\d+}/accuracy") config.add_route("player_accuracy_json", "/player/{id:\d+}/accuracy.json") config.add_view(player_accuracy_json, route_name="player_accuracy", renderer="jsonp") diff --git a/xonstat/views/__init__.py b/xonstat/views/__init__.py index f8eb769..879f546 100644 --- a/xonstat/views/__init__.py +++ b/xonstat/views/__init__.py @@ -3,7 +3,7 @@ from xonstat.views.player import player_index, player_info, player_game_index from xonstat.views.player import player_accuracy from xonstat.views.player import player_index_json, player_info_json from xonstat.views.player import player_game_index_json, player_accuracy_json -from xonstat.views.player import player_damage_json +from xonstat.views.player import player_damage_json, player_hashkey_info_json from xonstat.views.game import game_index, game_info, rank_index from xonstat.views.game import game_index_json, game_info_json, rank_index_json diff --git a/xonstat/views/player.py b/xonstat/views/player.py index d625dff..66e74eb 100644 --- a/xonstat/views/player.py +++ b/xonstat/views/player.py @@ -787,3 +787,76 @@ def player_damage_json(request): games = over how many games to display damage. Can be up to 50. """ return player_damage_data(request) + + +def player_hashkey_info_data(request): + hashkey = request.matchdict['hashkey'] + try: + player = DBSession.query(Player).\ + filter(Player.player_id == Hashkey.player_id).\ + filter(Player.active_ind == True).\ + filter(Hashkey.hashkey == hashkey).one() + + games_played = get_games_played(player.player_id) + overall_stats = get_overall_stats(player.player_id) + fav_maps = get_fav_maps(player.player_id) + elos = get_elos(player.player_id) + ranks = get_ranks(player.player_id) + + except Exception as e: + raise e + player = None + games_played = None + overall_stats = None + fav_maps = None + elos = None + ranks = None + + return {'player':player, + 'games_played':games_played, + 'overall_stats':overall_stats, + 'fav_maps':fav_maps, + 'elos':elos, + 'ranks':ranks, + } + + +def player_hashkey_info_json(request): + """ + Provides detailed information on a specific player. JSON. + """ + + # All player_info fields are converted into JSON-formattable dictionaries + player_info = player_hashkey_info_data(request) + + player = player_info['player'].to_dict() + + games_played = {} + for game in player_info['games_played']: + games_played[game.game_type_cd] = to_json(game) + + overall_stats = {} + for gt,stats in player_info['overall_stats'].items(): + overall_stats[gt] = to_json(stats) + + elos = {} + for gt,elo in player_info['elos'].items(): + elos[gt] = to_json(elo.to_dict()) + + ranks = {} + for gt,rank in player_info['ranks'].items(): + ranks[gt] = to_json(rank) + + fav_maps = {} + for gt,mapinfo in player_info['fav_maps'].items(): + fav_maps[gt] = to_json(mapinfo) + + return [{ + 'version': 1, + 'player': player, + 'games_played': games_played, + 'overall_stats': overall_stats, + 'fav_maps': fav_maps, + 'elos': elos, + 'ranks': ranks, + }] -- 2.39.2