Skip to content

Commit

Permalink
reduced unnecessary cookie saves
Browse files Browse the repository at this point in the history
  • Loading branch information
edloper committed Dec 24, 2024
1 parent 6409463 commit b9a5289
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
6 changes: 3 additions & 3 deletions javascript/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,8 @@ class Game {

loadProgress(progress) {
if (progress.finished) {
console.log("loadProgress finished")
this.hint(/*finishLevel=*/true);
} else {
console.log("loadProgress unfinished");
for (const edge of progress.edges ?? []) {
this.addEdge(this.graph.dots[edge[0]], this.graph.dots[edge[1]],
/*saveProgress=*/ false);
Expand Down Expand Up @@ -182,6 +180,7 @@ class Game {
}
if ( (event.ctrlKey || event.metaKey) && event.key === 'ArrowRight' ) {
this.hint(/*finishLevel=*/true);
this.saveProgress();
}
else {
return; // Do not preventDefault for unhandled keys.
Expand Down Expand Up @@ -339,7 +338,8 @@ class Game {
if (corner.numEdgesLeft() == 0) { continue; }
if (!((dot == corner) ||
this.dotsAreConnectedByEdge(dot, corner))) {
const edge = this.addEdge(dot, corner);
const edge = this.addEdge(dot, corner,
/*saveProgress=*/ !finishLevel);
this.history.push(new GameHistoryEvent("add", dot, corner));
if (!finishLevel) {
edge.flash();
Expand Down
13 changes: 9 additions & 4 deletions javascript/level_picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ class LevelPicker {
this.game.setBackButtonCallback(() => {
this.game.hide();
this.show();
this.saveCookies();
this.updateProgressBars();
});
this.saveCookies();
this.$resetConfirm = $("<div class='resetConfirm' title='Reset Progress?'>" +
"Are you sure? This can not be undone.</div>");
this.$container.append(this.$resetConfirm)
Expand Down Expand Up @@ -90,18 +88,24 @@ class LevelPicker {
this.cookieData.levels[levelUrl].locked ??= true;
this.cookieData.levels[levelUrl].index = i
}
this.cookieData.levels[this.levelUrls[0]].locked = false;
this.cookieData.levels[this.levelUrls[0]].locked = false;
}

loadCookies() {
const decodedCookie = decodeURIComponent(document.cookie);
this.cookieData ??= {};
this.cookieData.levels ??= {};

var prevLevelIsFinished = true;
for (let i = 0; i < this.levelUrls.length; i++) {
const levelUrl = this.levelUrls[i];
const cookieName = "MissingLink_"+levelUrl;
this.cookieData.levels[levelUrl] = this.loadCookie(decodedCookie, cookieName);
const levelData = this.loadCookie(decodedCookie, cookieName);
this.cookieData.levels[levelUrl] = levelData;
if (prevLevelIsFinished) {
levelData.locked = false;
}
prevLevelIsFinished = levelData.progress.finished;
}
this.setCookieDefaults();
}
Expand Down Expand Up @@ -201,6 +205,7 @@ class LevelPicker {
if (index < (this.levelUrls.length - 1)) {
const nextUrl = this.levelUrls[index + 1];
this.cookieData.levels[nextUrl].locked = false;
this.saveLevelCookie(nextUrl);
this.updateProgressBars();
}
this.saveLevelCookie(url);
Expand Down
2 changes: 2 additions & 0 deletions javascript/level_urls.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const LEVEL_URLS = [
"levels/semifinal_lvl.json",
"levels/am_finaldance.json",
"levels/agnes_car.json",
//"levels/xmastree.json",
//"levels/bunny.json",
//"levels/dog.json",
//"levels/fox.json",
//"levels/penguin.json",
Expand Down

0 comments on commit b9a5289

Please sign in to comment.