+ try:
+ overall_win_pct = float(overall_wins)/overall_games * 100
+ except:
+ overall_win_pct = 0.0
+
+ games_played.append(GamesPlayed('overall', overall_games, overall_wins,
+ overall_losses, overall_win_pct))
+
+ # sort the resulting list by # of games played
+ games_played = sorted(games_played, key=lambda x:x.games)
+ games_played.reverse()
+ return games_played
+
+
+def get_overall_stats(player_id):
+ """
+ Provides a breakdown of stats by gametype played by player_id.
+
+ Returns a dictionary of namedtuples with the following members:
+ - total_kills
+ - total_deaths
+ - k_d_ratio
+ - last_played (last time the player played the game type)
+ - last_played_epoch (same as above, but in seconds since epoch)
+ - last_played_fuzzy (same as above, but in relative date)
+ - total_playing_time (total amount of time played the game type)
+ - total_pickups (ctf only)
+ - total_captures (ctf only)
+ - cap_ratio (ctf only)
+ - total_carrier_frags (ctf only)
+ - game_type_cd
+
+ The key to the dictionary is the game type code. There is also an
+ "overall" game_type_cd which sums the totals and computes the total ratios.
+ """
+ OverallStats = namedtuple('OverallStats', ['total_kills', 'total_deaths',
+ 'k_d_ratio', 'last_played', 'last_played_epoch', 'last_played_fuzzy',
+ 'total_playing_time', 'total_pickups', 'total_captures', 'cap_ratio',
+ 'total_carrier_frags', 'game_type_cd'])
+
+ raw_stats = DBSession.query('game_type_cd', 'total_kills',
+ 'total_deaths', 'last_played', 'total_playing_time',
+ 'total_pickups', 'total_captures', 'total_carrier_frags').\
+ from_statement(
+ "SELECT g.game_type_cd, "
+ "Sum(pgs.kills) total_kills, "
+ "Sum(pgs.deaths) total_deaths, "
+ "Max(pgs.create_dt) last_played, "
+ "Sum(pgs.alivetime) total_playing_time, "
+ "Sum(pgs.pickups) total_pickups, "
+ "Sum(pgs.captures) total_captures, "
+ "Sum(pgs.carrier_frags) total_carrier_frags "
+ "FROM games g, "
+ "player_game_stats pgs "
+ "WHERE g.game_id = pgs.game_id "
+ "AND pgs.player_id = :player_id "
+ "GROUP BY g.game_type_cd "
+ ).params(player_id=player_id).all()
+
+ # to be indexed by game_type_cd
+ overall_stats = {}
+
+ # sums for the "overall" game type (which is fake)
+ overall_kills = 0
+ overall_deaths = 0
+ overall_last_played = None
+ overall_playing_time = datetime.timedelta(seconds=0)
+ overall_carrier_frags = 0
+
+ for row in raw_stats:
+ # running totals or mins
+ overall_kills += row.total_kills or 0
+ overall_deaths += row.total_deaths or 0
+
+ if overall_last_played is None or row.last_played > overall_last_played:
+ overall_last_played = row.last_played
+
+ overall_playing_time += row.total_playing_time
+
+ # individual gametype ratio calculations
+ try:
+ k_d_ratio = float(row.total_kills)/row.total_deaths
+ except:
+ k_d_ratio = None