124 lines
3.7 KiB
JavaScript
124 lines
3.7 KiB
JavaScript
/**
|
|
* PersistenceManager Module
|
|
* Handles saving and loading the game state.
|
|
*/
|
|
export class PersistenceManager {
|
|
/**
|
|
* Create a new PersistenceManager
|
|
* @param {Object} config - Configuration options
|
|
* @param {Storage} config.storage - The storage backend (e.g., localStorage)
|
|
* @param {string} config.saveStateKey - Key for saving the state
|
|
* @param {string} config.saveHistoryKey - Key for saving the history
|
|
*/
|
|
constructor(config = {}) {
|
|
this.storage = config.storage || window.localStorage;
|
|
this.saveStateKey = config.saveStateKey || 'save-state';
|
|
this.saveHistoryKey = config.saveHistoryKey || 'save-history';
|
|
}
|
|
|
|
/**
|
|
* Save the current state
|
|
* @param {Object} stateObject - The state object to save
|
|
* @param {string} stateObject.inkJson - The serialized Ink state
|
|
* @param {Array<string>} stateObject.history - Array of HTML strings representing the story history
|
|
* @returns {boolean} Whether the save was successful
|
|
*/
|
|
saveState(stateObject) {
|
|
try {
|
|
if (stateObject.inkJson) {
|
|
this.storage.setItem(this.saveStateKey, stateObject.inkJson);
|
|
}
|
|
|
|
if (stateObject.history) {
|
|
this.storage.setItem(this.saveHistoryKey, JSON.stringify(stateObject.history));
|
|
}
|
|
|
|
return true;
|
|
} catch (error) {
|
|
console.error('Error saving state:', error);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Load the saved state
|
|
* @returns {Object|null} The loaded state object or null if no save exists
|
|
*/
|
|
loadState() {
|
|
try {
|
|
const inkJson = this.storage.getItem(this.saveStateKey);
|
|
const historyJson = this.storage.getItem(this.saveHistoryKey);
|
|
|
|
if (!inkJson && !historyJson) {
|
|
return null;
|
|
}
|
|
|
|
const result = {};
|
|
|
|
if (inkJson) {
|
|
result.inkJson = inkJson;
|
|
}
|
|
|
|
if (historyJson) {
|
|
result.history = JSON.parse(historyJson);
|
|
}
|
|
|
|
return result;
|
|
} catch (error) {
|
|
console.error('Error loading state:', error);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Check if a saved state exists
|
|
* @returns {boolean} Whether a saved state exists
|
|
*/
|
|
hasSavedState() {
|
|
return this.storage.getItem(this.saveStateKey) !== null;
|
|
}
|
|
|
|
/**
|
|
* Delete the saved state
|
|
* @returns {boolean} Whether the deletion was successful
|
|
*/
|
|
deleteSavedState() {
|
|
try {
|
|
this.storage.removeItem(this.saveStateKey);
|
|
this.storage.removeItem(this.saveHistoryKey);
|
|
return true;
|
|
} catch (error) {
|
|
console.error('Error deleting saved state:', error);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Export the saved state as a JSON string
|
|
* @returns {string|null} The exported state as a JSON string or null if no save exists
|
|
*/
|
|
exportState() {
|
|
const state = this.loadState();
|
|
if (!state) {
|
|
return null;
|
|
}
|
|
|
|
return JSON.stringify(state);
|
|
}
|
|
|
|
/**
|
|
* Import a state from a JSON string
|
|
* @param {string} jsonString - The JSON string to import
|
|
* @returns {boolean} Whether the import was successful
|
|
*/
|
|
importState(jsonString) {
|
|
try {
|
|
const state = JSON.parse(jsonString);
|
|
return this.saveState(state);
|
|
} catch (error) {
|
|
console.error('Error importing state:', error);
|
|
return false;
|
|
}
|
|
}
|
|
}
|