-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats.js
More file actions
72 lines (62 loc) · 1.99 KB
/
Copy pathstats.js
File metadata and controls
72 lines (62 loc) · 1.99 KB
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
// TileLoom local statistics, kept in localStorage.
// Move records (best word, bingos, averages) count every human move made on
// this device. Game records and streaks count vs-Computer games only, from
// the human's perspective.
'use strict';
const Stats = (() => {
const KEY = 'tileloom-stats-v1';
const EMPTY = {
vsAI: { easy: { w: 0, l: 0, t: 0 }, medium: { w: 0, l: 0, t: 0 }, hard: { w: 0, l: 0, t: 0 } },
streak: { current: 0, best: 0 },
bestWord: null, // { word, score }
bestGame: null, // { score }
bingos: 0,
moves: 0,
points: 0,
};
function fresh() { return JSON.parse(JSON.stringify(EMPTY)); }
function load() {
try {
const s = JSON.parse(localStorage.getItem(KEY));
if (s && s.vsAI && s.streak) return s;
} catch (e) { /* corrupt or missing */ }
return fresh();
}
let data = load();
function save() {
try { localStorage.setItem(KEY, JSON.stringify(data)); } catch (e) { /* ignore */ }
}
function recordMove(result) {
data.moves++;
data.points += result.total;
if (result.bingo) data.bingos++;
const top = result.words.slice().sort((a, b) => b.score - a.score)[0];
if (top && (!data.bestWord || top.score > data.bestWord.score)) {
data.bestWord = { word: top.word, score: top.score };
}
save();
}
function recordGame(difficulty, humanScore, aiScore) {
const d = data.vsAI[difficulty] || (data.vsAI[difficulty] = { w: 0, l: 0, t: 0 });
if (humanScore > aiScore) {
d.w++;
data.streak.current++;
data.streak.best = Math.max(data.streak.best, data.streak.current);
} else if (humanScore < aiScore) {
d.l++;
data.streak.current = 0;
} else {
d.t++;
data.streak.current = 0;
}
if (!data.bestGame || humanScore > data.bestGame.score) {
data.bestGame = { score: humanScore };
}
save();
}
function reset() {
data = fresh();
save();
}
return { recordMove, recordGame, reset, get data() { return data; } };
})();