-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from sentrychris/feature/050-ability-to-save-g…
…ames-load-saved-games Add ability to save games
- Loading branch information
Showing
13 changed files
with
380 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
const os = require('os'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const { dialog } = require('electron'); | ||
|
||
module.exports = class Storage | ||
{ | ||
constructor (context) { | ||
this.path = path.normalize(os.homedir() + '/.undeadbytesgame/'); | ||
|
||
if (! fs.existsSync(this.path)) { | ||
fs.mkdirSync(this.path); | ||
} | ||
|
||
this.context = context; | ||
|
||
this.settingsFile = this.path + 'settings.json'; | ||
this.settings = { | ||
volumes: { | ||
fx: { | ||
weapon: 0.5, | ||
snippet: 0.5, | ||
}, | ||
soundtrack: 0.5 | ||
} | ||
} | ||
|
||
this.savedGamesDir = path.normalize(this.path + 'saved_games/'); | ||
this.savedGames = []; | ||
|
||
if (! fs.existsSync(this.savedGamesDir)) { | ||
fs.mkdirSync(this.savedGamesDir); | ||
} | ||
|
||
this.createSettingsIfNotExists(); | ||
this.loadSavedGamesFromDir(); | ||
} | ||
|
||
createSettingsIfNotExists (settings = {}) { | ||
if (! fs.existsSync(this.settingsFile)) { | ||
settings = { | ||
...this.settings, | ||
...settings | ||
}; | ||
|
||
this.saveSettingsToFile(settings); | ||
} | ||
} | ||
|
||
loadSettingsFromFile () { | ||
const settings = JSON.parse(fs.readFileSync(this.settingsFile, { | ||
encoding: 'utf-8' | ||
})); | ||
|
||
if (settings) { | ||
this.settings = settings; | ||
} | ||
|
||
return this.settings; | ||
} | ||
|
||
saveSettingsToFile (settings) { | ||
try { | ||
fs.writeFileSync(this.settingsFile, JSON.stringify(settings, null, 4), { | ||
encoding: 'utf-8' | ||
}); | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
} | ||
|
||
loadSavedGamesFromDir () { | ||
try { | ||
fs.readdirSync(this.savedGamesDir).forEach((file) => { | ||
this.savedGames.push(file); | ||
}); | ||
} catch (err) { | ||
console.log(err) | ||
} | ||
} | ||
|
||
async loadGameFromFile () { | ||
return new Promise((resolve) => { | ||
dialog.showOpenDialog({ | ||
defaultPath: this.savedGamesDir, | ||
filters: [ | ||
{ name: 'Undead Bytes Saves', extensions: ['json'] } | ||
], | ||
properties: ['openFile'] | ||
}).then(({ filePaths }) => { | ||
if (filePaths.length === 0) { | ||
throw new Error('noselection'); | ||
} | ||
|
||
const game = JSON.parse(fs.readFileSync(filePaths[0], { | ||
encoding: 'utf-8' | ||
})); | ||
|
||
|
||
return resolve(game); | ||
}).catch((err) => { | ||
console.log(err); | ||
}) | ||
}) | ||
} | ||
|
||
saveGameToFile (save) { | ||
try { | ||
console.log('Saving game - ', save); | ||
|
||
const date = (new Date()).toISOString() | ||
.slice(0, 19) | ||
.replace('T', ''); | ||
|
||
const file = `undeadbytes-save-level-${save.level}-${date.replace(/[:-]/g, '')}.json`; | ||
const path = this.savedGamesDir + file; | ||
|
||
fs.writeFileSync(path, JSON.stringify(save, null, 4), { | ||
encoding: 'utf-8', | ||
flag: 'a+' | ||
}); | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.