5 from pyramid.response import Response
6 from sqlalchemy import desc, func, over
7 from webhelpers.paginate import Page, PageURL
8 from xonstat.models import *
9 from xonstat.util import page_url
11 log = logging.getLogger(__name__)
14 def _game_index_data(request):
15 if request.params.has_key('page'):
16 current_page = request.params['page']
20 games_q = DBSession.query(Game, Server, Map).\
21 filter(Game.server_id == Server.server_id).\
22 filter(Game.map_id == Map.map_id).\
23 order_by(Game.game_id.desc())
25 games = Page(games_q, current_page, items_per_page=10, url=page_url)
28 for (game, server, map) in games:
29 pgstats[game.game_id] = DBSession.query(PlayerGameStat).\
30 filter(PlayerGameStat.game_id == game.game_id).\
31 order_by(PlayerGameStat.rank).\
32 order_by(PlayerGameStat.score).all()
34 return {'games':games,
38 def game_index(request):
40 Provides a list of current games, with the associated game stats.
41 These games are ordered by game_id, with the most current ones first.
44 return _game_index_data(request)
47 def game_index_json(request):
49 Provides a list of current games, with the associated game stats.
50 These games are ordered by game_id, with the most current ones first.
53 return [{'status':'not implemented'}]
56 def _game_info_data(request):
57 game_id = request.matchdict['id']
59 if request.params.has_key('show_elo'):
67 (game, server, map) = DBSession.query(Game, Server, Map).\
68 filter(Game.game_id == game_id).\
69 filter(Game.server_id == Server.server_id).\
70 filter(Game.map_id == Map.map_id).one()
72 pgstats = DBSession.query(PlayerGameStat).\
73 filter(PlayerGameStat.game_id == game_id).\
74 order_by(PlayerGameStat.rank).\
75 order_by(PlayerGameStat.score).\
79 if game.game_type_cd == 'ctf':
80 for pgstat in pgstats:
81 if pgstat.fastest_cap is not None:
82 captimes.append(pgstat)
84 captimes = sorted(captimes, key=lambda x:x.fastest_cap)
87 for (pwstat, pgstat, weapon) in DBSession.query(PlayerWeaponStat, PlayerGameStat, Weapon).\
88 filter(PlayerWeaponStat.game_id == game_id).\
89 filter(PlayerWeaponStat.weapon_cd == Weapon.weapon_cd).\
90 filter(PlayerWeaponStat.player_game_stat_id == \
91 PlayerGameStat.player_game_stat_id).\
92 order_by(PlayerGameStat.rank).\
93 order_by(PlayerGameStat.score).\
94 order_by(Weapon.descr).\
96 if pgstat.player_game_stat_id not in pwstats:
97 pwstats[pgstat.player_game_stat_id] = []
99 # NOTE adding pgstat to position 6 in order to display nick.
100 # You have to use a slice [0:5] to pass to the accuracy
102 pwstats[pgstat.player_game_stat_id].append((weapon.descr,
103 weapon.weapon_cd, pwstat.actual, pwstat.max,
104 pwstat.hit, pwstat.fired, pgstat))
106 except Exception as inst:
126 def game_info(request):
128 List the game stats (scoreboard) for a particular game. Paginated.
130 return _game_info_data(request)
133 def game_info_json(request):
135 List the game stats (scoreboard) for a particular game. Paginated. JSON.
137 return [{'status':'not implemented'}]
140 def _rank_index_data(request):
141 if request.params.has_key('page'):
142 current_page = request.params['page']
146 game_type_cd = request.matchdict['game_type_cd']
148 ranks_q = DBSession.query(PlayerRank).\
149 filter(PlayerRank.game_type_cd==game_type_cd).\
150 order_by(PlayerRank.rank)
152 ranks = Page(ranks_q, current_page, url=page_url)
159 'game_type_cd':game_type_cd,
163 def rank_index(request):
165 Provide a list of gametype ranks, paginated.
167 return _rank_index_data(request)
170 def rank_index_json(request):
172 Provide a list of gametype ranks, paginated. JSON.
174 return [{'status':'not implemented'}]