1 decade ago by FelipeBudinich
I've been refining a Persistence module to use with my games for quite some time, and I've decided to share it.
It's very simple to use, you only need to require the module then:
You can get the code below, or from this gist
It's very simple to use, you only need to require the module then:
// To set a variable
ig.persistence.set('key', 'value');
// To get a variable
var myVariable = ig.persistence.get('key');
// To get a variable, or a default value if not set previously
var myVariable = ig.persistence.get('key', 'default value');
// To get a float number
var volume = ig.persistence.getFloat("volume");
// To get a float number, and if the key is not set, use a default value of 0.5
var volume = ig.persistence.getFloat("volume", 0.5);
You can get the code below, or from this gist
/*global ig*/
ig.module(
'plugins.persistence'
).requires(
'impact.game'
).defines(function () {
'use strict';
ig.PersistenceError = function (message) {
this.name = "PersistenceError";
this.message = message;
};
ig.PersistenceError.prototype = Error.prototype;
ig.PersistenceManager = ig.Class.extend({
staticInstantiate: function (i) {
return ig.PersistenceManager.instance || null;
},
alias: function (name) {
Object.defineProperty(ig, name, {
value: this,
writable: false,
enumerable: true,
configurable: false
});
},
init: function () {
try {
if (window.localStorage) {
ig.PersistenceManager.instance = this;
this.alias('persistence');
}
} catch (e) {
throw new ig.PersistenceError("Enviroment is not capable of local storage.");
}
},
isSet: function (key) {
return (this.get(key) !== 'undefined');
},
get: function (key, defaultValue) {
var value = localStorage.getItem(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.");
}
},
getInt: function (key, defaultValue) {
return parseInt(this.get(key, defaultValue), null);
},
getFloat: function (key, defaultValue) {
return parseFloat(this.get(key, defaultValue));
},
getBool: function (key, defaultValue) {
var value = this.get(key, defaultValue);
switch (value.toLowerCase()) {
case 'true':
return true;
case 'false':
return false;
default:
throw new ig.PersistenceError("Value set for " + "'" + key + "'" + " is not boolean.");
}
},
set: function (key, value) {
window.localStorage.setItem(key, JSON.stringify(value));
},
setHigher: function (key, value) {
if (value > this.getFloat(key)) {
this.set(key, value);
}
},
setLower: function (key, value) {
if (value < this.getFloat(key)) {
this.set(key, value);
}
},
remove: function (key) {
if (this.isSet(key)) {
window.localStorage.removeItem(key);
} else {
throw new ig.PersistenceError("'" + key + "'" + " is not stored as a key.");
}
},
clear: function () {
window.localStorage.clear();
}
});
return new ig.PersistenceManager();
});
