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
15 log = logging.getLogger(__name__)
17 DBSession = scoped_session(sessionmaker())
18 Base = declarative_base()
20 # define objects for all tables
22 def nick_html_colors(self, limit=None):
24 return "Anonymous Player"
26 return html_colors(self.nick, limit)
28 def nick_strip_colors(self):
30 return "Anonymous Player"
32 return strip_colors(self.nick)
34 def joined_pretty_date(self):
35 return pretty_date(self.create_dt)
38 return "<Player(%s, %s)>" % (self.player_id, self.nick.encode('utf-8'))
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)}
47 return timegm(self.create_dt.timetuple())
50 class GameType(object):
52 return "<GameType(%s, %s, %s)>" % (self.game_type_cd, self.descr, self.active_ind)
55 return {'game_type_cd':self.game_type_cd, 'name':self.descr, 'active':self.active_ind}
60 return "<Weapon(%s, %s, %s)>" % (self.weapon_cd, self.descr, self.active_ind)
63 return {'weapon_cd':self.weapon_cd, 'name':self.descr, 'active':self.active_ind}
67 def __init__(self, name=None, hashkey=None, ip_addr=None):
69 self.hashkey = hashkey
70 self.ip_addr = ip_addr
71 self.create_dt = dt.utcnow()
74 return "<Server(%s, %s)>" % (self.server_id, self.name.encode('utf-8'))
77 return {'server_id':self.server_id, 'name':self.name,
78 'ip_addr':self.ip_addr, 'location':self.location}
81 return pretty_date(self.create_dt)
84 return timegm(self.create_dt.timetuple())
88 def __init__(self, name=None):
92 return "<Map(%s, %s, %s)>" % (self.map_id, self.name, self.version)
95 return {'map_id':self.map_id, 'name':self.name, 'version':self.version,}
98 return pretty_date(self.create_dt)
101 return timegm(self.create_dt.timetuple())
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
115 return "<Game(%s, %s, %s, %s)>" % (self.game_id, self.start_dt, self.game_type_cd, self.server_id)
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}
121 def fuzzy_date(self):
122 return pretty_date(self.start_dt)
125 return timegm(self.start_dt.timetuple())
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
134 return "<PlayerGameStat(%s, %s, %s)>" % (self.player_id, self.game_id, self.create_dt)
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}
141 def nick_stripped(self):
142 if self.nick is None:
143 return "Anonymous Player"
145 return strip_colors(self.nick)
147 def nick_html_colors(self, limit=None):
148 if self.nick is None:
149 return "Anonymous Player"
151 return html_colors(self.nick, limit)
153 def team_html_color(self):
164 class Achievement(object):
166 return "<Achievement(%s, %s, %s)>" % (self.achievement_cd, self.descr, self.limit)
169 return {'achievement_cd':self.achievement_cd, 'name':self.descr, 'limit':self.limit}
172 class PlayerAchievement(object):
174 return "<PlayerAchievement(%s, %s)>" % (self.player_id, self.achievement_cd)
177 return {'player_id':self.player_id, 'achievement_cd':self.achievement_cd}
180 class PlayerWeaponStat(object):
189 return "<PlayerWeaponStat(%s, %s, %s)>" % (self.player_weapon_stats_id, self.player_id, self.game_id)
192 return {'player_weapon_stats_id':self.player_weapon_stats_id, 'player_id':self.player_id, 'game_id':self.game_id}
195 class Hashkey(object):
196 def __init__(self, player_id=None, hashkey=None):
197 self.player_id = player_id
198 self.hashkey = hashkey
201 return "<Hashkey(%s, %s)>" % (self.player_id, self.hashkey)
204 return {'player_id':self.player_id, 'hashkey':self.hashkey}
207 class PlayerNick(object):
209 return "<PlayerNick(%s, %s)>" % (self.player_id, qfont_decode(self.stripped_nick))
212 return {'player_id':self.player_id, 'name':qfont_decode(self.stripped_nick)}
215 class PlayerElo(object):
216 def __init__(self, player_id=None, game_type_cd=None, elo=None):
218 self.player_id = player_id
219 self.game_type_cd = game_type_cd
225 return "<PlayerElo(pid=%s, gametype=%s, elo=%s, games=%s)>" % (self.player_id, self.game_type_cd, self.elo, self.games)
228 return {'player_id':self.player_id, 'game_type_cd':self.game_type_cd, 'elo':self.elo, 'games':self.games}
231 class PlayerRank(object):
233 def nick_html_colors(self, limit=None):
234 if self.nick is None:
235 return "Anonymous Player"
237 return html_colors(self.nick, limit)
240 return "<PlayerRank(pid=%s, gametype=%s, rank=%s)>" % (self.player_id, self.game_type_cd, self.rank)
243 return {'player_id':self.player_id, 'game_type_cd':self.game_type_cd, 'rank':self.rank}
246 class PlayerCaptime(object):
247 def __init__(self, player_id=None, game_id=None, map_id=None,
249 self.player_id = player_id
250 self.game_id = game_id
252 self.fastest_cap = fastest_cap
255 return "<PlayerCaptime(pid=%s, map_id=%s)>" % (self.player_id, self.map_id)
258 class SummaryStat(object):
260 return "<SummaryStat(total_players=%s, total_games=%s, total_servers=%s)>" % (self.total_players, self.total_games, self.total_servers)
263 def initialize_db(engine=None):
264 DBSession.configure(bind=engine)
265 Base.metadata.bind = engine
266 Base.metadata.create_all(engine)
267 MetaData = sqlalchemy.MetaData(bind=engine, reflect=True)
269 # assign all those tables to an object
270 achievements_table = MetaData.tables['achievements']
271 cd_achievement_table = MetaData.tables['cd_achievement']
272 cd_game_type_table = MetaData.tables['cd_game_type']
273 cd_weapon_table = MetaData.tables['cd_weapon']
274 db_version_table = MetaData.tables['db_version']
275 games_table = MetaData.tables['games']
276 hashkeys_table = MetaData.tables['hashkeys']
277 maps_table = MetaData.tables['maps']
278 player_game_stats_table = MetaData.tables['player_game_stats']
279 players_table = MetaData.tables['players']
280 player_weapon_stats_table = MetaData.tables['player_weapon_stats']
281 servers_table = MetaData.tables['servers']
282 player_nicks_table = MetaData.tables['player_nicks']
283 player_elos_table = MetaData.tables['player_elos']
284 player_ranks_table = MetaData.tables['player_ranks']
285 player_captimes_table = MetaData.tables['player_map_captimes']
286 summary_stats_table = MetaData.tables['summary_stats']
288 # now map the tables and the objects together
289 mapper(PlayerAchievement, achievements_table)
290 mapper(Achievement, cd_achievement_table)
291 mapper(GameType, cd_game_type_table)
292 mapper(Weapon, cd_weapon_table)
293 mapper(Game, games_table)
294 mapper(Hashkey, hashkeys_table)
295 mapper(Map, maps_table)
296 mapper(PlayerGameStat, player_game_stats_table)
297 mapper(Player, players_table)
298 mapper(PlayerWeaponStat, player_weapon_stats_table)
299 mapper(Server, servers_table)
300 mapper(PlayerNick, player_nicks_table)
301 mapper(PlayerElo, player_elos_table)
302 mapper(PlayerRank, player_ranks_table)
303 mapper(PlayerCaptime, player_captimes_table)
304 mapper(SummaryStat, summary_stats_table)