]> de.git.xonotic.org Git - xonotic/xonstat.git/blobdiff - xonstat/views/player.py
Implemented some (preliminary) JSON API to retrieve player data
[xonotic/xonstat.git] / xonstat / views / player.py
index 02e3cd3360a9b5437f29c470e0f03d615ae0b2c5..a783785324bf8a2ab021503717152b192194caa4 100644 (file)
@@ -10,12 +10,12 @@ from pyramid.url import current_route_url
 from sqlalchemy import desc, distinct
 from webhelpers.paginate import Page, PageURL
 from xonstat.models import *
-from xonstat.util import page_url
+from xonstat.util import page_url, datetime_seconds
 
 log = logging.getLogger(__name__)
 
 
-def _player_index_data(request):
+def player_index_data(request):
     if request.params.has_key('page'):
         current_page = request.params['page']
     else:
@@ -42,11 +42,20 @@ def player_index(request):
     """
     Provides a list of all the current players.
     """
-    return _player_index_data(request)
+    return player_index_data(request)
+
+
+def player_index_json(request):
+    """
+    Provides a list of all the current players. JSON.
+    """
+    return [{'status':'not implemented'}]
 
 
 def _get_games_played(player_id):
     """
+    DEPRECATED: Now included in _get_total_stats()
+    
     Provides a breakdown by gametype of the games played by player_id.
 
     Returns a tuple containing (total_games, games_breakdown), where
@@ -66,30 +75,68 @@ def _get_games_played(player_id):
     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'.
 
+    games = how many games a player has played
+    games_breakdown = how many games of given type a player has played (dictionary)
+    games_alivetime = how many time a player has spent in a give game type (dictionary)
     kills = how many kills a player has over all games
     deaths = how many deaths a player has over all games
+    suicides = how many suicides a player has over all games
     alivetime = how long a player has played over all games
+    alivetime_week = how long a player has played over all games in the last week
+    alivetime_month = how long a player has played over all games in the last month
+    wins = how many games a player has won
 
     If any of the above are None, they are set to 0.
     """
+    # 7 and 30 day windows
+    one_week_ago  = datetime.datetime.utcnow() - datetime.timedelta(days=7)
+    one_month_ago = datetime.datetime.utcnow() - datetime.timedelta(days=30)
+
     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()
+
+    games_played = DBSession.query(
+            Game.game_type_cd, func.count(), func.sum(PlayerGameStat.alivetime)).\
+            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_stats['games'] = 0
+    total_stats['games_breakdown'] = {}  # this is a dictionary inside a dictionary .. dictception?
+    total_stats['games_alivetime'] = {}
+    for (game_type_cd, games, alivetime) in games_played:
+        total_stats['games'] += games
+        total_stats['games_breakdown'][game_type_cd] = games
+        total_stats['games_alivetime'][game_type_cd] = alivetime
+
+     # more fields can be added here, e.g. 'collects' for kh games
+    (total_stats['kills'], total_stats['deaths'], total_stats['suicides'],
+     total_stats['alivetime'],) = DBSession.query(
+            func.sum(PlayerGameStat.kills),
+            func.sum(PlayerGameStat.deaths),
+            func.sum(PlayerGameStat.suicides),
+            func.sum(PlayerGameStat.alivetime)).\
+            filter(PlayerGameStat.player_id == player_id).\
+            one()
+
+    (total_stats['alivetime_week'],) = DBSession.query(
+            func.sum(PlayerGameStat.alivetime)).\
+            filter(PlayerGameStat.player_id == player_id).\
+            filter(PlayerGameStat.create_dt > one_week_ago).\
+            one()
+
+    (total_stats['alivetime_month'],) = DBSession.query(
+            func.sum(PlayerGameStat.alivetime)).\
+            filter(PlayerGameStat.player_id == player_id).\
+            filter(PlayerGameStat.create_dt > one_month_ago).\
+            one()
 
     (total_stats['wins'],) = DBSession.\
             query("total_wins").\
@@ -101,6 +148,84 @@ def _get_total_stats(player_id):
                 "and (g.winner = pgs.team or pgs.rank = 1)"
             ).params(player_id=player_id).one()
 
