-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPeriod.ts
48 lines (40 loc) · 1.05 KB
/
Period.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
import { Game, Outcome } from "./Game.ts";
import { Player } from "./Player.ts";
export class Period {
private playerMap: Map<string, Player> = new Map();
private numberOfGames = 0;
constructor(private k = 24) {
if (k <= 0) {
throw new Error("K-factor must be a positive number.");
}
}
get length() {
return this.numberOfGames;
}
private addPlayer(player: Player) {
this.playerMap.has(player.id)
? null
: this.playerMap.set(player.id, player);
}
addGame(player1: Player, player2: Player, score: Outcome) {
if (![0, 0.5, 1].includes(score)) {
throw new Error("Score must be 0, 0.5, or 1.");
}
this.addPlayer(player1);
this.addPlayer(player2);
const game = new Game(player1, player2, score);
player1.addGame(game);
player2.addGame(game);
++this.numberOfGames;
}
reset() {
for (const player of this.playerMap.values()) {
player.reset();
}
}
calculate() {
for (const player of this.playerMap.values()) {
player.updateRating(this.k);
}
}
}