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
14 log = logging.getLogger(__name__)
16 DBSession = scoped_session(sessionmaker())
17 Base = declarative_base()
19 # define objects for all tables
21 def nick_html_colors(self):
23 return "Anonymous Player"
25 return html_colors(self.nick)
27 def nick_strip_colors(self):
29 return "Anonymous Player"
31 return strip_colors(self.nick)
33 def joined_pretty_date(self):
34 return pretty_date(self.create_dt)
37 return "<Player(%s, %s)>" % (self.player_id, self.nick.encode('utf-8'))
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}
46 return timegm(self.create_dt.timetuple())
49 class GameType(object):
51 return "<GameType(%s, %s, %s)>" % (self.game_type_cd, self.descr, self.active_ind)
54 return {'game_type_cd':self.game_type_cd, 'name':self.descr, 'active':self.active_ind}
59 return "<Weapon(%s, %s, %s)>" % (self.weapon_cd, self.descr, self.active_ind)
62 return {'weapon_cd':self.weapon_cd, 'name':self.descr, 'active':self.active_ind}
66 def __init__(self, name=None, hashkey=None, ip_addr=None):
68 self.hashkey = hashkey
69 self.ip_addr = ip_addr
72 return "<Server(%s, %s)>" % (self.server_id, self.name.encode('utf-8'))
75 return {'server_id':self.server_id, 'name':self.name,
76 'ip_addr':self.ip_addr, 'location':self.location}
79 return pretty_date(self.create_dt)
82 return timegm(self.create_dt.timetuple())
86 def __init__(self, name=None):
90 return "<Map(%s, %s, %s)>" % (self.map_id, self.name, self.version)
93 return {'map_id':self.map_id, 'name':self.name, 'version':self.version,}
96 return pretty_date(self.create_dt)
99 return timegm(self.create_dt.timetuple())
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
113 return "<Game(%s, %s, %s, %s)>" % (self.game_id, self.start_dt, self.game_type_cd, self.server_id)
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}
119 def fuzzy_date(self):
120 return pretty_date(self.start_dt)
123 return timegm(self.start_dt.timetuple())
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
132 return "<PlayerGameStat(%s, %s, %s)>" % (self.player_id, self.game_id, self.create_dt)
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}
139 def nick_stripped(self):
140 if self.nick is None:
141 return "Anonymous Player"
143 return strip_colors(self.nick)
145 def nick_html_colors(self):
146 if self.nick is None:
147 return "Anonymous Player"
149 return html_colors(self.nick)
151 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, self.stripped_nick)
212 return {'player_id':self.player_id, 'name':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):
234 if self.nick is None:
235 return "Anonymous Player"
237 return html_colors(self.nick)
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 def initialize_db(engine=None):
247 DBSession.configure(bind=engine)
248 Base.metadata.bind = engine
249 Base.metadata.create_all(engine)
250 MetaData = sqlalchemy.MetaData(bind=engine, reflect=True)
252 # assign all those tables to an object
253 achievements_table = MetaData.tables['achievements']
254 cd_achievement_table = MetaData.tables['cd_achievement']
255 cd_game_type_table = MetaData.tables['cd_game_type']
256 cd_weapon_table = MetaData.tables['cd_weapon']
257 db_version_table = MetaData.tables['db_version']
258 games_table = MetaData.tables['games']
259 hashkeys_table = MetaData.tables['hashkeys']
260 maps_table = MetaData.tables['maps']
261 player_game_stats_table = MetaData.tables['player_game_stats']
262 players_table = MetaData.tables['players']
263 player_weapon_stats_table = MetaData.tables['player_weapon_stats']
264 servers_table = MetaData.tables['servers']
265 player_nicks_table = MetaData.tables['player_nicks']
266 player_elos_table = MetaData.tables['player_elos']
267 player_ranks_table = MetaData.tables['player_ranks']
269 # now map the tables and the objects together
270 mapper(PlayerAchievement, achievements_table)
271 mapper(Achievement, cd_achievement_table)
272 mapper(GameType, cd_game_type_table)
273 mapper(Weapon, cd_weapon_table)
274 mapper(Game, games_table)
275 mapper(Hashkey, hashkeys_table)
276 mapper(Map, maps_table)
277 mapper(PlayerGameStat, player_game_stats_table)
278 mapper(Player, players_table)
279 mapper(PlayerWeaponStat, player_weapon_stats_table)
280 mapper(Server, servers_table)
281 mapper(PlayerNick, player_nicks_table)
282 mapper(PlayerElo, player_elos_table)
283 mapper(PlayerRank, player_ranks_table)