-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcookies.ts
73 lines (60 loc) · 2.09 KB
/
cookies.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
const deletePanel = document.getElementById("delete-panel")!!;
function SaveGame() {
let saveData = {
games: {},
currentPuzzle: currentData.puzzleName,
};
for (const game of data) {
saveData.games[game.name] = {
finished: game.gameData?.finished,
bestMoves: game.gameData?.bestMoves,
};
}
const dataString = JSON.stringify(saveData);
document.cookie = `gameData=${dataString}; max-age=${86400 * cookieValidity}; SameSite=lax`;
}
function LoadGame() {
console.log(document.cookie)
if (document.cookie != "") {
try {
let regex = new RegExp("([^=]*)\s*=\s*([^;]*)(?:;\s*)?");
let match = regex.exec(document.cookie)
let obj: Object = {};
for (let i = 0; i < match.length; i += 3) {
obj[match[i + 1]] = match[i + 2];
}
if (obj.hasOwnProperty("gameData") && obj["gameData"].trim() != "") {
let saveData = JSON.parse(obj["gameData"]);
let startIndex = 0;
for (let i = 0; i < data.length; i++) {
let thisGameData = saveData.games[data[i].name];
if (thisGameData) {
data[i].gameData.finished = thisGameData.finished
data[i].gameData.bestMoves = thisGameData.bestMoves
}
if (saveData.currentPuzzle === data[i].name) {
startIndex = i;
}
}
StartGame(startIndex);
return;
}
}
catch (error) {
alert("Oops! The website couldn't load your progress from the cookies files: "
+ error + "\n" + document.cookie);
}
}
ShowTutorial();
StartGame(0);
}
function DeleteCookies() {
ToggleDeletePanel();
}
function DeleteCookiesWithoutPanel() {
document.cookie = "gameData=;";
window.location.reload();
}
function ToggleDeletePanel() {
deletePanel.classList.toggle("hidden");
}