-
Notifications
You must be signed in to change notification settings - Fork 3
/
GameTrackerBase.ts
68 lines (58 loc) · 1.34 KB
/
GameTrackerBase.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
import BaseTracker from './BaseTracker.js';
import type { Game } from '../interfaces/index.js';
class GameTrackerBase extends BaseTracker {
results: { white: number; black: number; draw: number };
cntGames: number;
ECO: { [eco: string]: number };
constructor() {
super('game');
this.results = { white: 0, black: 0, draw: 0 };
this.cntGames = 0;
this.ECO = {};
}
add(tracker: GameTrackerBase) {
this.results.white += tracker.results.white;
this.results.black += tracker.results.black;
this.results.draw += tracker.results.draw;
this.cntGames += tracker.cntGames;
this.time += tracker.time;
for (const key of Object.keys(tracker.ECO)) {
if (this.ECO[key] !== undefined) {
this.ECO[key] += tracker.ECO[key];
} else {
this.ECO[key] = tracker.ECO[key];
}
}
}
track(game: Game) {
this.cntGames += 1;
switch (game.Result) {
case '1-0':
this.results.white += 1;
break;
case '1/2-1/2':
this.results.draw += 1;
break;
case '0-1':
this.results.black += 1;
break;
default:
break;
}
if (this.ECO[game.ECO] !== undefined) {
this.ECO[game.ECO] += 1;
} else {
this.ECO[game.ECO] = 1;
}
}
finish() {
// sort keys
this.ECO = Object.keys(this.ECO)
.sort()
.reduce((a, c) => {
a[c] = this.ECO[c];
return a;
}, {});
}
}
export default GameTrackerBase;