]> de.git.xonotic.org Git - xonotic/xonstat.git/blob - xonstat/views/map.py
Merge remote-tracking branch 'antzucaro/master'
[xonotic/xonstat.git] / xonstat / views / map.py
1 import logging\r
2 import sqlalchemy.sql.functions as func\r
3 import sqlalchemy.sql.expression as expr\r
4 from datetime import datetime, timedelta\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 def _map_index_data(request):\r
14     """\r
15     Provides a list of all the current maps. \r
16     """\r
17     if request.params.has_key('page'):\r
18         current_page = request.params['page']\r
19     else:\r
20         current_page = 1\r
21 \r
22     try:\r
23         map_q = DBSession.query(Map).\\r
24                 order_by(Map.map_id.desc())\r
25 \r
26         maps = Page(map_q, current_page, items_per_page=10, url=page_url)\r
27 \r
28     except Exception as e:\r
29         maps = None\r
30 \r
31     return {'maps':maps, }\r
32 \r
33 \r
34 def map_index(request):\r
35     """\r
36     Provides a list of all the current maps. \r
37     """\r
38     return _map_index_data(request)\r
39 \r
40 \r
41 def map_index_json(request):\r
42     """\r
43     Provides a JSON-serialized list of all the current maps. \r
44     """\r
45     view_data =  _map_index_data(request)\r
46 \r
47     maps = [m.to_dict() for m in view_data['maps']]\r
48 \r
49     return maps\r
50 \r
51 \r
52 def map_info(request):\r
53     """\r
54     List the information stored about a given map. \r
55     """\r
56     map_id = request.matchdict['id']\r
57 \r
58     try: \r
59         leaderboard_lifetime = int(\r
60                 request.registry.settings['xonstat.leaderboard_lifetime'])\r
61     except:\r
62         leaderboard_lifetime = 30\r
63 \r
64     leaderboard_count = 10\r
65     recent_games_count = 20\r
66 \r
67     try:\r
68         gmap = DBSession.query(Map).filter_by(map_id=map_id).one()\r
69 \r
70         # recent games on this map\r
71         recent_games = DBSession.query(Game, Server, Map, PlayerGameStat).\\r
72             filter(Game.server_id==Server.server_id).\\r
73             filter(Game.map_id==Map.map_id).\\r
74             filter(Game.map_id==map_id).\\r
75             filter(PlayerGameStat.game_id==Game.game_id).\\r
76             filter(PlayerGameStat.rank==1).\\r
77             order_by(expr.desc(Game.start_dt)).all()[0:recent_games_count]\r
78 \r
79         for i in range(recent_games_count-len(recent_games)):\r
80             recent_games.append(('-', '-', '-', '-'))\r
81 \r
82 \r
83         # top players by score\r
84         top_scorers = DBSession.query(Player.player_id, Player.nick,\r
85                 func.sum(PlayerGameStat.score)).\\r
86                 filter(Player.player_id == PlayerGameStat.player_id).\\r
87                 filter(Game.game_id == PlayerGameStat.game_id).\\r
88                 filter(Game.map_id == map_id).\\r
89                 filter(Player.player_id > 2).\\r
90                 filter(PlayerGameStat.create_dt > \r
91                         (datetime.utcnow() - timedelta(days=leaderboard_lifetime))).\\r
92                 order_by(expr.desc(func.sum(PlayerGameStat.score))).\\r
93                 group_by(Player.nick).\\r
94                 group_by(Player.player_id).all()[0:10]\r
95 \r
96         top_scorers = [(player_id, html_colors(nick), score) \\r
97                 for (player_id, nick, score) in top_scorers]\r
98 \r
99         for i in range(leaderboard_count-len(top_scorers)):\r
100             top_scorers.append(('-', '-', '-'))\r
101 \r
102         # top players by playing time\r
103         top_players = DBSession.query(Player.player_id, Player.nick, \r
104                 func.sum(PlayerGameStat.alivetime)).\\r
105                 filter(Player.player_id == PlayerGameStat.player_id).\\r
106                 filter(Game.game_id == PlayerGameStat.game_id).\\r
107                 filter(Game.map_id == map_id).\\r
108                 filter(Player.player_id > 2).\\r
109                 filter(PlayerGameStat.create_dt > \r
110                         (datetime.utcnow() - timedelta(days=leaderboard_lifetime))).\\r
111                 order_by(expr.desc(func.sum(PlayerGameStat.alivetime))).\\r
112                 group_by(Player.nick).\\r
113                 group_by(Player.player_id).all()[0:10]\r
114 \r
115         top_players = [(player_id, html_colors(nick), score) \\r
116                 for (player_id, nick, score) in top_players]\r
117 \r
118         for i in range(leaderboard_count-len(top_players)):\r
119             top_players.append(('-', '-', '-'))\r
120 \r
121         # top servers using/playing this map\r
122         top_servers = DBSession.query(Server.server_id, Server.name, \r
123                 func.count(Game.game_id)).\\r
124                 filter(Game.server_id == Server.server_id).\\r
125                 filter(Game.map_id == map_id).\\r
126                 filter(Game.create_dt > \r
127                         (datetime.utcnow() - timedelta(days=leaderboard_lifetime))).\\r
128                 order_by(expr.desc(func.count(Game.game_id))).\\r
129                 group_by(Server.name).\\r
130                 group_by(Server.server_id).all()[0:10]\r
131 \r
132         for i in range(leaderboard_count-len(top_servers)):\r
133             top_servers.append(('-', '-', '-'))\r
134 \r
135     except Exception as e:\r
136         gmap = None\r
137     return {'gmap':gmap,\r
138             'recent_games':recent_games,\r
139             'top_scorers':top_scorers,\r
140             'top_players':top_players,\r
141             'top_servers':top_servers,\r
142             }\r