2 from sqlalchemy.orm import mapper
3 from sqlalchemy.orm import scoped_session
4 from sqlalchemy.orm import sessionmaker
5 from sqlalchemy.ext.declarative import declarative_base
6 from xonstat.util import strip_colors, html_colors, pretty_date
8 DBSession = scoped_session(sessionmaker())
9 Base = declarative_base()
11 # define objects for all tables
13 def nick_html_colors(self):
15 return "Anonymous Player"
17 return html_colors(self.nick)
19 def nick_strip_colors(self):
21 return "Anonymous Player"
23 return strip_colors(self.nick)
26 return "<Player(%s, %s)>" % (self.player_id,
27 self.nick.encode('utf-8'))
30 class GameType(object):
32 return "<GameType(%s, %s, %s)>" % (self.game_type_cd, self.descr,
38 return "<Weapon(%s, %s, %s)>" % (self.weapon_cd, self.descr,
43 def __init__(self, name=None, hashkey=None, ip_addr=None):
45 self.hashkey = hashkey
46 self.ip_addr = ip_addr
49 return "<Server(%s, %s)>" % (self.server_id, self.name.encode('utf-8'))
53 def __init__(self, name=None):
57 return "<Map(%s, %s, %s)>" % (self.map_id, self.name, self.version)
61 def __init__(self, start_dt=None, game_type_cd=None,
62 server_id=None, map_id=None, winner=None):
63 self.start_dt = start_dt
64 self.game_type_cd = game_type_cd
65 self.server_id = server_id
70 return "<Game(%s, %s, %s, %s)>" % (self.game_id, self.start_dt,
71 self.game_type_cd, self.server_id)
74 return pretty_date(self.start_dt)
77 class PlayerGameStat(object):
78 def __init__(self, create_dt=None):
79 self.create_dt = create_dt
82 return "<PlayerGameStat(%s, %s, %s)>" \
83 % (self.player_id, self.game_id, self.create_dt)
85 def nick_stripped(self):
87 return "Anonymous Player"
89 return strip_colors(self.nick)
91 def nick_html_colors(self):
93 return "Anonymous Player"
95 return html_colors(self.nick)
97 def team_html_color(self):
110 class Achievement(object):
112 return "<Achievement(%s, %s, %s)>" % (self.achievement_cd, self.descr,
116 class PlayerAchievement(object):
118 return "<PlayerAchievement(%s, %s)>" % (self.player_id, self.achievement_cd)
121 class PlayerWeaponStat(object):
123 return "<PlayerWeaponStat(%s, %s, %s)>" % (self.player_weapon_stats_id,
124 self.player_id, self.game_id)
127 class Hashkey(object):
128 def __init__(self, player_id=None, hashkey=None):
129 self.player_id = player_id
130 self.hashkey = hashkey
133 return "<Hashkey(%s, %s)>" % (self.player_id, self.hashkey)
136 class PlayerNick(object):
138 return "<PlayerNick(%s, %s)>" % (self.player_id, self.stripped_nick)
141 def initialize_db(engine=None):
142 DBSession.configure(bind=engine)
143 Base.metadata.bind = engine
144 Base.metadata.create_all(engine)
145 MetaData = sqlalchemy.MetaData(bind=engine, reflect=True)
147 # assign all those tables to an object
148 achievements_table = MetaData.tables['achievements']
149 cd_achievement_table = MetaData.tables['cd_achievement']
150 cd_game_type_table = MetaData.tables['cd_game_type']
151 cd_weapon_table = MetaData.tables['cd_weapon']
152 db_version_table = MetaData.tables['db_version']
153 games_table = MetaData.tables['games']
154 hashkeys_table = MetaData.tables['hashkeys']
155 maps_table = MetaData.tables['maps']
156 player_game_stats_table = MetaData.tables['player_game_stats']
157 players_table = MetaData.tables['players']
158 player_weapon_stats_table = MetaData.tables['player_weapon_stats']
159 servers_table = MetaData.tables['servers']
160 player_nicks_table = MetaData.tables['player_nicks']
162 # now map the tables and the objects together
163 mapper(PlayerAchievement, achievements_table)
164 mapper(Achievement, cd_achievement_table)
165 mapper(GameType, cd_game_type_table)
166 mapper(Weapon, cd_weapon_table)
167 mapper(Game, games_table)
168 mapper(Hashkey, hashkeys_table)
169 mapper(Map, maps_table)
170 mapper(PlayerGameStat, player_game_stats_table)
171 mapper(Player, players_table)
172 mapper(PlayerWeaponStat, player_weapon_stats_table)
173 mapper(Server, servers_table)
174 mapper(PlayerNick, player_nicks_table)