]> git.xonotic.org Git - xonotic/xonstat.git/blob - xonstat/models.py
Remove <ul>s, center tables, and tweak the team colors a bit.
[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
14     def nick_html_colors(self):
15         return html_colors(self.nick)
16
17     def __repr__(self):
18         return "<Player(%s, %s, %s, %s)>" % (self.player_id, self.nick, 
19                 self.create_dt, self.location)
20
21
22 class Mutator(object):
23     def __repr__(self):
24         return "<Mutator(%s, %s, %s, %s)>" % (self.mutator_cd, self.name, 
25                 self.descr, self.active_ind)
26
27
28 class GameType(object):
29     def __repr__(self):
30         return "<GameType(%s, %s, %s)>" % (self.game_type_cd, self.descr, 
31                 self.active_ind)
32
33
34 class Weapon(object):
35     def __repr__(self):
36         return "<Weapon(%s, %s, %s)>" % (self.weapon_cd, self.descr, 
37                 self.active_ind)
38
39
40 class Server(object):
41     def __init__(self, name=None):
42         self.name = name
43
44     def __repr__(self):
45         return "<Server(%s, %s, %s)>" % (self.server_id, self.name, 
46                 self.ip_addr)
47
48
49 class Map(object):
50     def __init__(self, name=None):
51         self.name = name
52
53     def __repr__(self):
54         return "<Map(%s, %s, %s)>" % (self.map_id, self.name, self.version)
55
56
57 class MapGameType(object):
58     def __repr__(self):
59         return "<MapGameType(%s, %s)>" % (self.map_id, self.game_type_cd)
60
61
62 class Game(object):
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
68         self.map_id = map_id
69         self.winner = winner
70
71     def __repr__(self):
72         return "<Game(%s, %s, %s, %s)>" % (self.game_id, self.start_dt, 
73                 self.game_type_cd, self.server_id)
74
75
76 class PlayerGameStat(object):
77     def __init__(self, create_dt=None):
78         self.create_dt = create_dt
79
80     def __repr__(self):
81         return "<PlayerGameStat(%s, %s, %s, %s)>" \
82         % (self.player_id, self.game_id, self.create_dt, self.stat_type)
83
84     def nick_stripped(self):
85         return strip_colors(self.nick)
86
87     def nick_html_colors(self):
88         return html_colors(self.nick)
89
90     def team_html_color(self):
91         # blue
92         if self.team == 5:
93             return "#312AB1"
94         # red
95         if self.team == 14:
96             return "#FF0033"
97         if self.team == 13:
98             return "#FFFF00"
99         if self.team == 10:
100             return "#FF00FF"
101
102
103 class GameMutator(object):
104     def __repr__(self):
105         return "<GameMutator(%s, %s)>" % (self.game_id, self.mutator_cd)
106
107 class Achievement(object):
108     def __repr__(self):
109         return "<Achievement(%s, %s, %s)>" % (self.achievement_cd, self.descr,
110                 self.limit)
111
112
113 class PlayerAchievement(object):
114     def __repr__(self):
115         return "<PlayerAchievement(%s, %s)>" % (self.player_id, self.achievement_cd)
116
117
118 class PlayerWeaponStat(object):
119     def __repr__(self):
120         return "<PlayerWeaponStat(%s, %s, %s)>" % (self.player_weapon_stats_id,
121                 self.player_id, self.game_id)
122
123
124 class Hashkey(object):
125     def __init__(self, player_id=None, hashkey=None):
126         self.player_id = player_id
127         self.hashkey = hashkey
128
129     def __repr__(self):
130         return "<Hashkey(%s, %s)>" % (self.player_id, self.hashkey)
131
132
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)
138
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']
155
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)