Use traditional (win-based) Elo instead of score-based.
[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 ELOPARMS, KREDUCTION
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         losers = []
118         for (p,s,a,r,t) in session.query(PlayerGameStat.player_id, 
119                 PlayerGameStat.score, PlayerGameStat.alivetime,
120                 PlayerGameStat.rank, PlayerGameStat.team).\
121                 filter(PlayerGameStat.game_id==self.game_id).\
122                 filter(PlayerGameStat.alivetime > timedelta(seconds=0)).\
123                 filter(PlayerGameStat.player_id > 2).\
124                 all():
125                     # scores are per second
126                     scores[p] = s/float(a.seconds)
127                     alivetimes[p] = a.seconds
128
129                     # winners are either rank 1 or on the winning team
130                     # team games are where the team is set (duh)
131                     if r == 1 or (t == self.winner and t is not None):
132                         winners.append(p)
133                     else:
134                         losers.append(p)
135
136         player_ids = scores.keys()
137
138         elos = {}
139         for e in session.query(PlayerElo).\
140                 filter(PlayerElo.player_id.in_(player_ids)).\
141                 filter(PlayerElo.game_type_cd==game_type_cd).all():
142                     elos[e.player_id] = e
143
144         # ensure that all player_ids have an elo record
145         for pid in player_ids:
146             if pid not in elos.keys():
147                 elos[pid] = PlayerElo(pid, game_type_cd)
148
149         for pid in player_ids:
150             elos[pid].k = KREDUCTION.eval(elos[pid].games, alivetimes[pid],
151                     duration)
152             if elos[pid].k == 0:
153                 del(elos[pid])
154                 del(scores[pid])
155                 del(alivetimes[pid])
156
157                 if pid in winners:
158                     winners.remove(pid)
159                 else:
160                     losers.remove(pid)
161
162         elos = self.update_elos(session, elos, scores, winners, losers, ELOPARMS)
163
164         # add the elos to the session for committing
165         for e in elos:
166             session.add(elos[e])
167
168     def update_elos(self, session, elos, scores, winners, losers, ep):
169         if len(elos) < 2 or len(winners) == 0 or len(losers) == 0:
170             return elos
171
172         pids = elos.keys()
173
174         elo_deltas = {}
175         for w_pid in winners:
176             w_elo = elos[w_pid]
177             for l_pid in losers:
178                 l_elo = elos[l_pid]
179
180                 w_q = math.pow(10, float(w_elo.elo)/400.0)
181                 l_q = math.pow(10, float(l_elo.elo)/400.0)
182
183                 w_delta = w_elo.k * ELOPARMS.global_K * (1 - w_q/(w_q + l_q))
184                 l_delta = l_elo.k * ELOPARMS.global_K * (0 - l_q/(l_q + w_q))
185
186                 elo_deltas[w_pid] = (elo_deltas.get(w_pid, 0.0) + w_delta)
187                 elo_deltas[l_pid] = (elo_deltas.get(l_pid, 0.0) + l_delta)
188
189                 log.debug("Winner {0}'s elo_delta vs Loser {1}: {2}".format(w_pid,
190                     l_pid, w_delta))
191
192                 log.debug("Loser {0}'s elo_delta vs Winner {1}: {2}".format(l_pid,
193                     w_pid, l_delta))
194
195                 log.debug("w_elo: {0}, w_k: {1}, w_q: {2}, l_elo: {3}, l_k: {4}, l_q: {5}".\
196                         format(w_elo.elo, w_elo.k, l_q, l_elo.elo, l_elo.k, l_q))
197
198         for pid in pids:
199             # average the elo gain for team games
200             if pid in winners:
201                 elo_deltas[pid] = elo_deltas.get(pid, 0.0) / len(losers)
202             else:
203                 elo_deltas[pid] = elo_deltas.get(pid, 0.0) / len(winners)
204
205             old_elo = float(elos[pid].elo)
206             new_elo = max(float(elos[pid].elo) + elo_deltas[pid], ep.floor)
207
208             # in case we've set a different delta from the above
209             elo_deltas[pid] = new_elo - old_elo
210
211             elos[pid].elo = new_elo
212             elos[pid].games += 1
213             log.debug("Setting Player {0}'s Elo delta to {1}. Elo is now {2} (was {3}).".\
214                     format(pid, elo_deltas[pid], new_elo, old_elo))
215
216         self.save_elo_deltas(session, elo_deltas)
217
218         return elos
219
220
221     def save_elo_deltas(self, session, elo_deltas):
222         """
223         Saves the amount by which each player's Elo goes up or down
224         in a given game in the PlayerGameStat row, allowing for scoreboard display.
225
226         elo_deltas is a dictionary such that elo_deltas[player_id] is the elo_delta
227         for that player_id.
228         """
229         pgstats = {}
230         for pgstat in session.query(PlayerGameStat).\
231                 filter(PlayerGameStat.game_id == self.game_id).\
232                 all():
233                     pgstats[pgstat.player_id] = pgstat
234
235         for pid in elo_deltas.keys():
236             try:
237                 pgstats[pid].elo_delta = elo_deltas[pid]
238                 session.add(pgstats[pid])
239             except:
240                 log.debug("Unable to save Elo delta value for player_id {0}".format(pid))
241
242
243
244
245 class PlayerGameStat(object):
246     def __init__(self, player_game_stat_id=None, create_dt=None):
247         self.player_game_stat_id = player_game_stat_id
248         self.create_dt = create_dt
249
250     def __repr__(self):
251         return "<PlayerGameStat(%s, %s, %s)>" % (self.player_id, self.game_id, self.create_dt)
252
253     def to_dict(self):
254         return {'player_id':self.player_id, 'game_id':self.game_id, 'create_dt':self.create_dt.strftime('%Y-%m-%dT%H:%M:%SZ')}
255
256     def nick_stripped(self):
257         if self.nick is None:
258             return "Anonymous Player"
259         else:
260             return strip_colors(self.nick)
261
262     def nick_html_colors(self):
263         if self.nick is None:
264             return "Anonymous Player"
265         else:
266             return html_colors(self.nick)
267
268     def team_html_color(self):
269         # blue
270         if self.team == 5:
271             return "red"
272         # red
273         if self.team == 14:
274             return "blue"
275         if self.team == 13:
276             return "yellow"
277         if self.team == 10:
278             return "pink"
279
280
281 class Achievement(object):
282     def __repr__(self):
283         return "<Achievement(%s, %s, %s)>" % (self.achievement_cd, self.descr, self.limit)
284
285     def to_dict(self):
286         return {'achievement_cd':self.achievement_cd, 'name':self.descr, 'limit':self.limit}
287
288
289 class PlayerAchievement(object):
290     def __repr__(self):
291         return "<PlayerAchievement(%s, %s)>" % (self.player_id, self.achievement_cd)
292
293     def to_dict(self):
294         return {'player_id':self.player_id, 'achievement_cd':self.achievement_cd}
295
296
297 class PlayerWeaponStat(object):
298     def __repr__(self):
299         return "<PlayerWeaponStat(%s, %s, %s)>" % (self.player_weapon_stats_id, self.player_id, self.game_id)
300
301     def to_dict(self):
302         return {'player_weapon_stats_id':self.player_weapon_stats_id, 'player_id':self.player_id, 'game_id':self.game_id}
303
304
305 class Hashkey(object):
306     def __init__(self, player_id=None, hashkey=None):
307         self.player_id = player_id
308         self.hashkey = hashkey
309
310     def __repr__(self):
311         return "<Hashkey(%s, %s)>" % (self.player_id, self.hashkey)
312
313     def to_dict(self):
314         return {'player_id':self.player_id, 'hashkey':self.hashkey}
315
316
317 class PlayerNick(object):
318     def __repr__(self):
319         return "<PlayerNick(%s, %s)>" % (self.player_id, self.stripped_nick)
320
321     def to_dict(self):
322         return {'player_id':self.player_id, 'name':self.stripped_nick}
323
324
325 class PlayerElo(object):
326     def __init__(self, player_id=None, game_type_cd=None):
327
328         self.player_id = player_id
329         self.game_type_cd = game_type_cd
330         self.score = 0
331         self.games = 0
332         self.elo = ELOPARMS.initial
333
334     def __repr__(self):
335         return "<PlayerElo(pid=%s, gametype=%s, elo=%s)>" % (self.player_id, self.game_type_cd, self.elo)
336
337     def to_dict(self):
338         return {'player_id':self.player_id, 'game_type_cd':self.game_type_cd, 'elo':self.elo}
339
340
341 class PlayerRank(object):
342
343     def nick_html_colors(self):
344         if self.nick is None:
345             return "Anonymous Player"
346         else:
347             return html_colors(self.nick)
348
349     def __repr__(self):
350         return "<PlayerRank(pid=%s, gametype=%s, rank=%s)>" % (self.player_id, self.game_type_cd, self.rank)
351
352     def to_dict(self):
353         return {'player_id':self.player_id, 'game_type_cd':self.game_type_cd, 'rank':self.rank}
354
355
356 def initialize_db(engine=None):
357     DBSession.configure(bind=engine)
358     Base.metadata.bind = engine
359     Base.metadata.create_all(engine)
360     MetaData = sqlalchemy.MetaData(bind=engine, reflect=True)
361
362     # assign all those tables to an object
363     achievements_table = MetaData.tables['achievements']
364     cd_achievement_table = MetaData.tables['cd_achievement']
365     cd_game_type_table = MetaData.tables['cd_game_type']
366     cd_weapon_table = MetaData.tables['cd_weapon']
367     db_version_table = MetaData.tables['db_version']
368     games_table = MetaData.tables['games']
369     hashkeys_table = MetaData.tables['hashkeys']
370     maps_table = MetaData.tables['maps']
371     player_game_stats_table = MetaData.tables['player_game_stats']
372     players_table = MetaData.tables['players']
373     player_weapon_stats_table = MetaData.tables['player_weapon_stats']
374     servers_table = MetaData.tables['servers']
375     player_nicks_table = MetaData.tables['player_nicks']
376     player_elos_table = MetaData.tables['player_elos']
377     player_ranks_table = MetaData.tables['player_ranks']
378
379     # now map the tables and the objects together
380     mapper(PlayerAchievement, achievements_table)
381     mapper(Achievement, cd_achievement_table)
382     mapper(GameType, cd_game_type_table)
383     mapper(Weapon, cd_weapon_table)
384     mapper(Game, games_table)
385     mapper(Hashkey, hashkeys_table)
386     mapper(Map, maps_table)
387     mapper(PlayerGameStat, player_game_stats_table)
388     mapper(Player, players_table)
389     mapper(PlayerWeaponStat, player_weapon_stats_table)
390     mapper(Server, servers_table)
391     mapper(PlayerNick, player_nicks_table)
392     mapper(PlayerElo, player_elos_table)
393     mapper(PlayerRank, player_ranks_table)