Added Game Save
This commit is contained in:
parent
170685411d
commit
d03f48abd4
|
|
@ -10,6 +10,8 @@
|
|||
<!-- Libs -->
|
||||
<script src="js/libs/jquery.js"></script>
|
||||
<script src="js/libs/copyCSS.js"></script>
|
||||
<script src="js/libs/js.cookie.js"></script>
|
||||
<script src="js/libs/resurrect.js"></script>
|
||||
<script src="jquery-ui/jquery-ui.min.js"></script>
|
||||
<!-- Own scripts-->
|
||||
<script src="js/userinterface.js"></script>
|
||||
|
|
|
|||
|
|
@ -171,11 +171,13 @@ class Item {
|
|||
class Factory {
|
||||
constructor(tier) {
|
||||
this.tier = tier
|
||||
if (this.tier == undefined)
|
||||
this.tier = 0
|
||||
this.tiles = []
|
||||
this.items = []
|
||||
for (var x = 0; x < 32; x++) {
|
||||
for (var x = 0; x < 25; x++) {
|
||||
this.tiles[x] = []
|
||||
for (var y = 0; y < 16; y++) {
|
||||
for (var y = 0; y < 12; y++) {
|
||||
this.tiles[x][y] = 0
|
||||
}
|
||||
}
|
||||
|
|
@ -196,8 +198,8 @@ class Factory {
|
|||
|
||||
getTiles() {
|
||||
var temp = []
|
||||
for (var x = 0; x < 32; x++) {
|
||||
for (var y = 0; y < 16; y++) {
|
||||
for (var x = 0; x < 25; x++) {
|
||||
for (var y = 0; y < 12; y++) {
|
||||
if (this.tiles[x][y] != 0) {
|
||||
temp.push(this.tiles[x][y])
|
||||
}
|
||||
|
|
@ -216,8 +218,8 @@ class Factory {
|
|||
}
|
||||
|
||||
workTiles() {
|
||||
for (var x = 0; x < 32; x++) {
|
||||
for (var y = 0; y < 16; y++) {
|
||||
for (var x = 0; x < 25; x++) {
|
||||
for (var y = 0; y < 12; y++) {
|
||||
if (this.tiles[x][y] != 0) {
|
||||
this.tiles[x][y].work()
|
||||
}
|
||||
|
|
|
|||
72
js/game.js
72
js/game.js
|
|
@ -30,10 +30,80 @@ $(document).ready(function() {
|
|||
requestAnimationFrame(loop)
|
||||
})
|
||||
|
||||
var game = {}
|
||||
|
||||
$(window).on("beforeunload", function() {
|
||||
save()
|
||||
})
|
||||
|
||||
function save() {
|
||||
game = {}
|
||||
game.money = money
|
||||
game.inventory = []
|
||||
for (var i = 0; i < inventory.items.length; i++) {
|
||||
game.inventory.push(inventory.items[i].id)
|
||||
}
|
||||
game.factorys = []
|
||||
for (var i = 0; i < factorys.length; i++) {
|
||||
game.factorys.push({})
|
||||
game.factorys[i].tier = factorys[i].tier
|
||||
game.factorys[i].tiles = []
|
||||
for (var x = 0; x < 25; x++) {
|
||||
game.factorys[i].tiles[x] = []
|
||||
for (var y = 0; y < 12; y++) {
|
||||
game.factorys[i].tiles[x][y] = 0
|
||||
}
|
||||
}
|
||||
for (var x = 0; x < 25; x++) {
|
||||
for (var y = 0; y < 12; y++) {
|
||||
if (factorys[i].tiles[x][y] != 0) {
|
||||
game.factorys[i].tiles[x][y] = {}
|
||||
game.factorys[i].tiles[x][y].index = factorys[i].tiles[x][y].index
|
||||
game.factorys[i].tiles[x][y].direction = factorys[i].tiles[x][y].direction
|
||||
game.factorys[i].tiles[x][y].x = factorys[i].tiles[x][y].x
|
||||
game.factorys[i].tiles[x][y].y = factorys[i].tiles[x][y].y
|
||||
if (factorys[i].tiles[x][y].options != undefined) {
|
||||
for (var o = 0; o < factorys[i].tiles[x][y].options.length; o++) {
|
||||
var val = factorys[i].tiles[x][y].options[o].var
|
||||
game.factorys[i].tiles[x][y][val] = factorys[i].tiles[x][y][val]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Cookies.set("game", JSON.stringify(game))
|
||||
}
|
||||
|
||||
function loadGameData() {
|
||||
//TODO: Check for Cookies
|
||||
//Keine Save Vorhanden
|
||||
factorys.push(new Factory())
|
||||
inventory = new Inventory()
|
||||
game = Cookies.get("game")
|
||||
if (game != undefined) {
|
||||
game = JSON.parse(game)
|
||||
money = game.money
|
||||
inventory = new Inventory()
|
||||
factorys = []
|
||||
for (var i = 0; i < game.inventory.length; i++) {
|
||||
inventory.addItem(new Item(game.inventory[i]))
|
||||
}
|
||||
for (var i = 0; i < game.factorys.length; i++) {
|
||||
factorys.push(new Factory)
|
||||
for (var x = 0; x < 25; x++) {
|
||||
for (var y = 0; y < 12; y++) {
|
||||
if (game.factorys[i].tiles[x][y] != 0) {
|
||||
var keys = Object.keys(game.factorys[i].tiles[x][y])
|
||||
factorys[i].tiles[x][y] = new tileClasses[game.factorys[i].tiles[x][y].index](game.factorys[i].tiles[x][y].x, game.factorys[i].tiles[x][y].y)
|
||||
factorys[i].tiles[x][y].factory = factorys[i]
|
||||
for (var key = 0; key < keys.length; key++) {
|
||||
factorys[i].tiles[x][y][keys[key]] = game.factorys[i].tiles[x][y][keys[key]]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function loadItems() {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,165 @@
|
|||
/*!
|
||||
* JavaScript Cookie v2.2.0
|
||||
* https://github.com/js-cookie/js-cookie
|
||||
*
|
||||
* Copyright 2006, 2015 Klaus Hartl & Fagner Brack
|
||||
* Released under the MIT license
|
||||
*/
|
||||
;(function (factory) {
|
||||
var registeredInModuleLoader = false;
|
||||
if (typeof define === 'function' && define.amd) {
|
||||
define(factory);
|
||||
registeredInModuleLoader = true;
|
||||
}
|
||||
if (typeof exports === 'object') {
|
||||
module.exports = factory();
|
||||
registeredInModuleLoader = true;
|
||||
}
|
||||
if (!registeredInModuleLoader) {
|
||||
var OldCookies = window.Cookies;
|
||||
var api = window.Cookies = factory();
|
||||
api.noConflict = function () {
|
||||
window.Cookies = OldCookies;
|
||||
return api;
|
||||
};
|
||||
}
|
||||
}(function () {
|
||||
function extend () {
|
||||
var i = 0;
|
||||
var result = {};
|
||||
for (; i < arguments.length; i++) {
|
||||
var attributes = arguments[ i ];
|
||||
for (var key in attributes) {
|
||||
result[key] = attributes[key];
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function init (converter) {
|
||||
function api (key, value, attributes) {
|
||||
var result;
|
||||
if (typeof document === 'undefined') {
|
||||
return;
|
||||
}
|
||||
|
||||
// Write
|
||||
|
||||
if (arguments.length > 1) {
|
||||
attributes = extend({
|
||||
path: '/'
|
||||
}, api.defaults, attributes);
|
||||
|
||||
if (typeof attributes.expires === 'number') {
|
||||
var expires = new Date();
|
||||
expires.setMilliseconds(expires.getMilliseconds() + attributes.expires * 864e+5);
|
||||
attributes.expires = expires;
|
||||
}
|
||||
|
||||
// We're using "expires" because "max-age" is not supported by IE
|
||||
attributes.expires = attributes.expires ? attributes.expires.toUTCString() : '';
|
||||
|
||||
try {
|
||||
result = JSON.stringify(value);
|
||||
if (/^[\{\[]/.test(result)) {
|
||||
value = result;
|
||||
}
|
||||
} catch (e) {}
|
||||
|
||||
if (!converter.write) {
|
||||
value = encodeURIComponent(String(value))
|
||||
.replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent);
|
||||
} else {
|
||||
value = converter.write(value, key);
|
||||
}
|
||||
|
||||
key = encodeURIComponent(String(key));
|
||||
key = key.replace(/%(23|24|26|2B|5E|60|7C)/g, decodeURIComponent);
|
||||
key = key.replace(/[\(\)]/g, escape);
|
||||
|
||||
var stringifiedAttributes = '';
|
||||
|
||||
for (var attributeName in attributes) {
|
||||
if (!attributes[attributeName]) {
|
||||
continue;
|
||||
}
|
||||
stringifiedAttributes += '; ' + attributeName;
|
||||
if (attributes[attributeName] === true) {
|
||||
continue;
|
||||
}
|
||||
stringifiedAttributes += '=' + attributes[attributeName];
|
||||
}
|
||||
return (document.cookie = key + '=' + value + stringifiedAttributes);
|
||||
}
|
||||
|
||||
// Read
|
||||
|
||||
if (!key) {
|
||||
result = {};
|
||||
}
|
||||
|
||||
// To prevent the for loop in the first place assign an empty array
|
||||
// in case there are no cookies at all. Also prevents odd result when
|
||||
// calling "get()"
|
||||
var cookies = document.cookie ? document.cookie.split('; ') : [];
|
||||
var rdecode = /(%[0-9A-Z]{2})+/g;
|
||||
var i = 0;
|
||||
|
||||
for (; i < cookies.length; i++) {
|
||||
var parts = cookies[i].split('=');
|
||||
var cookie = parts.slice(1).join('=');
|
||||
|
||||
if (!this.json && cookie.charAt(0) === '"') {
|
||||
cookie = cookie.slice(1, -1);
|
||||
}
|
||||
|
||||
try {
|
||||
var name = parts[0].replace(rdecode, decodeURIComponent);
|
||||
cookie = converter.read ?
|
||||
converter.read(cookie, name) : converter(cookie, name) ||
|
||||
cookie.replace(rdecode, decodeURIComponent);
|
||||
|
||||
if (this.json) {
|
||||
try {
|
||||
cookie = JSON.parse(cookie);
|
||||
} catch (e) {}
|
||||
}
|
||||
|
||||
if (key === name) {
|
||||
result = cookie;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!key) {
|
||||
result[name] = cookie;
|
||||
}
|
||||
} catch (e) {}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
api.set = api;
|
||||
api.get = function (key) {
|
||||
return api.call(api, key);
|
||||
};
|
||||
api.getJSON = function () {
|
||||
return api.apply({
|
||||
json: true
|
||||
}, [].slice.call(arguments));
|
||||
};
|
||||
api.defaults = {};
|
||||
|
||||
api.remove = function (key, attributes) {
|
||||
api(key, '', extend(attributes, {
|
||||
expires: -1
|
||||
}));
|
||||
};
|
||||
|
||||
api.withConverter = init;
|
||||
|
||||
return api;
|
||||
}
|
||||
|
||||
return init(function () {});
|
||||
}));
|
||||
File diff suppressed because one or more lines are too long
|
|
@ -0,0 +1,539 @@
|
|||
/**
|
||||
* # ResurrectJS
|
||||
* @version 1.0.3
|
||||
* @license Public Domain
|
||||
*
|
||||
* ResurrectJS preserves object behavior (prototypes) and reference
|
||||
* circularity with a special JSON encoding. Unlike regular JSON,
|
||||
* Date, RegExp, DOM objects, and `undefined` are also properly
|
||||
* preserved.
|
||||
*
|
||||
* ## Examples
|
||||
*
|
||||
* function Foo() {}
|
||||
* Foo.prototype.greet = function() { return "hello"; };
|
||||
*
|
||||
* // Behavior is preserved:
|
||||
* var necromancer = new Resurrect();
|
||||
* var json = necromancer.stringify(new Foo());
|
||||
* var foo = necromancer.resurrect(json);
|
||||
* foo.greet(); // => "hello"
|
||||
*
|
||||
* // References to the same object are preserved:
|
||||
* json = necromancer.stringify([foo, foo]);
|
||||
* var array = necromancer.resurrect(json);
|
||||
* array[0] === array[1]; // => true
|
||||
* array[1].greet(); // => "hello"
|
||||
*
|
||||
* // Dates are restored properly
|
||||
* json = necromancer.stringify(new Date());
|
||||
* var date = necromancer.resurrect(json);
|
||||
* Object.prototype.toString.call(date); // => "[object Date]"
|
||||
*
|
||||
* ## Options
|
||||
*
|
||||
* Options are provided to the constructor as an object with these
|
||||
* properties:
|
||||
*
|
||||
* prefix ('#'): A prefix string used for temporary properties added
|
||||
* to objects during serialization and deserialization. It is
|
||||
* important that you don't use any properties beginning with this
|
||||
* string. This option must be consistent between both
|
||||
* serialization and deserialization.
|
||||
*
|
||||
* cleanup (false): Perform full property cleanup after both
|
||||
* serialization and deserialization using the `delete`
|
||||
* operator. This may cause performance penalties (breaking hidden
|
||||
* classes in V8) on objects that ResurrectJS touches, so enable
|
||||
* with care.
|
||||
*
|
||||
* revive (true): Restore behavior (__proto__) to objects that have
|
||||
* been resurrected. If this is set to false during serialization,
|
||||
* resurrection information will not be encoded. You still get
|
||||
* circularity and Date support.
|
||||
*
|
||||
* resolver (Resurrect.NamespaceResolver(window)): Converts between
|
||||
* a name and a prototype. Create a custom resolver if your
|
||||
* constructors are not stored in global variables. The resolver
|
||||
* has two methods: getName(object) and getPrototype(string).
|
||||
*
|
||||
* For example,
|
||||
*
|
||||
* var necromancer = new Resurrect({
|
||||
* prefix: '__#',
|
||||
* cleanup: true
|
||||
* });
|
||||
*
|
||||
* ## Caveats
|
||||
*
|
||||
* * With the default resolver, all constructors must be named and
|
||||
* stored in the global variable under that name. This is required
|
||||
* so that the prototypes can be looked up and reconnected at
|
||||
* resurrection time.
|
||||
*
|
||||
* * The wrapper objects Boolean, String, and Number will be
|
||||
* unwrapped. This means extra properties added to these objects
|
||||
* will not be preserved.
|
||||
*
|
||||
* * Functions cannot ever be serialized. Resurrect will throw an
|
||||
* error if a function is found when traversing a data structure.
|
||||
*
|
||||
* @see http://nullprogram.com/blog/2013/03/28/
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {Object} [options] See options documentation.
|
||||
* @namespace
|
||||
* @constructor
|
||||
*/
|
||||
function Resurrect(options) {
|
||||
this.table = null;
|
||||
this.prefix = '#';
|
||||
this.cleanup = false;
|
||||
this.revive = true;
|
||||
for (var option in options) {
|
||||
if (options.hasOwnProperty(option)) {
|
||||
this[option] = options[option];
|
||||
}
|
||||
}
|
||||
this.refcode = this.prefix + 'id';
|
||||
this.origcode = this.prefix + 'original';
|
||||
this.buildcode = this.prefix + '.';
|
||||
this.valuecode = this.prefix + 'v';
|
||||
}
|
||||
|
||||
/**
|
||||
* Portable access to the global object (window, global).
|
||||
* Uses indirect eval.
|
||||
* @constant
|
||||
*/
|
||||
Resurrect.GLOBAL = (0, eval)('this');
|
||||
|
||||
/**
|
||||
* Escape special regular expression characters in a string.
|
||||
* @param {string} string
|
||||
* @returns {string} The string escaped for exact matches.
|
||||
* @see http://stackoverflow.com/a/6969486
|
||||
*/
|
||||
Resurrect.escapeRegExp = function (string) {
|
||||
return string.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
|
||||
};
|
||||
|
||||
/* Helper Objects */
|
||||
|
||||
/**
|
||||
* @param {string} [message]
|
||||
* @constructor
|
||||
*/
|
||||
Resurrect.prototype.Error = function ResurrectError(message) {
|
||||
this.message = message || '';
|
||||
this.stack = new Error().stack;
|
||||
};
|
||||
Resurrect.prototype.Error.prototype = Object.create(Error.prototype);
|
||||
Resurrect.prototype.Error.prototype.name = 'ResurrectError';
|
||||
|
||||
/**
|
||||
* Resolves prototypes through the properties on an object and
|
||||
* constructor names.
|
||||
* @param {Object} scope
|
||||
* @constructor
|
||||
*/
|
||||
Resurrect.NamespaceResolver = function(scope) {
|
||||
this.scope = scope;
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets the prototype of the given property name from an object. If
|
||||
* not found, it throws an error.
|
||||
* @param {string} name
|
||||
* @returns {Object}
|
||||
* @method
|
||||
*/
|
||||
Resurrect.NamespaceResolver.prototype.getPrototype = function(name) {
|
||||
var constructor = this.scope[name];
|
||||
if (constructor) {
|
||||
return constructor.prototype;
|
||||
} else {
|
||||
throw new Resurrect.prototype.Error('Unknown constructor: ' + name);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the prototype name for an object, to be fetched later with getPrototype.
|
||||
* @param {Object} object
|
||||
* @returns {?string} Null if the constructor is Object.
|
||||
* @method
|
||||
*/
|
||||
Resurrect.NamespaceResolver.prototype.getName = function(object) {
|
||||
var constructor = object.constructor.name;
|
||||
if (constructor == null) { // IE
|
||||
var funcPattern = /^\s*function\s*([A-Za-z0-9_$]*)/;
|
||||
constructor = funcPattern.exec(object.constructor)[1];
|
||||
}
|
||||
|
||||
if (constructor === '') {
|
||||
var msg = "Can't serialize objects with anonymous constructors.";
|
||||
throw new Resurrect.prototype.Error(msg);
|
||||
} else if (constructor === 'Object' || constructor === 'Array') {
|
||||
return null;
|
||||
} else {
|
||||
return constructor;
|
||||
}
|
||||
};
|
||||
|
||||
/* Set the default resolver searches the global object. */
|
||||
Resurrect.prototype.resolver =
|
||||
new Resurrect.NamespaceResolver(Resurrect.GLOBAL);
|
||||
|
||||
/**
|
||||
* Create a DOM node from HTML source; behaves like a constructor.
|
||||
* @param {string} html
|
||||
* @constructor
|
||||
*/
|
||||
Resurrect.Node = function(html) {
|
||||
var div = document.createElement('div');
|
||||
div.innerHTML = html;
|
||||
return div.firstChild;
|
||||
};
|
||||
|
||||
/* Type Tests */
|
||||
|
||||
/**
|
||||
* @param {string} type
|
||||
* @returns {Function} A function that tests for type.
|
||||
*/
|
||||
Resurrect.is = function(type) {
|
||||
var string = '[object ' + type + ']';
|
||||
return function(object) {
|
||||
return Object.prototype.toString.call(object) === string;
|
||||
};
|
||||
};
|
||||
|
||||
Resurrect.isArray = Resurrect.is('Array');
|
||||
Resurrect.isString = Resurrect.is('String');
|
||||
Resurrect.isBoolean = Resurrect.is('Boolean');
|
||||
Resurrect.isNumber = Resurrect.is('Number');
|
||||
Resurrect.isFunction = Resurrect.is('Function');
|
||||
Resurrect.isDate = Resurrect.is('Date');
|
||||
Resurrect.isRegExp = Resurrect.is('RegExp');
|
||||
Resurrect.isObject = Resurrect.is('Object');
|
||||
|
||||
Resurrect.isAtom = function(object) {
|
||||
return !Resurrect.isObject(object) && !Resurrect.isArray(object);
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {*} object
|
||||
* @returns {boolean} True if object is a primitive or a primitive wrapper.
|
||||
*/
|
||||
Resurrect.isPrimitive = function(object) {
|
||||
return object == null ||
|
||||
Resurrect.isNumber(object) ||
|
||||
Resurrect.isString(object) ||
|
||||
Resurrect.isBoolean(object);
|
||||
};
|
||||
|
||||
/* Methods */
|
||||
|
||||
/**
|
||||
* Create a reference (encoding) to an object.
|
||||
* @param {(Object|undefined)} object
|
||||
* @returns {Object}
|
||||
* @method
|
||||
*/
|
||||
Resurrect.prototype.ref = function(object) {
|
||||
var ref = {};
|
||||
if (object === undefined) {
|
||||
ref[this.prefix] = -1;
|
||||
} else {
|
||||
ref[this.prefix] = object[this.refcode];
|
||||
}
|
||||
return ref;
|
||||
};
|
||||
|
||||
/**
|
||||
* Lookup an object in the table by reference object.
|
||||
* @param {Object} ref
|
||||
* @returns {(Object|undefined)}
|
||||
* @method
|
||||
*/
|
||||
Resurrect.prototype.deref = function(ref) {
|
||||
return this.table[ref[this.prefix]];
|
||||
};
|
||||
|
||||
/**
|
||||
* Put a temporary identifier on an object and store it in the table.
|
||||
* @param {Object} object
|
||||
* @returns {number} The unique identifier number.
|
||||
* @method
|
||||
*/
|
||||
Resurrect.prototype.tag = function(object) {
|
||||
if (this.revive) {
|
||||
var constructor = this.resolver.getName(object);
|
||||
if (constructor) {
|
||||
var proto = Object.getPrototypeOf(object);
|
||||
if (this.resolver.getPrototype(constructor) !== proto) {
|
||||
throw new this.Error('Constructor mismatch!');
|
||||
} else {
|
||||
object[this.prefix] = constructor;
|
||||
}
|
||||
}
|
||||
}
|
||||
object[this.refcode] = this.table.length;
|
||||
this.table.push(object);
|
||||
return object[this.refcode];
|
||||
};
|
||||
|
||||
/**
|
||||
* Create a builder object (encoding) for serialization.
|
||||
* @param {string} name The name of the constructor.
|
||||
* @param value The value to pass to the constructor.
|
||||
* @returns {Object}
|
||||
* @method
|
||||
*/
|
||||
Resurrect.prototype.builder = function(name, value) {
|
||||
var builder = {};
|
||||
builder[this.buildcode] = name;
|
||||
builder[this.valuecode] = value;
|
||||
return builder;
|
||||
};
|
||||
|
||||
/**
|
||||
* Build a value from a deserialized builder.
|
||||
* @param {Object} ref
|
||||
* @returns {Object}
|
||||
* @method
|
||||
* @see http://stackoverflow.com/a/14378462
|
||||
* @see http://nullprogram.com/blog/2013/03/24/
|
||||
*/
|
||||
Resurrect.prototype.build = function(ref) {
|
||||
var type = ref[this.buildcode].split(/\./).reduce(function(object, name) {
|
||||
return object[name];
|
||||
}, Resurrect.GLOBAL);
|
||||
/* Brilliant hack by kybernetikos: */
|
||||
var args = [null].concat(ref[this.valuecode]);
|
||||
var factory = type.bind.apply(type, args);
|
||||
var result = new factory();
|
||||
if (Resurrect.isPrimitive(result)) {
|
||||
return result.valueOf(); // unwrap
|
||||
} else {
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Dereference or build an object or value from an encoding.
|
||||
* @param {Object} ref
|
||||
* @returns {(Object|undefined)}
|
||||
* @method
|
||||
*/
|
||||
Resurrect.prototype.decode = function(ref) {
|
||||
if (this.prefix in ref) {
|
||||
return this.deref(ref);
|
||||
} else if (this.buildcode in ref) {
|
||||
return this.build(ref);
|
||||
} else {
|
||||
throw new this.Error('Unknown encoding.');
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {Object} object
|
||||
* @returns {boolean} True if the provided object is tagged for serialization.
|
||||
* @method
|
||||
*/
|
||||
Resurrect.prototype.isTagged = function(object) {
|
||||
return (this.refcode in object) && (object[this.refcode] != null);
|
||||
};
|
||||
|
||||
/**
|
||||
* Visit root and all its ancestors, visiting atoms with f.
|
||||
* @param {*} root
|
||||
* @param {Function} f
|
||||
* @param {Function} replacer
|
||||
* @returns {*} A fresh copy of root to be serialized.
|
||||
* @method
|
||||
*/
|
||||
Resurrect.prototype.visit = function(root, f, replacer) {
|
||||
if (Resurrect.isAtom(root)) {
|
||||
return f(root);
|
||||
} else if (!this.isTagged(root)) {
|
||||
var copy = null;
|
||||
if (Resurrect.isArray(root)) {
|
||||
copy = [];
|
||||
root[this.refcode] = this.tag(copy);
|
||||
for (var i = 0; i < root.length; i++) {
|
||||
copy.push(this.visit(root[i], f, replacer));
|
||||
}
|
||||
} else { /* Object */
|
||||
copy = Object.create(Object.getPrototypeOf(root));
|
||||
root[this.refcode] = this.tag(copy);
|
||||
for (var key in root) {
|
||||
var value = root[key];
|
||||
if (root.hasOwnProperty(key)) {
|
||||
if (replacer && value !== undefined) {
|
||||
// Call replacer like JSON.stringify's replacer
|
||||
value = replacer.call(root, key, root[key]);
|
||||
if (value === undefined) {
|
||||
continue; // Omit from result
|
||||
}
|
||||
}
|
||||
copy[key] = this.visit(value, f, replacer);
|
||||
}
|
||||
}
|
||||
}
|
||||
copy[this.origcode] = root;
|
||||
return this.ref(copy);
|
||||
} else {
|
||||
return this.ref(root);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Manage special atom values, possibly returning an encoding.
|
||||
* @param {*} atom
|
||||
* @returns {*}
|
||||
* @method
|
||||
*/
|
||||
Resurrect.prototype.handleAtom = function(atom) {
|
||||
var Node = Resurrect.GLOBAL.Node || function() {};
|
||||
if (Resurrect.isFunction(atom)) {
|
||||
throw new this.Error("Can't serialize functions.");
|
||||
} else if (atom instanceof Node) {
|
||||
var xmls = new XMLSerializer();
|
||||
return this.builder('Resurrect.Node', [xmls.serializeToString(atom)]);
|
||||
} else if (Resurrect.isDate(atom)) {
|
||||
return this.builder('Date', [atom.toISOString()]);
|
||||
} else if (Resurrect.isRegExp(atom)) {
|
||||
var args = atom.toString().match(/\/(.+)\/([gimy]*)/).slice(1);
|
||||
return this.builder('RegExp', args);
|
||||
} else if (atom === undefined) {
|
||||
return this.ref(undefined);
|
||||
} else if (Resurrect.isNumber(atom) && (isNaN(atom) || !isFinite(atom))) {
|
||||
return this.builder('Number', [atom.toString()]);
|
||||
} else {
|
||||
return atom;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Hides intrusive keys from a user-supplied replacer.
|
||||
* @param {Function} replacer function of two arguments (key, value)
|
||||
* @returns {Function} A function that skips the replacer for intrusive keys.
|
||||
* @method
|
||||
*/
|
||||
Resurrect.prototype.replacerWrapper = function(replacer) {
|
||||
var skip = new RegExp('^' + Resurrect.escapeRegExp(this.prefix));
|
||||
return function(k, v) {
|
||||
if (skip.test(k)) {
|
||||
return v;
|
||||
} else {
|
||||
return replacer(k, v);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Serialize an arbitrary JavaScript object, carefully preserving it.
|
||||
* @param {*} object
|
||||
* @param {(Function|Array)} replacer
|
||||
* @param {string} space
|
||||
* @method
|
||||
*/
|
||||
Resurrect.prototype.stringify = function(object, replacer, space) {
|
||||
if (Resurrect.isFunction(replacer)) {
|
||||
replacer = this.replacerWrapper(replacer);
|
||||
} else if (Resurrect.isArray(replacer)) {
|
||||
var acceptKeys = replacer;
|
||||
replacer = function(k, v) {
|
||||
return acceptKeys.indexOf(k) >= 0 ? v : undefined;
|
||||
};
|
||||
}
|
||||
if (Resurrect.isAtom(object)) {
|
||||
return JSON.stringify(this.handleAtom(object), replacer, space);
|
||||
} else {
|
||||
this.table = [];
|
||||
this.visit(object, this.handleAtom.bind(this), replacer);
|
||||
for (var i = 0; i < this.table.length; i++) {
|
||||
if (this.cleanup) {
|
||||
delete this.table[i][this.origcode][this.refcode];
|
||||
} else {
|
||||
this.table[i][this.origcode][this.refcode] = null;
|
||||
}
|
||||
delete this.table[i][this.refcode];
|
||||
delete this.table[i][this.origcode];
|
||||
}
|
||||
var table = this.table;
|
||||
this.table = null;
|
||||
return JSON.stringify(table, null, space);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Restore the __proto__ of the given object to the proper value.
|
||||
* @param {Object} object
|
||||
* @returns {Object} Its argument, or a copy, with the prototype restored.
|
||||
* @method
|
||||
*/
|
||||
Resurrect.prototype.fixPrototype = function(object) {
|
||||
if (this.prefix in object) {
|
||||
var name = object[this.prefix];
|
||||
var prototype = this.resolver.getPrototype(name);
|
||||
if ('__proto__' in object) {
|
||||
object.__proto__ = prototype;
|
||||
if (this.cleanup) {
|
||||
delete object[this.prefix];
|
||||
}
|
||||
return object;
|
||||
} else { // IE
|
||||
var copy = Object.create(prototype);
|
||||
for (var key in object) {
|
||||
if (object.hasOwnProperty(key) && key !== this.prefix) {
|
||||
copy[key] = object[key];
|
||||
}
|
||||
}
|
||||
return copy;
|
||||
}
|
||||
} else {
|
||||
return object;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Deserialize an encoded object, restoring circularity and behavior.
|
||||
* @param {string} string
|
||||
* @returns {*} The decoded object or value.
|
||||
* @method
|
||||
*/
|
||||
Resurrect.prototype.resurrect = function(string) {
|
||||
var result = null;
|
||||
var data = JSON.parse(string);
|
||||
if (Resurrect.isArray(data)) {
|
||||
this.table = data;
|
||||
/* Restore __proto__. */
|
||||
if (this.revive) {
|
||||
for (var i = 0; i < this.table.length; i++) {
|
||||
this.table[i] = this.fixPrototype(this.table[i]);
|
||||
}
|
||||
}
|
||||
/* Re-establish object references and construct atoms. */
|
||||
for (i = 0; i < this.table.length; i++) {
|
||||
var object = this.table[i];
|
||||
for (var key in object) {
|
||||
if (object.hasOwnProperty(key)) {
|
||||
if (!(Resurrect.isAtom(object[key]))) {
|
||||
object[key] = this.decode(object[key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
result = this.table[0];
|
||||
} else if (Resurrect.isObject(data)) {
|
||||
this.table = [];
|
||||
result = this.decode(data);
|
||||
} else {
|
||||
result = data;
|
||||
}
|
||||
this.table = null;
|
||||
return result;
|
||||
};
|
||||
|
|
@ -7,6 +7,7 @@ class Conveyorbelt extends Tile {
|
|||
super(x, y, factory)
|
||||
this.name = "conveyorbelt"
|
||||
this.hasNoInventory = true
|
||||
this.index = 0
|
||||
this.cost = [{
|
||||
"id": 0,
|
||||
"count": 20
|
||||
|
|
@ -26,6 +27,7 @@ class Treefarm extends Tile {
|
|||
this.maxwork = 96 * 5
|
||||
this.currentwork = 0
|
||||
this.name = "treefarm"
|
||||
this.index = 1
|
||||
this.hasNoInventory = true
|
||||
this.cost = [{
|
||||
"id": 0,
|
||||
|
|
@ -55,6 +57,7 @@ class Saw extends Tile {
|
|||
this.maxwork = 48
|
||||
this.currentwork = 0
|
||||
this.name = "saw"
|
||||
this.index = 2
|
||||
this.cost = [{
|
||||
"id": 0,
|
||||
"count": 750
|
||||
|
|
@ -99,6 +102,7 @@ class Charcoalmeiler extends Tile {
|
|||
this.maxwork = 96 * 10
|
||||
this.currentwork = 0
|
||||
this.name = "charcoalmeiler"
|
||||
this.index = 3
|
||||
this.cost = [{
|
||||
"id": 0,
|
||||
"count": 1000
|
||||
|
|
@ -148,6 +152,7 @@ class Quarry extends Tile {
|
|||
constructor(x, y, factory) {
|
||||
super(x, y, factory)
|
||||
this.maxwork = 96 * 3
|
||||
this.index = 4
|
||||
this.currentwork = 0
|
||||
this.name = "quarry"
|
||||
this.hasNoInventory = true
|
||||
|
|
@ -181,6 +186,7 @@ class Collector extends Tile {
|
|||
super(x, y, factory)
|
||||
this.name = "collector"
|
||||
this.hasNoInventory = true
|
||||
this.index = 5
|
||||
this.cost = [{
|
||||
"id": 0,
|
||||
"count": 50
|
||||
|
|
@ -207,6 +213,7 @@ class Spliter extends Tile {
|
|||
super(x, y, factory)
|
||||
this.name = "spliter"
|
||||
this.hasNoInventory = true
|
||||
this.index = 6
|
||||
this.odd = true
|
||||
this.cost = [{
|
||||
"id": 0,
|
||||
|
|
@ -268,6 +275,7 @@ class Warehouse extends Tile {
|
|||
this.hasNoInventory = true
|
||||
this.maxwork = 48 * 10
|
||||
this.sellPower = 500
|
||||
this.index = 7
|
||||
this.cost = [{
|
||||
"id": 0,
|
||||
"count": 20000
|
||||
|
|
|
|||
Loading…
Reference in New Issue