]> git.rm.cloudns.org Git - xonotic/xonstat.git/commitdiff
Make the game finder JSON-enabled.
authorAnt Zucaro <azucaro@gmail.com>
Thu, 21 Jan 2016 02:43:13 +0000 (21:43 -0500)
committerAnt Zucaro <azucaro@gmail.com>
Thu, 21 Jan 2016 02:43:13 +0000 (21:43 -0500)
This allows applications to get "recent games" via an API.

xonstat/__init__.py
xonstat/views/__init__.py
xonstat/views/game.py
xonstat/views/helpers.py

index 760a6f3284aeabcf0b10d245145e7cf375fd2691..403b64581a7371cdbfa4e21d8fdc85fe615fada0 100644 (file)
@@ -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")
index b9bfd2b556f5d6b3590195bceaed80b6f8c21730..d293a83cde7697b984acbfb1bfc7d0e8b80f4073 100644 (file)
@@ -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
index 190db833ff9864904bfe346fb6c3246e4d3111ff..46970f4168196aacbed23fdd0330861697846a21 100644 (file)
@@ -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"]]
index 7172279203065589f8bce300bb3f618fead95838..ec7219323f4a9e948f4ef3356aa81b3f35e93ed0 100644 (file)
@@ -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):