-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.js
139 lines (125 loc) · 4.83 KB
/
logger.js
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const getLogger = ({ mode, seed }) => {
let logItems = [];
const addItems = (newLogItem) => {
const logList = document.querySelector(".log-list");
const isShuffle = newLogItem === "Shuffle";
const lastItemWasShuffle =
logItems.length > 0 && logItems[logItems.length - 1].match("Shuffle");
if (isShuffle && lastItemWasShuffle) {
const numberOfPreviousShuffles =
Number(logItems[logItems.length - 1].split(" x")[1]) || 1;
logItems.pop();
logItems.push(`Shuffle x${numberOfPreviousShuffles + 1}`);
} else {
logItems.push(newLogItem);
}
logList.innerHTML = logItems.map((log) => `<div>${log}</div>`).join("\n");
};
const loggerBaseElement = document.createElement("div");
loggerBaseElement.classList.add("logger");
// on score click, toggle log
const scoreElement = document.querySelector("#score-wrapper");
const logOpenButton = document.createElement("div");
logOpenButton.classList.add("open-logger");
logOpenButton.onclick = () => {
const isLoggerVisible = loggerBaseElement.style.display === "block";
if (isLoggerVisible) {
// if logger up, close it
loggerBaseElement.classList.add("logger-leave");
setTimeout(() => {
loggerBaseElement.classList.remove("logger-leave");
loggerBaseElement.style.display = "none";
}, 500);
} else {
// else change it to be visible
loggerBaseElement.style.display = "block";
}
};
scoreElement.appendChild(logOpenButton);
// copy seed button
const copySeedUrlElement = document.createElement("div");
if (seed) {
copySeedUrlElement.classList.add("seed-link");
copySeedUrlElement.innerHTML = `${mode} - seed: ${seed}<div class="copy-text">click here to copy link</div>`;
copySeedUrlElement.onclick = () => {
navigator.clipboard.writeText(
`${window.location.origin}${window.location.pathname}?seed=${seed}`
);
};
} else {
copySeedUrlElement.classList.add("seed-link");
copySeedUrlElement.innerHTML = `${mode} - ${new Date().toDateString()}<div class="copy-text">click here to copy link</div>`;
copySeedUrlElement.onclick = () => {
navigator.clipboard.writeText(`${window.location.href}`);
};
}
loggerBaseElement.appendChild(copySeedUrlElement);
// close button
const closeElement = document.createElement("div");
closeElement.classList.add("close-logger");
closeElement.onclick = () => {
loggerBaseElement.classList.add("logger-leave");
setTimeout(() => {
loggerBaseElement.classList.remove("logger-leave");
loggerBaseElement.style.display = "none";
}, 500);
};
loggerBaseElement.appendChild(closeElement);
// streak
if (mode === "Daily") {
const streakElement = document.createElement("div");
streakElement.classList.add("streak");
streakElement.innerText = 0;
loggerBaseElement.appendChild(streakElement);
}
// high score
const highScoreElement = document.createElement("div");
highScoreElement.classList.add("highscore");
highScoreElement.innerText =
localStorage.getItem(`${mode.toLowerCase()}-highscore`) || 0;
loggerBaseElement.appendChild(highScoreElement);
// line break
loggerBaseElement.appendChild(document.createElement("hr"));
// log list
const logList = document.createElement("div");
logList.classList.add("log-list");
loggerBaseElement.appendChild(logList);
// log copy button
const logCopyButton = document.createElement("div");
logCopyButton.classList.add("log-copy-button");
logCopyButton.innerText = "click to copy log";
logCopyButton.onclick = () => {
const logListItems = document.querySelector(".log-list");
const score = document.querySelector("#score").innerText;
navigator.clipboard.writeText(
`${mode} ${
mode === "Daily" ? new Date().toDateString() : `(seed ${seed})`
}\n${logListItems.innerText}\nScore: ${score}`
);
};
loggerBaseElement.appendChild(logCopyButton);
if (mode === "Daily") {
// restart button
const restart = document.createElement("a");
restart.classList.add("restart-button");
restart.href = window.location.pathname;
restart.innerText = "Restart";
loggerBaseElement.appendChild(restart);
}
if (mode !== "Daily") {
// restart button
const restart = document.createElement("a");
restart.classList.add("new-game-button");
restart.href = window.location.pathname;
restart.innerText = "New Game (new board)";
loggerBaseElement.appendChild(restart);
// restart w/ seed button
const restartWithSeed = document.createElement("a");
restartWithSeed.classList.add("restart-button");
restartWithSeed.href = `${window.location.pathname}?seed=${seed}`;
restartWithSeed.innerText = "Restart (same board)";
loggerBaseElement.appendChild(restartWithSeed);
}
document.body.appendChild(loggerBaseElement);
return addItems;
};