+#    (total_stats['wins'],) = DBSession.query(
+#            func.count("*")).\
+#            filter(Game.game_id == PlayerGameStat.game_id).\
+#            filter(PlayerGameStat.player_id == player_id).\
+#            filter(Game.winner == PlayerGameStat.team or PlayerGameStat.rank == 1).\
+#            one()
+
+    (total_stats['duel_wins'],) = DBSession.query(
+            func.count("*")).\
+            filter(Game.game_id == PlayerGameStat.game_id).\
+            filter(Game.game_type_cd == "duel").\
+            filter(PlayerGameStat.player_id == player_id).\
+            filter(PlayerGameStat.rank == 1).\
+            one()
+
+    (total_stats['duel_kills'], total_stats['duel_deaths'], total_stats['duel_suicides'],) = DBSession.query(
+            func.sum(PlayerGameStat.kills),
+            func.sum(PlayerGameStat.deaths),
+            func.sum(PlayerGameStat.suicides)).\
+            filter(Game.game_id == PlayerGameStat.game_id).\
+            filter(Game.game_type_cd == "duel").\
+            filter(PlayerGameStat.player_id == player_id).\
+            one()
+
+    (total_stats['dm_wins'],) = DBSession.query(
+            func.count("*")).\
+            filter(Game.game_id == PlayerGameStat.game_id).\
+            filter(Game.game_type_cd == "dm").\
+            filter(PlayerGameStat.player_id == player_id).\
+            filter(PlayerGameStat.rank == 1).\
+            one()
+
+    (total_stats['dm_kills'], total_stats['dm_deaths'], total_stats['dm_suicides'],) = DBSession.query(
+            func.sum(PlayerGameStat.kills),
+            func.sum(PlayerGameStat.deaths),
+            func.sum(PlayerGameStat.suicides)).\
+            filter(Game.game_id == PlayerGameStat.game_id).\
+            filter(Game.game_type_cd == "dm").\
+            filter(PlayerGameStat.player_id == player_id).\
+            one()
+
+    (total_stats['tdm_kills'], total_stats['tdm_deaths'], total_stats['tdm_suicides'],) = DBSession.query(
+            func.sum(PlayerGameStat.kills),
+            func.sum(PlayerGameStat.deaths),
+            func.sum(PlayerGameStat.suicides)).\
+            filter(Game.game_id == PlayerGameStat.game_id).\
+            filter(Game.game_type_cd == "tdm").\
+            filter(PlayerGameStat.player_id == player_id).\
+            one()
+
+    (total_stats['tdm_wins'],) = DBSession.query(
+            func.count("*")).\
+            filter(Game.game_id == PlayerGameStat.game_id).\
+            filter(Game.game_type_cd == "tdm").\
+            filter(PlayerGameStat.player_id == player_id).\
+            filter(PlayerGameStat.rank == 1).\
+            one()
+
+    (total_stats['ctf_wins'],) = DBSession.query(
+            func.count("*")).\
+            filter(Game.game_id == PlayerGameStat.game_id).\
+            filter(Game.game_type_cd == "ctf").\
+            filter(PlayerGameStat.player_id == player_id).\
+            filter(PlayerGameStat.rank == 1).\
+            one()
+
+    (total_stats['ctf_caps'], total_stats['ctf_pickups'], total_stats['ctf_drops'],
+     total_stats['ctf_returns'], total_stats['ctf_fckills'],) = DBSession.query(
+            func.sum(PlayerGameStat.captures),
+            func.sum(PlayerGameStat.pickups),
+            func.sum(PlayerGameStat.drops),
+            func.sum(PlayerGameStat.returns),
+            func.sum(PlayerGameStat.carrier_frags)).\
+            filter(Game.game_id == PlayerGameStat.game_id).\
+            filter(Game.game_type_cd == "ctf").\
+            filter(PlayerGameStat.player_id == player_id).\
+            one()
+
     for (key,value) in total_stats.items():
         if value == None:
             total_stats[key] = 0
@@ -108,6 +233,117 @@ def _get_total_stats(player_id):
     return total_stats
 
 
