return recent_weapons
-def _get_games_played(player_id):
- """
- Provides a breakdown by gametype of the games played by player_id.
-
- Returns a tuple containing (total_games, games_breakdown), where
- total_games is the absolute number of games played by player_id
- and games_breakdown is an array containing (game_type_cd, # games)
- """
- games_played = DBSession.query(Game.game_type_cd, func.count()).\
- filter(Game.game_id == PlayerGameStat.game_id).\
- filter(PlayerGameStat.player_id == player_id).\
- group_by(Game.game_type_cd).\
- order_by(func.count().desc()).all()
-
- total = 0
- for (game_type_cd, games) in games_played:
- total += games
-
- return (total, games_played)
-
-
-# TODO: should probably factor the above function into this one such that
-# total_stats['ctf_games'] is the count of CTF games and so on...
-def _get_total_stats(player_id):
- """
- Provides aggregated stats by player_id.
-
- Returns a dict with the keys 'kills', 'deaths', 'alivetime'.
-
- kills = how many kills a player has over all games
- deaths = how many deaths a player has over all games
- alivetime = how long a player has played over all games
-
- If any of the above are None, they are set to 0.
- """
- total_stats = {}
- (total_stats['kills'], total_stats['deaths'], total_stats['alivetime']) = DBSession.\
- query("total_kills", "total_deaths", "total_alivetime").\
- from_statement(
- "select sum(kills) total_kills, "
- "sum(deaths) total_deaths, "
- "sum(alivetime) total_alivetime "
- "from player_game_stats "
- "where player_id=:player_id"
- ).params(player_id=player_id).one()
-
- (total_stats['wins'],) = DBSession.\
- query("total_wins").\
- from_statement(
- "select count(*) total_wins "
- "from games g, player_game_stats pgs "
- "where g.game_id = pgs.game_id "
- "and player_id=:player_id "
- "and (g.winner = pgs.team or pgs.rank = 1)"
- ).params(player_id=player_id).one()
-
- for (key,value) in total_stats.items():
- if value == None:
- total_stats[key] = 0
-
- return total_stats
-
-
-def _get_fav_maps(player_id):
- """
- Get the player's favorite map. The favorite map is defined
- as the map that he or she has played the most with game
- types considered separate. This is to say that if a person
- plays dm and duel on stormkeep with 25 games in each mode,
- final_rage could still be the favorite map overall if it has
- 26 dm games.
-
- Returns a dictionary with entries for each played game type.
- Each game type ditionary value contained a nested dictionary
- with the following keys:
- id = the favorite map id
- name = the favorite map's name
- times_played = the number of times the map was played in that mode
-
- Note also that there's a superficial "overall" game type that is
- meant to hold the top map overall. It'll be a dupe of one of the
- other game types' nested dictionary.
- """
- fav_maps = {}
- for (game_type_cd, name, map_id, times_played) in DBSession.\
- query("game_type_cd", "name", "map_id", "times_played").\
- from_statement(
- "SELECT game_type_cd, "
- "name, "
- "map_id, "
- "times_played "
- "FROM (SELECT g.game_type_cd, "
- "m.name, "
- "m.map_id, "
- "count(*) times_played, "
- "row_number() "
- "over ( "
- "PARTITION BY g.game_type_cd "
- "ORDER BY Count(*) DESC, m.map_id ASC) rank "
- "FROM games g, "
- "player_game_stats pgs, "
- "maps m "
- "WHERE g.game_id = pgs.game_id "
- "AND g.map_id = m.map_id "
- "AND pgs.player_id = :player_id "
- "GROUP BY g.game_type_cd, "
- "m.map_id, "
- "m.name) most_played "
- "WHERE rank = 1"
- ).params(player_id=player_id).all():
- fav_map_detail = {}
- fav_map_detail['name'] = name
- fav_map_detail['map_id'] = map_id
- fav_map_detail['times_played'] = times_played
- fav_maps[game_type_cd] = fav_map_detail
-
- max_played = 0
- overall = {}
- for fav_map_detail in fav_maps.values():
- if fav_map_detail['times_played'] > max_played:
- max_played = fav_map_detail['times_played']
- overall = fav_map_detail
-
- fav_maps['overall'] = overall
-
- return fav_maps
-
-
-def _get_rank(player_id):
- """
- Get the player's rank as well as the total number of ranks.
- """
- rank = DBSession.query("game_type_cd", "rank", "max_rank").\
- from_statement(
- "select pr.game_type_cd, pr.rank, overall.max_rank "
- "from player_ranks pr, "
- "(select game_type_cd, max(rank) max_rank "
- "from player_ranks "
- "group by game_type_cd) overall "
- "where pr.game_type_cd = overall.game_type_cd "
- "and player_id = :player_id "
- "order by rank").\
- params(player_id=player_id).all()
-
- return rank;
-
-
def get_accuracy_stats(player_id, weapon_cd, games):
"""
Provides accuracy for weapon_cd by player_id for the past N games.