Fixes for serialization

This commit is contained in:
Cmdr McDonald
2016-11-04 17:13:28 +00:00
parent c1f4a8d416
commit d08e5e2858
3 changed files with 36 additions and 12 deletions

View File

@@ -33,6 +33,15 @@ export default class ModificationsMenu extends TranslatedComponent {
*/
_updateValue(value) {
let scaledValue = Math.floor(Number(value) * 100) / 10000;
// Limit to +1000% / -100%
if (scaledValue > 10) {
scaledValue = 10;
value = 1000;
}
if (scaledValue < -1) {
scaledValue = -1;
value = -100;
}
let m = this.props.m;
let name = this.props.name;
let ship = this.props.ship;

View File

@@ -24,7 +24,7 @@ function standardToSchema(standard) {
o.name = standard.m.name;
}
if (standard.m.mods && standard.m.mods != {}) {
if (standard.m.mods && Object.keys(standard.m.mods).length > 0) {
o.modifications = standard.m.mods;
}
@@ -57,7 +57,7 @@ function slotToSchema(slot) {
if (slot.m.missile) {
o.missile = slot.m.missile;
}
if (slot.m.mods && slot.m.mods != {}) {
if (slot.m.mods && Object.keys(slot.m.mods).length > 0) {
o.modifications = slot.m.mods;
}
return o;
@@ -115,12 +115,27 @@ export function toDetailedBuild(buildName, ship) {
return data;
};
export function fromDetailedBuild(detailedBuild) {
let shipId = Object.keys(Ships).find((shipId) => Ships[shipId].properties.name.toLowerCase() == detailedBuild.ship.toLowerCase());
if (!shipId) {
throw 'No such ship: ' + detailedBuild.ship;
}
let comps = detailedBuild.components;
let stn = comps.standard;
let shipData = Ships[shipId];
let ship = new Ship(shipId, shipData.properties, shipData.slots);
return ship.buildFrom(detailedBuild.references[0].code);
};
/**
* Instantiates a ship from a ship-loadout object
* @param {Object} detailedBuild ship-loadout object
* @return {Ship} Ship instance
*/
export function fromDetailedBuild(detailedBuild) {
export function oldfromDetailedBuild(detailedBuild) {
let shipId = Object.keys(Ships).find((shipId) => Ships[shipId].properties.name.toLowerCase() == detailedBuild.ship.toLowerCase());
if (!shipId) {

View File

@@ -1116,7 +1116,7 @@ export default class Ship {
let bufsize = 0;
for (let slot of slots) {
if (slot.length > 0) {
bufsize = bufsize + 1 + (3 * slot.length) + 1; //
bufsize = bufsize + 1 + (5 * slot.length) + 1;
}
}
@@ -1131,8 +1131,8 @@ export default class Ship {
buffer.writeInt8(i, curpos++);
for (let slotMod of slot) {
buffer.writeInt8(slotMod.id, curpos++);
buffer.writeInt16BE(slotMod.value, curpos);
curpos += 2;
buffer.writeInt32LE(slotMod.value, curpos);
curpos += 4;
}
buffer.writeInt8(-1, curpos++);
}
@@ -1157,18 +1157,18 @@ export default class Ship {
*/
decodeModificationsStruct(buffer, arr) {
let curpos = 0;
let module = buffer.readInt8(curpos++);
while (module != -1) {
let slot = buffer.readInt8(curpos++);
while (slot != -1) {
let modifications = {};
let modificationId = buffer.readInt8(curpos++);
while (modificationId != -1) {
let modificationValue = buffer.readInt16BE(curpos);
curpos += 2;
let modificationValue = buffer.readInt32LE(curpos);
curpos += 4;
modifications[Modifications.modifiers[modificationId]] = modificationValue;
modificationId = buffer.readInt8(curpos++);
}
arr[module] = modifications;
module = buffer.readInt8(curpos++);
arr[slot] = modifications;
slot = buffer.readInt8(curpos++);
}
}