-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstats.html
74 lines (71 loc) · 2.54 KB
/
stats.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Reflect stats</title>
</head>
<body>
<h3>Stats</h3>
<div id="content"></div>
<script type="text/javascript">
// Format a date in ISO format (YYYY-MM-DD) according to local time
// From https://stackoverflow.com/a/50130338
function formatDate(date) {
return new Date(date.getTime() - date.getTimezoneOffset() * 60000)
.toISOString()
.split("T")[0];
}
// Return "today" in YYYY-MM-DD
// Allow overriding with url param ?date=YYYY-MM-DD
function getEffectiveDate() {
const urlParams = new URLSearchParams(window.location.search);
const dateParam = urlParams.get("date");
return dateParam ? dateParam : formatDate(new Date());
}
const today = getEffectiveDate();
function getStats() {
let content = "";
content += `Device: ${localStorage.getItem("deviceId")}<br/>`;
const playedHistoryJson = localStorage.getItem("playedHistory");
const playedHistory =
playedHistoryJson == null
? []
: Array.from(new Set(JSON.parse(playedHistoryJson))).sort();
const played = Array.from(new Set(playedHistory)).length;
console.log(`Played: ${played}`);
content += `Played: ${played}<br/>`;
const solvedHistoryJson = localStorage.getItem("solvedHistory");
const solvedHistory =
solvedHistoryJson == null
? []
: Array.from(new Set(JSON.parse(solvedHistoryJson))).sort();
const solved = Array.from(new Set(solvedHistory)).length;
console.log(`Solved: ${solved}`);
content += `Solved: ${solved}<br/>`;
let currentStreak = 0;
Array.from(new Set(solvedHistory))
.sort()
.reverse()
.map((d) => new Date(d))
.forEach((d, i) => {
if (new Date(today) - d === i * 60 * 60 * 24 * 1000) {
currentStreak++;
}
});
console.log(`Streak: ${currentStreak}`);
content += `Streak: ${currentStreak}<br/>`;
content += "<br/>Played history</br>";
for (let d of new Set(playedHistory)) {
content += `${d}<br/>`;
}
content += "<br/>Solved history</br>";
for (let d of new Set(solvedHistory)) {
content += `${d}<br/>`;
}
return content;
}
const node = document.getElementById("content");
node.innerHTML = `<p>${getStats()}</p>`;
</script>
</body>
</html>