]> de.git.xonotic.org Git - xonotic/xonstat.git/blob - xonstat/views/helpers.py
Make the "recent games" section more modular.
[xonotic/xonstat.git] / xonstat / views / helpers.py
1 import logging
2 import sqlalchemy.sql.expression as expr
3 from datetime import datetime
4 from xonstat.models import *
5 from xonstat.util import *
6
7 log = logging.getLogger(__name__)
8
9 class RecentGame(object):
10     '''
11     This is a helper class to facilitate showing recent games
12     data within mako. The idea is to take the results of a query
13     and transform it into class members easily accessible later.
14     It is like a namedtuple but a little easier to form.
15
16     The constructor takes a query row that has been fetched, and
17     it requires the following columns to be present in the row:
18
19         game_id, game_type_cd, winner, create_dt, server_id, server_name,
20         map_id, map_name, player_id, nick, rank, team
21
22     The following columns are optional:
23
24         elo_delta
25
26     This class is meant to be used in conjunction with recent_games_q,
27     which will return rows matching this specification.
28     '''
29     def __init__(self, row):
30         self.game_id = row.game_id
31         self.game_type_cd = row.game_type_cd
32         self.winner = row.winner
33         self.create_dt = row.create_dt
34         self.fuzzy_date = pretty_date(row.create_dt)
35         self.epoch = timegm(row.create_dt.timetuple())
36         self.server_id = row.server_id
37         self.server_name = row.server_name
38         self.map_id = row.map_id
39         self.map_name = row.map_name
40         self.player_id = row.player_id
41         self.nick = row.nick
42         self.nick_html_colors = html_colors(row.nick)
43         self.rank = row.rank
44         self.team = row.team
45
46         try:
47             self.elo_delta = row.elo_delta
48         except:
49             self.elo_delta = None
50
51
52 def recent_games_q(server_id=None, map_id=None, player_id=None, cutoff=None):
53     '''
54     Returns a SQLA query of recent game data. Parameters filter
55     the results returned if they are provided. If not, it is
56     assumed that results from all servers and maps is desired.
57
58     The cutoff parameter provides a way to limit how far back to
59     look when querying. Only games that happened on or after the
60     cutoff (which is a datetime object) will be returned.
61     '''
62     recent_games_q = DBSession.query(Game.game_id, Game.game_type_cd,
63             Game.winner, Game.create_dt, Server.server_id,
64             Server.name.label('server_name'), Map.map_id,
65             Map.name.label('map_name'), PlayerGameStat.player_id,
66             PlayerGameStat.nick, PlayerGameStat.rank, PlayerGameStat.team,
67             PlayerGameStat.elo_delta).\
68             filter(Game.server_id==Server.server_id).\
69             filter(Game.map_id==Map.map_id).\
70             filter(Game.game_id==PlayerGameStat.game_id).\
71             order_by(expr.desc(Game.create_dt))
72
73     # the various filters provided get tacked on to the query
74     if server_id is not None:
75         recent_games_q = recent_games_q.\
76             filter(Server.server_id==server_id)
77
78     if map_id is not None:
79         recent_games_q = recent_games_q.\
80             filter(Map.map_id==map_id)
81
82     if player_id is not None:
83         recent_games_q = recent_games_q.\
84             filter(PlayerGameStat.player_id==player_id)
85     else:
86         recent_games_q = recent_games_q.\
87             filter(PlayerGameStat.rank==1)
88
89     if cutoff is not None:
90         right_now = datetime.utcnow()
91         recent_games_q = recent_games_q.\
92             filter(expr.between(Game.create_dt, cutoff, right_now))
93
94     return recent_games_q