]> git.xonotic.org Git - xonotic/xonstat.git/blob - xonstat/models.py
Store player-to-map captimes by mod.
[xonotic/xonstat.git] / xonstat / models.py
1 import json
2 import logging
3 import math
4 import sqlalchemy
5 import sqlalchemy.sql.functions as sfunc
6 from calendar import timegm
7 from datetime import datetime as dt
8 from datetime import timedelta
9 from sqlalchemy.orm import mapper
10 from sqlalchemy.orm import scoped_session
11 from sqlalchemy.orm import sessionmaker
12 from sqlalchemy.ext.declarative import declarative_base
13 from xonstat.util import qfont_decode, strip_colors, html_colors, pretty_date
14
15 log = logging.getLogger(__name__)
16
17 DBSession = scoped_session(sessionmaker())
18 Base = declarative_base()
19
20 # define objects for all tables
21 class Player(object):
22     def nick_html_colors(self, limit=None):
23         if self.nick is None:
24             return "Anonymous Player"
25         else:
26             return html_colors(self.nick, limit)
27
28     def nick_strip_colors(self):
29         if self.nick is None:
30             return "Anonymous Player"
31         else:
32             return strip_colors(self.nick)
33
34     def joined_pretty_date(self):
35         return pretty_date(self.create_dt)
36
37     def __repr__(self):
38         return "<Player(%s, %s)>" % (self.player_id, self.nick.encode('utf-8'))
39
40     def to_dict(self):
41         return {'player_id':self.player_id, 'nick':self.nick,
42             'joined':self.create_dt.strftime('%Y-%m-%dT%H:%M:%SZ'),
43             'active_ind':self.active_ind, 'location':self.location,
44             'stripped_nick':qfont_decode(self.stripped_nick)}
45
46     def epoch(self):
47         return timegm(self.create_dt.timetuple())
48
49
50 class GameType(object):
51     def __repr__(self):
52         return "<GameType(%s, %s, %s)>" % (self.game_type_cd, self.descr, self.active_ind)
53
54     def to_dict(self):
55         return {'game_type_cd':self.game_type_cd, 'name':self.descr, 'active':self.active_ind}
56
57
58 class Weapon(object):
59     def __repr__(self):
60         return "<Weapon(%s, %s, %s)>" % (self.weapon_cd, self.descr, self.active_ind)
61
62     def to_dict(self):
63         return {'weapon_cd':self.weapon_cd, 'name':self.descr, 'active':self.active_ind}
64
65
66 class Server(object):
67     def __init__(self, name=None, hashkey=None, ip_addr=None):
68         self.name = name
69         self.hashkey = hashkey
70         self.ip_addr = ip_addr
71         self.create_dt = dt.utcnow()
72
73     def __repr__(self):
74         return "<Server(%s, %s)>" % (self.server_id, self.name.encode('utf-8'))
75
76     def to_dict(self):
77         return {'server_id':self.server_id, 'name':self.name,
78             'ip_addr':self.ip_addr, 'location':self.location}
79
80     def fuzzy_date(self):
81         return pretty_date(self.create_dt)
82
83     def epoch(self):
84         return timegm(self.create_dt.timetuple())
85
86
87 class Map(object):
88     def __init__(self, name=None):
89         self.name = name
90
91     def __repr__(self):
92         return "<Map(%s, %s, %s)>" % (self.map_id, self.name, self.version)
93
94     def to_dict(self):
95         return {'map_id':self.map_id, 'name':self.name, 'version':self.version,}
96
97     def fuzzy_date(self):
98         return pretty_date(self.create_dt)
99
100     def epoch(self):
101         return timegm(self.create_dt.timetuple())
102
103
104 class Game(object):
105     def __init__(self, game_id=None, start_dt=None, game_type_cd=None,
106             server_id=None, map_id=None, winner=None):
107         self.game_id = game_id
108         self.start_dt = start_dt
109         self.game_type_cd = game_type_cd
110         self.server_id = server_id
111         self.map_id = map_id
112         self.winner = winner
113
114     def __repr__(self):
115         return "<Game(%s, %s, %s, %s)>" % (self.game_id, self.start_dt, self.game_type_cd, self.server_id)
116
117     def to_dict(self):
118         return {'game_id':self.game_id, 'start':self.start_dt.strftime('%Y-%m-%dT%H:%M:%SZ'),
119                 'game_type_cd':self.game_type_cd, 'server_id':self.server_id}
120
121     def fuzzy_date(self):
122         return pretty_date(self.start_dt)
123
124     def epoch(self):
125         return timegm(self.start_dt.timetuple())
126
127
128 class PlayerGameStat(object):
129     def __init__(self, player_game_stat_id=None, create_dt=None):
130         self.player_game_stat_id = player_game_stat_id
131         self.create_dt = create_dt
132
133     def __repr__(self):
134         return "<PlayerGameStat(%s, %s, %s)>" % (self.player_id, self.game_id, self.create_dt)
135
136     def to_dict(self):
137         return {'player_id':self.player_id, 'game_id':self.game_id,
138             'create_dt':self.create_dt.strftime('%Y-%m-%dT%H:%M:%SZ'),
139             'alivetime':self.alivetime, 'rank':self.rank, 'score':self.score, 'team':self.team}
140
141     def nick_stripped(self):
142         if self.nick is None:
143             return "Anonymous Player"
144         else:
145             return strip_colors(self.nick)
146
147     def nick_html_colors(self, limit=None):
148         if self.nick is None:
149             return "Anonymous Player"
150         else:
151             return html_colors(self.nick, limit)
152
153     def team_html_color(self):
154         if self.team == 5:
155             return "red"
156         if self.team == 14:
157             return "blue"
158         if self.team == 13:
159             return "yellow"
160         if self.team == 10:
161             return "pink"
162
163
164 class Achievement(object):
165     def __repr__(self):
166         return "<Achievement(%s, %s, %s)>" % (self.achievement_cd, self.descr, self.limit)
167
168     def to_dict(self):
169         return {'achievement_cd':self.achievement_cd, 'name':self.descr, 'limit':self.limit}
170
171
172 class PlayerAchievement(object):
173     def __repr__(self):
174         return "<PlayerAchievement(%s, %s)>" % (self.player_id, self.achievement_cd)
175
176     def to_dict(self):
177         return {'player_id':self.player_id, 'achievement_cd':self.achievement_cd}
178
179
180 class PlayerWeaponStat(object):
181     def __init__(self, player_id=None, game_id=None, weapon_cd=None):
182         self.player_id = player_id
183         self.game_id = game_id
184         self.weapon_cd = weapon_cd
185         self.fired = 0
186         self.max = 0
187         self.hit = 0
188         self.actual = 0
189         self.frags = 0
190
191     def __repr__(self):
192         return "<PlayerWeaponStat(%s, %s, %s)>" % (self.player_weapon_stats_id, self.player_id, self.game_id)
193
194     def to_dict(self):
195         return {
196             'weapon_cd':self.weapon_cd,
197             'player_weapon_stats_id':self.player_weapon_stats_id,
198             'player_id':self.player_id,
199             'game_id':self.game_id,
200             'fired':self.fired,
201             'max':self.max,
202             'hit':self.hit,
203             'actual':self.actual,
204             'frags':self.frags,
205         }
206
207
208 class Hashkey(object):
209     def __init__(self, player_id=None, hashkey=None):
210         self.player_id = player_id
211         self.hashkey = hashkey
212
213     def __repr__(self):
214         return "<Hashkey(%s, %s)>" % (self.player_id, self.hashkey)
215
216     def to_dict(self):
217         return {'player_id':self.player_id, 'hashkey':self.hashkey}
218
219
220 class PlayerNick(object):
221     def __repr__(self):
222         return "<PlayerNick(%s, %s)>" % (self.player_id, qfont_decode(self.stripped_nick))
223
224     def to_dict(self):
225         return {'player_id':self.player_id, 'name':qfont_decode(self.stripped_nick)}
226
227
228 class PlayerElo(object):
229     def __init__(self, player_id=None, game_type_cd=None, elo=None):
230
231         self.player_id = player_id
232         self.game_type_cd = game_type_cd
233         self.elo = elo
234         self.score = 0
235         self.games = 0
236
237     def __repr__(self):
238         return "<PlayerElo(pid=%s, gametype=%s, elo=%s, games=%s)>" % (self.player_id, self.game_type_cd, self.elo, self.games)
239
240     def to_dict(self):
241         return {'player_id':self.player_id, 'game_type_cd':self.game_type_cd, 'elo':self.elo, 'games':self.games}
242
243
244 class PlayerRank(object):
245
246     def nick_html_colors(self, limit=None):
247         if self.nick is None:
248             return "Anonymous Player"
249         else:
250             return html_colors(self.nick, limit)
251
252     def __repr__(self):
253         return "<PlayerRank(pid=%s, gametype=%s, rank=%s)>" % (self.player_id, self.game_type_cd, self.rank)
254
255     def to_dict(self):
256         return {'player_id':self.player_id, 'game_type_cd':self.game_type_cd, 'rank':self.rank}
257
258
259 class PlayerCaptime(object):
260     def __init__(self, player_id=None, game_id=None, map_id=None,
261             fastest_cap=None, mod=None):
262         self.player_id = player_id
263         self.game_id = game_id
264         self.map_id = map_id
265         self.fastest_cap = fastest_cap
266         self.mod = mod
267
268     def __repr__(self):
269         return "<PlayerCaptime(pid=%s, map_id=%s, mod=%s)>" % (self.player_id,
270                 self.map_id, self.mod)
271
272     def fuzzy_date(self):
273         return pretty_date(self.create_dt)
274
275     def epoch(self):
276         return timegm(self.create_dt.timetuple())
277
278
279 class SummaryStat(object):
280     def __repr__(self):
281         return "<SummaryStat(total_players=%s, total_games=%s, total_servers=%s)>" % (self.total_players, self.total_games, self.total_servers)
282
283
284 class TeamGameStat(object):
285     def __init__(self, team_game_stat_id=None, create_dt=None):
286         self.team_game_stat_id = team_game_stat_id
287         self.create_dt = create_dt
288
289     def __repr__(self):
290         return "<TeamGameStat(%s, %s, %s)>" % (self.team_game_stat_id, self.game_id, self.team)
291
292     def to_dict(self):
293         return {
294             'team_game_stat_id':self.team_game_stat_id,
295             'game_id':self.game_id,
296             'team':self.team,
297             'score':self.score,
298             'rounds':self.rounds,
299             'caps':self.caps,
300             'create_dt':self.create_dt.strftime('%Y-%m-%dT%H:%M:%SZ'),
301         }
302
303     def team_html_color(self):
304         if self.team == 5:
305             return "red"
306         if self.team == 14:
307             return "blue"
308         if self.team == 13:
309             return "yellow"
310         if self.team == 10:
311             return "pink"
312
313
314 class PlayerGameAnticheat(object):
315     def __init__(self, player_id=None, game_id=None, key=None,
316             value=None, create_dt=None):
317         self.player_id                = player_id
318         self.game_id                  = game_id
319         self.key                      = key
320         self.value                    = value
321         self.create_dt                = create_dt
322
323     def __repr__(self):
324         return "<PlayerGameAnticheat(%s, %d)>" % (self.key, self.value)
325
326
327 class PlayerGroups(object):
328     def __init__(self, player_id=None, group_name=None):
329         self.player_id  = player_id
330         self.group_name = group_name
331
332     def __repr__(self):
333         return "<PlayerGroups(%s, %s)>" % (self.player_id, self.group_name)
334
335
336 def initialize_db(engine=None):
337     DBSession.configure(bind=engine)
338     Base.metadata.bind = engine
339     Base.metadata.create_all(engine)
340     MetaData = sqlalchemy.MetaData(bind=engine)
341     MetaData.reflect()
342
343     # assign all those tables to an object
344     achievements_table = MetaData.tables['achievements']
345     cd_achievement_table = MetaData.tables['cd_achievement']
346     cd_game_type_table = MetaData.tables['cd_game_type']
347     cd_weapon_table = MetaData.tables['cd_weapon']
348     db_version_table = MetaData.tables['db_version']
349     games_table = MetaData.tables['games']
350     hashkeys_table = MetaData.tables['hashkeys']
351     maps_table = MetaData.tables['maps']
352     player_game_stats_table = MetaData.tables['player_game_stats']
353     players_table = MetaData.tables['players']
354     player_weapon_stats_table = MetaData.tables['player_weapon_stats']
355     servers_table = MetaData.tables['servers']
356     player_nicks_table = MetaData.tables['player_nicks']
357     player_elos_table = MetaData.tables['player_elos']
358     player_ranks_table = MetaData.tables['player_ranks']
359     player_captimes_table = MetaData.tables['player_map_captimes']
360     summary_stats_table = MetaData.tables['summary_stats']
361     team_game_stats_table = MetaData.tables['team_game_stats']
362     player_game_anticheats_table = MetaData.tables['player_game_anticheats']
363     player_groups_table = MetaData.tables['player_groups']
364
365     # now map the tables and the objects together
366     mapper(PlayerAchievement, achievements_table)
367     mapper(Achievement, cd_achievement_table)
368     mapper(GameType, cd_game_type_table)
369     mapper(Weapon, cd_weapon_table)
370     mapper(Game, games_table)
371     mapper(Hashkey, hashkeys_table)
372     mapper(Map, maps_table)
373     mapper(PlayerGameStat, player_game_stats_table)
374     mapper(Player, players_table)
375     mapper(PlayerWeaponStat, player_weapon_stats_table)
376     mapper(Server, servers_table)
377     mapper(PlayerNick, player_nicks_table)
378     mapper(PlayerElo, player_elos_table)
379     mapper(PlayerRank, player_ranks_table)
380     mapper(PlayerCaptime, player_captimes_table)
381     mapper(SummaryStat, summary_stats_table)
382     mapper(TeamGameStat, team_game_stats_table)
383     mapper(PlayerGameAnticheat, player_game_anticheats_table)
384     mapper(PlayerGroups, player_groups_table)