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