5 import sqlalchemy as sa
\r
6 import sqlalchemy.sql.functions as func
\r
8 from pyramid.response import Response
\r
9 from pyramid.url import current_route_url
\r
10 from sqlalchemy import desc, distinct
\r
11 from webhelpers.paginate import Page, PageURL
\r
12 from xonstat.models import *
\r
13 from xonstat.util import page_url
\r
15 log = logging.getLogger(__name__)
\r
18 def player_index(request):
\r
20 Provides a list of all the current players.
\r
22 if request.params.has_key('page'):
\r
23 current_page = request.params['page']
\r
28 player_q = DBSession.query(Player).\
\r
29 filter(Player.player_id > 2).\
\r
30 filter(Player.active_ind == True).\
\r
31 filter(sa.not_(Player.nick.like('Anonymous Player%'))).\
\r
32 order_by(Player.player_id.desc())
\r
34 players = Page(player_q, current_page, items_per_page=10, url=page_url)
\r
36 except Exception as e:
\r
40 return {'players':players
\r
44 def get_games_played(player_id):
\r
46 Provides a breakdown by gametype of the games played by player_id.
\r
48 Returns a tuple containing (total_games, games_breakdown), where
\r
49 total_games is the absolute number of games played by player_id
\r
50 and games_breakdown is an array containing (game_type_cd, # games)
\r
52 games_played = DBSession.query(Game.game_type_cd, func.count()).\
\r
53 filter(Game.game_id == PlayerGameStat.game_id).\
\r
54 filter(PlayerGameStat.player_id == player_id).\
\r
55 group_by(Game.game_type_cd).\
\r
56 order_by(func.count().desc()).all()
\r
59 for (game_type_cd, games) in games_played:
\r
62 return (total, games_played)
\r
65 # TODO: should probably factor the above function into this one such that
\r
66 # total_stats['ctf_games'] is the count of CTF games and so on...
\r
67 def get_total_stats(player_id):
\r
69 Provides aggregated stats by player_id.
\r
71 Returns a dict with the keys 'kills', 'deaths', 'alivetime'.
\r
73 kills = how many kills a player has over all games
\r
74 deaths = how many deaths a player has over all games
\r
75 alivetime = how long a player has played over all games
\r
77 If any of the above are None, they are set to 0.
\r
80 (total_stats['kills'], total_stats['deaths'], total_stats['alivetime']) = DBSession.\
\r
81 query("total_kills", "total_deaths", "total_alivetime").\
\r
83 "select sum(kills) total_kills, "
\r
84 "sum(deaths) total_deaths, "
\r
85 "sum(alivetime) total_alivetime "
\r
86 "from player_game_stats "
\r
87 "where player_id=:player_id"
\r
88 ).params(player_id=player_id).one()
\r
90 (total_stats['wins'],) = DBSession.\
\r
91 query("total_wins").\
\r
93 "select count(*) total_wins "
\r
94 "from games g, player_game_stats pgs "
\r
95 "where g.game_id = pgs.game_id "
\r
96 "and player_id=:player_id "
\r
97 "and (g.winner = pgs.team or pgs.rank = 1)"
\r
98 ).params(player_id=player_id).one()
\r
100 for (key,value) in total_stats.items():
\r
102 total_stats[key] = 0
\r
107 def get_accuracy_stats(player_id, weapon_cd, games):
\r
109 Provides accuracy for weapon_cd by player_id for the past N games.
\r
111 # Reaching back 90 days should give us an accurate enough average
\r
112 # We then multiply this out for the number of data points (games) to
\r
113 # create parameters for a flot graph
\r
115 raw_avg = DBSession.query(func.sum(PlayerWeaponStat.hit),
\r
116 func.sum(PlayerWeaponStat.fired)).\
\r
117 filter(PlayerWeaponStat.player_id == player_id).\
\r
118 filter(PlayerWeaponStat.weapon_cd == weapon_cd).\
\r
121 avg = round(float(raw_avg[0])/raw_avg[1]*100, 2)
\r
123 # Determine the raw accuracy (hit, fired) numbers for $games games
\r
124 # This is then enumerated to create parameters for a flot graph
\r
125 raw_accs = DBSession.query(PlayerWeaponStat.game_id,
\r
126 PlayerWeaponStat.hit, PlayerWeaponStat.fired).\
\r
127 filter(PlayerWeaponStat.player_id == player_id).\
\r
128 filter(PlayerWeaponStat.weapon_cd == weapon_cd).\
\r
129 order_by(PlayerWeaponStat.game_id.desc()).\
\r
133 # they come out in opposite order, so flip them in the right direction
\r
137 for i in range(len(raw_accs)):
\r
138 accs.append((raw_accs[i][0], round(float(raw_accs[i][1])/raw_accs[i][2]*100, 2)))
\r
146 def player_info(request):
\r
148 Provides detailed information on a specific player
\r
150 player_id = int(request.matchdict['id'])
\r
155 player = DBSession.query(Player).filter_by(player_id=player_id).\
\r
156 filter(Player.active_ind == True).one()
\r
158 # games played, alivetime, wins, kills, deaths
\r
159 total_stats = get_total_stats(player.player_id)
\r
161 # games breakdown - N games played (X ctf, Y dm) etc
\r
162 (total_games, games_breakdown) = get_games_played(player.player_id)
\r
165 # friendly display of elo information and preliminary status
\r
166 elos = DBSession.query(PlayerElo).filter_by(player_id=player_id).\
\r
167 filter(PlayerElo.game_type_cd.in_(['ctf','duel','dm'])).\
\r
168 order_by(PlayerElo.elo.desc()).all()
\r
177 elos_display.append(str.format(round(elo.elo, 3),
\r
180 # which weapons have been used in the past 90 days
\r
181 # and also, used in 5 games or more?
\r
182 back_then = datetime.datetime.utcnow() - datetime.timedelta(days=90)
\r
183 recent_weapons = []
\r
184 for weapon in DBSession.query(PlayerWeaponStat.weapon_cd, func.count()).\
\r
185 filter(PlayerWeaponStat.player_id == player_id).\
\r
186 filter(PlayerWeaponStat.create_dt > back_then).\
\r
187 group_by(PlayerWeaponStat.weapon_cd).\
\r
188 having(func.count() > 4).\
\r
190 recent_weapons.append(weapon[0])
\r
192 # recent games table, all data
\r
193 recent_games = DBSession.query(PlayerGameStat, Game, Server, Map).\
\r
194 filter(PlayerGameStat.player_id == player_id).\
\r
195 filter(PlayerGameStat.game_id == Game.game_id).\
\r
196 filter(Game.server_id == Server.server_id).\
\r
197 filter(Game.map_id == Map.map_id).\
\r
198 order_by(Game.game_id.desc())[0:10]
\r
200 except Exception as e:
\r
202 elos_display = None
\r
204 recent_games = None
\r
206 games_breakdown = None
\r
207 recent_weapons = []
\r
209 return {'player':player,
\r
210 'elos_display':elos_display,
\r
211 'recent_games':recent_games,
\r
212 'total_stats':total_stats,
\r
213 'total_games':total_games,
\r
214 'games_breakdown':games_breakdown,
\r
215 'recent_weapons':recent_weapons,
\r
219 def player_game_index(request):
\r
221 Provides an index of the games in which a particular
\r
222 player was involved. This is ordered by game_id, with
\r
223 the most recent game_ids first. Paginated.
\r
225 player_id = request.matchdict['player_id']
\r
227 if request.params.has_key('page'):
\r
228 current_page = request.params['page']
\r
233 games_q = DBSession.query(Game, Server, Map).\
\r
234 filter(PlayerGameStat.game_id == Game.game_id).\
\r
235 filter(PlayerGameStat.player_id == player_id).\
\r
236 filter(Game.server_id == Server.server_id).\
\r
237 filter(Game.map_id == Map.map_id).\
\r
238 order_by(Game.game_id.desc())
\r
240 games = Page(games_q, current_page, items_per_page=10, url=page_url)
\r
243 for (game, server, map) in games:
\r
244 pgstats[game.game_id] = DBSession.query(PlayerGameStat).\
\r
245 filter(PlayerGameStat.game_id == game.game_id).\
\r
246 order_by(PlayerGameStat.rank).\
\r
247 order_by(PlayerGameStat.score).all()
\r
249 except Exception as e:
\r
253 return {'player_id':player_id,
\r
257 def player_accuracy(request):
\r
259 Provides a JSON response representing the accuracy for the given weapon.
\r
262 weapon = which weapon to display accuracy for. Valid values are 'nex',
\r
263 'shotgun', 'uzi', and 'minstanex'.
\r
264 games = over how many games to display accuracy. Can be up to 50.
\r
266 player_id = request.matchdict['id']
\r
267 allowed_weapons = ['nex', 'rifle', 'shotgun', 'uzi', 'minstanex']
\r
271 if request.params.has_key('weapon'):
\r
272 if request.params['weapon'] in allowed_weapons:
\r
273 weapon_cd = request.params['weapon']
\r
275 if request.params.has_key('games'):
\r
277 games = request.params['games']
\r
286 (avg, accs) = get_accuracy_stats(player_id, weapon_cd, games)
\r
288 # if we don't have enough data for the given weapon
\r
289 if len(accs) < games:
\r
293 'player_id':player_id,
\r
294 'player_url':request.route_url('player_info', id=player_id),
\r
295 'weapon':weapon_cd,
\r