]> git.xonotic.org Git - xonotic/xonstat.git/blob - xonstat/models.py
Address unicode issues with repr and finding servers. Make nick and server fields...
[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, pretty_date
7
8 DBSession = scoped_session(sessionmaker())
9 Base = declarative_base()
10
11 # define objects for all tables
12 class Player(object):
13
14     def nick_html_colors(self):
15         if self.nick is None:
16             return "Anonymous Player"
17         else:
18             return html_colors(self.nick)
19
20     def nick_strip_colors(self):
21         if self.nick is None:
22             return "Anonymous Player"
23         else:
24             return strip_colors(self.nick)
25
26     def __repr__(self):
27         return "<Player(%s, %s)>" % (self.player_id, 
28                 self.nick.encode('utf-8'))
29
30
31 class Mutator(object):
32     def __repr__(self):
33         return "<Mutator(%s, %s, %s, %s)>" % (self.mutator_cd, self.name, 
34                 self.descr, self.active_ind)
35
36
37 class GameType(object):
38     def __repr__(self):
39         return "<GameType(%s, %s, %s)>" % (self.game_type_cd, self.descr, 
40                 self.active_ind)
41
42
43 class Weapon(object):
44     def __repr__(self):
45         return "<Weapon(%s, %s, %s)>" % (self.weapon_cd, self.descr, 
46                 self.active_ind)
47
48
49 class Server(object):
50     def __init__(self, name=None):
51         self.name = name
52
53     def __repr__(self):
54         return "<Server(%s, %s)>" % (self.server_id, self.name.encode('utf-8'))
55
56
57 class Map(object):
58     def __init__(self, name=None):
59         self.name = name
60
61     def __repr__(self):
62         return "<Map(%s, %s, %s)>" % (self.map_id, self.name, self.version)
63
64
65 class MapGameType(object):
66     def __repr__(self):
67         return "<MapGameType(%s, %s)>" % (self.map_id, self.game_type_cd)
68
69
70 class Game(object):
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
76         self.map_id = map_id
77         self.winner = winner
78
79     def __repr__(self):
80         return "<Game(%s, %s, %s, %s)>" % (self.game_id, self.start_dt, 
81                 self.game_type_cd, self.server_id)
82
83     def fuzzy_date(self):
84         return pretty_date(self.start_dt)
85
86
87 class PlayerGameStat(object):
88     def __init__(self, create_dt=None):
89         self.create_dt = create_dt
90
91     def __repr__(self):
92         return "<PlayerGameStat(%s, %s, %s, %s)>" \
93         % (self.player_id, self.game_id, self.create_dt, self.stat_type)
94
95     def nick_stripped(self):
96         if self.nick is None:
97             return "Anonymous Player"
98         else:
99             return strip_colors(self.nick)
100
101     def nick_html_colors(self):
102         if self.nick is None:
103             return "Anonymous Player"
104         else:
105             return html_colors(self.nick)
106
107     def team_html_color(self):
108         # blue
109         if self.team == 5:
110             return "blue"
111         # red
112         if self.team == 14:
113             return "red"
114         if self.team == 13:
115             return "yellow"
116         if self.team == 10:
117             return "pink"
118
119
120 class GameMutator(object):
121     def __repr__(self):
122         return "<GameMutator(%s, %s)>" % (self.game_id, self.mutator_cd)
123
124 class Achievement(object):
125     def __repr__(self):
126         return "<Achievement(%s, %s, %s)>" % (self.achievement_cd, self.descr,
127                 self.limit)
128
129
130 class PlayerAchievement(object):
131     def __repr__(self):
132         return "<PlayerAchievement(%s, %s)>" % (self.player_id, self.achievement_cd)
133
134
135 class PlayerWeaponStat(object):
136     def __repr__(self):
137         return "<PlayerWeaponStat(%s, %s, %s)>" % (self.player_weapon_stats_id,
138                 self.player_id, self.game_id)
139
140
141 class Hashkey(object):
142     def __init__(self, player_id=None, hashkey=None):
143         self.player_id = player_id
144         self.hashkey = hashkey
145
146     def __repr__(self):
147         return "<Hashkey(%s, %s)>" % (self.player_id, self.hashkey)
148
149
150 class PlayerNick(object):
151     def __repr__(self):
152         return "<PlayerNick(%s, %s)>" % (self.player_id, self.stripped_nick)
153
154
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)
160
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']
178
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)