-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.ts
52 lines (42 loc) · 1.06 KB
/
Player.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
import { Game } from "./Game.ts";
import { uuid } from "./deps.ts";
export class Player {
private pre: number;
private post: number;
private games: Game[] = [];
constructor(rating = 1500, readonly id = uuid.v4.generate()) {
if (rating <= 0) {
throw new Error("Player rating must be a positive number.");
}
this.pre = rating;
this.post = rating;
}
get rating() {
return this.post;
}
addGame(game: Game) {
this.games.push(game);
}
reset() {
this.games = [];
this.pre = this.post;
}
updateRating(k: number) {
this.post += k * (this.totalScore() - this.totalExpectedScore());
}
private expectedScore(game: Game): number {
return 1 / (1 + Math.pow(10, (game.opponent(this).pre - this.pre) / 400));
}
private totalExpectedScore(): number {
return this.games.reduce(
(score: number, game: Game) => score + this.expectedScore(game),
0
);
}
private totalScore(): number {
return this.games.reduce(
(score: number, game: Game) => score + game.result(this),
0
);
}
}