]> de.git.xonotic.org Git - xonotic/xonstat.git/blob - xonstat/views/player.py
8f8b4d6eb67c28ac8e656e742cf180e3dfb9a136
[xonotic/xonstat.git] / xonstat / views / player.py
1 import datetime\r
2 import logging\r
3 import re\r
4 import time\r
5 from pyramid.response import Response\r
6 from sqlalchemy import desc\r
7 from webhelpers.paginate import Page, PageURL\r
8 from xonstat.models import *\r
9 from xonstat.util import page_url\r
10 \r
11 log = logging.getLogger(__name__)\r
12 \r
13 \r
14 def player_index(request):\r
15     """\r
16     Provides a list of all the current players. \r
17     """\r
18     players = DBSession.query(Player)\r
19 \r
20     return {'players':players}\r
21 \r
22 def player_info(request):\r
23     """\r
24     Provides detailed information on a specific player\r
25     """\r
26     player_id = request.matchdict['id']\r
27     try:\r
28         player = DBSession.query(Player).filter_by(player_id=player_id).one()\r
29 \r
30         weapon_stats = DBSession.query("descr", "weapon_cd", "actual_total", \r
31                 "max_total", "hit_total", "fired_total", "frags_total").\\r
32                 from_statement(\r
33                     "select cw.descr, cw.weapon_cd, sum(actual) actual_total, "\r
34                     "sum(max) max_total, sum(hit) hit_total, "\r
35                     "sum(fired) fired_total, sum(frags) frags_total "\r
36                     "from xonstat.player_weapon_stats ws, xonstat.cd_weapon cw "\r
37                     "where ws.weapon_cd = cw.weapon_cd "\r
38                     "and player_id = :player_id "\r
39                     "group by descr, cw.weapon_cd "\r
40                     "order by descr"\r
41                 ).params(player_id=player_id).all()\r
42 \r
43         recent_games = DBSession.query(PlayerGameStat, Game, Server, Map).\\r
44                 filter(PlayerGameStat.player_id == player_id).\\r
45                 filter(PlayerGameStat.game_id == Game.game_id).\\r
46                 filter(Game.server_id == Server.server_id).\\r
47                 filter(Game.map_id == Map.map_id).\\r
48                 order_by(Game.game_id.desc())[0:10]\r
49 \r
50         game_stats = {}\r
51         (game_stats['avg_rank'], game_stats['total_kills'], \r
52                 game_stats['total_deaths'], game_stats['total_suicides'], \r
53                 game_stats['total_score'], game_stats['total_time'], \r
54                 game_stats['total_held'], game_stats['total_captures'], \r
55                 game_stats['total_pickups'],game_stats['total_drops'], \r
56                 game_stats['total_returns'], game_stats['total_collects'], \r
57                 game_stats['total_destroys'], game_stats['total_dhk'], \r
58                 game_stats['total_pushes'], game_stats['total_pushed'], \r
59                 game_stats['total_carrier_frags'], \r
60                 game_stats['total_alivetime'],\r
61                 game_stats['total_games_played']) = DBSession.\\r
62                         query("avg_rank", "total_kills", "total_deaths", \r
63                 "total_suicides", "total_score", "total_time", "total_held",\r
64                 "total_captures", "total_pickups", "total_drops", \r
65                 "total_returns", "total_collects", "total_destroys", \r
66                 "total_dhk", "total_pushes", "total_pushed", \r
67                 "total_carrier_frags", "total_alivetime", \r
68                 "total_games_played").\\r
69                 from_statement(\r
70                     "select round(avg(rank)) avg_rank, sum(kills) total_kills, "\r
71                     "sum(deaths) total_deaths, sum(suicides) total_suicides, "\r
72                     "sum(score) total_score, sum(time) total_time, "\r
73                     "sum(held) total_held, sum(captures) total_captures, "\r
74                     "sum(pickups) total_pickups, sum(drops) total_drops, "\r
75                     "sum(returns) total_returns, sum(collects) total_collects, "\r
76                     "sum(destroys) total_destroys, sum(destroys_holding_key) total_dhk, "\r
77                     "sum(pushes) total_pushes, sum(pushed) total_pushed, "\r
78                     "sum(carrier_frags) total_carrier_frags, "\r
79                     "sum(alivetime) total_alivetime, count(*) total_games_played "\r
80                     "from player_game_stats "\r
81                     "where player_id=:player_id"\r
82                 ).params(player_id=player_id).one()\r
83 \r
84         for (key,value) in game_stats.items():\r
85             if value == None:\r
86                 game_stats[key] = '-'\r
87 \r
88     except Exception as e:\r
89         player = None\r
90         weapon_stats = None\r
91         game_stats = None\r
92         recent_games = None\r
93         raise e\r
94     return {'player':player, \r
95             'recent_games':recent_games,\r
96             'weapon_stats':weapon_stats,\r
97             'game_stats':game_stats}\r
98 \r
99 \r
100 def player_game_index(request):\r
101     """\r
102     Provides an index of the games in which a particular\r
103     player was involved. This is ordered by game_id, with\r
104     the most recent game_ids first. Paginated.\r
105     """\r
106     player_id = request.matchdict['player_id']\r
107 \r
108     if 'page' in request.matchdict:\r
109         current_page = request.matchdict['page']\r
110     else:\r
111         current_page = 1\r
112 \r
113     try:\r
114         player = DBSession.query(Player).filter_by(player_id=player_id).one()\r
115 \r
116         games_q = DBSession.query(PlayerGameStat, Game, Server, Map).\\r
117                 filter(PlayerGameStat.player_id == player_id).\\r
118                 filter(PlayerGameStat.game_id == Game.game_id).\\r
119                 filter(Game.server_id == Server.server_id).\\r
120                 filter(Game.map_id == Map.map_id).\\r
121                 order_by(Game.game_id.desc())\r
122 \r
123         games = Page(games_q, current_page, url=page_url)\r
124 \r
125         \r
126     except Exception as e:\r
127         player = None\r
128         games = None\r
129 \r
130     return {'player':player,\r
131             'games':games}\r