-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
glicko2.ts
297 lines (274 loc) · 7.78 KB
/
glicko2.ts
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import { newProcedure, volatilityArgs } from '../algorithms/volatility'
import { Player } from './player'
import { Race } from './race'
import { Team } from './team'
/**
* The main class of the rating system
*/
export class Glicko2 {
/**
* Internal default tau value for new players
*/
private _tau
/**
* Internal default rating in glicko format for new players
*/
private _default_rating
/**
* Internal default rating deviation for new players
*/
private _default_rd
/**
* Internal default volatility for new players
*/
private _default_vol
/**
* An object of all the players cached with their key as their id
* @default {}
*/
private players: Record<string, Player> = {}
/**
* The number of players in the record
* @default 0
*/
private players_index = 0
/**
* The internal default volatility algorithm used by the Glicko2 object when making new players
*/
private _volatilityAlgorithm: (
v: number,
delta: number,
{ vol, tau, rd, rating }: volatilityArgs
) => number
constructor(
settings: {
/**
* The system constant, which constrains the change in volatility over time
* @default 0.5
*/
tau: number
/**
* Base rating
* @default 1500
*/
rating: number
/**
* Base volatility
* The volatility measure indicates the degree of expected fluctuation in a player’s rating
* @default 0.06
*/
vol: number
/**
* Base rating deviation
* @default 350
*/
rd: number
/**
* The algorithm to calculate the volatility
* @default {@link newProcedure}
*/
volatilityAlgorithm: (
v: number,
delta: number,
{ vol, tau, rd, rating }: volatilityArgs
) => number
} = {
tau: 0.5,
rating: 1500,
rd: 350,
vol: 0.06,
volatilityAlgorithm: newProcedure,
}
) {
this._tau = settings.tau
this._default_rating = settings.rating
this._default_rd = settings.rd
this._default_vol = settings.vol
this._volatilityAlgorithm = settings.volatilityAlgorithm
}
/**
* @returns A {@link Race} object from the results
*/
public makeRace(results: Player[][]): Race {
return new Race(results)
}
/**
* Removes all the players from the cache
*/
public removePlayers(): void {
this.players = {}
this.players_index = 0
}
/**
* @returns A object with the player index id as the key and the {@link Player} object as the member
*/
public getPlayers(): Record<string, Player> {
return this.players
}
/**
* Removes all of the previous matches from each of the players objects
*/
public cleanPreviousMatches(): void {
for (let i = 0; i < Object.keys(this.players).length; i++) {
this.players[i].adv_ranks = []
this.players[i].adv_rds = []
this.players[i].outcomes = []
}
}
/**
* Updates the ratings for all the players
*/
public calculatePlayersRatings(): void {
const keys = Object.keys(this.players)
for (let i = 0; i < keys.length; i++) {
this.players[keys[i]].update_rank()
}
}
/**
* Creates players and match result to be taken in account for the new rankings calculation
* players must have ids, they are not created if it has been done already.
* @param player1 The first player
* @param player2 The second player
* @param outcome The outcome for the first player: 0 = defeat, 1 = victory, 0.5 = draw
* @returns An object of the new player objects
*/
public addMatch(
player1: { rating: number; rd: number; vol: number; id: number },
player2: { rating: number; rd: number; vol: number; id: number },
outcome: matchResult | number
): { pl1: Player; pl2: Player } {
const pl1 = this._createInternalPlayer(
player1.rating,
player1.rd,
player1.vol,
player1.id
)
const pl2 = this._createInternalPlayer(
player2.rating,
player2.rd,
player2.vol,
player2.id
)
this.addResult(pl1, pl2, outcome)
return { pl1: pl1, pl2: pl2 }
}
/**
* Creates a new {@link Player} and adds it to the cache
* We do not expose {@link Glicko2._createInternalPlayer} directly in order to prevent the assignation of a custom player id whose uniqueness could not be guaranteed
* @returns A {@link Player} object of the new player
*/
public makePlayer(rating?: number, rd?: number, vol?: number): Player {
return this._createInternalPlayer(rating, rd, vol)
}
private _createInternalPlayer(
rating?: number,
rd?: number,
vol?: number,
id?: number
) {
if (id === undefined) {
id = this.players_index
this.players_index = this.players_index + 1
} else {
//We check if the player has already been created
const candidate = this.players[id]
if (candidate !== undefined) {
return candidate
}
}
const player = new Player(
rating || this._default_rating,
rd || this._default_rd,
vol || this._default_vol,
this._tau
)
const playerProto = Object.getPrototypeOf(player)
// Set this specific Player's `defaultRating`. This _has_ to be done
// here in order to ensure that new `Glicko2` instances do not change
// the `defaultRating` of `Player` instances created under previous
// `Glicko2` instances.
playerProto.defaultRating = this._default_rating
// Same reasoning and purpose as the above code regarding
// `defaultRating`
playerProto.volatilityAlgorithm = this._volatilityAlgorithm
// Since this `Player`'s rating was calculated upon instantiation,
// before the `defaultRating` was defined above, we much re-calculate
// the rating manually.
player.setRating(rating || this._default_rating)
player.id = id
player.adv_ranks = []
player.adv_rds = []
player.outcomes = []
this.players[id] = player
return player
}
/**
* Add a match result to be taken in account for the new rankings calculation
* @param player1 The first player
* @param player2 The second player
* @param outcome The outcome of the first player : 0 = defeat, 1 = victory, 0.5 = draw
*/
public addResult(
player1: Player,
player2: Player,
outcome: matchResult | number
): void {
player1.addResult(player2, outcome)
player2.addResult(player1, 1 - outcome)
}
/**
* Gets all the matches from the race(if is an instance of one), calculates player ratings
* @example
* ```ts
* Glicko2.updateRatings([
* [Player1, Player2, 1],
* [Player2, Player4, 0],
* [Player3, Player1, 1],
* [Team1, Team2, 0],
* race
* ]);
* ```
*/
public updateRatings(matches: (playerMatch | Race | teamMatch)[]): void {
this.cleanPreviousMatches()
matches.forEach((match) => {
if (match instanceof Race) {
match
.getMatches()
.forEach((val) => this.addResult(val[0], val[1], val[2]))
} else if (
match[0] instanceof Player &&
match[1] instanceof Player
) {
this.addResult(match[0], match[1], match[2])
} else {
this.getTeamMatches(match as teamMatch).forEach((val) =>
this.addResult(val[0], val[1], val[2])
)
}
})
this.calculatePlayersRatings()
}
/**
* Gets an array of matches between 2 teams and their composite player object
*/
public getTeamMatches(match: teamMatch) {
const finalMatches: playerMatch[] = []
const team1Composite = match[0].makeCompositeOpponent()
const team2Composite = match[1].makeCompositeOpponent()
match[0].getPlayers().forEach((val) => {
finalMatches.push([val, team1Composite, match[2]])
})
match[1].getPlayers().forEach((val) => {
finalMatches.push([val, team2Composite, 1 - match[2]])
})
return finalMatches
}
}
export type playerMatch = [Player, Player, matchResult | number]
export type teamMatch = [Team, Team, matchResult | number]
export enum matchResult {
WIN = 1,
LOSS = 0,
DRAW = 0.5,
}