]> de.git.xonotic.org Git - xonotic/xonstat.git/blob - xonstat/views/map.py
Don't pre-fill the "recent games" section with hyphens.
[xonotic/xonstat.git] / xonstat / views / map.py
1 import logging
2 import sqlalchemy.sql.functions as func
3 import sqlalchemy.sql.expression as expr
4 from collections import namedtuple
5 from datetime import datetime, timedelta
6 from sqlalchemy import desc
7 from webhelpers.paginate import Page, PageURL
8 from xonstat.models import *
9 from xonstat.util import page_url, html_colors
10 from xonstat.views.helpers import RecentGame, recent_games_q
11
12 log = logging.getLogger(__name__)
13
14 def _map_index_data(request):
15     if request.params.has_key('page'):
16         current_page = request.params['page']
17     else:
18         current_page = 1
19
20     try:
21         map_q = DBSession.query(Map).\
22                 order_by(Map.map_id.desc())
23
24         maps = Page(map_q, current_page, items_per_page=10, url=page_url)
25
26     except Exception as e:
27         maps = None
28
29     return {'maps':maps, }
30
31
32 def map_index(request):
33     """
34     Provides a list of all the current maps.
35     """
36     return _map_index_data(request)
37
38
39 def map_index_json(request):
40     """
41     Provides a JSON-serialized list of all the current maps. 
42     """
43     view_data = _map_index_data(request)
44
45     maps = [m.to_dict() for m in view_data['maps']]
46
47     return maps
48
49
50 def _map_info_data(request):
51     map_id = request.matchdict['id']
52
53     try: 
54         leaderboard_lifetime = int(
55                 request.registry.settings['xonstat.leaderboard_lifetime'])
56     except:
57         leaderboard_lifetime = 30
58
59     leaderboard_count = 10
60     recent_games_count = 20
61
62     # captime tuples
63     Captime = namedtuple('Captime', ['player_id', 'nick_html_colors',
64         'fastest_cap', 'game_id'])
65
66     try:
67         gmap = DBSession.query(Map).filter_by(map_id=map_id).one()
68
69         # recent games played in descending order
70         rgs = recent_games_q(map_id=map_id).limit(recent_games_count).all()
71         recent_games = [RecentGame(row) for row in rgs]
72
73         # top players by score
74         top_scorers = DBSession.query(Player.player_id, Player.nick,
75                 func.sum(PlayerGameStat.score)).\
76                 filter(Player.player_id == PlayerGameStat.player_id).\
77                 filter(Game.game_id == PlayerGameStat.game_id).\
78                 filter(Game.map_id == map_id).\
79                 filter(Player.player_id > 2).\
80                 filter(PlayerGameStat.create_dt > 
81                         (datetime.utcnow() - timedelta(days=leaderboard_lifetime))).\
82                 order_by(expr.desc(func.sum(PlayerGameStat.score))).\
83                 group_by(Player.nick).\
84                 group_by(Player.player_id).all()[0:leaderboard_count]
85
86         top_scorers = [(player_id, html_colors(nick), score) \
87                 for (player_id, nick, score) in top_scorers]
88
89         # top players by playing time
90         top_players = DBSession.query(Player.player_id, Player.nick, 
91                 func.sum(PlayerGameStat.alivetime)).\
92                 filter(Player.player_id == PlayerGameStat.player_id).\
93                 filter(Game.game_id == PlayerGameStat.game_id).\
94                 filter(Game.map_id == map_id).\
95                 filter(Player.player_id > 2).\
96                 filter(PlayerGameStat.create_dt > 
97                         (datetime.utcnow() - timedelta(days=leaderboard_lifetime))).\
98                 order_by(expr.desc(func.sum(PlayerGameStat.alivetime))).\
99                 group_by(Player.nick).\
100                 group_by(Player.player_id).all()[0:leaderboard_count]
101
102         top_players = [(player_id, html_colors(nick), score) \
103                 for (player_id, nick, score) in top_players]
104
105         # top servers using/playing this map
106         top_servers = DBSession.query(Server.server_id, Server.name, 
107                 func.count(Game.game_id)).\
108                 filter(Game.server_id == Server.server_id).\
109                 filter(Game.map_id == map_id).\
110                 filter(Game.create_dt > 
111                         (datetime.utcnow() - timedelta(days=leaderboard_lifetime))).\
112                 order_by(expr.desc(func.count(Game.game_id))).\
113                 group_by(Server.name).\
114                 group_by(Server.server_id).all()[0:leaderboard_count]
115
116         # top captimes
117         captimes_raw = DBSession.query(Player.player_id, Player.nick,
118             PlayerCaptime.fastest_cap, PlayerCaptime.game_id).\
119                 filter(PlayerCaptime.map_id == map_id).\
120                 filter(Player.player_id == PlayerCaptime.player_id).\
121                 order_by(PlayerCaptime.fastest_cap).\
122                 limit(10).all()
123
124         captimes = [Captime(c.player_id, html_colors(c.nick),
125             c.fastest_cap, c.game_id) for c in captimes_raw]
126
127     except Exception as e:
128         gmap = None
129     return {'gmap':gmap,
130             'recent_games':recent_games,
131             'top_scorers':top_scorers,
132             'top_players':top_players,
133             'top_servers':top_servers,
134             'captimes':captimes,
135             }
136
137
138 def map_info(request):
139     """
140     List the information stored about a given map.
141     """
142     mapinfo_data =  _map_info_data(request)
143
144     # FIXME: code clone, should get these from _map_info_data
145     leaderboard_count = 10
146     recent_games_count = 20
147
148     for i in range(leaderboard_count-len(mapinfo_data['top_scorers'])):
149         mapinfo_data['top_scorers'].append(('-', '-', '-'))
150
151     for i in range(leaderboard_count-len(mapinfo_data['top_players'])):
152         mapinfo_data['top_players'].append(('-', '-', '-'))
153
154     for i in range(leaderboard_count-len(mapinfo_data['top_servers'])):
155         mapinfo_data['top_servers'].append(('-', '-', '-'))
156
157     return mapinfo_data
158
159
160 def map_info_json(request):
161     """
162     List the information stored about a given map. JSON.
163     """
164     return [{'status':'not implemented'}]