5 import sqlalchemy as sa
6 import sqlalchemy.sql.functions as func
8 from pyramid.response import Response
9 from pyramid.url import current_route_url
10 from sqlalchemy import desc, distinct
11 from webhelpers.paginate import Page, PageURL
12 from xonstat.models import *
13 from xonstat.util import page_url
15 log = logging.getLogger(__name__)
18 def player_index_data(request):
19 if request.params.has_key('page'):
20 current_page = request.params['page']
25 player_q = DBSession.query(Player).\
26 filter(Player.player_id > 2).\
27 filter(Player.active_ind == True).\
28 filter(sa.not_(Player.nick.like('Anonymous Player%'))).\
29 order_by(Player.player_id.desc())
31 players = Page(player_q, current_page, items_per_page=10, url=page_url)
33 except Exception as e:
37 return {'players':players
41 def player_index(request):
43 Provides a list of all the current players.
45 return player_index_data(request)
48 def player_index_json(request):
50 Provides a list of all the current players. JSON.
52 return [{'status':'not implemented'}]
55 def _get_games_played(player_id):
57 Provides a breakdown by gametype of the games played by player_id.
59 Returns a tuple containing (total_games, games_breakdown), where
60 total_games is the absolute number of games played by player_id
61 and games_breakdown is an array containing (game_type_cd, # games)
63 games_played = DBSession.query(Game.game_type_cd, func.count()).\
64 filter(Game.game_id == PlayerGameStat.game_id).\
65 filter(PlayerGameStat.player_id == player_id).\
66 group_by(Game.game_type_cd).\
67 order_by(func.count().desc()).all()
70 for (game_type_cd, games) in games_played:
73 return (total, games_played)
76 # TODO: should probably factor the above function into this one such that
77 # total_stats['ctf_games'] is the count of CTF games and so on...
78 def _get_total_stats(player_id):
80 Provides aggregated stats by player_id.
82 Returns a dict with the keys 'kills', 'deaths', 'alivetime'.
84 kills = how many kills a player has over all games
85 deaths = how many deaths a player has over all games
86 alivetime = how long a player has played over all games
88 If any of the above are None, they are set to 0.
91 (total_stats['kills'], total_stats['deaths'], total_stats['alivetime']) = DBSession.\
92 query("total_kills", "total_deaths", "total_alivetime").\
94 "select sum(kills) total_kills, "
95 "sum(deaths) total_deaths, "
96 "sum(alivetime) total_alivetime "
97 "from player_game_stats "
98 "where player_id=:player_id"
99 ).params(player_id=player_id).one()
101 (total_stats['wins'],) = DBSession.\
102 query("total_wins").\
104 "select count(*) total_wins "
105 "from games g, player_game_stats pgs "
106 "where g.game_id = pgs.game_id "
107 "and player_id=:player_id "
108 "and (g.winner = pgs.team or pgs.rank = 1)"
109 ).params(player_id=player_id).one()
111 for (key,value) in total_stats.items():
118 def _get_fav_map(player_id):
120 Get the player's favorite map. The favorite map is defined
121 as the map that he or she has played the most in the past
124 Returns a dictionary with keys for the map's name and id.
127 back_then = datetime.datetime.utcnow() - datetime.timedelta(days=90)
129 raw_fav_map = DBSession.query(Map.name, Map.map_id).\
130 filter(Game.game_id == PlayerGameStat.game_id).\
131 filter(Game.map_id == Map.map_id).\
132 filter(PlayerGameStat.player_id == player_id).\
133 filter(PlayerGameStat.create_dt > back_then).\
134 group_by(Map.name, Map.map_id).\
135 order_by(func.count().desc()).\
139 fav_map['name'] = raw_fav_map[0]
140 fav_map['id'] = raw_fav_map[1]
145 def _get_rank(player_id):
147 Get the player's rank as well as the total number of ranks.
149 rank = DBSession.query("game_type_cd", "rank", "max_rank").\
151 "select pr.game_type_cd, pr.rank, overall.max_rank "
152 "from player_ranks pr, "
153 "(select game_type_cd, max(rank) max_rank "
155 "group by game_type_cd) overall "
156 "where pr.game_type_cd = overall.game_type_cd "
157 "and player_id = :player_id "
159 params(player_id=player_id).all()
164 def get_accuracy_stats(player_id, weapon_cd, games):
166 Provides accuracy for weapon_cd by player_id for the past N games.
168 # Reaching back 90 days should give us an accurate enough average
169 # We then multiply this out for the number of data points (games) to
170 # create parameters for a flot graph
172 raw_avg = DBSession.query(func.sum(PlayerWeaponStat.hit),
173 func.sum(PlayerWeaponStat.fired)).\
174 filter(PlayerWeaponStat.player_id == player_id).\
175 filter(PlayerWeaponStat.weapon_cd == weapon_cd).\
178 avg = round(float(raw_avg[0])/raw_avg[1]*100, 2)
180 # Determine the raw accuracy (hit, fired) numbers for $games games
181 # This is then enumerated to create parameters for a flot graph
182 raw_accs = DBSession.query(PlayerWeaponStat.game_id,
183 PlayerWeaponStat.hit, PlayerWeaponStat.fired).\
184 filter(PlayerWeaponStat.player_id == player_id).\
185 filter(PlayerWeaponStat.weapon_cd == weapon_cd).\
186 order_by(PlayerWeaponStat.game_id.desc()).\
190 # they come out in opposite order, so flip them in the right direction
194 for i in range(len(raw_accs)):
195 accs.append((raw_accs[i][0], round(float(raw_accs[i][1])/raw_accs[i][2]*100, 2)))
203 def get_damage_stats(player_id, weapon_cd, games):
205 Provides damage info for weapon_cd by player_id for the past N games.
208 raw_avg = DBSession.query(func.sum(PlayerWeaponStat.actual),
209 func.sum(PlayerWeaponStat.hit)).\
210 filter(PlayerWeaponStat.player_id == player_id).\
211 filter(PlayerWeaponStat.weapon_cd == weapon_cd).\
214 avg = round(float(raw_avg[0])/raw_avg[1], 2)
216 # Determine the damage efficiency (hit, fired) numbers for $games games
217 # This is then enumerated to create parameters for a flot graph
218 raw_dmgs = DBSession.query(PlayerWeaponStat.game_id,
219 PlayerWeaponStat.actual, PlayerWeaponStat.hit).\
220 filter(PlayerWeaponStat.player_id == player_id).\
221 filter(PlayerWeaponStat.weapon_cd == weapon_cd).\
222 order_by(PlayerWeaponStat.game_id.desc()).\
226 # they come out in opposite order, so flip them in the right direction
230 for i in range(len(raw_dmgs)):
231 # try to derive, unless we've hit nothing then set to 0!
233 dmg = round(float(raw_dmgs[i][1])/raw_dmgs[i][2], 2)
237 dmgs.append((raw_dmgs[i][0], dmg))
238 except Exception as e:
245 def player_info_data(request):
246 player_id = int(request.matchdict['id'])
251 player = DBSession.query(Player).filter_by(player_id=player_id).\
252 filter(Player.active_ind == True).one()
254 # games played, alivetime, wins, kills, deaths
255 total_stats = _get_total_stats(player.player_id)
257 # games breakdown - N games played (X ctf, Y dm) etc
258 (total_games, games_breakdown) = _get_games_played(player.player_id)
260 # favorite map from the past 90 days
262 fav_map = _get_fav_map(player.player_id)
266 # friendly display of elo information and preliminary status
267 elos = DBSession.query(PlayerElo).filter_by(player_id=player_id).\
268 filter(PlayerElo.game_type_cd.in_(['ctf','duel','dm'])).\
269 order_by(PlayerElo.elo.desc()).all()
278 elos_display.append(str.format(round(elo.elo, 3),
281 # get current rank information
282 ranks = _get_rank(player_id)
283 ranks_display = ', '.join(["{1} of {2} ({0})".format(gtc, rank,
284 max_rank) for gtc, rank, max_rank in ranks])
287 # which weapons have been used in the past 90 days
288 # and also, used in 5 games or more?
289 back_then = datetime.datetime.utcnow() - datetime.timedelta(days=90)
291 for weapon in DBSession.query(PlayerWeaponStat.weapon_cd, func.count()).\
292 filter(PlayerWeaponStat.player_id == player_id).\
293 filter(PlayerWeaponStat.create_dt > back_then).\
294 group_by(PlayerWeaponStat.weapon_cd).\
295 having(func.count() > 4).\
297 recent_weapons.append(weapon[0])
299 # recent games table, all data
300 recent_games = DBSession.query(PlayerGameStat, Game, Server, Map).\
301 filter(PlayerGameStat.player_id == player_id).\
302 filter(PlayerGameStat.game_id == Game.game_id).\
303 filter(Game.server_id == Server.server_id).\
304 filter(Game.map_id == Map.map_id).\
305 order_by(Game.game_id.desc())[0:10]
307 except Exception as e:
313 games_breakdown = None
316 ranks_display = None;
318 return {'player':player,
319 'elos_display':elos_display,
320 'recent_games':recent_games,
321 'total_stats':total_stats,
322 'total_games':total_games,
323 'games_breakdown':games_breakdown,
324 'recent_weapons':recent_weapons,
326 'ranks_display':ranks_display,
330 def player_info(request):
332 Provides detailed information on a specific player
334 return player_info_data(request)
337 def player_info_json(request):
339 Provides detailed information on a specific player. JSON.
341 return [{'status':'not implemented'}]
344 def player_game_index_data(request):
345 player_id = request.matchdict['player_id']
347 if request.params.has_key('page'):
348 current_page = request.params['page']
353 games_q = DBSession.query(Game, Server, Map).\
354 filter(PlayerGameStat.game_id == Game.game_id).\
355 filter(PlayerGameStat.player_id == player_id).\
356 filter(Game.server_id == Server.server_id).\
357 filter(Game.map_id == Map.map_id).\
358 order_by(Game.game_id.desc())
360 games = Page(games_q, current_page, items_per_page=10, url=page_url)
363 for (game, server, map) in games:
364 pgstats[game.game_id] = DBSession.query(PlayerGameStat).\
365 filter(PlayerGameStat.game_id == game.game_id).\
366 order_by(PlayerGameStat.rank).\
367 order_by(PlayerGameStat.score).all()
369 except Exception as e:
373 return {'player_id':player_id,
378 def player_game_index(request):
380 Provides an index of the games in which a particular
381 player was involved. This is ordered by game_id, with
382 the most recent game_ids first. Paginated.
384 return player_game_index_data(request)
387 def player_game_index_json(request):
389 Provides an index of the games in which a particular
390 player was involved. This is ordered by game_id, with
391 the most recent game_ids first. Paginated. JSON.
393 return [{'status':'not implemented'}]
396 def player_accuracy_data(request):
397 player_id = request.matchdict['id']
398 allowed_weapons = ['nex', 'rifle', 'shotgun', 'uzi', 'minstanex']
402 if request.params.has_key('weapon'):
403 if request.params['weapon'] in allowed_weapons:
404 weapon_cd = request.params['weapon']
406 if request.params.has_key('games'):
408 games = request.params['games']
417 (avg, accs) = get_accuracy_stats(player_id, weapon_cd, games)
419 # if we don't have enough data for the given weapon
420 if len(accs) < games:
424 'player_id':player_id,
425 'player_url':request.route_url('player_info', id=player_id),
433 def player_accuracy(request):
435 Provides the accuracy for the given weapon. (JSON only)
437 return player_accuracy_data(request)
440 def player_accuracy_json(request):
442 Provides a JSON response representing the accuracy for the given weapon.
445 weapon = which weapon to display accuracy for. Valid values are 'nex',
446 'shotgun', 'uzi', and 'minstanex'.
447 games = over how many games to display accuracy. Can be up to 50.
449 return player_accuracy_data(request)
452 def player_damage_data(request):
453 player_id = request.matchdict['id']
454 allowed_weapons = ['grenadelauncher', 'electro', 'crylink', 'hagar',
455 'rocketlauncher', 'laser']
456 weapon_cd = 'rocketlauncher'
459 if request.params.has_key('weapon'):
460 if request.params['weapon'] in allowed_weapons:
461 weapon_cd = request.params['weapon']
463 if request.params.has_key('games'):
465 games = request.params['games']
474 (avg, dmgs) = get_damage_stats(player_id, weapon_cd, games)
476 # if we don't have enough data for the given weapon
477 if len(dmgs) < games:
481 'player_id':player_id,
482 'player_url':request.route_url('player_info', id=player_id),
490 def player_damage_json(request):
492 Provides a JSON response representing the damage for the given weapon.
495 weapon = which weapon to display damage for. Valid values are
496 'grenadelauncher', 'electro', 'crylink', 'hagar', 'rocketlauncher',
498 games = over how many games to display damage. Can be up to 50.
500 return player_damage_data(request)