Skip to content

Instantly share code, notes, and snippets.

@FelipeBudinich
Last active August 29, 2015 14:00
Show Gist options
  • Save FelipeBudinich/11145608 to your computer and use it in GitHub Desktop.
Save FelipeBudinich/11145608 to your computer and use it in GitHub Desktop.
Persistence Plugin for ImpactJs
/*global ig*/
ig.module(
'plugins.persistence.localstorage'
).requires(
'impact.game'
).defines(function () {
'use strict';
ig.PersistenceLocalStorage = ig.Class.extend({
/**
* Initialize Persistence as localStorage
*/
init: function () {
try {
if (window.localStorage) {
ig.Persistence.instance = this;
}
} catch (e) {
throw new ig.PersistenceError("Enviroment is not capable of localStorage.");
}
},
/**
* Returns true if key is set, false otherwise
*/
isSet: function (key) {
return (this.get(this.currentSaveSlot + key) !== 'undefined');
},
/**
* Get a key's value
*/
get: function (key, defaultValue) {
var value = localStorage.getItem(this.currentSaveSlot + key);
if (value !== null) {
if (value !== 'undefined') {
value = JSON.parse(value);
return value;
} else {
throw new ig.PersistenceError("The value stored for " + "'" + key + "'" + " is undefined.");
}
} else if (typeof defaultValue !== 'undefined') {
return defaultValue;
} else {
throw new ig.PersistenceError("No value stored for " + "'" + key + "'" + ", nor default value provided.");
}
},
/**
* Set a key's value, if it doesn't exist, it also creates the key
*/
set: function (key, value) {
try {
window.localStorage.setItem(this.currentSaveSlot + key, JSON.stringify(value));
} catch (e) {
throw new ig.PersistenceError(e.message);
}
},
/**
* Remove a key and it's value from local storage
*/
remove: function (key) {
if (this.isSet(this.currentSaveSlot + key)) {
window.localStorage.removeItem(this.currentSaveSlot + key);
} else {
throw new ig.PersistenceError("'" + key + "'" + " is not stored as a key.");
}
}
});
});
/*global ig, process, require*/
ig.module(
'plugins.persistence.nwfilesystem'
).requires(
'impact.game'
).defines(function () {
'use strict';
ig.PersistenceNWFileSystem = ig.Class.extend({
/**
* Initialize Persistence as Node-Webkit file system
*/
init: function () {
try {
if (process.versions['node-webkit']) {
ig.Persistence.instance = this;
this.fs = require('fs');
}
} catch (e) {
throw new ig.PersistenceError("Enviroment is not capable of accessing File System via NodeJs.");
}
},
/**
* Returns true if key is set, false otherwise
*/
isSet: function (key) {
var saveSlot = 'saves/save_' + this.currentSaveSlot + '.json',
data = {};
try {
data = JSON.parse(this.fs.readFileSync(saveSlot, 'utf8'));
if (data.hasOwnProperty(key)) {
return true;
} else {
return false;
}
} catch (e) {
if (e.code === 'ENOENT') {
return false;
} else {
throw new ig.PersistenceError(e);
}
}
},
/**
* Get a key's value
*/
get: function (key, defaultValue) {
var saveSlot = 'saves/save_' + this.currentSaveSlot + '.json',
data = {};
try {
data = JSON.parse(this.fs.readFileSync(saveSlot, 'utf8'));
if (data.hasOwnProperty(key)) {
return data[key];
} else if (typeof defaultValue !== 'undefined') {
return defaultValue;
} else {
throw new ig.PersistenceError("No value stored for " + "'" + key + "'" + ", nor default value provided.");
}
} catch (e) {
if (e.code === 'ENOENT') {
return defaultValue;
} else {
throw new ig.PersistenceError(e);
}
}
},
/**
* Set a key's value, if it doesn't exist, it also creates the key
*/
set: function (key, value) {
var saveSlot = 'saves/save_' + this.currentSaveSlot + '.json',
data = {};
try {
data = JSON.parse(this.fs.readFileSync(saveSlot, 'utf8'));
data[key] = value;
data = JSON.stringify(data, null, 4);
this.fs.writeFileSync(saveSlot, data);
} catch (e) {
if (e.code === 'ENOENT') {
data[key] = value;
data = JSON.stringify(data, null, 4);
this.fs.writeFileSync(saveSlot, data);
} else {
throw new ig.PersistenceError(e);
}
}
},
/**
* Remove a key and it's value
*/
remove: function (key) {
if (this.isSet(key)) {
var saveSlot = 'saves/save_' + this.currentSaveSlot + '.json',
data = {};
try {
data = JSON.parse(this.fs.readFileSync(saveSlot, 'utf8'));
delete data[key];
data = JSON.stringify(data, null, 4);
this.fs.writeFileSync(saveSlot, data);
} catch (e) {
if (e.code === 'ENOENT') {
delete data[key];
data = JSON.stringify(data, null, 4);
this.fs.writeFileSync(saveSlot, data);
} else {
throw new ig.PersistenceError(e);
}
}
} else {
throw new ig.PersistenceError("'" + key + "'" + " is not stored as a key.");
}
}
});
});
/*global ig*/
ig.module(
'plugins.persistence'
).requires(
'impact.game',
'plugins.persistence.nwfilesystem',
'plugins.persistence.localstorage'
).defines(function () {
'use strict';
ig.PersistenceError = function (message) {
this.name = "PersistenceError";
this.message = message;
};
ig.PersistenceError.prototype = Error.prototype;
ig.Persistence = ig.Class.extend({
currentSaveSlot: 0,
/**
* Create a static instance of this class
*/
staticInstantiate: function (i) {
this.alias('persistence');
// for demostration purposes I'll create a ig.config object that contains the configuration we will be using
ig.config = {};
ig.config.persist = 'NodeWebkitFileSystem';
switch (ig.config.persist) {
case 'localStorage':
ig.Persistence.inject(ig.PersistenceLocalStorage.prototype);
break;
case 'NodeWebkitFileSystem':
ig.Persistence.inject(ig.PersistenceNWFileSystem.prototype);
break;
default:
throw new ig.PersistenceError("No persistence enviroment defined.");
}
return ig.Persistence.instance || null;
},
/**
* Sets an alias that can be used to access this singleton
*/
alias: function (name) {
Object.defineProperty(ig, name, {
value: this,
writable: false,
enumerable: true,
configurable: false
});
},
/**
* Set current save slot
*/
setSaveSlot: function (slot) {
this.currentSaveSlot = slot;
},
/**
* Get current save slot
*/
getCurrentSaveSlot: function () {
return this.currentSaveSlot;
},
/**
* Get a key's value as a Integer
*/
getInt: function (key, defaultValue) {
return parseInt(this.get(key, defaultValue), null);
},
/**
* Get a key's value as a Float
*/
getFloat: function (key, defaultValue) {
return parseFloat(this.get(key, defaultValue));
},
/**
* Get a key's value as a Boleean
*/
getBool: function (key, defaultValue) {
var value = this.get(key, defaultValue);
switch (value.toString()) {
case 'true':
return true;
case 'false':
return false;
default:
throw new ig.PersistenceError("Value set for " + "'" + key + "'" + " is not boolean.");
}
},
/**
* Set a key's value if it's value is higher than the current persisted value
*/
setHigher: function (key, value) {
if (value > this.getFloat(key)) {
this.set(key, value);
}
},
/**
* Set a key's value if it's value is lower than the current persisted value
*/
setLower: function (key, value) {
if (value < this.getFloat(key)) {
this.set(key, value);
}
}
});
return new ig.Persistence();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment