From 09e8030dc5dfc467c887f137ac493d231edfa9b6 Mon Sep 17 00:00:00 2001 From: Ant Zucaro Date: Wed, 20 Jan 2016 21:43:13 -0500 Subject: [PATCH] Make the game finder JSON-enabled. This allows applications to get "recent games" via an API. --- xonstat/__init__.py | 3 ++ xonstat/views/__init__.py | 2 +- xonstat/views/game.py | 54 ++++++++++++++++++++++------------- xonstat/views/helpers.py | 60 ++++++++++++++++++--------------------- 4 files changed, 65 insertions(+), 54 deletions(-) diff --git a/xonstat/__init__.py b/xonstat/__init__.py index 760a6f3..403b645 100644 --- a/xonstat/__init__.py +++ b/xonstat/__init__.py @@ -126,6 +126,9 @@ def main(global_config, **settings): config.add_route("game_index", "/games") config.add_view(game_finder, route_name="game_index", renderer="game_finder.mako") + config.add_route("game_index_json", "/games.json") + config.add_view(game_finder_json, route_name="game_index_json", renderer="jsonp") + # SERVER ROUTES config.add_route("server_index", "/servers") config.add_view(server_index, route_name="server_index", renderer="server_index.mako") diff --git a/xonstat/views/__init__.py b/xonstat/views/__init__.py index b9bfd2b..d293a83 100644 --- a/xonstat/views/__init__.py +++ b/xonstat/views/__init__.py @@ -11,7 +11,7 @@ from xonstat.views.player import player_weaponstats_data_json from xonstat.views.game import game_info, rank_index from xonstat.views.game import game_info_json, rank_index_json -from xonstat.views.game import game_finder +from xonstat.views.game import game_finder, game_finder_json from xonstat.views.map import map_info, map_index from xonstat.views.map import map_info_json, map_index_json diff --git a/xonstat/views/game.py b/xonstat/views/game.py index 190db83..46970f4 100644 --- a/xonstat/views/game.py +++ b/xonstat/views/game.py @@ -3,6 +3,7 @@ import logging import re import time from collections import OrderedDict +from pyramid import httpexceptions from pyramid.response import Response from sqlalchemy import desc, func, over from webhelpers.paginate import Page, PageURL @@ -207,26 +208,30 @@ def game_finder_data(request): player_id=player_id, game_type_cd=game_type_cd, start_game_id=start_game_id, end_game_id=end_game_id) - recent_games = [RecentGame(row) for row in rgs_q.limit(20).all()] - - if len(recent_games) > 0: - query['start_game_id'] = recent_games[-1].game_id + 1 - - # build the list of links for the stripe across the top - game_type_links = [] - - # clear out the game_id window - gt_query = query.copy() - if 'start_game_id' in gt_query: - del gt_query['start_game_id'] - if 'end_game_id' in gt_query: - del gt_query['end_game_id'] - - for gt in ('overall','duel','ctf','dm','tdm','ca','kh','ft', - 'lms','as','dom','nb','cts','rc'): - gt_query['type'] = gt - url = request.route_url("game_index", _query=gt_query) - game_type_links.append((gt, url)) + try: + recent_games = [RecentGame(row) for row in rgs_q.limit(20).all()] + + if len(recent_games) > 0: + query['start_game_id'] = recent_games[-1].game_id + 1 + + # build the list of links for the stripe across the top + game_type_links = [] + + # clear out the game_id window + gt_query = query.copy() + if 'start_game_id' in gt_query: + del gt_query['start_game_id'] + if 'end_game_id' in gt_query: + del gt_query['end_game_id'] + + for gt in ('overall','duel','ctf','dm','tdm','ca','kh','ft', + 'lms','as','dom','nb','cts','rc'): + gt_query['type'] = gt + url = request.route_url("game_index", _query=gt_query) + game_type_links.append((gt, url)) + + except: + raise httpexceptions.HTTPBadRequest("Malformed Query") return { 'recent_games':recent_games, @@ -235,8 +240,17 @@ def game_finder_data(request): 'game_type_links':game_type_links, } + def game_finder(request): """ Provide a list of recent games with an advanced filter. """ return game_finder_data(request) + + +def game_finder_json(request): + """ + Provide a list of recent games in JSON format. + """ + data = game_finder_data(request) + return [rg.to_dict() for rg in data["recent_games"]] diff --git a/xonstat/views/helpers.py b/xonstat/views/helpers.py index 7172279..ec72193 100644 --- a/xonstat/views/helpers.py +++ b/xonstat/views/helpers.py @@ -29,47 +29,41 @@ class RecentGame(object): which will return rows matching this specification. ''' def __init__(self, row): - self.game_id = row.game_id - self.game_type_cd = row.game_type_cd - self.game_type_descr = row.game_type_descr - self.winner = row.winner - self.start_dt = row.start_dt - self.fuzzy_date = pretty_date(row.start_dt) - self.epoch = timegm(row.start_dt.timetuple()) - self.server_id = row.server_id - self.server_name = row.server_name - self.map_id = row.map_id - self.map_name = row.map_name - self.player_id = row.player_id - self.nick = row.nick + self.game_id = row.game_id + self.game_type_cd = row.game_type_cd + self.game_type_descr = row.game_type_descr + self.winner = row.winner + self.start_dt = row.start_dt + self.fuzzy_date = pretty_date(row.start_dt) + self.epoch = timegm(row.start_dt.timetuple()) + self.server_id = row.server_id + self.server_name = row.server_name + self.map_id = row.map_id + self.map_name = row.map_name + self.player_id = row.player_id + self.nick = row.nick self.nick_html_colors = html_colors(row.nick) - self.rank = row.rank - self.team = row.team + self.rank = row.rank + self.team = row.team try: self.elo_delta = row.elo_delta except: self.elo_delta = None - def _asdict(self): + def to_dict(self): return { - "game_id": self.game_id, - "game_type_cd": self.game_type_cd, - "game_type_descr": self.game_type_descr, - "winner": self.winner, - "start_dt": self.start_dt, - "fuzzy_dt": self.fuzzy_date, - "epoch": self.epoch, - "server_id": self.server_id, - "server_name": self.server_name, - "map_id": self.map_id, - "map_name": self.map_name, - "player_id": self.player_id, - "nick": self.nick, - "nick_html_colors": self.nick_html_colors, - "rank": self.rank, - "team": self.team, - "elo_delta": self.elo_delta, + "game_id" : self.game_id, + "game_type_cd" : self.game_type_cd, + "winning_team" : self.winner, + "fuzzy_dt" : self.fuzzy_date, + "epoch" : self.epoch, + "server_id" : self.server_id, + "server_name" : self.server_name, + "map_id" : self.map_id, + "map_name" : self.map_name, + "player_id" : self.player_id, + "winner" : self.nick, } def __repr__(self): -- 2.39.2