mirror of
https://github.com/EDCD/coriolis.git
synced 2025-12-10 07:05:35 +00:00
Massive refactor for 3rd party import
This commit is contained in:
@@ -1,88 +1,110 @@
|
||||
angular.module('app').controller('ImportController', ['$scope', '$stateParams', 'ShipsDB', 'Ship', 'Persist', 'Serializer', function($scope, $stateParams, Ships, Ship, Persist, Serializer) {
|
||||
angular.module('app').controller('ImportController', ['lodash', '$rootScope', '$scope', '$stateParams', 'ShipsDB', 'Ship', 'Persist', 'Serializer', function(_, $rootScope, $scope, $stateParams, Ships, Ship, Persist, Serializer) {
|
||||
$scope.jsonValid = false;
|
||||
$scope.importData = null;
|
||||
$scope.importJSON = null;
|
||||
$scope.errorMsg = null;
|
||||
$scope.canEdit = true;
|
||||
$scope.builds = $stateParams.obj || null;
|
||||
$scope.ships = Ships;
|
||||
|
||||
$scope.validateJson = function() {
|
||||
var importObj = null, shipData = null;
|
||||
$scope.jsonValid = false;
|
||||
$scope.errorMsg = null;
|
||||
$scope.builds = null;
|
||||
function validateBuild(shipId, code, name) {
|
||||
var shipData = Ships[shipId];
|
||||
|
||||
if (!$scope.importData) { return; }
|
||||
if (!shipData) {
|
||||
throw '"' + shipId + '" is not a valid Ship Id!';
|
||||
}
|
||||
if (typeof name != 'string' || name.length < 3) {
|
||||
throw shipData.properties.name + ' build "' + name + '" must be a string at least 3 characters long!';
|
||||
}
|
||||
if (typeof code != 'string' || code.length < 10) {
|
||||
throw shipData.properties.name + ' build "' + name + '" is not valid!';
|
||||
}
|
||||
try {
|
||||
Serializer.toShip(new Ship(shipId, shipData.properties, shipData.slots), code);
|
||||
} catch (e) {
|
||||
throw shipData.properties.name + ' build "' + name + '" is not valid!';
|
||||
}
|
||||
}
|
||||
|
||||
function detailedJsonToBuild(detailedBuild) {
|
||||
var ship;
|
||||
if (!detailedBuild.name) {
|
||||
throw 'Build Name missing!';
|
||||
}
|
||||
|
||||
try {
|
||||
importObj = angular.fromJson($scope.importData);
|
||||
ship = Serializer.fromDetailedBuild(detailedBuild);
|
||||
} catch (e) {
|
||||
throw detailedBuild.ship + ' Build "' + detailedBuild.name + '": Invalid data';
|
||||
}
|
||||
|
||||
return { shipId: ship.id, name: detailedBuild.name, code: Serializer.fromShip(ship) };
|
||||
}
|
||||
|
||||
function importBackup(importData) {
|
||||
if (importData.builds && typeof importData.builds == 'object') {
|
||||
for (var shipId in importData.builds) {
|
||||
for (var buildName in importData.builds[shipId]) {
|
||||
validateBuild(shipId, importData.builds[shipId][buildName], buildName);
|
||||
}
|
||||
}
|
||||
$scope.builds = importData.builds;
|
||||
} else {
|
||||
throw 'builds must be an object!';
|
||||
}
|
||||
if (importData.comparisons) {
|
||||
// TODO: check ship/builds exist for comparison
|
||||
$scope.comparisons = importData.comparisons;
|
||||
}
|
||||
if (importData.discounts instanceof Array && importData.discounts.length == 2) {
|
||||
$scope.discounts = importData.discounts;
|
||||
}
|
||||
if (typeof importData.insurance == 'string' && importData.insurance.length > 3) {
|
||||
$scope.insurance = importData.insurance;
|
||||
}
|
||||
}
|
||||
|
||||
function importDetailedArray(importArr) {
|
||||
var builds = {};
|
||||
for (var i = 0, l = importArr.length; i < l; i++) {
|
||||
var build = detailedJsonToBuild(importArr[i]);
|
||||
if (!builds[build.shipId]) {
|
||||
builds[build.shipId] = {};
|
||||
}
|
||||
builds[build.shipId][build.name] = build.code;
|
||||
}
|
||||
$scope.builds = builds;
|
||||
}
|
||||
|
||||
$scope.validateJson = function() {
|
||||
var importData = null;
|
||||
$scope.jsonValid = false;
|
||||
$scope.errorMsg = null;
|
||||
$scope.builds = $scope.discounts = $scope.comparisons = $scope.insurance = null;
|
||||
|
||||
if (!$scope.importJSON) { return; }
|
||||
|
||||
try {
|
||||
importData = angular.fromJson($scope.importJSON);
|
||||
} catch (e) {
|
||||
$scope.errorMsg = 'Cannot Parse JSON!';
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof importObj != 'object') {
|
||||
if (!importData || typeof importData != 'object') {
|
||||
$scope.errorMsg = 'Must be an object or array!';
|
||||
return;
|
||||
}
|
||||
|
||||
// Using JSON from a simple/shortform/standard export
|
||||
if (importObj.builds && Object.keys(importObj.builds).length) {
|
||||
for (var shipId in importObj.builds) {
|
||||
shipData = Ships[shipId];
|
||||
if (shipData) {
|
||||
for (var buildName in importObj.builds[shipId]) {
|
||||
if (typeof importObj.builds[shipId][buildName] != 'string') {
|
||||
$scope.errorMsg = shipData.properties.name + ' build "' + buildName + '" must be a string!';
|
||||
return;
|
||||
}
|
||||
try {
|
||||
// Actually build the ship with the code to ensure it's valid
|
||||
Serializer.toShip(new Ship(shipId, shipData.properties, shipData.slots), importObj.builds[shipId][buildName]);
|
||||
} catch (e) {
|
||||
$scope.errorMsg = shipData.properties.name + ' build "' + buildName + '" is not valid!';
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$scope.errorMsg = '"' + shipId + '"" is not a valid Ship Id!';
|
||||
return;
|
||||
}
|
||||
$scope.builds = importObj.builds;
|
||||
try {
|
||||
if (importData instanceof Array) { // Must be detailed export json
|
||||
importDetailedArray(importData);
|
||||
} else if (importData.ship && importData.name) { // Using JSON from a single ship build export
|
||||
importDetailedArray([importData]); // Convert to array with singleobject
|
||||
} else { // Using Backup JSON
|
||||
importBackup(importData);
|
||||
}
|
||||
|
||||
// Using JSON from a detailed export
|
||||
} else if (importObj.length && importObj[0].references && importObj[0].references.length) {
|
||||
var builds = {};
|
||||
for (var i = 0, l = importObj.length; i < l; i++) {
|
||||
if (typeof importObj[i].name != 'string' || typeof importObj[i].ship != 'string') {
|
||||
$scope.errorMsg = 'Build [' + i + '] must have a ship and build name!';
|
||||
return;
|
||||
}
|
||||
for (var r = 0, rl = importObj[i].references.length; r < rl; r++) {
|
||||
var ref = importObj[i].references[r];
|
||||
if (ref.name == 'Coriolis.io' && ref.code && ref.shipId) {
|
||||
if (!builds[ref.shipId]) {
|
||||
builds[ref.shipId] = {};
|
||||
}
|
||||
try {
|
||||
// Actually build the ship with the code to ensure it's valid
|
||||
shipData = Ships[ref.shipId];
|
||||
Serializer.toShip(new Ship(ref.shipId, shipData.properties, shipData.slots), ref.code);
|
||||
} catch (e) {
|
||||
$scope.errorMsg = importObj[i].ship + ' build "' + importObj[i].name + '" is not valid!';
|
||||
return;
|
||||
}
|
||||
builds[ref.shipId][importObj[i].name] = ref.code;
|
||||
} else {
|
||||
$scope.errorMsg = importObj[i].ship + ' build "' + importObj[i].name + '" has an invalid Coriolis reference!';
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
$scope.builds = builds;
|
||||
} else {
|
||||
$scope.errorMsg = 'No builds in data';
|
||||
} catch (e) {
|
||||
$scope.errorMsg = e;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -93,32 +115,73 @@ angular.module('app').controller('ImportController', ['$scope', '$stateParams',
|
||||
return Persist.getBuild(shipId, name) !== null;
|
||||
};
|
||||
|
||||
$scope.hasComparison = function(name) {
|
||||
return Persist.getComparison(name) !== null;
|
||||
};
|
||||
|
||||
$scope.process = function() {
|
||||
var builds = $scope.builds;
|
||||
for (var shipId in builds) {
|
||||
for (var buildName in builds[shipId]) {
|
||||
var code = builds[shipId][buildName];
|
||||
// Update builds object such that orginal name retained, but can be renamed
|
||||
builds[shipId][buildName] = {
|
||||
code: code,
|
||||
useName: buildName
|
||||
};
|
||||
if ($scope.builds) {
|
||||
var builds = $scope.builds;
|
||||
for (var shipId in builds) {
|
||||
for (var buildName in builds[shipId]) {
|
||||
var code = builds[shipId][buildName];
|
||||
// Update builds object such that orginal name retained, but can be renamed
|
||||
builds[shipId][buildName] = {
|
||||
code: code,
|
||||
useName: buildName
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($scope.comparisons) {
|
||||
var comparisons = $scope.comparisons;
|
||||
for (var name in comparisons) {
|
||||
comparisons[name].useName = name;
|
||||
}
|
||||
}
|
||||
|
||||
$scope.processed = true;
|
||||
};
|
||||
|
||||
$scope.import = function() {
|
||||
var builds = $scope.builds;
|
||||
for (var shipId in builds) {
|
||||
for (var buildName in builds[shipId]) {
|
||||
var build = builds[shipId][buildName];
|
||||
var name = build.useName.trim();
|
||||
if (name) {
|
||||
Persist.saveBuild(shipId, name, build.code);
|
||||
|
||||
if ($scope.builds) {
|
||||
var builds = $scope.builds;
|
||||
for (var shipId in builds) {
|
||||
for (var buildName in builds[shipId]) {
|
||||
var build = builds[shipId][buildName];
|
||||
var name = build.useName.trim();
|
||||
if (name) {
|
||||
Persist.saveBuild(shipId, name, build.code);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($scope.comparisons) {
|
||||
var comparisons = $scope.comparisons;
|
||||
for (var comp in comparisons) {
|
||||
var comparison = comparisons[comp];
|
||||
var useName = comparison.useName.trim();
|
||||
if (useName) {
|
||||
Persist.saveComparison(useName, comparison.builds, comparison.facets);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($scope.discounts) {
|
||||
$rootScope.discounts.ship = $scope.discounts[0];
|
||||
$rootScope.discounts.components = $scope.discounts[1];
|
||||
$rootScope.$broadcast('discountChange');
|
||||
Persist.setDiscount($scope.discounts);
|
||||
}
|
||||
|
||||
if ($scope.insurance) {
|
||||
$rootScope.insurance.current = $scope.insurance;
|
||||
Persist.setInsurance($scope.insurance);
|
||||
}
|
||||
|
||||
$scope.$parent.dismiss();
|
||||
};
|
||||
|
||||
|
||||
@@ -219,6 +219,8 @@ angular.module('app').controller('OutfitController', ['$window', '$rootScope', '
|
||||
|
||||
if ($scope.buildName) {
|
||||
$state.go('modal.export', {
|
||||
title: $scope.buildName + ' Export',
|
||||
description: 'A detailed JSON export of your build for use in other sites and tools',
|
||||
data: Serializer.toDetailedBuild($scope.buildName, ship, $scope.code || Serializer.fromShip(ship))
|
||||
});
|
||||
}
|
||||
|
||||
@@ -33,11 +33,26 @@ angular.module('app').directive('shipyardHeader', ['lodash', '$rootScope', '$sta
|
||||
$rootScope.$broadcast('discountChange');
|
||||
};
|
||||
|
||||
scope.backup = function(e) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
scope.openedMenu = null;
|
||||
$state.go('modal.export', {
|
||||
title: 'Backup',
|
||||
data: Persist.getAll(),
|
||||
description: 'Backup of all Coriolis data to save or transfer to another browser/device'
|
||||
});
|
||||
};
|
||||
|
||||
scope.detailedExport = function(e) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
scope.openedMenu = null;
|
||||
$state.go('modal.export', { data: Serializer.toDetailedExport(scope.allBuilds) });
|
||||
$state.go('modal.export', {
|
||||
title: 'Detailed Export',
|
||||
data: Serializer.toDetailedExport(scope.allBuilds),
|
||||
description: 'Detailed export of all builds for use with other tools and sites'
|
||||
});
|
||||
};
|
||||
|
||||
scope.openMenu = function(e, menu) {
|
||||
|
||||
@@ -120,7 +120,7 @@ angular.module('app').service('Persist', ['$window', 'lodash', function($window,
|
||||
}
|
||||
this.comparisons[name] = {
|
||||
facets: facets,
|
||||
builds: _.map(builds, function(b) { return { shipId: b.id, buildName: b.buildName }; })
|
||||
builds: _.map(builds, function(b) { return { shipId: b.id || b.shipId, buildName: b.buildName }; })
|
||||
};
|
||||
localStorage.setItem(LS_KEY_COMPARISONS, angular.toJson(this.comparisons));
|
||||
this.state.hasComparisons = true;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* Service managing seralization and deserialization of models for use in URLs and persistene.
|
||||
*/
|
||||
angular.module('app').service('Serializer', ['lodash', 'GroupMap', 'MountMap', 'ShipsDB', 'Ship', '$state', function(_, GroupMap, MountMap, ShipsDB, Ship, $state) {
|
||||
angular.module('app').service('Serializer', ['lodash', 'GroupMap', 'MountMap', 'ShipsDB', 'Ship', 'Components', '$state', function(_, GroupMap, MountMap, ShipsDB, Ship, Components, $state) {
|
||||
|
||||
/**
|
||||
* Serializes the ships selected components for all slots to a URL friendly string.
|
||||
@@ -108,6 +108,46 @@ angular.module('app').service('Serializer', ['lodash', 'GroupMap', 'MountMap', '
|
||||
return data;
|
||||
};
|
||||
|
||||
this.fromDetailedBuild = function(detailedBuild) {
|
||||
var shipId = _.findKey(ShipsDB, { properties: { name: detailedBuild.ship } });
|
||||
|
||||
if (!shipId) {
|
||||
throw 'No such ship: ' + detailedBuild.ship;
|
||||
}
|
||||
|
||||
var comps = detailedBuild.components;
|
||||
var shipData = ShipsDB[shipId];
|
||||
var ship = new Ship(shipId, shipData.properties, shipData.slots);
|
||||
var bulkheads = Components.bulkheadIndex(comps.standard.bulkheads);
|
||||
|
||||
if (bulkheads < 0) {
|
||||
throw 'Invalid bulkheads: ' + comps.standard.bulkheads;
|
||||
}
|
||||
|
||||
var common = _.map(
|
||||
['powerPlant', 'thrusters', 'frameShiftDrive', 'lifeSupport', 'powerDistributor', 'sensors', 'fuelTank'],
|
||||
function(c) {
|
||||
if (!comps.standard[c].class || !comps.standard[c].rating) {
|
||||
throw 'Invalid value for ' + c;
|
||||
}
|
||||
return comps.standard[c].class + comps.standard[c].rating;
|
||||
}
|
||||
);
|
||||
|
||||
var internal = _.map(comps.internal, function(c) { return c ? Components.findInternalId(c.group, c.class, c.rating, c.name) : 0; });
|
||||
|
||||
var hardpoints = _.map(comps.hardpoints, function(c) {
|
||||
return c ? Components.findHardpointId(c.group, c.class, c.rating, c.name, MountMap[c.mount], c.missile) : 0;
|
||||
});
|
||||
hardpoints = hardpoints.concat(_.map(comps.utility, function(c) {
|
||||
return c ? Components.findHardpointId(c.group, c.class, c.rating, c.name, MountMap[c.mount]) : 0;
|
||||
}));
|
||||
|
||||
ship.buildWith({ bulkheads: bulkheads, common: common, hardpoints: hardpoints, internal: internal });
|
||||
|
||||
return ship;
|
||||
};
|
||||
|
||||
this.toDetailedExport = function(builds) {
|
||||
var data = [];
|
||||
|
||||
@@ -180,6 +220,9 @@ angular.module('app').service('Serializer', ['lodash', 'GroupMap', 'MountMap', '
|
||||
if (slot.c.mode) {
|
||||
o.mount = MountMap[slot.c.mode];
|
||||
}
|
||||
if (slot.c.missile) {
|
||||
o.missile = slot.c.missile;
|
||||
}
|
||||
return o;
|
||||
}
|
||||
return null;
|
||||
|
||||
@@ -32,7 +32,7 @@ angular.module('shipyard', ['ngLodash'])
|
||||
|
||||
// Internal
|
||||
fs: 'Fuel Scoop',
|
||||
sc: 'Scanners',
|
||||
sc: 'Scanner',
|
||||
am: 'Auto Field-Maintenance Unit',
|
||||
cr: 'Cargo Rack',
|
||||
fi: 'FSD Interdictor',
|
||||
@@ -69,7 +69,10 @@ angular.module('shipyard', ['ngLodash'])
|
||||
.value('MountMap', {
|
||||
'F': 'Fixed',
|
||||
'G': 'Gimballed',
|
||||
'T': 'Turret'
|
||||
'T': 'Turret',
|
||||
'Fixed': 'F',
|
||||
'Gimballed': 'G',
|
||||
'Turret': 'T'
|
||||
})
|
||||
.value('shipSize', [
|
||||
'N/A',
|
||||
|
||||
@@ -32,6 +32,41 @@ angular.module('shipyard').service('Components', ['lodash', 'ComponentsDB', 'Shi
|
||||
return null;
|
||||
};
|
||||
|
||||
this.findInternalId = function(groupName, clss, rating, name) {
|
||||
var group = C.internal[groupName];
|
||||
|
||||
if (!group) {
|
||||
throw 'Invalid internal group: ' + groupName;
|
||||
}
|
||||
|
||||
for (var i = 0, l = group.length; i < l; i++) {
|
||||
if (group[i].class == clss && group[i].rating == rating && ((!name && !group[i].name) || group[i].name == name)) {
|
||||
return group[i].id;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
};
|
||||
|
||||
this.findHardpointId = function(groupName, clss, rating, name, mode, missile) {
|
||||
var group = C.hardpoints[groupName];
|
||||
|
||||
if (!group) {
|
||||
throw 'Invalid hardpoint group: ' + groupName;
|
||||
}
|
||||
|
||||
for (var i = 0, l = group.length; i < l; i++) {
|
||||
if (group[i].class == clss && group[i].rating == rating && group[i].mode == mode
|
||||
&& ((!name && !group[i].name) || group[i].name == name)
|
||||
&& ((!missile && !group[i].missile) || group[i].missile == missile)
|
||||
) {
|
||||
return group[i].id;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* Looks up the bulkhead component for a specific ship and bulkhead
|
||||
* @param {string} shipId Unique ship Id/Key
|
||||
@@ -42,6 +77,10 @@ angular.module('shipyard').service('Components', ['lodash', 'ComponentsDB', 'Shi
|
||||
return C.bulkheads[shipId][bulkheadsId];
|
||||
};
|
||||
|
||||
this.bulkheadIndex = function(bulkheadName) {
|
||||
return ['Lightweight Alloy', 'Reinforced Alloy', 'Military Grade Composite', 'Mirrored Surface Composite', 'Reactive Surface Composite'].indexOf(bulkheadName);
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a new ComponentSet that contains all available components
|
||||
* that the specified ship is eligible to use.
|
||||
|
||||
Reference in New Issue
Block a user