+def _get_fav_map(player_id):
+    """
+    Get the player's favorite map. The favorite map is defined
+    as the map that he or she has played the most in the past 
+    90 days.
+
+    Returns a dictionary with keys for the map's name and id.
+    """
+    # 90 day window
+    back_then = datetime.datetime.utcnow() - datetime.timedelta(days=90)
+
+    raw_fav_map = DBSession.query(Map.name, Map.map_id).\
+            filter(Game.game_id == PlayerGameStat.game_id).\
+            filter(Game.map_id == Map.map_id).\
+            filter(PlayerGameStat.player_id == player_id).\
+            filter(PlayerGameStat.create_dt > back_then).\
+            group_by(Map.name, Map.map_id).\
+            order_by(func.count().desc()).\
+            limit(5).all()
+
+    fav_map = []
+    for map_e in raw_fav_map:
+        entry = {}
+        entry['name'] = map_e[0]
+        entry['id']   = map_e[1]
+        fav_map.append(entry)
+
+    return fav_map
+
+
+def _get_fav_weapon(player_id):
+    """
+    Get the player's favorite weapon. The favorite weapon is defined
+    as the weapon that he or she has employed the most in the past 
+    90 days.
+
+    Returns a sequence of dictionaries with keys for the weapon's name and id.
+    The sequence holds the most-used weapons in decreasing order.
+    """
+    # 90 day window
+    back_then = datetime.datetime.utcnow() - datetime.timedelta(days=90)
+
+    raw_fav_weapon = DBSession.query(Weapon.descr, Weapon.weapon_cd).\
+            filter(Game.game_id == PlayerWeaponStat.game_id).\
+            filter(PlayerWeaponStat.player_id == player_id).\
+            filter(PlayerWeaponStat.weapon_cd == Weapon.weapon_cd).\
+            filter(PlayerWeaponStat.create_dt > back_then).\
+            group_by(Weapon.descr, Weapon.weapon_cd).\
+            order_by(func.count().desc()).\
+            limit(5).all()
+
+    fav_weapon = []
+    for wpn_e in raw_fav_weapon:
+        entry = {}
+        entry['name'] = wpn_e[0]
+        entry['id']   = wpn_e[1]
+        fav_weapon.append(entry)
+
+    return fav_weapon
+
+
+def _get_fav_server(player_id):
+    """
+    Get the player's favorite server. The favorite server is defined
+    as the server that he or she has played on the most in the past 
+    90 days.
+
+    Returns a sequence of dictionaries with keys for the server's name and id.
+    The sequence holds the most-used servers in decreasing order.
+    """
+    # 90 day window
+    back_then = datetime.datetime.utcnow() - datetime.timedelta(days=90)
+
+    raw_fav_server = DBSession.query(Server.name, Server.server_id).\
+            filter(Game.game_id == PlayerGameStat.game_id).\
+            filter(Game.server_id == Server.server_id).\
+            filter(PlayerGameStat.player_id == player_id).\
+            filter(PlayerGameStat.create_dt > back_then).\
+            group_by(Server.name, Server.server_id).\
+            order_by(func.count().desc()).\
+            limit(5).all()
+
+    fav_server = []
+    for srv_e in raw_fav_server:
+        entry = {}
+        entry['name'] = srv_e[0]
+        entry['id']   = srv_e[1]
+        fav_server.append(entry)
+
+    return fav_server
+
+
+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.
@@ -147,7 +383,49 @@ def get_accuracy_stats(player_id, weapon_cd, games):
     return (avg, accs)
 
 
-def _player_info_data(request):
+def get_damage_stats(player_id, weapon_cd, games):
+    """
+    Provides damage info for weapon_cd by player_id for the past N games.
+    """
+    try:
+        raw_avg = DBSession.query(func.sum(PlayerWeaponStat.actual),
+                func.sum(PlayerWeaponStat.hit)).\
+                filter(PlayerWeaponStat.player_id == player_id).\
+                filter(PlayerWeaponStat.weapon_cd == weapon_cd).\
+                one()
+
+        avg = round(float(raw_avg[0])/raw_avg[1], 2)
+
+        # Determine the damage efficiency (hit, fired) numbers for $games games
+        # This is then enumerated to create parameters for a flot graph
+        raw_dmgs = DBSession.query(PlayerWeaponStat.game_id, 
+            PlayerWeaponStat.actual, PlayerWeaponStat.hit).\
+                filter(PlayerWeaponStat.player_id == player_id).\
+                filter(PlayerWeaponStat.weapon_cd == weapon_cd).\
+                order_by(PlayerWeaponStat.game_id.desc()).\
+                limit(games).\
+                all()
+
+        # they come out in opposite order, so flip them in the right direction
+        raw_dmgs.reverse()
+
+        dmgs = []
+        for i in range(len(raw_dmgs)):
+            # try to derive, unless we've hit nothing then set to 0!
+            try:
+                dmg = round(float(raw_dmgs[i][1])/raw_dmgs[i][2], 2)
+            except:
+                dmg = 0.0
+
+            dmgs.append((raw_dmgs[i][0], dmg))
+    except Exception as e:
+        dmgs = []
+        avg = 0.0
+
+    return (avg, dmgs)
+
+
+def player_info_data(request):
     player_id = int(request.matchdict['id'])
     if player_id <= 2:
         player_id = -1;
