33c33ecf361732785ed70cff9a4714985594ebf9
[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.elo import KREDUCTION, ELOPARMS
12 from xonstat.util import strip_colors, html_colors, pretty_date
13
14 log = logging.getLogger(__name__)
15
16 DBSession = scoped_session(sessionmaker())
17 Base = declarative_base()
18
19 # define objects for all tables
20 class Player(object):
21     def nick_html_colors(self):
22         if self.nick is None:
23             return "Anonymous Player"
24         else:
25             return html_colors(self.nick)
26
27     def nick_strip_colors(self):
28         if self.nick is None:
29             return "Anonymous Player"
30         else:
31             return strip_colors(self.nick)
32
33     def joined_pretty_date(self):
34         return pretty_date(self.create_dt)
35
36     def __repr__(self):
37         return "<Player(%s, %s)>" % (self.player_id, self.nick.encode('utf-8'))
38
39     def to_dict(self):
40         return {'player_id':self.player_id, 'name':self.nick.encode('utf-8')}
41
42
43 class GameType(object):
44     def __repr__(self):
45         return "<GameType(%s, %s, %s)>" % (self.game_type_cd, self.descr, self.active_ind)
46
47     def to_dict(self):
48         return {'game_type_cd':self.game_type_cd, 'name':self.descr, 'active':self.active_ind}
49
50
51 class Weapon(object):
52     def __repr__(self):
53         return "<Weapon(%s, %s, %s)>" % (self.weapon_cd, self.descr, self.active_ind)
54
55     def to_dict(self):
56         return {'weapon_cd':self.weapon_cd, 'name':self.descr, 'active':self.active_ind}
57
58
59 class Server(object):
60     def __init__(self, name=None, hashkey=None, ip_addr=None):
61         self.name = name
62         self.hashkey = hashkey
63         self.ip_addr = ip_addr
64
65     def __repr__(self):
66         return "<Server(%s, %s)>" % (self.server_id, self.name.encode('utf-8'))
67
68     def to_dict(self):
69         return {'server_id':self.server_id, 'name':self.name.encode('utf-8')}
70
71
72 class Map(object):
73     def __init__(self, name=None):
74         self.name = name
75
76     def __repr__(self):
77         return "<Map(%s, %s, %s)>" % (self.map_id, self.name, self.version)
78
79     def to_dict(self):
80         return {'map_id':self.map_id, 'name':self.name, 'version':self.version}
81
82
83 class Game(object):
84     def __init__(self, game_id=None, start_dt=None, game_type_cd=None, 
85             server_id=None, map_id=None, winner=None):
86         self.game_id = game_id
87         self.start_dt = start_dt
88         self.game_type_cd = game_type_cd
89         self.server_id = server_id
90         self.map_id = map_id
91         self.winner = winner
92
93     def __repr__(self):
94         return "<Game(%s, %s, %s, %s)>" % (self.game_id, self.start_dt, self.game_type_cd, self.server_id)
95
96     def to_dict(self):
97         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}
98
99     def fuzzy_date(self):
100         return pretty_date(self.start_dt)
101
102     def process_elos(self, session, game_type_cd=None):
103         if game_type_cd is None:
104             game_type_cd = self.game_type_cd
105
106         # we do not have the actual duration of the game, so use the 
107         # maximum alivetime of the players instead
108         duration = 0
109         for d in session.query(sfunc.max(PlayerGameStat.alivetime)).\
110                     filter(PlayerGameStat.game_id==self.game_id).\
111                     one():
112             duration = d.seconds
113
114         scores = {}
115         alivetimes = {}
116         winners = []
117         for (p,s,a,r,t) in session.query(PlayerGameStat.player_id, 
118                 PlayerGameStat.score, PlayerGameStat.alivetime,
119                 PlayerGameStat.rank, PlayerGameStat.team).\
120                 filter(PlayerGameStat.game_id==self.game_id).\
121                 filter(PlayerGameStat.alivetime > timedelta(seconds=0)).\
122                 filter(PlayerGameStat.player_id > 2).\
123                 all():
124                     # scores are per second
125                     scores[p] = s/float(a.seconds)
126                     alivetimes[p] = a.seconds
127
128                     # winners are either rank 1 or on the winning team
129                     # team games are where the team is set (duh)
130                     if r == 1 or (t == self.winner and t is not None):
131                         winners.append(p)
132
133         player_ids = scores.keys()
134
135         elos = {}
136         for e in session.query(PlayerElo).\
137                 filter(PlayerElo.player_id.in_(player_ids)).\
138                 filter(PlayerElo.game_type_cd==game_type_cd).all():
139                     elos[e.player_id] = e
140
141         # ensure that all player_ids have an elo record
142         for pid in player_ids:
143             if pid not in elos.keys():
144                 elos[pid] = PlayerElo(pid, game_type_cd)
145
146         for pid in player_ids:
147             elos[pid].k = KREDUCTION.eval(elos[pid].games, alivetimes[pid],
148                     duration)
149             if elos[pid].k == 0:
150                 del(elos[pid])
151                 del(scores[pid])
152                 del(alivetimes[pid])
153
154         elos = self.update_elos(session, elos, scores, winners, ELOPARMS)
155
156         # add the elos to the session for committing
157         for e in elos:
158             session.add(elos[e])
159
160         # no longer calculate DM elo for a duel game
161         # if game_type_cd == 'duel':
162             # self.process_elos(session, "dm")
163
164
165     def update_elos(self, session, elos, scores, winners, ep):
166         eloadjust = {}
167         for pid in elos.keys():
168             eloadjust[pid] = 0
169
170         if len(elos) < 2:
171             return elos
172
173         pids = elos.keys()
174
175         for i in xrange(0, len(pids)):
176             ei = elos[pids[i]]
177             for j in xrange(i+1, len(pids)):
178                 ej = elos[pids[j]]
179                 si = scores[ei.player_id]
180                 sj = scores[ej.player_id]
181
182                 # normalize scores
183                 ofs = min(0, si, sj)
184                 si -= ofs
185                 sj -= ofs
186                 if si + sj == 0:
187                     si, sj = 1, 1 # a draw
188
189                 # real score factor
190                 scorefactor_real = si / float(si + sj)
191
192                 # estimated score factor by elo
193                 elodiff = min(ep.maxlogdistance, max(-ep.maxlogdistance,
194                     (float(ei.elo) - float(ej.elo)) * ep.logdistancefactor))
195                 scorefactor_elo = 1 / (1 + math.exp(-elodiff))
196
197                 # how much adjustment is good?
198                 # scorefactor(elodiff) = 1 / (1 + e^(-elodiff * logdistancefactor))
199                 # elodiff(scorefactor) = -ln(1/scorefactor - 1) / logdistancefactor
200                 # elodiff'(scorefactor) = 1 / ((scorefactor) (1 - scorefactor) logdistancefactor)
201                 # elodiff'(scorefactor) >= 4 / logdistancefactor
202
203                 # adjust'(scorefactor) = K1 + K2
204
205                 # so we want:
206                 # K1 + K2 <= 4 / logdistancefactor <= elodiff'(scorefactor)
207                 # as we then don't overcompensate
208
209                 adjustment = scorefactor_real - scorefactor_elo
210                 eloadjust[ei.player_id] += adjustment
211                 eloadjust[ej.player_id] -= adjustment
212
213         elo_deltas = {}
214         for pid in pids:
215             old_elo = elos[pid].elo
216             new_elo = max(float(elos[pid].elo) + eloadjust[pid] * elos[pid].k * ep.global_K / float(len(elos) - 1), ep.floor)
217
218             # winners are not penalized with negative elo
219             if pid in winners and new_elo < elos[pid].elo:
220                 log.debug("Not penalizing Player {0} for winning. Elo delta set to 0.0. Elo is unchanged at {1}".format(pid, old_elo))
221                 elo_deltas[pid] = 0.0
222             else:
223                 elo_deltas[pid] = new_elo - float(elos[pid].elo)
224                 log.debug("Setting Player {0}'s Elo delta to {1}. Elo is now {2} (was {3}).".format(pid, elo_deltas[pid], new_elo, old_elo))
225                 elos[pid].elo = new_elo
226
227             elos[pid].games += 1
228
229         self.save_elo_deltas(session, elo_deltas)
230
231         return elos
232
233
234     def save_elo_deltas(self, session, elo_deltas):
235         """
236         Saves the amount by which each player's Elo goes up or down
237         in a given game in the PlayerGameStat row, allowing for scoreboard display.
238
239         elo_deltas is a dictionary such that elo_deltas[player_id] is the elo_delta
240         for that player_id.
241         """
242         pgstats = {}
243         for pgstat in session.query(PlayerGameStat).\
244                 filter(PlayerGameStat.game_id == self.game_id).\
245                 all():
246                     pgstats[pgstat.player_id] = pgstat
247
248         for pid in elo_deltas.keys():
249             try:
250                 pgstats[pid].elo_delta = elo_deltas[pid]
251                 session.add(pgstats[pid])
252             except:
253                 log.debug("Unable to save Elo delta value for player_id {0}".format(pid))
254
255
256 class PlayerGameStat(object):
257     def __init__(self, player_game_stat_id=None, create_dt=None):
258         self.player_game_stat_id = player_game_stat_id
259         self.create_dt = create_dt
260
261     def __repr__(self):
262         return "<PlayerGameStat(%s, %s, %s)>" % (self.player_id, self.game_id, self.create_dt)
263
264     def to_dict(self):
265         return {'player_id':self.player_id, 'game_id':self.game_id, 'create_dt':self.create_dt.strftime('%Y-%m-%dT%H:%M:%SZ')}
266
267     def nick_stripped(self):
268         if self.nick is None:
269             return "Anonymous Player"
270         else:
271             return strip_colors(self.nick)
272
273     def nick_html_colors(self):
274         if self.nick is None:
275             return "Anonymous Player"
276         else:
277             return html_colors(self.nick)
278
279     def team_html_color(self):
280         # blue
281         if self.team == 5:
282             return "red"
283         # red
284         if self.team == 14:
285             return "blue"
286         if self.team == 13:
287             return "yellow"
288         if self.team == 10:
289             return "pink"
290
291
292 class Achievement(object):
293     def __repr__(self):
294         return "<Achievement(%s, %s, %s)>" % (self.achievement_cd, self.descr, self.limit)
295
296     def to_dict(self):
297         return {'achievement_cd':self.achievement_cd, 'name':self.descr, 'limit':self.limit}
298
299
300 class PlayerAchievement(object):
301     def __repr__(self):
302         return "<PlayerAchievement(%s, %s)>" % (self.player_id, self.achievement_cd)
303
304     def to_dict(self):
305         return {'player_id':self.player_id, 'achievement_cd':self.achievement_cd}
306
307
308 class PlayerWeaponStat(object):
309     def __repr__(self):
310         return "<PlayerWeaponStat(%s, %s, %s)>" % (self.player_weapon_stats_id, self.player_id, self.game_id)
311
312     def to_dict(self):
313         return {'player_weapon_stats_id':self.player_weapon_stats_id, 'player_id':self.player_id, 'game_id':self.game_id}
314
315
316 class Hashkey(object):
317     def __init__(self, player_id=None, hashkey=None):
318         self.player_id = player_id
319         self.hashkey = hashkey
320
321     def __repr__(self):
322         return "<Hashkey(%s, %s)>" % (self.player_id, self.hashkey)
323
324     def to_dict(self):
325         return {'player_id':self.player_id, 'hashkey':self.hashkey}
326
327
328 class PlayerNick(object):
329     def __repr__(self):
330         return "<PlayerNick(%s, %s)>" % (self.player_id, self.stripped_nick)
331
332     def to_dict(self):
333         return {'player_id':self.player_id, 'name':self.stripped_nick}
334
335
336 class PlayerElo(object):
337     def __init__(self, player_id=None, game_type_cd=None):
338
339         self.player_id = player_id
340         self.game_type_cd = game_type_cd
341         self.score = 0
342         self.games = 0
343         self.elo = ELOPARMS.initial
344
345     def __repr__(self):
346         return "<PlayerElo(pid=%s, gametype=%s, elo=%s)>" % (self.player_id, self.game_type_cd, self.elo)
347
348     def to_dict(self):
349         return {'player_id':self.player_id, 'game_type_cd':self.game_type_cd, 'elo':self.elo}
350
351
352 class PlayerRank(object):
353
354     def nick_html_colors(self):
355         if self.nick is None:
356             return "Anonymous Player"
357         else:
358             return html_colors(self.nick)
359
360     def __repr__(self):
361         return "<PlayerRank(pid=%s, gametype=%s, rank=%s)>" % (self.player_id, self.game_type_cd, self.rank)
362
363     def to_dict(self):
364         return {'player_id':self.player_id, 'game_type_cd':self.game_type_cd, 'rank':self.rank}
365
366
367 def initialize_db(engine=None):
368     DBSession.configure(bind=engine)
369     Base.metadata.bind = engine
370     Base.metadata.create_all(engine)
371     MetaData = sqlalchemy.MetaData(bind=engine, reflect=True)
372
373     # assign all those tables to an object
374     achievements_table = MetaData.tables['achievements']
375     cd_achievement_table = MetaData.tables['cd_achievement']
376     cd_game_type_table = MetaData.tables['cd_game_type']
377     cd_weapon_table = MetaData.tables['cd_weapon']
378     db_version_table = MetaData.tables['db_version']
379     games_table = MetaData.tables['games']
380     hashkeys_table = MetaData.tables['hashkeys']
381     maps_table = MetaData.tables['maps']
382     player_game_stats_table = MetaData.tables['player_game_stats']
383     players_table = MetaData.tables['players']
384     player_weapon_stats_table = MetaData.tables['player_weapon_stats']
385     servers_table = MetaData.tables['servers']
386     player_nicks_table = MetaData.tables['player_nicks']
387     player_elos_table = MetaData.tables['player_elos']
388     player_ranks_table = MetaData.tables['player_ranks']
389
390     # now map the tables and the objects together
391     mapper(PlayerAchievement, achievements_table)
392     mapper(Achievement, cd_achievement_table)
393     mapper(GameType, cd_game_type_table)
394     mapper(Weapon, cd_weapon_table)
395     mapper(Game, games_table)
396     mapper(Hashkey, hashkeys_table)
397     mapper(Map, maps_table)
398     mapper(PlayerGameStat, player_game_stats_table)
399     mapper(Player, players_table)
400     mapper(PlayerWeaponStat, player_weapon_stats_table)
401     mapper(Server, servers_table)
402     mapper(PlayerNick, player_nicks_table)
403     mapper(PlayerElo, player_elos_table)
404     mapper(PlayerRank, player_ranks_table)