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
14 def nick_html_colors(self):
16 return "Anonymous Player"
18 return html_colors(self.nick)
20 def nick_strip_colors(self):
22 return "Anonymous Player"
24 return strip_colors(self.nick)
27 return "<Player(%s, %s)>" % (self.player_id,
28 self.nick.encode('utf-8'))
31 class Mutator(object):
33 return "<Mutator(%s, %s, %s, %s)>" % (self.mutator_cd, self.name,
34 self.descr, self.active_ind)
37 class GameType(object):
39 return "<GameType(%s, %s, %s)>" % (self.game_type_cd, self.descr,
45 return "<Weapon(%s, %s, %s)>" % (self.weapon_cd, self.descr,
50 def __init__(self, name=None):
54 return "<Server(%s, %s)>" % (self.server_id, self.name.encode('utf-8'))
58 def __init__(self, name=None):
62 return "<Map(%s, %s, %s)>" % (self.map_id, self.name, self.version)
65 class MapGameType(object):
67 return "<MapGameType(%s, %s)>" % (self.map_id, self.game_type_cd)
71 def __init__(self, start_dt=None, game_type_cd=None,
72 server_id=None, map_id=None, winner=None):
73 self.start_dt = start_dt
74 self.game_type_cd = game_type_cd
75 self.server_id = server_id
80 return "<Game(%s, %s, %s, %s)>" % (self.game_id, self.start_dt,
81 self.game_type_cd, self.server_id)
84 return pretty_date(self.start_dt)
87 class PlayerGameStat(object):
88 def __init__(self, create_dt=None):
89 self.create_dt = create_dt
92 return "<PlayerGameStat(%s, %s, %s, %s)>" \
93 % (self.player_id, self.game_id, self.create_dt, self.stat_type)
95 def nick_stripped(self):
97 return "Anonymous Player"
99 return strip_colors(self.nick)
101 def nick_html_colors(self):
102 if self.nick is None:
103 return "Anonymous Player"
105 return html_colors(self.nick)
107 def team_html_color(self):
120 class GameMutator(object):
122 return "<GameMutator(%s, %s)>" % (self.game_id, self.mutator_cd)
124 class Achievement(object):
126 return "<Achievement(%s, %s, %s)>" % (self.achievement_cd, self.descr,
130 class PlayerAchievement(object):
132 return "<PlayerAchievement(%s, %s)>" % (self.player_id, self.achievement_cd)
135 class PlayerWeaponStat(object):
137 return "<PlayerWeaponStat(%s, %s, %s)>" % (self.player_weapon_stats_id,
138 self.player_id, self.game_id)
141 class Hashkey(object):
142 def __init__(self, player_id=None, hashkey=None):
143 self.player_id = player_id
144 self.hashkey = hashkey
147 return "<Hashkey(%s, %s)>" % (self.player_id, self.hashkey)
150 class PlayerNick(object):
152 return "<PlayerNick(%s, %s)>" % (self.player_id, self.stripped_nick)
155 def initialize_db(engine=None):
156 DBSession.configure(bind=engine)
157 Base.metadata.bind = engine
158 Base.metadata.create_all(engine)
159 MetaData = sqlalchemy.MetaData(bind=engine, reflect=True)
161 # assign all those tables to an object
162 achievements_table = MetaData.tables['achievements']
163 cd_achievement_table = MetaData.tables['cd_achievement']
164 cd_game_type_table = MetaData.tables['cd_game_type']
165 cd_mutator_table = MetaData.tables['cd_mutator']
166 cd_weapon_table = MetaData.tables['cd_weapon']
167 db_version_table = MetaData.tables['db_version']
168 game_mutators_table = MetaData.tables['game_mutators']
169 games_table = MetaData.tables['games']
170 hashkeys_table = MetaData.tables['hashkeys']
171 map_game_types_table = MetaData.tables['map_game_types']
172 maps_table = MetaData.tables['maps']
173 player_game_stats_table = MetaData.tables['player_game_stats']
174 players_table = MetaData.tables['players']
175 player_weapon_stats_table = MetaData.tables['player_weapon_stats']
176 servers_table = MetaData.tables['servers']
177 player_nicks_table = MetaData.tables['player_nicks']
179 # now map the tables and the objects together
180 mapper(PlayerAchievement, achievements_table)
181 mapper(Achievement, cd_achievement_table)
182 mapper(GameType, cd_game_type_table)
183 mapper(Mutator, cd_mutator_table)
184 mapper(Weapon, cd_weapon_table)
185 mapper(GameMutator, game_mutators_table)
186 mapper(Game, games_table)
187 mapper(Hashkey, hashkeys_table)
188 mapper(MapGameType, map_game_types_table)
189 mapper(Map, maps_table)
190 mapper(PlayerGameStat, player_game_stats_table)
191 mapper(Player, players_table)
192 mapper(PlayerWeaponStat, player_weapon_stats_table)
193 mapper(Server, servers_table)
194 mapper(PlayerNick, player_nicks_table)