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
8 DBSession = scoped_session(sessionmaker())
9 Base = declarative_base()
11 # define objects for all tables
14 def nick_html_colors(self):
15 return html_colors(self.nick)
18 return "<Player(%s, %s, %s, %s)>" % (self.player_id, self.nick,
19 self.create_dt, self.location)
22 class Mutator(object):
24 return "<Mutator(%s, %s, %s, %s)>" % (self.mutator_cd, self.name,
25 self.descr, self.active_ind)
28 class GameType(object):
30 return "<GameType(%s, %s, %s)>" % (self.game_type_cd, self.descr,
36 return "<Weapon(%s, %s, %s)>" % (self.weapon_cd, self.descr,
41 def __init__(self, name=None):
45 return "<Server(%s, %s, %s)>" % (self.server_id, self.name,
50 def __init__(self, name=None):
54 return "<Map(%s, %s, %s)>" % (self.map_id, self.name, self.version)
57 class MapGameType(object):
59 return "<MapGameType(%s, %s)>" % (self.map_id, self.game_type_cd)
63 def __init__(self, start_dt=None, game_type_cd=None,
64 server_id=None, map_id=None, winner=None):
65 self.start_dt = start_dt
66 self.game_type_cd = game_type_cd
67 self.server_id = server_id
72 return "<Game(%s, %s, %s, %s)>" % (self.game_id, self.start_dt,
73 self.game_type_cd, self.server_id)
76 class PlayerGameStat(object):
77 def __init__(self, create_dt=None):
78 self.create_dt = create_dt
81 return "<PlayerGameStat(%s, %s, %s, %s)>" \
82 % (self.player_id, self.game_id, self.create_dt, self.stat_type)
84 def nick_stripped(self):
85 return strip_colors(self.nick)
87 def nick_html_colors(self):
88 return html_colors(self.nick)
90 def team_html_color(self):
103 class GameMutator(object):
105 return "<GameMutator(%s, %s)>" % (self.game_id, self.mutator_cd)
107 class Achievement(object):
109 return "<Achievement(%s, %s, %s)>" % (self.achievement_cd, self.descr,
113 class PlayerAchievement(object):
115 return "<PlayerAchievement(%s, %s)>" % (self.player_id, self.achievement_cd)
118 class PlayerWeaponStat(object):
120 return "<PlayerWeaponStat(%s, %s, %s)>" % (self.player_weapon_stats_id,
121 self.player_id, self.game_id)
124 class Hashkey(object):
125 def __init__(self, player_id=None, hashkey=None):
126 self.player_id = player_id
127 self.hashkey = hashkey
130 return "<Hashkey(%s, %s)>" % (self.player_id, self.hashkey)
133 def initialize_db(engine=None):
134 DBSession.configure(bind=engine)
135 Base.metadata.bind = engine
136 Base.metadata.create_all(engine)
137 MetaData = sqlalchemy.MetaData(bind=engine, reflect=True)
139 # assign all those tables to an object
140 achievements_table = MetaData.tables['achievements']
141 cd_achievement_table = MetaData.tables['cd_achievement']
142 cd_game_type_table = MetaData.tables['cd_game_type']
143 cd_mutator_table = MetaData.tables['cd_mutator']
144 cd_weapon_table = MetaData.tables['cd_weapon']
145 db_version_table = MetaData.tables['db_version']
146 game_mutators_table = MetaData.tables['game_mutators']
147 games_table = MetaData.tables['games']
148 hashkeys_table = MetaData.tables['hashkeys']
149 map_game_types_table = MetaData.tables['map_game_types']
150 maps_table = MetaData.tables['maps']
151 player_game_stats_table = MetaData.tables['player_game_stats']
152 players_table = MetaData.tables['players']
153 player_weapon_stats_table = MetaData.tables['player_weapon_stats']
154 servers_table = MetaData.tables['servers']
156 # now map the tables and the objects together
157 mapper(PlayerAchievement, achievements_table)
158 mapper(Achievement, cd_achievement_table)
159 mapper(GameType, cd_game_type_table)
160 mapper(Mutator, cd_mutator_table)
161 mapper(Weapon, cd_weapon_table)
162 mapper(GameMutator, game_mutators_table)
163 mapper(Game, games_table)
164 mapper(Hashkey, hashkeys_table)
165 mapper(MapGameType, map_game_types_table)
166 mapper(Map, maps_table)
167 mapper(PlayerGameStat, player_game_stats_table)
168 mapper(Player, players_table)
169 mapper(PlayerWeaponStat, player_weapon_stats_table)
170 mapper(Server, servers_table)