]> de.git.xonotic.org Git - xonotic/xonstat.git/blobdiff - xonstat/views/player.py
Return actual data in the /map/<map id> JSON views.
[xonotic/xonstat.git] / xonstat / views / player.py
index 7668774501756d054330a8920fce623aa6d168fc..2fb716c226d97071bc67f715b84e9477cb711609 100644 (file)
@@ -1,17 +1,20 @@
 import datetime
 import logging
+from calendar import timegm
+from collections import namedtuple
+from urllib import unquote
+
 import pyramid.httpexceptions
 import sqlalchemy as sa
-import sqlalchemy.sql.functions as func
 import sqlalchemy.sql.expression as expr
-from calendar import timegm
-from collections import namedtuple
+import sqlalchemy.sql.functions as func
 from webhelpers.paginate import Page
-from xonstat.models import *
-from xonstat.util import page_url, to_json, pretty_date, datetime_seconds
+from xonstat.models import DBSession, Server, Map, Game, PlayerWeaponStat, Player, Hashkey
+from xonstat.models import PlayerElo, PlayerCaptime, PlayerMedal, GameType
+from xonstat.models.player import PlayerCapTime
 from xonstat.util import is_cake_day, verify_request
+from xonstat.util import page_url, to_json, pretty_date, datetime_seconds
 from xonstat.views.helpers import RecentGame, recent_games_q
-from urllib import unquote
 
 log = logging.getLogger(__name__)
 
@@ -101,15 +104,20 @@ def get_games_played(player_id):
     for row in raw_games_played:
         games = row.wins + row.losses
         overall_games += games
-        overall_wins += row.wins
-        overall_losses += row.losses
-        win_pct = float(row.wins)/games * 100
+
+        # DM, CTS, and KA don't really have "winners"
+        if row.game_type_cd in ["dm", "cts", "ka"]:
+            win_pct = None
+        else:
+            overall_wins += row.wins
+            overall_losses += row.losses
+            win_pct = float(row.wins)/games * 100
 
         games_played.append(GamesPlayed(row.game_type_cd, games, row.wins,
             row.losses, win_pct))
 
     try:
-        overall_win_pct = float(overall_wins)/overall_games * 100
+        overall_win_pct = float(overall_wins)/(overall_wins + overall_losses) * 100
     except:
         overall_win_pct = 0.0
 
@@ -494,6 +502,23 @@ def get_damage_stats(player_id, weapon_cd, games):
     return (avg, dmgs)
 
 
+def get_player_medals(player_id):
+    """Retrieves the list of medals the player has received from tournaments or
+    other contests."""
+    try:
+        medals = DBSession.query(PlayerMedal)\
+                .filter(PlayerMedal.player_id==player_id)\
+                .order_by(PlayerMedal.place)\
+                .order_by(PlayerMedal.create_dt)\
+                .all()
+
+        return medals
+
+    except Exception as e:
+        log.debug(e)
+        return []
+
+
 def player_info_data(request):
     player_id = int(request.matchdict['id'])
     if player_id <= 2:
@@ -507,7 +532,8 @@ def player_info_data(request):
         overall_stats  = get_overall_stats(player_id)
         fav_maps       = get_fav_maps(player_id)
         elos           = get_elos(player_id)
-        ranks          = get_ranks(player_id)
+        ranks          = {}
+        medals         = get_player_medals(player_id)
         recent_games   = get_recent_games(player_id)
         cake_day       = is_cake_day(player.create_dt)
 
@@ -523,6 +549,7 @@ def player_info_data(request):
             'fav_maps':fav_maps,
             'elos':elos,
             'ranks':ranks,
+            'medals':medals,
             'recent_games':recent_games,
             'cake_day':cake_day,
             }
@@ -789,7 +816,7 @@ def player_hashkey_info_data(request):
         overall_stats     = get_overall_stats(player.player_id)
         fav_maps          = get_fav_maps(player.player_id)
         elos              = get_elos(player.player_id)
-        ranks             = get_ranks(player.player_id)
+        ranks             = {}
         most_recent_game  = get_recent_games(player.player_id, 1)[0]
 
     except Exception as e:
@@ -1053,7 +1080,7 @@ def player_weaponstats_data_json(request):
             limit = 50
 
 
-    # the game_ids of the most recently played ones 
+    # the game_ids of the most recently played ones
     # of the given game type is used for a subquery
     games_list = DBSession.query(Game.game_id).\
             filter(Game.players.contains([player_id]))
@@ -1081,7 +1108,7 @@ def player_weaponstats_data_json(request):
         sum_avgs[ws.weapon_cd] = sum_avgs.get(ws.weapon_cd, 0) + float(ws.hit)/float(ws.fired)
 
     # Creating zero-valued weapon stat entries for games where a weapon was not
-    # used in that game, but was used in another game for the set. This makes 
+    # used in that game, but was used in another game for the set. This makes
     # the charts look smoother
     for game_id in games_to_weapons.keys():
         for weapon_cd in set(weapons_used.keys()) - set(games_to_weapons[game_id]):
@@ -1131,19 +1158,19 @@ def player_versus_data(request):
         # note that wins and losses are from p1's perspective
         win_loss_sql = """select win_loss, count(1)
             from (
-              select case 
-                when pgsp1.score >= pgsp2.score then 'win' 
-                else 'loss' 
+              select case
+                when pgsp1.score >= pgsp2.score then 'win'
+                else 'loss'
               end win_loss
-              from games g join player_game_stats pgsp1 
+              from games g join player_game_stats pgsp1
                 on g.game_id = pgsp1.game_id and pgsp1.player_id = :p1
-              join player_game_stats pgsp2 
+              join player_game_stats pgsp2
                 on g.game_id = pgsp2.game_id and pgsp2.player_id = :p2
               where g.players @> ARRAY[:p1,:p2]
               and g.game_type_cd = 'duel'
-              and pgsp1.create_dt between g.create_dt - interval '1 hour' 
+              and pgsp1.create_dt between g.create_dt - interval '1 hour'
                 and g.create_dt + interval '1 hour'
-              and pgsp2.create_dt between g.create_dt - interval '1 hour' 
+              and pgsp2.create_dt between g.create_dt - interval '1 hour'
                 and g.create_dt + interval '1 hour'
             ) wl
             group by win_loss
@@ -1160,16 +1187,25 @@ def player_versus_data(request):
                 p2_wins = row.count
 
         # grab the 20 most recent games between the two
-        rgs_raw = recent_games_q(player_id=p1_id, player_id_2=p2_id, 
+        rgs_raw = recent_games_q(player_id=p1_id, player_id_2=p2_id,
                 game_type_cd="duel").limit(20).all()
 
         rgs = [RecentGame(row) for row in rgs_raw]
 
     except Exception as e:
         log.debug(e)
-        raise pyramid.httpexceptions.HTTPNotFound
+
+        p1_id = None
+        p2_id = None
+        p1 = None
+        p2 = None
+        p1_wins = None
+        p2_wins = None
+        rgs = None
 
     return {
+            "p1_id" : p1_id,
+            "p2_id" : p2_id,
             "p1" : p1,
             "p2" : p2,
             "p1_wins" : p1_wins,