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 return "<Player(%s, %s, %s, %s)>" % (self.player_id, self.nick,
15 self.create_dt, self.location)
18 class Mutator(object):
20 return "<Mutator(%s, %s, %s, %s)>" % (self.mutator_cd, self.name,
21 self.descr, self.active_ind)
24 class GameType(object):
26 return "<GameType(%s, %s, %s)>" % (self.game_type_cd, self.descr,
32 return "<Weapon(%s, %s, %s)>" % (self.weapon_cd, self.descr,
37 def __init__(self, name=None):
41 return "<Server(%s, %s, %s)>" % (self.server_id, self.name,
46 def __init__(self, name=None):
50 return "<Map(%s, %s, %s)>" % (self.map_id, self.name, self.version)
53 class MapGameType(object):
55 return "<MapGameType(%s, %s)>" % (self.map_id, self.game_type_cd)
59 def __init__(self, start_dt=None, game_type_cd=None,
60 server_id=None, map_id=None, winner=None):
61 self.start_dt = start_dt
62 self.game_type_cd = game_type_cd
63 self.server_id = server_id
68 return "<Game(%s, %s, %s, %s)>" % (self.game_id, self.start_dt,
69 self.game_type_cd, self.server_id)
72 class PlayerGameStat(object):
73 def __init__(self, create_dt=None):
74 self.create_dt = create_dt
77 return "<PlayerGameStat(%s, %s, %s, %s)>" \
78 % (self.player_id, self.game_id, self.create_dt, self.stat_type)
80 def nick_stripped(self):
81 return strip_colors(self.nick)
83 def nick_html_colors(self):
84 return html_colors(self.nick)
87 class GameMutator(object):
89 return "<GameMutator(%s, %s)>" % (self.game_id, self.mutator_cd)
91 class Achievement(object):
93 return "<Achievement(%s, %s, %s)>" % (self.achievement_cd, self.descr,
97 class PlayerAchievement(object):
99 return "<PlayerAchievement(%s, %s)>" % (self.player_id, self.achievement_cd)
102 class PlayerWeaponStat(object):
104 return "<PlayerWeaponStat(%s, %s, %s)>" % (self.player_weapon_stats_id,
105 self.player_id, self.game_id)
108 class Hashkey(object):
109 def __init__(self, player_id=None, hashkey=None):
110 self.player_id = player_id
111 self.hashkey = hashkey
114 return "<Hashkey(%s, %s)>" % (self.player_id, self.hashkey)
117 def initialize_db(engine=None):
118 DBSession.configure(bind=engine)
119 Base.metadata.bind = engine
120 Base.metadata.create_all(engine)
121 MetaData = sqlalchemy.MetaData(bind=engine, reflect=True)
123 # assign all those tables to an object
124 achievements_table = MetaData.tables['achievements']
125 cd_achievement_table = MetaData.tables['cd_achievement']
126 cd_game_type_table = MetaData.tables['cd_game_type']
127 cd_mutator_table = MetaData.tables['cd_mutator']
128 cd_weapon_table = MetaData.tables['cd_weapon']
129 db_version_table = MetaData.tables['db_version']
130 game_mutators_table = MetaData.tables['game_mutators']
131 games_table = MetaData.tables['games']
132 hashkeys_table = MetaData.tables['hashkeys']
133 map_game_types_table = MetaData.tables['map_game_types']
134 maps_table = MetaData.tables['maps']
135 player_game_stats_table = MetaData.tables['player_game_stats']
136 players_table = MetaData.tables['players']
137 player_weapon_stats_table = MetaData.tables['player_weapon_stats']
138 servers_table = MetaData.tables['servers']
140 # now map the tables and the objects together
141 mapper(PlayerAchievement, achievements_table)
142 mapper(Achievement, cd_achievement_table)
143 mapper(GameType, cd_game_type_table)
144 mapper(Mutator, cd_mutator_table)
145 mapper(Weapon, cd_weapon_table)
146 mapper(GameMutator, game_mutators_table)
147 mapper(Game, games_table)
148 mapper(Hashkey, hashkeys_table)
149 mapper(MapGameType, map_game_types_table)
150 mapper(Map, maps_table)
151 mapper(PlayerGameStat, player_game_stats_table)
152 mapper(Player, players_table)
153 mapper(PlayerWeaponStat, player_weapon_stats_table)
154 mapper(Server, servers_table)