+ ranks = {}
+ found_top_rank = False
+ for row in raw_ranks:
+ rank = Rank(rank=row.rank,
+ max_rank=row.max_rank,
+ percentile=100 - 100*float(row.rank)/row.max_rank,
+ game_type_cd=row.game_type_cd)
+
+
+ if not found_top_rank:
+ ranks['overall'] = rank
+ found_top_rank = True
+ elif rank.percentile > ranks['overall'].percentile:
+ ranks['overall'] = rank
+
+ ranks[row.game_type_cd] = rank
+
+ return ranks;
+
+
+def get_elos(player_id):
+ """
+ Provides a breakdown of the player's elos by game type.
+
+ Returns a dictionary of namedtuples with the following members:
+ - player_id
+ - game_type_cd
+ - games
+ - elo
+
+ The key to the dictionary is the game type code. There is also an
+ "overall" game_type_cd which is the overall best rank.
+ """
+ raw_elos = DBSession.query(PlayerElo).filter_by(player_id=player_id).\
+ order_by(PlayerElo.elo.desc()).all()
+
+ elos = {}
+ found_max_elo = False
+ for row in raw_elos:
+ if not found_max_elo:
+ elos['overall'] = row
+ found_max_elo = True
+
+ elos[row.game_type_cd] = row
+
+ return elos
+
+
+def get_recent_games(player_id):
+ """
+ Provides a list of recent games.
+
+ Returns the full PlayerGameStat, Game, Server, Map
+ objects for all recent games.
+ """
+ RecentGame = namedtuple('RecentGame', ['player_stats', 'game', 'server', 'map'])
+
+ # recent games table, all data
+ recent_games = DBSession.query(PlayerGameStat, Game, Server, Map).\
+ filter(PlayerGameStat.player_id == player_id).\
+ filter(PlayerGameStat.game_id == Game.game_id).\
+ filter(Game.server_id == Server.server_id).\
+ filter(Game.map_id == Map.map_id).\
+ order_by(Game.game_id.desc())[0:10]
+
+ return [
+ RecentGame(player_stats=row.PlayerGameStat,
+ game=row.Game,
+ server=row.Server,
+ map=row.Map)
+ for row in recent_games ]
+
+
+def get_recent_weapons(player_id):
+ """
+ Returns the weapons that have been used in the past 90 days
+ and also used in 5 games or more.
+ """
+ cutoff = datetime.datetime.utcnow() - datetime.timedelta(days=90)
+ recent_weapons = []
+ for weapon in DBSession.query(PlayerWeaponStat.weapon_cd, func.count()).\
+ filter(PlayerWeaponStat.player_id == player_id).\
+ filter(PlayerWeaponStat.create_dt > cutoff).\
+ group_by(PlayerWeaponStat.weapon_cd).\
+ having(func.count() > 4).\
+ all():
+ recent_weapons.append(weapon[0])
+
+ return recent_weapons