]> git.xonotic.org Git - xonotic/xonstat.git/blob - xonstat/models.py
Merge branch 'master' of github.com:antzucaro/XonStat
[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     def nick_html_colors(self):
14         if self.nick is None:
15             return "Anonymous Player"
16         else:
17             return html_colors(self.nick)
18
19     def nick_strip_colors(self):
20         if self.nick is None:
21             return "Anonymous Player"
22         else:
23             return strip_colors(self.nick)
24
25     def __repr__(self):
26         return "<Player(%s, %s)>" % (self.player_id, 
27                 self.nick.encode('utf-8'))
28
29
30 class GameType(object):
31     def __repr__(self):
32         return "<GameType(%s, %s, %s)>" % (self.game_type_cd, self.descr, 
33                 self.active_ind)
34
35
36 class Weapon(object):
37     def __repr__(self):
38         return "<Weapon(%s, %s, %s)>" % (self.weapon_cd, self.descr, 
39                 self.active_ind)
40
41
42 class Server(object):
43     def __init__(self, name=None, hashkey=None, ip_addr=None):
44         self.name = name
45         self.hashkey = hashkey
46         self.ip_addr = ip_addr
47
48     def __repr__(self):
49         return "<Server(%s, %s)>" % (self.server_id, self.name.encode('utf-8'))
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 Game(object):
61     def __init__(self, start_dt=None, game_type_cd=None, 
62             server_id=None, map_id=None, winner=None):
63         self.start_dt = start_dt
64         self.game_type_cd = game_type_cd
65         self.server_id = server_id
66         self.map_id = map_id
67         self.winner = winner
68
69     def __repr__(self):
70         return "<Game(%s, %s, %s, %s)>" % (self.game_id, self.start_dt, 
71                 self.game_type_cd, self.server_id)
72
73     def fuzzy_date(self):
74         return pretty_date(self.start_dt)
75
76
77 class PlayerGameStat(object):
78     def __init__(self, create_dt=None):
79         self.create_dt = create_dt
80
81     def __repr__(self):
82         return "<PlayerGameStat(%s, %s, %s)>" \
83         % (self.player_id, self.game_id, self.create_dt)
84
85     def nick_stripped(self):
86         if self.nick is None:
87             return "Anonymous Player"
88         else:
89             return strip_colors(self.nick)
90
91     def nick_html_colors(self):
92         if self.nick is None:
93             return "Anonymous Player"
94         else:
95             return html_colors(self.nick)
96
97     def team_html_color(self):
98         # blue
99         if self.team == 5:
100             return "blue"
101         # red
102         if self.team == 14:
103             return "red"
104         if self.team == 13:
105             return "yellow"
106         if self.team == 10:
107             return "pink"
108
109
110 class Achievement(object):
111     def __repr__(self):
112         return "<Achievement(%s, %s, %s)>" % (self.achievement_cd, self.descr,
113                 self.limit)
114
115
116 class PlayerAchievement(object):
117     def __repr__(self):
118         return "<PlayerAchievement(%s, %s)>" % (self.player_id, self.achievement_cd)
119
120
121 class PlayerWeaponStat(object):
122     def __repr__(self):
123         return "<PlayerWeaponStat(%s, %s, %s)>" % (self.player_weapon_stats_id,
124                 self.player_id, self.game_id)
125
126
127 class Hashkey(object):
128     def __init__(self, player_id=None, hashkey=None):
129         self.player_id = player_id
130         self.hashkey = hashkey
131
132     def __repr__(self):
133         return "<Hashkey(%s, %s)>" % (self.player_id, self.hashkey)
134
135
136 class PlayerNick(object):
137     def __repr__(self):
138         return "<PlayerNick(%s, %s)>" % (self.player_id, self.stripped_nick)
139
140
141 def initialize_db(engine=None):
142     DBSession.configure(bind=engine)
143     Base.metadata.bind = engine
144     Base.metadata.create_all(engine)
145     MetaData = sqlalchemy.MetaData(bind=engine, reflect=True)
146
147     # assign all those tables to an object
148     achievements_table = MetaData.tables['achievements']
149     cd_achievement_table = MetaData.tables['cd_achievement']
150     cd_game_type_table = MetaData.tables['cd_game_type']
151     cd_weapon_table = MetaData.tables['cd_weapon']
152     db_version_table = MetaData.tables['db_version']
153     games_table = MetaData.tables['games']
154     hashkeys_table = MetaData.tables['hashkeys']
155     maps_table = MetaData.tables['maps']
156     player_game_stats_table = MetaData.tables['player_game_stats']
157     players_table = MetaData.tables['players']
158     player_weapon_stats_table = MetaData.tables['player_weapon_stats']
159     servers_table = MetaData.tables['servers']
160     player_nicks_table = MetaData.tables['player_nicks']
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(Weapon, cd_weapon_table)
167     mapper(Game, games_table)
168     mapper(Hashkey, hashkeys_table)
169     mapper(Map, maps_table)
170     mapper(PlayerGameStat, player_game_stats_table)
171     mapper(Player, players_table)
172     mapper(PlayerWeaponStat, player_weapon_stats_table)
173     mapper(Server, servers_table)
174     mapper(PlayerNick, player_nicks_table)