1
0
Fork 0
mirror of https://gitlab.com/Oreolek/salet.git synced 2024-07-04 07:45:03 +03:00

Save and load working now

This commit is contained in:
Alexander Yakovlev 2016-02-01 20:23:20 +07:00
parent 3215b865f5
commit 86612614f5
2 changed files with 29 additions and 26 deletions

View file

@ -50,8 +50,7 @@ class Salet
processing could also be done by the first situation's processing could also be done by the first situation's
enter function. enter function.
### ###
init: (character) -> init: () ->
@character = character
### ###
This function is called before entering any new This function is called before entering any new
@ -227,7 +226,7 @@ class Salet
# Gets the unique id used to identify saved games. # Gets the unique id used to identify saved games.
getSaveId: (slot = "") -> 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 # This gets called when a link needs to be followed, regardless
# of whether it was user action that initiated it. # 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. no longer has to be the end-all be-all repository of game state.
### ###
eraseSave: (force = false) => eraseSave: (force = false) =>
save_id = @getSaveId() # save slot saveId = @getSaveId() # save slot
if (localStorage.getItem(saveId) and (force or confirm("erase_message".l()))) if (localStorage.getItem(saveId) and (force or confirm("erase_message".l())))
localStorage.removeItem(saveId) localStorage.removeItem(saveId)
window.location.reload() window.location.reload()
@ -345,15 +344,18 @@ class Salet
break break
return result return result
# Saves the character to local storage. # Saves the character and the walking history to local storage.
saveGame: () -> saveGame: () ->
# Store when we're saving the game, to avoid exploits where a # Store when we're saving the game, to avoid exploits where a
# player loads their file to gain extra time. # player loads their file to gain extra time.
now = (new Date()).getTime() * 0.001 now = (new Date()).getTime() * 0.001
@progress.saveTime = now - startTime @progress.saveTime = now - @startTime
# Save the game. # Save the game.
localStorage.setItem(getSaveId(), JSON.stringify(@progress)) window.localStorage.setItem(@getSaveId(), JSON.stringify({
progress: @progress,
character: @character
}))
# Switch the button highlights. # Switch the button highlights.
@view.disableSaving() @view.disableSaving()
@ -361,24 +363,24 @@ class Salet
@view.enableLoading() @view.enableLoading()
# Loads the game from the given data # Loads the game from the given data
loadGame: (characterData) -> loadGame: (saveFile) ->
@progress = characterData @progress = saveFile.progress
@character = saveFile.character
character = new Character()
@rnd = new Random(@progress.seed) @rnd = new Random(@progress.seed)
@view.clearContent() @view.clearContent()
# Now play through the actions so far: # Now play through the actions so far:
if (@init) if (@init)
@init(character) @init()
# Run through all the player's history. # Run through all the player's history.
interactive = false interactive = false
for step in @progress.sequence for step in @progress.sequence
# The action must be done at the recorded time. # The action must be done at the recorded time.
@time = step.when @time = step.when
processLink(step.link) @processLink(step.link)
interactive = true interactive = true
# Reverse engineer the start time. # Reverse engineer the start time.
@ -389,16 +391,18 @@ class Salet
beginGame: () -> beginGame: () ->
# Handle storage. # Handle storage.
storedCharacter = false saveFile = false
if (@view.hasLocalStorage()) if (@view.hasLocalStorage())
storedCharacter = localStorage.getItem(@getSaveId()) saveFile = localStorage.getItem(@getSaveId())
if (storedCharacter) if (saveFile)
try try
@loadGame(JSON.parse(storedCharacter)) @loadGame(JSON.parse(saveFile))
@view.disableSaving() @view.disableSaving()
@view.enableErasing() @view.enableErasing()
catch err catch err
console.log "There was an error loading your save. The save is deleted."
console.log err
@eraseSave(true) @eraseSave(true)
else else
@progress.seed = new Date().toString() @progress.seed = new Date().toString()

View file

@ -41,8 +41,14 @@ class SaletView
window.location.reload() window.location.reload()
) )
if (@hasLocalStorage()) if (@hasLocalStorage())
$("#erase").click(salet.erase_save) # is Salet defined here? $("#erase").click((event) ->
$("#save").click(salet.saveGame) event.preventDefault()
return salet.eraseSave()
)
$("#save").click((event) ->
event.preventDefault()
return salet.saveGame()
)
disableSaving: () -> disableSaving: () ->
$("#save").addClass('disabled') $("#save").addClass('disabled')
@ -224,14 +230,7 @@ class SaletView
# Feature detection # Feature detection
hasLocalStorage: () -> hasLocalStorage: () ->
hasStorage = false return window.localStorage?
try
hasStorage = ('localStorage' in window) &&
window.localStorage != null &&
window.localStorage != undefined;
catch err
hasStorage = false
return hasStorage
updateWays: (salet, ways, name) -> updateWays: (salet, ways, name) ->
content = "" content = ""