diff --git a/lib/salet.coffee b/lib/salet.coffee index ebfa935..6379d9d 100644 --- a/lib/salet.coffee +++ b/lib/salet.coffee @@ -50,8 +50,7 @@ class Salet processing could also be done by the first situation's enter function. ### - init: (character) -> - @character = character + init: () -> ### This function is called before entering any new @@ -227,7 +226,7 @@ class Salet # Gets the unique id used to identify saved games. getSaveId: (slot = "") -> - return 'salet_'+@game_id+'_'+@game_version+'_'+slot; + return 'salet_'+@game_id+'_'+@game_version#+'_'+slot # This gets called when a link needs to be followed, regardless # of whether it was user action that initiated it. @@ -330,7 +329,7 @@ class Salet no longer has to be the end-all be-all repository of game state. ### eraseSave: (force = false) => - save_id = @getSaveId() # save slot + saveId = @getSaveId() # save slot if (localStorage.getItem(saveId) and (force or confirm("erase_message".l()))) localStorage.removeItem(saveId) window.location.reload() @@ -345,15 +344,18 @@ class Salet break return result - # Saves the character to local storage. + # Saves the character and the walking history to local storage. saveGame: () -> # Store when we're saving the game, to avoid exploits where a # player loads their file to gain extra time. now = (new Date()).getTime() * 0.001 - @progress.saveTime = now - startTime + @progress.saveTime = now - @startTime # Save the game. - localStorage.setItem(getSaveId(), JSON.stringify(@progress)) + window.localStorage.setItem(@getSaveId(), JSON.stringify({ + progress: @progress, + character: @character + })) # Switch the button highlights. @view.disableSaving() @@ -361,24 +363,24 @@ class Salet @view.enableLoading() # Loads the game from the given data - loadGame: (characterData) -> - @progress = characterData + loadGame: (saveFile) -> + @progress = saveFile.progress + @character = saveFile.character - character = new Character() @rnd = new Random(@progress.seed) @view.clearContent() # Now play through the actions so far: if (@init) - @init(character) + @init() # Run through all the player's history. interactive = false for step in @progress.sequence # The action must be done at the recorded time. @time = step.when - processLink(step.link) + @processLink(step.link) interactive = true # Reverse engineer the start time. @@ -389,16 +391,18 @@ class Salet beginGame: () -> # Handle storage. - storedCharacter = false + saveFile = false if (@view.hasLocalStorage()) - storedCharacter = localStorage.getItem(@getSaveId()) + saveFile = localStorage.getItem(@getSaveId()) - if (storedCharacter) + if (saveFile) try - @loadGame(JSON.parse(storedCharacter)) + @loadGame(JSON.parse(saveFile)) @view.disableSaving() @view.enableErasing() catch err + console.log "There was an error loading your save. The save is deleted." + console.log err @eraseSave(true) else @progress.seed = new Date().toString() diff --git a/lib/view.coffee b/lib/view.coffee index 0c12d8f..4aa440e 100644 --- a/lib/view.coffee +++ b/lib/view.coffee @@ -41,8 +41,14 @@ class SaletView window.location.reload() ) if (@hasLocalStorage()) - $("#erase").click(salet.erase_save) # is Salet defined here? - $("#save").click(salet.saveGame) + $("#erase").click((event) -> + event.preventDefault() + return salet.eraseSave() + ) + $("#save").click((event) -> + event.preventDefault() + return salet.saveGame() + ) disableSaving: () -> $("#save").addClass('disabled') @@ -224,14 +230,7 @@ class SaletView # Feature detection hasLocalStorage: () -> - hasStorage = false - try - hasStorage = ('localStorage' in window) && - window.localStorage != null && - window.localStorage != undefined; - catch err - hasStorage = false - return hasStorage + return window.localStorage? updateWays: (salet, ways, name) -> content = ""