]> git.xonotic.org Git - xonotic/xonstat.git/blob - xonstat/models.py
Merge pull request #15 from baneofblabs/master
[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 timedelta
8 from sqlalchemy.orm import mapper
9 from sqlalchemy.orm import scoped_session
10 from sqlalchemy.orm import sessionmaker
11 from sqlalchemy.ext.declarative import declarative_base
12 from xonstat.util import strip_colors, html_colors, pretty_date
13
14 log = logging.getLogger(__name__)
15
16 DBSession = scoped_session(sessionmaker())
17 Base = declarative_base()
18
19 # define objects for all tables
20 class Player(object):
21     def nick_html_colors(self):
22         if self.nick is None:
23             return "Anonymous Player"
24         else:
25             return html_colors(self.nick)
26
27     def nick_strip_colors(self):
28         if self.nick is None:
29             return "Anonymous Player"
30         else:
31             return strip_colors(self.nick)
32
33     def joined_pretty_date(self):
34         return pretty_date(self.create_dt)
35
36     def __repr__(self):
37         return "<Player(%s, %s)>" % (self.player_id, self.nick.encode('utf-8'))
38
39     def to_dict(self):
40         return {'player_id':self.player_id, 'nick':self.nick,
41             'joined':self.create_dt.strftime('%Y-%m-%dT%H:%M:%SZ'),
42             'active_ind':self.active_ind, 'location':self.location,
43             'stripped_nick':self.stripped_nick}
44
45     def epoch(self):
46         return timegm(self.create_dt.timetuple())
47
48
49 class GameType(object):
50     def __repr__(self):
51         return "<GameType(%s, %s, %s)>" % (self.game_type_cd, self.descr, self.active_ind)
52
53     def to_dict(self):
54         return {'game_type_cd':self.game_type_cd, 'name':self.descr, 'active':self.active_ind}
55
56
57 class Weapon(object):
58     def __repr__(self):
59         return "<Weapon(%s, %s, %s)>" % (self.weapon_cd, self.descr, self.active_ind)
60
61     def to_dict(self):
62         return {'weapon_cd':self.weapon_cd, 'name':self.descr, 'active':self.active_ind}
63
64
65 class Server(object):
66     def __init__(self, name=None, hashkey=None, ip_addr=None):
67         self.name = name
68         self.hashkey = hashkey
69         self.ip_addr = ip_addr
70
71     def __repr__(self):
72         return "<Server(%s, %s)>" % (self.server_id, self.name.encode('utf-8'))
73
74     def to_dict(self):
75         return {'server_id':self.server_id, 'name':self.name,
76             'ip_addr':self.ip_addr, 'location':self.location}
77
78     def fuzzy_date(self):
79         return pretty_date(self.create_dt)
80
81     def epoch(self):
82         return timegm(self.create_dt.timetuple())
83
84
85 class Map(object):
86     def __init__(self, name=None):
87         self.name = name
88
89     def __repr__(self):
90         return "<Map(%s, %s, %s)>" % (self.map_id, self.name, self.version)
91
92     def to_dict(self):
93         return {'map_id':self.map_id, 'name':self.name, 'version':self.version,}
94
95     def fuzzy_date(self):
96         return pretty_date(self.create_dt)
97
98     def epoch(self):
99         return timegm(self.create_dt.timetuple())
100
101
102 class Game(object):
103     def __init__(self, game_id=None, start_dt=None, game_type_cd=None, 
104             server_id=None, map_id=None, winner=None):
105         self.game_id = game_id
106         self.start_dt = start_dt
107         self.game_type_cd = game_type_cd
108         self.server_id = server_id
109         self.map_id = map_id
110         self.winner = winner
111
112     def __repr__(self):
113         return "<Game(%s, %s, %s, %s)>" % (self.game_id, self.start_dt, self.game_type_cd, self.server_id)
114
115     def to_dict(self):
116         return {'game_id':self.game_id, 'start':self.start_dt.strftime('%Y-%m-%dT%H:%M:%SZ'),
117                 'game_type_cd':self.game_type_cd, 'server_id':self.server_id}
118
119     def fuzzy_date(self):
120         return pretty_date(self.start_dt)
121
122     def epoch(self):
123         return timegm(self.start_dt.timetuple())
124
125
126 class PlayerGameStat(object):
127     def __init__(self, player_game_stat_id=None, create_dt=None):
128         self.player_game_stat_id = player_game_stat_id
129         self.create_dt = create_dt
130
131     def __repr__(self):
132         return "<PlayerGameStat(%s, %s, %s)>" % (self.player_id, self.game_id, self.create_dt)
133
134     def to_dict(self):
135         return {'player_id':self.player_id, 'game_id':self.game_id,
136             'create_dt':self.create_dt.strftime('%Y-%m-%dT%H:%M:%SZ'),
137             'alivetime':self.alivetime, 'rank':self.rank, 'score':self.score, 'team':self.team}
138
139     def nick_stripped(self):
140         if self.nick is None:
141             return "Anonymous Player"
142         else:
143             return strip_colors(self.nick)
144
145     def nick_html_colors(self):
146         if self.nick is None:
147             return "Anonymous Player"
148         else:
149             return html_colors(self.nick)
150
151     def team_html_color(self):
152         # blue
153         if self.team == 5:
154             return "red"
155         # 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):
182         self.fired = 0
183         self.max = 0
184         self.hit = 0
185         self.actual = 0
186         self.frags = 0
187
188     def __repr__(self):
189         return "<PlayerWeaponStat(%s, %s, %s)>" % (self.player_weapon_stats_id, self.player_id, self.game_id)
190
191     def to_dict(self):
192         return {'player_weapon_stats_id':self.player_weapon_stats_id, 'player_id':self.player_id, 'game_id':self.game_id}
193
194
195 class Hashkey(object):
196     def __init__(self, player_id=None, hashkey=None):
197         self.player_id = player_id
198         self.hashkey = hashkey
199
200     def __repr__(self):
201         return "<Hashkey(%s, %s)>" % (self.player_id, self.hashkey)
202
203     def to_dict(self):
204         return {'player_id':self.player_id, 'hashkey':self.hashkey}
205
206
207 class PlayerNick(object):
208     def __repr__(self):
209         return "<PlayerNick(%s, %s)>" % (self.player_id, self.stripped_nick)
210
211     def to_dict(self):
212         return {'player_id':self.player_id, 'name':self.stripped_nick}
213
214
215 class PlayerElo(object):
216     def __init__(self, player_id=None, game_type_cd=None, elo=None):
217
218         self.player_id = player_id
219         self.game_type_cd = game_type_cd
220         self.elo = elo
221         self.score = 0
222         self.games = 0
223
224     def __repr__(self):
225         return "<PlayerElo(pid=%s, gametype=%s, elo=%s, games=%s)>" % (self.player_id, self.game_type_cd, self.elo, self.games)
226
227     def to_dict(self):
228         return {'player_id':self.player_id, 'game_type_cd':self.game_type_cd, 'elo':self.elo, 'games':self.games}
229
230
231 class PlayerRank(object):
232
233     def nick_html_colors(self):
234         if self.nick is None:
235             return "Anonymous Player"
236         else:
237             return html_colors(self.nick)
238
239     def __repr__(self):
240         return "<PlayerRank(pid=%s, gametype=%s, rank=%s)>" % (self.player_id, self.game_type_cd, self.rank)
241
242     def to_dict(self):
243         return {'player_id':self.player_id, 'game_type_cd':self.game_type_cd, 'rank':self.rank}
244
245
246 class PlayerCaptime(object):
247     def __init__(self, player_id=None, game_id=None, map_id=None,
248             fastest_cap=None):
249         self.player_id = player_id
250         self.game_id = game_id
251         self.map_id = map_id
252         self.fastest_cap = fastest_cap
253
254     def __repr__(self):
255         return "<PlayerCaptime(pid=%s, map_id=%s)>" % (self.player_id, self.map_id)
256
257
258 def initialize_db(engine=None):
259     DBSession.configure(bind=engine)
260     Base.metadata.bind = engine
261     Base.metadata.create_all(engine)
262     MetaData = sqlalchemy.MetaData(bind=engine, reflect=True)
263
264     # assign all those tables to an object
265     achievements_table = MetaData.tables['achievements']
266     cd_achievement_table = MetaData.tables['cd_achievement']
267     cd_game_type_table = MetaData.tables['cd_game_type']
268     cd_weapon_table = MetaData.tables['cd_weapon']
269     db_version_table = MetaData.tables['db_version']
270     games_table = MetaData.tables['games']
271     hashkeys_table = MetaData.tables['hashkeys']
272     maps_table = MetaData.tables['maps']
273     player_game_stats_table = MetaData.tables['player_game_stats']
274     players_table = MetaData.tables['players']
275     player_weapon_stats_table = MetaData.tables['player_weapon_stats']
276     servers_table = MetaData.tables['servers']
277     player_nicks_table = MetaData.tables['player_nicks']
278     player_elos_table = MetaData.tables['player_elos']
279     player_ranks_table = MetaData.tables['player_ranks']
280     player_captimes_table = MetaData.tables['player_map_captimes']
281
282     # now map the tables and the objects together
283     mapper(PlayerAchievement, achievements_table)
284     mapper(Achievement, cd_achievement_table)
285     mapper(GameType, cd_game_type_table)
286     mapper(Weapon, cd_weapon_table)
287     mapper(Game, games_table)
288     mapper(Hashkey, hashkeys_table)
289     mapper(Map, maps_table)
290     mapper(PlayerGameStat, player_game_stats_table)
291     mapper(Player, players_table)
292     mapper(PlayerWeaponStat, player_weapon_stats_table)
293     mapper(Server, servers_table)
294     mapper(PlayerNick, player_nicks_table)
295     mapper(PlayerElo, player_elos_table)
296     mapper(PlayerRank, player_ranks_table)
297     mapper(PlayerCaptime, player_captimes_table)