]> git.xonotic.org Git - xonotic/xonstat.git/blob - xonstat/models.py
317e004dd8eda7c498b264ada958b3f1d4faab73
[xonotic/xonstat.git] / xonstat / models.py
1 import sqlalchemy
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
7
8 DBSession = scoped_session(sessionmaker())
9 Base = declarative_base()
10
11 # define objects for all tables
12 class Player(object):
13     def __repr__(self):
14         return "<Player(%s, %s, %s, %s)>" % (self.player_id, self.nick, 
15                 self.create_dt, self.location)
16
17
18 class Mutator(object):
19     def __repr__(self):
20         return "<Mutator(%s, %s, %s, %s)>" % (self.mutator_cd, self.name, 
21                 self.descr, self.active_ind)
22
23
24 class GameType(object):
25     def __repr__(self):
26         return "<GameType(%s, %s, %s)>" % (self.game_type_cd, self.descr, 
27                 self.active_ind)
28
29
30 class Weapon(object):
31     def __repr__(self):
32         return "<Weapon(%s, %s, %s)>" % (self.weapon_cd, self.descr, 
33                 self.active_ind)
34
35
36 class Server(object):
37     def __init__(self, name=None):
38         self.name = name
39
40     def __repr__(self):
41         return "<Server(%s, %s, %s)>" % (self.server_id, self.name, 
42                 self.ip_addr)
43
44
45 class Map(object):
46     def __init__(self, name=None):
47         self.name = name
48
49     def __repr__(self):
50         return "<Map(%s, %s, %s)>" % (self.map_id, self.name, self.version)
51
52
53 class MapGameType(object):
54     def __repr__(self):
55         return "<MapGameType(%s, %s)>" % (self.map_id, self.game_type_cd)
56
57
58 class Game(object):
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
64         self.map_id = map_id
65         self.winner = winner
66
67     def __repr__(self):
68         return "<Game(%s, %s, %s, %s)>" % (self.game_id, self.start_dt, 
69                 self.game_type_cd, self.server_id)
70
71
72 class PlayerGameStat(object):
73     def __init__(self, create_dt=None):
74         self.create_dt = create_dt
75
76     def __repr__(self):
77         return "<PlayerGameStat(%s, %s, %s, %s)>" \
78         % (self.player_id, self.game_id, self.create_dt, self.stat_type)
79
80     def nick_stripped(self):
81         return strip_colors(self.nick)
82
83     def nick_html_colors(self):
84         return html_colors(self.nick)
85
86
87 class GameMutator(object):
88     def __repr__(self):
89         return "<GameMutator(%s, %s)>" % (self.game_id, self.mutator_cd)
90
91 class Achievement(object):
92     def __repr__(self):
93         return "<Achievement(%s, %s, %s)>" % (self.achievement_cd, self.descr,
94                 self.limit)
95
96
97 class PlayerAchievement(object):
98     def __repr__(self):
99         return "<PlayerAchievement(%s, %s)>" % (self.player_id, self.achievement_cd)
100
101
102 class PlayerWeaponStat(object):
103     def __repr__(self):
104         return "<PlayerWeaponStat(%s, %s, %s)>" % (self.player_weapon_stats_id,
105                 self.player_id, self.game_id)
106
107
108 class Hashkey(object):
109     def __init__(self, player_id=None, hashkey=None):
110         self.player_id = player_id
111         self.hashkey = hashkey
112
113     def __repr__(self):
114         return "<Hashkey(%s, %s)>" % (self.player_id, self.hashkey)
115
116
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)
122
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']
139
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)