-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathskill_tracker.py
167 lines (131 loc) · 5.72 KB
/
skill_tracker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
from trueskill import Rating, TrueSkill
from collections import defaultdict
import numpy as np
class Player:
def __init__(self, name, id):
self.name = name
self.id = id
self.games = 0
def __repr__(self):
return self.name
def __eq__(self, other):
return self.id.__eq__(other.id)
def __hash__(self):
return self.id.__hash__()
class TrueSkillTracker:
def __init__(self, username_tracker, mu=1000, sigma=8.33*40/2, beta=4.16*40, tau=0.083*40*4, mode='match', min_ranked_matches=2):
self.min_ranked_matches = min_ranked_matches
assert mode in ['round', 'match']
self.mode = mode
self.ts = TrueSkill(mu=mu, sigma=sigma, beta=beta, tau=tau, draw_probability=0) #, backend=(logistic.cdf, logistic.pdf, logistic.ppf))
self.skills = defaultdict(lambda: self.ts.create_rating())
self.hltv = defaultdict(int)
self.player_counts = defaultdict(int)
self.skill_history = [self.skills.copy()]
self.match_ids = [] # To avoid repeating matches
self.hltv = 0.75
self.user_names = username_tracker
self.user_last_diffs = {}
self.player_rounds_played = defaultdict(int)
self.player_rounds_won = defaultdict(int)
self.player_matches_played = defaultdict(int)
self.player_matches_won = defaultdict(int)
self.player_hltv_history = defaultdict(list)
self.player_adr_history = defaultdict(list)
#print(f'RATING mu={mu} sigma={sigma} beta={beta}, tau={tau}, hltv={hltv}, mode=GAME')
def process_match(self, match):
if match['match_id'] in self.match_ids:
print('Warning: tried to process the same match twice')
return
self.match_ids.append(match['match_id'])
trace = match['match_id'] == '1149271'
if trace:
print('TRACING MATCH', match['match_id'])
t1table = match['team1table']
t2table = match['team2table']
t1players = [Player(p['Name'], p['id']) for p in t1table.values()]
t2players = [Player(p['Name'], p['id']) for p in t2table.values()]
if trace:
print('* before match:')
trace_skill1 = {p: int(self.skills[p].mu) for p in t1players}
trace_skill2 = {p: int(self.skills[p].mu) for p in t2players}
print('team1:', trace_skill1)
print('team2:', trace_skill2)
for p in t1players:
self.player_counts[p] += 1
self.player_rounds_played[p] += match['team1score'] + match['team2score']
self.player_rounds_won[p] += match['team1score']
self.player_matches_played[p] += 1
if match['team1score'] > match['team2score']:
self.player_matches_won[p] += 1
for p in t2players:
self.player_counts[p] += 1
self.player_rounds_played[p] += match['team1score'] + match['team2score']
self.player_rounds_won[p] += match['team2score']
self.player_matches_played[p] += 1
if match['team2score'] > match['team1score']:
self.player_matches_won[p] += 1
# Calculate number of wins each team got
if self.mode == 'match':
round_diff = match['team1score'] - match['team2score']
rounds = [1] if round_diff >= 0 else [2]
if match['team1score'] == match['team2score']:
rounds = [0] # special case!!
elif self.mode == 'round':
rounds = [1]*match['team1score'] + [2]*match['team2score']
elif self.mode == 'round_diff':
round_diff = match['team1score'] - match['team2score']
rounds = [1]*round_diff if round_diff > 0 else [2]*(-round_diff)
if round_diff == 0: # draw
rounds = [0]
np.random.seed(42)
rounds = np.random.permutation(rounds)
if trace:
print('Generated round sequence:', rounds)
# Keep track of HLTVs
table = {**t1table, **t2table}
for p in t1players + t2players:
hltv = table[p.id]['HLTV']
adr = table[p.id]['ADR']
self.user_names[p.id] = p
self.player_hltv_history[p].append(hltv)
self.player_adr_history[p].append(adr)
for i, r in enumerate(rounds):
if trace:
print('* round', i, 'winner team', r)
t1skills = [self.skills[p] for p in t1players]
t2skills = [self.skills[p] for p in t2players]
t1weights = np.array([p['HLTV'] for p in t1table.values()])
t2weights = np.array([p['HLTV'] for p in t2table.values()])
#### Calculating ratings (weighted by HLTV)
if r == 0: # draw
ranks = [0, 0]
t1weights = [1, 1, 1, 1, 1] # Not sure how to do ratings on a draw
t2weights = [1, 1, 1, 1, 1]
else:
ranks = [1, 0] if r==2 else [0, 1]
if r==1:
t2weights = 1/t2weights
else:
t1weights = 1/t1weights
t1weights **= self.hltv
t2weights **= self.hltv
t1weights /= (t1weights.sum() / 5)
t2weights /= (t2weights.sum() / 5)
if trace:
print('weights:', np.around(t1weights, 1), np.around(t2weights, 1))
newt1skills, newt2skills = self.ts.rate([t1skills, t2skills], ranks, weights=[t1weights, t2weights])
if trace:
print('team1:', {p: round(newt1skills[i].mu - self.skills[p].mu, 2) for i, p in enumerate(t1players)})
print('team2:', {p: round(newt2skills[i].mu - self.skills[p].mu, 2) for i, p in enumerate(t2players)})
for p, n, old in zip(t1players, newt1skills, t1skills):
self.skills[p] = n
self.user_last_diffs[p] = n.mu - old.mu
for p, n, old in zip(t2players, newt2skills, t2skills):
self.skills[p] = n
self.user_last_diffs[p] = n.mu - old.mu
self.skill_history.append(self.skills.copy())
if trace:
print('* OVERALL CHANGE:')
print('team1:', {p: round(self.skills[p].mu - self.skill_history[-2][p].mu, 2) for i, p in enumerate(t1players)})
print('team2:', {p: round(self.skills[p].mu - self.skill_history[-2][p].mu, 2) for i, p in enumerate(t2players)})