+ 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