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