Use score-scaling Elo for non-duels.
[xonotic/xonstat.git] / xonstat / models.py
1 import json
2 import logging
3 import math
4 import sqlalchemy
5 import sqlalchemy.sql.functions as sfunc
6 from datetime import timedelta
7 from sqlalchemy.orm import mapper
8 from sqlalchemy.orm import scoped_session
9 from sqlalchemy.orm import sessionmaker
10 from sqlalchemy.ext.declarative import declarative_base
11 from xonstat.util import strip_colors, html_colors, pretty_date
12
13 log = logging.getLogger(__name__)
14
15 DBSession = scoped_session(sessionmaker())
16 Base = declarative_base()
17
18 # define objects for all tables
19 class Player(object):
20     def nick_html_colors(self):
21         if self.nick is None:
22             return "Anonymous Player"
23         else:
24             return html_colors(self.nick)
25
26     def nick_strip_colors(self):
27         if self.nick is None:
28             return "Anonymous Player"
29         else:
30             return strip_colors(self.nick)
31
32     def joined_pretty_date(self):
33         return pretty_date(self.create_dt)
34
35     def __repr__(self):
36         return "<Player(%s, %s)>" % (self.player_id, self.nick.encode('utf-8'))
37
38     def to_dict(self):
39         return {'player_id':self.player_id, 'name':self.nick.encode('utf-8')}
40
41
42 class GameType(object):
43     def __repr__(self):
44         return "<GameType(%s, %s, %s)>" % (self.game_type_cd, self.descr, self.active_ind)
45
46     def to_dict(self):
47         return {'game_type_cd':self.game_type_cd, 'name':self.descr, 'active':self.active_ind}
48
49
50 class Weapon(object):
51     def __repr__(self):
52         return "<Weapon(%s, %s, %s)>" % (self.weapon_cd, self.descr, self.active_ind)
53
54     def to_dict(self):
55         return {'weapon_cd':self.weapon_cd, 'name':self.descr, 'active':self.active_ind}
56
57
58 class Server(object):
59     def __init__(self, name=None, hashkey=None, ip_addr=None):
60         self.name = name
61         self.hashkey = hashkey
62         self.ip_addr = ip_addr
63
64     def __repr__(self):
65         return "<Server(%s, %s)>" % (self.server_id, self.name.encode('utf-8'))
66
67     def to_dict(self):
68         return {'server_id':self.server_id, 'name':self.name.encode('utf-8')}
69
70
71 class Map(object):
72     def __init__(self, name=None):
73         self.name = name
74
75     def __repr__(self):
76         return "<Map(%s, %s, %s)>" % (self.map_id, self.name, self.version)
77
78     def to_dict(self):
79         return {'map_id':self.map_id, 'name':self.name, 'version':self.version}
80
81
82 class Game(object):
83     def __init__(self, game_id=None, start_dt=None, game_type_cd=None, 
84             server_id=None, map_id=None, winner=None):
85         self.game_id = game_id
86         self.start_dt = start_dt
87         self.game_type_cd = game_type_cd
88         self.server_id = server_id
89         self.map_id = map_id
90         self.winner = winner
91
92     def __repr__(self):
93         return "<Game(%s, %s, %s, %s)>" % (self.game_id, self.start_dt, self.game_type_cd, self.server_id)
94
95     def to_dict(self):
96         return {'game_id':self.game_id, 'start':self.start_dt.strftime('%Y-%m-%dT%H:%M:%SZ'), 'game_type_cd':self.game_type_cd, 'server_id':self.server_id}
97
98     def fuzzy_date(self):
99         return pretty_date(self.start_dt)
100
101
102 class PlayerGameStat(object):
103     def __init__(self, player_game_stat_id=None, create_dt=None):
104         self.player_game_stat_id = player_game_stat_id
105         self.create_dt = create_dt
106
107     def __repr__(self):
108         return "<PlayerGameStat(%s, %s, %s)>" % (self.player_id, self.game_id, self.create_dt)
109
110     def to_dict(self):
111         return {'player_id':self.player_id, 'game_id':self.game_id, 'create_dt':self.create_dt.strftime('%Y-%m-%dT%H:%M:%SZ')}
112
113     def nick_stripped(self):
114         if self.nick is None:
115             return "Anonymous Player"
116         else:
117             return strip_colors(self.nick)
118
119     def nick_html_colors(self):
120         if self.nick is None:
121             return "Anonymous Player"
122         else:
123             return html_colors(self.nick)
124
125     def team_html_color(self):
126         # blue
127         if self.team == 5:
128             return "red"
129         # red
130         if self.team == 14:
131             return "blue"
132         if self.team == 13:
133             return "yellow"
134         if self.team == 10:
135             return "pink"
136
137
138 class Achievement(object):
139     def __repr__(self):
140         return "<Achievement(%s, %s, %s)>" % (self.achievement_cd, self.descr, self.limit)
141
142     def to_dict(self):
143         return {'achievement_cd':self.achievement_cd, 'name':self.descr, 'limit':self.limit}
144
145
146 class PlayerAchievement(object):
147     def __repr__(self):
148         return "<PlayerAchievement(%s, %s)>" % (self.player_id, self.achievement_cd)
149
150     def to_dict(self):
151         return {'player_id':self.player_id, 'achievement_cd':self.achievement_cd}
152
153
154 class PlayerWeaponStat(object):
155     def __repr__(self):
156         return "<PlayerWeaponStat(%s, %s, %s)>" % (self.player_weapon_stats_id, self.player_id, self.game_id)
157
158     def to_dict(self):
159         return {'player_weapon_stats_id':self.player_weapon_stats_id, 'player_id':self.player_id, 'game_id':self.game_id}
160
161
162 class Hashkey(object):
163     def __init__(self, player_id=None, hashkey=None):
164         self.player_id = player_id
165         self.hashkey = hashkey
166
167     def __repr__(self):
168         return "<Hashkey(%s, %s)>" % (self.player_id, self.hashkey)
169
170     def to_dict(self):
171         return {'player_id':self.player_id, 'hashkey':self.hashkey}
172
173
174 class PlayerNick(object):
175     def __repr__(self):
176         return "<PlayerNick(%s, %s)>" % (self.player_id, self.stripped_nick)
177
178     def to_dict(self):
179         return {'player_id':self.player_id, 'name':self.stripped_nick}
180
181
182 class PlayerElo(object):
183     def __init__(self, player_id=None, game_type_cd=None, elo=None):
184
185         self.player_id = player_id
186         self.game_type_cd = game_type_cd
187         self.elo = elo
188         self.score = 0
189         self.games = 0
190
191     def __repr__(self):
192         return "<PlayerElo(pid=%s, gametype=%s, elo=%s)>" % (self.player_id, self.game_type_cd, self.elo)
193
194     def to_dict(self):
195         return {'player_id':self.player_id, 'game_type_cd':self.game_type_cd, 'elo':self.elo}
196
197
198 class PlayerRank(object):
199
200     def nick_html_colors(self):
201         if self.nick is None:
202             return "Anonymous Player"
203         else:
204             return html_colors(self.nick)
205
206     def __repr__(self):
207         return "<PlayerRank(pid=%s, gametype=%s, rank=%s)>" % (self.player_id, self.game_type_cd, self.rank)
208
209     def to_dict(self):
210         return {'player_id':self.player_id, 'game_type_cd':self.game_type_cd, 'rank':self.rank}
211
212
213 def initialize_db(engine=None):
214     DBSession.configure(bind=engine)
215     Base.metadata.bind = engine
216     Base.metadata.create_all(engine)
217     MetaData = sqlalchemy.MetaData(bind=engine, reflect=True)
218
219     # assign all those tables to an object
220     achievements_table = MetaData.tables['achievements']
221     cd_achievement_table = MetaData.tables['cd_achievement']
222     cd_game_type_table = MetaData.tables['cd_game_type']
223     cd_weapon_table = MetaData.tables['cd_weapon']
224     db_version_table = MetaData.tables['db_version']
225     games_table = MetaData.tables['games']
226     hashkeys_table = MetaData.tables['hashkeys']
227     maps_table = MetaData.tables['maps']
228     player_game_stats_table = MetaData.tables['player_game_stats']
229     players_table = MetaData.tables['players']
230     player_weapon_stats_table = MetaData.tables['player_weapon_stats']
231     servers_table = MetaData.tables['servers']
232     player_nicks_table = MetaData.tables['player_nicks']
233     player_elos_table = MetaData.tables['player_elos']
234     player_ranks_table = MetaData.tables['player_ranks']
235
236     # now map the tables and the objects together
237     mapper(PlayerAchievement, achievements_table)
238     mapper(Achievement, cd_achievement_table)
239     mapper(GameType, cd_game_type_table)
240     mapper(Weapon, cd_weapon_table)
241     mapper(Game, games_table)
242     mapper(Hashkey, hashkeys_table)
243     mapper(Map, maps_table)
244     mapper(PlayerGameStat, player_game_stats_table)
245     mapper(Player, players_table)
246     mapper(PlayerWeaponStat, player_weapon_stats_table)
247     mapper(Server, servers_table)
248     mapper(PlayerNick, player_nicks_table)
249     mapper(PlayerElo, player_elos_table)
250     mapper(PlayerRank, player_ranks_table)