-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocalstorage.js
185 lines (159 loc) · 5.71 KB
/
localstorage.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Stores basic information needed for application functionality
// Note: PID is a unique identifier assigned to each puzzle. EX: p0,p1,p2
// FID is a unique identifier assigned to each folder. EX: f0,f1,f2
// Description of keys:
// "nextPuzzleID" - integer - tracks the number of puzzles created
// "nextFolderID" - integer - tracks the number of folders created
// "puzzles_root" - JSON - tracks the puzzles that exist at the root menu level. EX: { PID:{}, FID:name }
// "puzzles_" + FID - JSON - tracks the puzzles that exist in that folder. Folders cannot be nested. EX: { PID:{}, PID:{} }
// "stats_" + PID - JSON - tracks stats for the puzzle. TODO: determine structure (# of times completed and best time, per difficulty)
// "soundEnabled" - boolean - tracks whether sound should be enabled or not
// "ghostImageEnabled" - boolean - tracks whether the ghost puzzle image should be displayed on the board
//
// Puzzle Object
// {
// title: string
// aspectRatio: [number, number] - [x, y]
// width: integer
// height: integer
// }
function getAndIncrementNextPuzzleID() {
let id = parseInt(localStorage.getItem("nextPuzzleID") || "0");
localStorage.setItem("nextPuzzleID", id + 1);
return "p" + id;
}
function getAndIncrementNextFolderID() {
let id = parseInt(localStorage.getItem("nextFolderID") || "0");
localStorage.setItem("nextFolderID", id + 1);
return "f" + id;
}
// @param fid - string - folder ID
// @return JSON - { PID:{}, FID:name }
function getPuzzles(fid) {
return JSON.parse(localStorage.getItem("puzzles_" + fid) || "{}");
}
// @param fid - string - folder ID
// @param pid - string - pizzle ID
// @return JSON - {}
function getPuzzle(fid, pid) {
let puzzles = getPuzzles(fid);
return puzzles[pid];
}
// @param fid - string - folder ID
// @param json - JSON - { PID:{}, FID:name }
function savePuzzles(fid, json) {
localStorage.setItem("puzzles_" + fid, json ? JSON.stringify(json) : "{}");
}
// @param fid - string - folder ID
// @param pid - string - puzzle ID
// @param puzzle - {} - puzzle object
function savePuzzle(fid, pid, puzzle) {
let puzzles = getPuzzles(fid);
puzzles[pid] = puzzle;
savePuzzles(fid, puzzles);
}
// @param fid - string - folder ID
// @param pid - string - puzzle ID
// @param title - string - puzzle title
// @param aspectRatio - [number, number] - image aspect ratio in [x, y]
// @param width - integer - image width in px
// @param height - integer - image height in px
function savePuzzleFromAttrs(fid, pid, title, aspectRatio, width, height) {
let puzzles = getPuzzles(fid);
puzzles[pid] = {
title: title,
aspectRatio: aspectRatio,
width: width,
height: height
};
savePuzzles(fid, puzzles);
}
// @param fid - string - folder ID
// @param name - string - folder name
function addFolder(fid, name) {
let puzzles = getPuzzles("root");
puzzles[fid] = name;
savePuzzles("root", puzzles);
}
// @param fid - string - folder ID
// @param pid - string - puzzle ID
// @return string - deleted puzzle object {}
function deletePuzzle(fid, pid) {
let puzzles = getPuzzles(fid);
let puzzle = puzzles[pid];
delete puzzles[pid];
savePuzzles(fid, puzzles);
return puzzle;
}
// @param fid - string - folder ID
function deleteFolder(fid) {
// Remove folder from root
let puzzles = getPuzzles("root");
delete puzzles[fid];
savePuzzles("root", puzzles);
// Remove the puzzles in the folder
localStorage.removeItem("puzzles_" + fid);
}
// @param fromFid - string - folder ID to move from
// @param toFid - string - folder ID to move to
// @param pid - string - puzzle ID to move
function movePuzzle(fromFid, toFid, pid) {
let puzzle = deletePuzzle(fromFid, pid);
savePuzzle(toFid, pid, puzzle);
}
function isSoundEnabled() {
let enabled = localStorage.getItem("soundEnabled");
return enabled || enabled == null;
}
function setSoundEnabled(enabled) {
localStorage.setItem("soundEnabled", enabled);
}
function isGhostImageEnabled() {
let enabled = "true" == localStorage.getItem("ghostImageEnabled");
return enabled || enabled == null;
}
function setGhostImageEnabled(enabled) {
localStorage.setItem("ghostImageEnabled", enabled);
}
function localStorageExists(key) {
return localStorage.getItem(key) != null;
}
function localStorageSet(key, value) {
localStorage.setItem(key, value);
}
function findFolderByName(name) {
let puzzles = getPuzzles("root");
let matches = Object.entries(puzzles).filter(([k, v]) => k.startsWith("f") && v == name);
return matches.length > 0 ? matches[0] : undefined;
}
function findPuzzleByTitle(fid, title) {
let puzzles = getPuzzles(fid);
let matches = Object.entries(puzzles).filter(([k, v]) => k.startsWith("p") && v.title == title);
return matches.length > 0 ? matches[0] : undefined;
}
function autoSave() {
localStorage.setItem("autoSaveId", BOARD.id);
localStorage.setItem("autoSave", JSON.stringify(BOARD));
console.log("AutoSaved: " + new Date().toTimeString().split(' ')[0]);
// TODO: Debug what values are not being auto-saved
// Looks like we at least need to relink path.piece.object = path;
console.log("Before save");
console.log(BOARD);
console.log("After save");
var json = JSON.parse(JlocalStorage.getItem("autoSave"));
for (let jsonEntry of Object.entries(json)) {
json[jsonEntry[0]] = JSON.parse(jsonEntry[1]);
}
console.log(json);
}
function isAutoSave() {
return BOARD.id == localStorage.getItem("autoSaveId");
}
function loadAutoSave() {
BOARD.clear();
BOARD.loadFromJSON(localStorage.getItem("autoSave"));
}
function clearAutoSave() {
localStorage.removeItem("autoSaveId");
localStorage.removeItem("autoSave");
}