From d03d61516ffbca859fea046c5c95ab258019dade Mon Sep 17 00:00:00 2001 From: "Eevee (Evelyn Woods)" Date: Wed, 21 Oct 2020 23:34:59 -0600 Subject: [PATCH] Fix NaNs sneaking into the save file --- js/format-base.js | 12 +++++++----- js/main.js | 21 +++++++++++++++++---- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/js/format-base.js b/js/format-base.js index 2932ffc..f9cf4d0 100644 --- a/js/format-base.js +++ b/js/format-base.js @@ -66,11 +66,13 @@ export class StoredGame { if (meta.error) throw meta.error; - // The editor stores inflated levels at times, so respect that - if (meta.stored_level) + if (meta.stored_level) { + // The editor stores inflated levels at times, so respect that return meta.stored_level; - - // Otherwise, attempt to load the level - return this._level_loader(meta.bytes); + } + else { + // Otherwise, attempt to load the level + return this._level_loader(meta.bytes, meta.number); + } } } diff --git a/js/main.js b/js/main.js index 819d01a..34ba3af 100644 --- a/js/main.js +++ b/js/main.js @@ -1240,9 +1240,15 @@ class Splash extends PrimaryView { let score; let packinfo = conductor.stash.packs[packdef.ident]; if (packinfo && packinfo.total_score !== undefined) { - // TODO tack on a star if the game is "beaten"? what's that mean? every level - // beaten i guess? - score = packinfo.total_score.toLocaleString(); + if (packinfo.total_score === null) { + // Whoops, some NaNs got in here :( + score = "computing..."; + } + else { + // TODO tack on a star if the game is "beaten"? what's that mean? every level + // beaten i guess? + score = packinfo.total_score.toLocaleString(); + } } else { score = "unplayed"; @@ -1312,7 +1318,7 @@ class Splash extends PrimaryView { // Bind to "create level" button this.root.querySelector('#splash-create-level').addEventListener('click', ev => { - let stored_level = new format_base.StoredLevel; + let stored_level = new format_base.StoredLevel(1); stored_level.size_x = 32; stored_level.size_y = 32; for (let i = 0; i < 1024; i++) { @@ -1779,6 +1785,13 @@ class Conductor { if (identifier !== null) { // TODO again, enforce something about the shape here this.current_pack_savefile = JSON.parse(window.localStorage.getItem(STORAGE_PACK_PREFIX + identifier)); + if (this.current_pack_savefile.total_score === null) { + // Fix some NaNs that slipped in + this.current_pack_savefile.total_score = this.current_pack_savefile.scorecards + .map(scorecard => scorecard ? scorecard.score : 0) + .reduce((a, b) => a + b, 0); + this.save_savefile(); + } } if (! this.current_pack_savefile) { this.current_pack_savefile = {