-
Notifications
You must be signed in to change notification settings - Fork 3
/
BaseTracker.ts
101 lines (88 loc) · 2.41 KB
/
BaseTracker.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
import { performance } from 'node:perf_hooks';
import Utils from '../core/Utils.js';
import type {
Game,
Action,
Tracker,
TrackerConfig,
HeatmapAnalysisFunc,
HeatmapData
} from '../interfaces/index.js';
class BaseTracker implements Tracker {
type: 'move' | 'game';
cfg: TrackerConfig;
time: number;
t0: number;
heatmapPresets: { [name: string]: { calc: HeatmapAnalysisFunc } };
constructor(type: 'move' | 'game') {
this.type = type;
this.cfg = {
profilingActive: false
};
this.time = 0;
this.t0 = 0;
this.heatmapPresets = {};
if (this.type === undefined) {
throw new Error('Your analyzer must specify a type!');
}
}
analyze(data: Game | Action[]) {
if (this.cfg.profilingActive) this.t0 = performance.now();
this.track(data);
if (this.cfg.profilingActive) this.time += performance.now() - this.t0;
}
track(_data: Game | Action[]) {
throw new Error('Your analyzer must implement a track(...) method!');
}
add(_data: this) {
throw new Error(
'Your analyzer must implement an add(...) method if you are using multihread mode!'
);
}
generateHeatmap(
analysisFunc: string | HeatmapAnalysisFunc,
square?: string | number[],
optData?: unknown
): HeatmapData {
let heatmapFunction: HeatmapAnalysisFunc;
if (typeof analysisFunc === 'string') {
if (Object.keys(this.heatmapPresets).length === 0)
throw new Error(
'Your analyzer does not define any heatmap presets!'
);
heatmapFunction = this.heatmapPresets[analysisFunc]?.calc;
if (!heatmapFunction)
throw new Error(`Heatmap preset '${analysisFunc}' not found!`);
} else {
heatmapFunction = analysisFunc;
}
return Utils.generateHeatmap(this, heatmapFunction, square, optData);
}
generateComparisonHeatmap(
compData: this,
analysisFunc: string | HeatmapAnalysisFunc,
square?: string | number[],
optData?: unknown
): HeatmapData {
let heatmapFunction: HeatmapAnalysisFunc;
if (typeof analysisFunc === 'string') {
if (Object.keys(this.heatmapPresets).length === 0)
throw new Error(
'Your analyzer does not define any heatmap presets!'
);
heatmapFunction = this.heatmapPresets[analysisFunc]?.calc;
if (!heatmapFunction)
throw new Error(`Heatmap preset '${analysisFunc}' not found!`);
} else {
heatmapFunction = analysisFunc;
}
return Utils.generateComparisonHeatmap(
this,
compData,
heatmapFunction,
square,
optData
);
}
}
export default BaseTracker;