Persist power and priority changes

This commit is contained in:
Colin McLeod
2015-06-11 00:08:56 -07:00
parent f77443156d
commit 2f3ee50ec8
2 changed files with 84 additions and 46 deletions

View File

@@ -9,11 +9,20 @@ angular.module('app').service('Serializer', ['lodash', function (_) {
* @return {string} Encoded string of components
*/
this.fromShip = function(ship) {
var power = {
enabled: [ship.cargoScoop.enabled? 1 : 0],
priorities: [ship.cargoScoop.priority]
};
var data = [
ship.bulkheads.id,
_.map(ship.common, idToStr),
_.map(ship.hardpoints, idToStr),
_.map(ship.internal, idToStr),
_.map(ship.common, mapGroup, power),
_.map(ship.hardpoints, mapGroup, power),
_.map(ship.internal, mapGroup, power),
'.',
LZString.compressToBase64(power.enabled.join('')).replace(/\//g,'-'),
'.',
LZString.compressToBase64(power.priorities.join('')).replace(/\//g,'-')
];
return _.flatten(data).join('');
@@ -26,32 +35,38 @@ angular.module('app').service('Serializer', ['lodash', function (_) {
* @param {Ship} ship The ship instance to be updated
* @param {string} code The string to deserialize
*/
this.toShip = function (ship, code) {
var commonCount = ship.common.length;
var hpCount = commonCount + ship.hardpoints.length;
var comps = {
bulkheads: code.charAt(0) * 1,
common: new Array(ship.common.length),
hardpoints: new Array(ship.hardpoints.length),
internal: new Array(ship.internal.length)
};
this.toShip = function (ship, dataString) {
var commonCount = ship.common.length,
hpCount = commonCount + ship.hardpoints.length,
totalCount = hpCount + ship.internal.length,
common = new Array(ship.common.length),
hardpoints = new Array(ship.hardpoints.length),
internal = new Array(ship.internal.length),
parts = dataString.split('.'),
priorities = null,
enabled = null,
code = parts[0];
// TODO: improve...
for (var i = 1, c = 0, l = code.length; i < l; i++) {
var empty = code.charAt(i) == '-';
if (c < commonCount) {
comps.common[c] = empty? 0 : code.substring(i, i + 2);
} else if (c < hpCount) {
comps.hardpoints[c - commonCount] = empty? 0 : code.substring(i, i + 2);
} else {
comps.internal[c - hpCount] = empty? 0 : code.substring(i, i + 2);
if(parts[1]) {
enabled = LZString.decompressFromBase64(parts[1].replace(/-/g,'/')).split('');
}
if (!empty) {
i++;
if(parts[2]) {
priorities = LZString.decompressFromBase64(parts[2].replace(/-/g,'/')).split('');
}
c++;
}
ship.buildWith(comps);
decodeToArray(code, internal, decodeToArray(code, hardpoints, decodeToArray(code, common, 1)));
// get the remaining substring / split into parts for
// - priorities
// - enabled/disabled
ship.buildWith({
bulkheads: code.charAt(0) * 1,
common: common,
hardpoints: hardpoints,
internal: internal,
}, priorities, enabled);
};
this.fromComparison = function (name, builds, facets, predicate, desc) {
@@ -82,8 +97,24 @@ angular.module('app').service('Serializer', ['lodash', function (_) {
* @param {object} slot The slot object.
* @return {string} The id of the selected component or '-' if none selected
*/
function idToStr(slot) {
function mapGroup(slot) {
this.enabled.push(slot.enabled? 1 : 0);
this.priorities.push(slot.priority);
return (slot.id === null)? '-' : slot.id;
}
function decodeToArray(code, arr, codePos) {
for (i = 0; i < arr.length; i++) {
if (code.charAt(codePos) == '-') {
arr[i] = 0;
codePos++;
} else {
arr[i] = code.substring(codePos, codePos + 2);
codePos += 2;
}
}
return codePos;
}
}]);

View File

@@ -9,7 +9,7 @@ angular.module('shipyard').factory('Ship', ['Components', 'calcShieldStrength',
*/
function Ship(id, properties, slots) {
this.id = id;
this.cargoScoop = { enabled: true, c: Components.cargoScoop(), type: 'SYS' };
this.cargoScoop = { c: Components.cargoScoop(), type: 'SYS' };
this.bulkheads = { incCost: true, maxClass: 8 };
for (var p in properties) { this[p] = properties[p]; } // Copy all base properties from shipData
@@ -18,7 +18,7 @@ angular.module('shipyard').factory('Ship', ['Components', 'calcShieldStrength',
var slotGroup = slots[slotType];
var group = this[slotType] = []; // Initialize Slot group (Common, Hardpoints, Internal)
for(var i = 0; i < slotGroup.length; i++){
group.push({id: null, c: null, enabled: true, incCost: true, maxClass: slotGroup[i]});
group.push({id: null, c: null, incCost: true, maxClass: slotGroup[i]});
}
}
this.c = { incCost: true, c: { name: this.name, cost: this.cost } }; // Make a 'Ship' component similar to other components
@@ -35,6 +35,7 @@ angular.module('shipyard').factory('Ship', ['Components', 'calcShieldStrength',
this.powerList.unshift(this.common[3]); // Add Life Support
this.powerList.unshift(this.common[2]); // Add FSD
this.powerList.unshift(this.common[0]); // Add Power Plant
this.priorityBands = [
{deployed: 0, retracted: 0},
{deployed: 0, retracted: 0},
@@ -43,7 +44,7 @@ angular.module('shipyard').factory('Ship', ['Components', 'calcShieldStrength',
{deployed: 0, retracted: 0}
];
// Reset cumulative and aggragate stats
// Cumulative and aggragate stats
this.fuelCapacity = 0;
this.cargoCapacity = 0;
this.ladenMass = 0;
@@ -58,20 +59,24 @@ angular.module('shipyard').factory('Ship', ['Components', 'calcShieldStrength',
* Builds/Updates the ship instance with the components[comps] passed in.
* @param {object} comps Collection of components used to build the ship
*/
Ship.prototype.buildWith = function(comps) {
var internal = this.internal;
var common = this.common;
var hps = this.hardpoints;
var bands = this.priorityBands;
var i,l;
Ship.prototype.buildWith = function(comps, priorities, enabled) {
var internal = this.internal,
common = this.common,
hps = this.hardpoints,
bands = this.priorityBands,
cl = common.length, hl = hps.length, il = internal.length,
i,l;
this.useBulkhead(comps.bulkheads || 0, true);
this.cargoScoop.priority = 0; // TODO set from comps
this.cargoScoop.priority = priorities? priorities[0] * 1 : 0;
this.cargoScoop.enabled = enabled? enabled[0] * 1 : true;
if (this.cargoScoop.enabled) {
bands[this.cargoScoop.priority].retracted += this.cargoScoop.c.power;
}
for(i = 0, l = comps.common.length; i < l; i++) {
common[i].enabled = true; // TODO set enabled from comps
common[i].priority = 0; // TODO set from comps
for(i = 0; i < cl; i++) {
common[i].enabled = enabled? enabled[i] * 1 : true;
common[i].priority = priorities? priorities[i] * 1 : 0;
common[i].type = 'SYS';
this.use(common[i], comps.common[i], Components.common(i, comps.common[i]), true);
}
@@ -80,9 +85,10 @@ angular.module('shipyard').factory('Ship', ['Components', 'calcShieldStrength',
common[2].type = 'ENG'; // FSD
for(i = 0, l = comps.hardpoints.length; i < l; i++) {
hps[i].enabled = true; // TODO set enabled from comps
hps[i].priority = 0; // TODO set from comps
hps[i].enabled = enabled? enabled[cl + i] * 1 : true;
hps[i].priority = priorities? priorities[cl + i] * 1 : 0;
hps[i].type = hps[i].maxClass? 'WEP' : 'SYS';
if (comps.hardpoints[i] !== 0) {
this.use(hps[i], comps.hardpoints[i], Components.hardpoints(comps.hardpoints[i]), true);
} else {
@@ -91,9 +97,10 @@ angular.module('shipyard').factory('Ship', ['Components', 'calcShieldStrength',
}
for(i = 0, l = comps.internal.length; i < l; i++) {
internal[i].enabled = true; // TODO set enabled from comps
internal[i].priority = 0; // TODO set from comps
internal[i].enabled = enabled? enabled[hl + cl + i] * 1 : true;
internal[i].priority = priorities? priorities[hl + cl + i] * 1 : 0;
internal[i].type = 'SYS';
if (comps.internal[i] !== 0) {
this.use(internal[i], comps.internal[i], Components.internal(comps.internal[i]), true);
} else {