@@ -160,8 +438,26 @@ def _player_info_data(request):
         total_stats = _get_total_stats(player.player_id)
 
         # games breakdown - N games played (X ctf, Y dm) etc
-        (total_games, games_breakdown) = _get_games_played(player.player_id)
+        # DEPRECATED: included in total_stats, see above
+        # (total_games, games_breakdown) = _get_games_played(player.player_id)
 
+        # favorite map from the past 90 days
+        try:
+            fav_map = _get_fav_map(player.player_id)
+        except:
+            fav_map = None
+
+        # favorite weapon from the past 90 days
+        try:
+            fav_weapon = _get_fav_weapon(player.player_id)
+        except:
+            fav_weapon = None
+
+        # favorite server from the past 90 days
+        try:
+            fav_server = _get_fav_server(player.player_id)
+        except:
+            fav_server = None
 
         # friendly display of elo information and preliminary status
         elos = DBSession.query(PlayerElo).filter_by(player_id=player_id).\
@@ -169,6 +465,7 @@ def _player_info_data(request):
                 order_by(PlayerElo.elo.desc()).all()
 
         elos_display = []
+        elos_dict    = {}
         for elo in elos:
             if elo.games > 32:
                 str = "{0} ({1})"
@@ -177,6 +474,19 @@ def _player_info_data(request):
 
             elos_display.append(str.format(round(elo.elo, 3),
                 elo.game_type_cd))
+            elos_dict[elo.game_type_cd] = round(elo.elo, 3)
+        elos_display = ', '.join(elos_display)
+
+        # get current rank information
+        ranks = _get_rank(player_id)
+        
+        ranks_display = []
+        ranks_dict    = {}
+        for gtc,rank,max_rank in ranks:
+            ranks_display.append("{1} of {2} ({0})".format(gtc, rank, max_rank))
+            ranks_dict[gtc] = (rank, max_rank)
+        ranks_display = ', '.join(ranks_display)
+
 
         # which weapons have been used in the past 90 days
         # and also, used in 5 games or more?
@@ -200,20 +510,34 @@ def _player_info_data(request):
 
     except Exception as e:
         player = None
+        elos = None
         elos_display = None
         total_stats = None
         recent_games = None
-        total_games = None
-        games_breakdown = None
+        # DEPRECATED: included in total_stats, see above
+        #total_games = None
+        #games_breakdown = None
         recent_weapons = []
+        fav_map = None
+        fav_weapon = None
+        fav_server = None
+        ranks = None
+        ranks_display = None;
 
     return {'player':player,
+            'elos':elos_dict,
             'elos_display':elos_display,
             'recent_games':recent_games,
             'total_stats':total_stats,
-            'total_games':total_games,
-            'games_breakdown':games_breakdown,
+            # DEPRECATED: included in total_stats, see above
+            #'total_games':total_games,
+            #'games_breakdown':games_breakdown,
             'recent_weapons':recent_weapons,
+            'fav_map':fav_map,
+            'fav_weapon':fav_weapon,
+            'fav_server':fav_server,
+            'ranks':ranks_dict,
+            'ranks_display':ranks_display,
             }
 
 
@@ -221,10 +545,54 @@ def player_info(request):
     """
     Provides detailed information on a specific player
     """
