This allows applications to get "recent games" via an API.
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")
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
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
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,
'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"]]
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):