5 from collections import OrderedDict
6 from pyramid import httpexceptions
7 from pyramid.response import Response
8 from sqlalchemy import desc, func, over
9 from sqlalchemy.orm.exc import *
10 from webhelpers.paginate import Page, PageURL
11 from xonstat.models import *
12 from xonstat.util import page_url
13 from xonstat.views.helpers import RecentGame, recent_games_q
16 log = logging.getLogger(__name__)
19 def _game_info_data(request):
20 game_id = int(request.matchdict['id'])
22 # show an extra column if "show_elo" is a GET parameter
23 show_elo = bool(request.params.get("show_elo", False))
26 (game, server, map, gametype) = DBSession.query(Game, Server, Map, GameType).\
27 filter(Game.game_id == game_id).\
28 filter(Game.server_id == Server.server_id).\
29 filter(Game.map_id == Map.map_id).\
30 filter(Game.game_type_cd == GameType.game_type_cd).one()
32 pgstats = DBSession.query(PlayerGameStat).\
33 filter(PlayerGameStat.game_id == game_id).\
34 order_by(PlayerGameStat.scoreboardpos).\
35 order_by(PlayerGameStat.score).\
38 # Really old games don't have latency sent, so we have to check. If at
39 # least one player has a latency value, we'll show the ping column.
41 for pgstat in pgstats:
42 if pgstat.avg_latency is not None:
46 q = DBSession.query(TeamGameStat).\
47 filter(TeamGameStat.game_id == game_id)
48 if game.game_type_cd == 'ctf':
49 q = q.order_by(TeamGameStat.caps.desc())
50 elif game.game_type_cd == 'ca':
51 q = q.order_by(TeamGameStat.rounds.desc())
52 # dom -> ticks, rc -> laps, nb -> goals, as -> objectives
54 q = q.order_by(TeamGameStat.score.desc())
58 stats_by_team = OrderedDict()
59 for pgstat in pgstats:
60 if pgstat.team not in stats_by_team.keys():
61 stats_by_team[pgstat.team] = []
62 stats_by_team[pgstat.team].append(pgstat)
65 if game.game_type_cd == 'ctf':
66 for pgstat in pgstats:
67 if pgstat.fastest is not None:
68 captimes.append(pgstat)
69 captimes = sorted(captimes, key=lambda x:x.fastest)
72 for (pwstat, weapon) in DBSession.query(PlayerWeaponStat, Weapon).\
73 filter(PlayerWeaponStat.game_id == game_id).\
74 filter(PlayerWeaponStat.weapon_cd == Weapon.weapon_cd).\
75 order_by(PlayerWeaponStat.actual.desc()).\
77 if pwstat.player_game_stat_id not in pwstats:
78 pwstats[pwstat.player_game_stat_id] = []
80 pwstats[pwstat.player_game_stat_id].append((weapon.descr,
81 weapon.weapon_cd, pwstat.actual, pwstat.max,
82 pwstat.hit, pwstat.fired, pwstat.frags))
84 except NoResultFound as e:
85 raise httpexceptions.HTTPNotFound("Could not find that game!")
87 except Exception as e:
99 'show_latency':show_latency,
100 'stats_by_team':stats_by_team,
104 def game_info(request):
106 List the game stats (scoreboard) for a particular game. Paginated.
108 return _game_info_data(request)
111 def game_info_json(request):
113 List the game stats (scoreboard) for a particular game. Paginated. JSON.
115 return [{'status':'not implemented'}]
118 def _rank_index_data(request):
119 if request.params.has_key('page'):
120 current_page = request.params['page']
124 # game type whitelist
125 game_types_allowed = ["ca", "ctf", "dm", "duel", "ft", "ka", "tdm"]
127 game_type_cd = request.matchdict['game_type_cd']
128 if game_type_cd not in game_types_allowed:
129 raise httpexceptions.HTTPNotFound()
131 ranks_q = DBSession.query(PlayerRank).\
132 filter(PlayerRank.game_type_cd==game_type_cd).\
133 order_by(PlayerRank.rank)
135 ranks = Page(ranks_q, current_page, url=page_url)
142 'game_type_cd':game_type_cd,
146 def rank_index(request):
148 Provide a list of gametype ranks, paginated.
150 return _rank_index_data(request)
153 def rank_index_json(request):
155 Provide a list of gametype ranks, paginated. JSON.
157 return [{'status':'not implemented'}]
160 def game_finder_data(request):
161 if request.params.has_key('page'):
162 current_page = request.params['page']
168 server_id, map_id, player_id = None, None, None
169 game_type_cd, start_game_id, end_game_id = None, None, None
170 game_type_descr = None
172 # these become WHERE clauses when present
173 if request.params.has_key('server_id'):
174 server_id = request.params['server_id']
175 query['server_id'] = server_id
177 if request.params.has_key('map_id'):
178 map_id = request.params['map_id']
179 query['map_id'] = map_id
181 if request.params.has_key('player_id'):
182 player_id = request.params['player_id']
183 query['player_id'] = player_id
185 if request.params.has_key('start_game_id'):
186 start_game_id = request.params['start_game_id']
187 query['start_game_id'] = start_game_id
189 if request.params.has_key('end_game_id'):
190 end_game_id = request.params['end_game_id']
191 query['end_game_id'] = end_game_id
193 if request.params.has_key('type'):
194 game_type_cd = request.params['type']
195 query['type'] = game_type_cd
197 game_type_descr = DBSession.query(GameType.descr).\
198 filter(GameType.game_type_cd == game_type_cd).\
200 except Exception as e:
203 rgs_q = recent_games_q(server_id=server_id, map_id=map_id,
204 player_id=player_id, game_type_cd=game_type_cd,
205 start_game_id=start_game_id, end_game_id=end_game_id)
208 recent_games = [RecentGame(row) for row in rgs_q.limit(20).all()]
210 if len(recent_games) > 0:
211 query['start_game_id'] = recent_games[-1].game_id + 1
213 # build the list of links for the stripe across the top
216 # clear out the game_id window
217 gt_query = query.copy()
218 if 'start_game_id' in gt_query:
219 del gt_query['start_game_id']
220 if 'end_game_id' in gt_query:
221 del gt_query['end_game_id']
223 for gt in ('overall','duel','ctf','dm','tdm','ca','kh','ft',
224 'lms','as','dom','nb','cts','rc'):
225 gt_query['type'] = gt
226 url = request.route_url("game_index", _query=gt_query)
227 game_type_links.append((gt, url))
230 raise httpexceptions.HTTPBadRequest("Malformed Query")
233 'recent_games':recent_games,
235 'game_type_cd':game_type_cd,
236 'game_type_links':game_type_links,
240 def game_finder(request):
242 Provide a list of recent games with an advanced filter.
244 return game_finder_data(request)
247 def game_finder_json(request):
249 Provide a list of recent games in JSON format.
251 data = game_finder_data(request)
252 return [rg.to_dict() for rg in data["recent_games"]]