Add ranks view based on game type.
[xonotic/xonstat.git] / xonstat / views / game.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 game_index(request):\r
15     """\r
16     Provides a list of current games, with the associated game stats.\r
17     These games are ordered by game_id, with the most current ones first.\r
18     Paginated.\r
19     """\r
20     if 'page' in request.matchdict:\r
21         current_page = request.matchdict['page']\r
22     else:\r
23         current_page = 1\r
24 \r
25     games_q = DBSession.query(Game, Server, Map).\\r
26             filter(Game.server_id == Server.server_id).\\r
27             filter(Game.map_id == Map.map_id).\\r
28             order_by(Game.game_id.desc())\r
29 \r
30     games = Page(games_q, current_page, url=page_url)\r
31 \r
32     pgstats = {}\r
33     for (game, server, map) in games:\r
34         pgstats[game.game_id] = DBSession.query(PlayerGameStat).\\r
35                 filter(PlayerGameStat.game_id == game.game_id).\\r
36                 order_by(PlayerGameStat.rank).\\r
37                 order_by(PlayerGameStat.score).all()\r
38 \r
39     return {'games':games, \r
40             'pgstats':pgstats}\r
41 \r
42 \r
43 def game_info(request):\r
44     """\r
45     List the game stats (scoreboard) for a particular game. Paginated.\r
46     """\r
47     game_id = request.matchdict['id']\r
48     try:\r
49         notfound = False\r
50 \r
51         (game, server, map) = DBSession.query(Game, Server, Map).\\r
52                 filter(Game.game_id == game_id).\\r
53                 filter(Game.server_id == Server.server_id).\\r
54                 filter(Game.map_id == Map.map_id).one()\r
55 \r
56         pgstats = DBSession.query(PlayerGameStat).\\r
57                 filter(PlayerGameStat.game_id == game_id).\\r
58                 order_by(PlayerGameStat.rank).\\r
59                 order_by(PlayerGameStat.score).\\r
60                 all()\r
61 \r
62         pwstats = {}\r
63         for (pwstat, pgstat, weapon) in DBSession.query(PlayerWeaponStat, PlayerGameStat, Weapon).\\r
64                 filter(PlayerWeaponStat.game_id == game_id).\\r
65                 filter(PlayerWeaponStat.weapon_cd == Weapon.weapon_cd).\\r
66                 filter(PlayerWeaponStat.player_game_stat_id == \\r
67                     PlayerGameStat.player_game_stat_id).\\r
68                 order_by(PlayerGameStat.rank).\\r
69                 order_by(PlayerGameStat.score).\\r
70                 order_by(Weapon.descr).\\r
71                 all():\r
72                     if pgstat.player_game_stat_id not in pwstats:\r
73                         pwstats[pgstat.player_game_stat_id] = []\r
74 \r
75                     # NOTE adding pgstat to position 6 in order to display nick.\r
76                     # You have to use a slice [0:5] to pass to the accuracy \r
77                     # template\r
78                     pwstats[pgstat.player_game_stat_id].append((weapon.descr, \r
79                         weapon.weapon_cd, pwstat.actual, pwstat.max, \r
80                         pwstat.hit, pwstat.fired, pgstat))\r
81 \r
82     except Exception as inst:\r
83         game = None\r
84         server = None\r
85         map = None\r
86         pgstats = None\r
87         pwstats = None\r
88         raise inst\r
89 \r
90     return {'game':game,\r
91             'server':server,\r
92             'map':map,\r
93             'pgstats':pgstats,\r
94             'pwstats':pwstats,\r
95             }\r
96 \r
97 \r
98 def rank_index(request):\r
99     """\r
100     Provide a list of gametype ranks, paginated.\r
101     """\r
102     if 'page' in request.matchdict:\r
103         current_page = request.matchdict['page']\r
104     else:\r
105         current_page = 1\r
106 \r
107     game_type_cd = request.matchdict['game_type_cd']\r
108 \r
109     ranks_q = DBSession.query(PlayerElo, Player).\\r
110             filter(PlayerElo.game_type_cd==game_type_cd).\\r
111             filter(PlayerElo.player_id==Player.player_id).\\r
112             order_by(PlayerElo.elo.desc())\r
113 \r
114     ranks = Page(ranks_q, current_page, url=page_url)\r
115 \r
116     if len(ranks) == 0:\r
117         ranks = None\r
118 \r
119     return {\r
120             'ranks':ranks,\r
121             'game_type_cd':game_type_cd,\r
122            }\r