-    return _player_info_data(request)
+    return player_info_data(request)
 
 
-def _player_game_index_data(request):
+def player_info_json(request):
+    """
+    Provides detailed information on a specific player. JSON.
+    """
+    player_info = player_info_data(request)
+    json_result = {
+        'player': {
+            'id':               player_info['player'].player_id,
+            'nick':             player_info['player'].nick.encode('utf-8'),
+            'stripped_nick':    player_info['player'].nick_strip_colors(),
+            'joined':           player_info['player'].create_dt.isoformat(),
+            },
+        'elos':         player_info['elos'],
+        'ranks':        player_info['ranks'],
+        'total_stats':  {
+                'games':            player_info['total_stats']['games'],
+                'games_breakdown':  player_info['total_stats']['games_breakdown'],
+                'alivetime':        datetime_seconds(player_info['total_stats']['alivetime']),
+                'kills':            player_info['total_stats']['kills'],
+                'deaths':           player_info['total_stats']['deaths'],
+                'suicides':         player_info['total_stats']['suicides'],
+                'wins':             player_info['total_stats']['wins'],
+                # FIXME - current "wins" query is flawed!
+                #'losses':           player_info['total_stats']['loses'],
+            },
+        'recent_games': [
+                {
+                    'game_id':      game.game_id,
+                    'game_type':    game.game_type_cd,
+                    'server':       server.name,
+                    'map':          map.name,
+                    'team':         gamestat.team,
+                    'rank':         gamestat.rank,
+                    'win':          ((gamestat.team != None and gamestat.team == game.winner)
+                                        or (gamestat.team == None and gamestat.rank == 1)),
+                    'time':         game.create_dt.isoformat(),
+                }
+                for (gamestat, game, server, map) in player_info['recent_games'][:5]
+            ],
+        }
+    print json_result
+    return json_result
+
+
+def player_game_index_data(request):
     player_id = request.matchdict['player_id']
 
     if request.params.has_key('page'):
@@ -264,10 +632,19 @@ def player_game_index(request):
     player was involved. This is ordered by game_id, with
     the most recent game_ids first. Paginated.
     """
-    return _player_game_index_data(request)
+    return player_game_index_data(request)
 
 
-def _player_accuracy_data(request):
+def player_game_index_json(request):
+    """
+    Provides an index of the games in which a particular
+    player was involved. This is ordered by game_id, with
+    the most recent game_ids first. Paginated. JSON.
+    """
+    return [{'status':'not implemented'}]
+
+
+def player_accuracy_data(request):
     player_id = request.matchdict['id']
     allowed_weapons = ['nex', 'rifle', 'shotgun', 'uzi', 'minstanex']
     weapon_cd = 'nex'
@@ -304,6 +681,13 @@ def _player_accuracy_data(request):
             }
 
 
+def player_accuracy(request):
+    """
+    Provides the accuracy for the given weapon. (JSON only)
+    """
+    return player_accuracy_data(request)
+
+
 def player_accuracy_json(request):
     """
     Provides a JSON response representing the accuracy for the given weapon.
@@ -313,4 +697,55 @@ def player_accuracy_json(request):
                 'shotgun', 'uzi', and 'minstanex'.
        games = over how many games to display accuracy. Can be up to 50.
     """
-    return _player_accuracy_data(request)
+    return player_accuracy_data(request)
+
+
+def player_damage_data(request):
+    player_id = request.matchdict['id']
+    allowed_weapons = ['grenadelauncher', 'electro', 'crylink', 'hagar',
+            'rocketlauncher', 'laser']
+    weapon_cd = 'rocketlauncher'
+    games = 20
+
+    if request.params.has_key('weapon'):
+        if request.params['weapon'] in allowed_weapons:
+            weapon_cd = request.params['weapon']
+
+    if request.params.has_key('games'):
+        try:
+            games = request.params['games']
+
+            if games < 0:
+                games = 20
+            if games > 50:
+                games = 50
+        except:
+            games = 20
+
+    (avg, dmgs) = get_damage_stats(player_id, weapon_cd, games)
+
+    # if we don't have enough data for the given weapon
+    if len(dmgs) < games:
+        games = len(dmgs)
+
+    return {
+            'player_id':player_id, 
+            'player_url':request.route_url('player_info', id=player_id), 
+            'weapon':weapon_cd, 
+            'games':games, 
+            'avg':avg, 
+            'dmgs':dmgs
+            }
+
+
+def player_damage_json(request):
+    """
+    Provides a JSON response representing the damage for the given weapon.
+
+    Parameters:
+       weapon = which weapon to display damage for. Valid values are
+         'grenadelauncher', 'electro', 'crylink', 'hagar', 'rocketlauncher',
+         'laser'.
+       games = over how many games to display damage. Can be up to 50.
+    """
+    return player_damage_data(request)