6999691c6cb51508d739b0bfe56083a061f7822e
[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         return html_colors(self.nick)
16
17     def nick_strip_colors(self):
18         return strip_colors(self.nick)
19
20     def __repr__(self):
21         return "<Player(%s, %s, %s, %s)>" % (self.player_id, self.nick, 
22                 self.create_dt, self.location)
23
24
25 class Mutator(object):
26     def __repr__(self):
27         return "<Mutator(%s, %s, %s, %s)>" % (self.mutator_cd, self.name, 
28                 self.descr, self.active_ind)
29
30
31 class GameType(object):
32     def __repr__(self):
33         return "<GameType(%s, %s, %s)>" % (self.game_type_cd, self.descr, 
34                 self.active_ind)
35
36
37 class Weapon(object):
38     def __repr__(self):
39         return "<Weapon(%s, %s, %s)>" % (self.weapon_cd, self.descr, 
40                 self.active_ind)
41
42
43 class Server(object):
44     def __init__(self, name=None):
45         self.name = name
46
47     def __repr__(self):
48         return "<Server(%s, %s, %s)>" % (self.server_id, self.name, 
49                 self.ip_addr)
50
51
52 class Map(object):
53     def __init__(self, name=None):
54         self.name = name
55
56     def __repr__(self):
57         return "<Map(%s, %s, %s)>" % (self.map_id, self.name, self.version)
58
59
60 class MapGameType(object):
61     def __repr__(self):
62         return "<MapGameType(%s, %s)>" % (self.map_id, self.game_type_cd)
63
64
65 class Game(object):
66     def __init__(self, start_dt=None, game_type_cd=None, 
67             server_id=None, map_id=None, winner=None):
68         self.start_dt = start_dt
69         self.game_type_cd = game_type_cd
70         self.server_id = server_id
71         self.map_id = map_id
72         self.winner = winner
73
74     def __repr__(self):
75         return "<Game(%s, %s, %s, %s)>" % (self.game_id, self.start_dt, 
76                 self.game_type_cd, self.server_id)
77
78     def fuzzy_date(self):
79         return pretty_date(self.start_dt)
80
81
82 class PlayerGameStat(object):
83     def __init__(self, create_dt=None):
84         self.create_dt = create_dt
85
86     def __repr__(self):
87         return "<PlayerGameStat(%s, %s, %s, %s)>" \
88         % (self.player_id, self.game_id, self.create_dt, self.stat_type)
89
90     def nick_stripped(self):
91         return strip_colors(self.nick)
92
93     def nick_html_colors(self):
94         return html_colors(self.nick)
95
96     def team_html_color(self):
97         # blue
98         if self.team == 5:
99             return "#312AB1"
100         # red
101         if self.team == 14:
102             return "#FF0033"
103         if self.team == 13:
104             return "#FFFF00"
105         if self.team == 10:
106             return "#FF00FF"
107
108
109 class GameMutator(object):
110     def __repr__(self):
111         return "<GameMutator(%s, %s)>" % (self.game_id, self.mutator_cd)
112
113 class Achievement(object):
114     def __repr__(self):
115         return "<Achievement(%s, %s, %s)>" % (self.achievement_cd, self.descr,
116                 self.limit)
117
118
119 class PlayerAchievement(object):
120     def __repr__(self):
121         return "<PlayerAchievement(%s, %s)>" % (self.player_id, self.achievement_cd)
122
123
124 class PlayerWeaponStat(object):
125     def __repr__(self):
126         return "<PlayerWeaponStat(%s, %s, %s)>" % (self.player_weapon_stats_id,
127                 self.player_id, self.game_id)
128
129
130 class Hashkey(object):
131     def __init__(self, player_id=None, hashkey=None):
132         self.player_id = player_id
133         self.hashkey = hashkey
134
135     def __repr__(self):
136         return "<Hashkey(%s, %s)>" % (self.player_id, self.hashkey)
137
138
139 def initialize_db(engine=None):
140     DBSession.configure(bind=engine)
141     Base.metadata.bind = engine
142     Base.metadata.create_all(engine)
143     MetaData = sqlalchemy.MetaData(bind=engine, reflect=True)
144
145     # assign all those tables to an object
146     achievements_table = MetaData.tables['achievements']
147     cd_achievement_table = MetaData.tables['cd_achievement']
148     cd_game_type_table = MetaData.tables['cd_game_type']
149     cd_mutator_table = MetaData.tables['cd_mutator']
150     cd_weapon_table = MetaData.tables['cd_weapon']
151     db_version_table = MetaData.tables['db_version']
152     game_mutators_table = MetaData.tables['game_mutators']
153     games_table = MetaData.tables['games']
154     hashkeys_table = MetaData.tables['hashkeys']
155     map_game_types_table = MetaData.tables['map_game_types']
156     maps_table = MetaData.tables['maps']
157     player_game_stats_table = MetaData.tables['player_game_stats']
158     players_table = MetaData.tables['players']
159     player_weapon_stats_table = MetaData.tables['player_weapon_stats']
160     servers_table = MetaData.tables['servers']
161
162     # now map the tables and the objects together
163     mapper(PlayerAchievement, achievements_table)
164     mapper(Achievement, cd_achievement_table)
165     mapper(GameType, cd_game_type_table)
166     mapper(Mutator, cd_mutator_table)
167     mapper(Weapon, cd_weapon_table)
168     mapper(GameMutator, game_mutators_table)
169     mapper(Game, games_table)
170     mapper(Hashkey, hashkeys_table)
171     mapper(MapGameType, map_game_types_table)
172     mapper(Map, maps_table)
173     mapper(PlayerGameStat, player_game_stats_table)
174     mapper(Player, players_table)
175     mapper(PlayerWeaponStat, player_weapon_stats_table)
176     mapper(Server, servers_table)