From 0377c99a549bed141d1371577a8d0b10dd09f40d Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Sun, 17 Mar 2024 01:23:40 +0100 Subject: [javascript] refactoring --- nodejs/automation/contracting.js | 4 ++-- nodejs/automation/utils.js | 3 --- nodejs/lib/systems.js | 4 +++- nodejs/lib/utils.js | 3 +++ 4 files changed, 8 insertions(+), 6 deletions(-) delete mode 100644 nodejs/automation/utils.js create mode 100644 nodejs/lib/utils.js diff --git a/nodejs/automation/contracting.js b/nodejs/automation/contracting.js index 220b21b..75bc96c 100644 --- a/nodejs/automation/contracting.js +++ b/nodejs/automation/contracting.js @@ -5,7 +5,7 @@ import * as api from '../lib/api.js'; import * as contracts from '../lib/contracts.js'; import * as libShips from '../lib/ships.js'; import * as systems from '../lib/systems.js'; -import * as utils from './utils.js'; +import * as utils from '../lib/utils.js'; export async function init() { const cs = dbContracts.getContracts(); @@ -14,7 +14,7 @@ export async function init() { async function run(contract) { await contracts.accept({id: contract.id}); - const contractSystem = utils.waypointToSystem(contract.terms.deliver[0].destinationSymbol); + const contractSystem = utils.systemFromWaypoint(contract.terms.deliver[0].destinationSymbol); let ships = dbShips.getShipsAt(contractSystem); ships = ships.filter(ship => ship.registration.role !== 'SATELLITE'); // filter out probes diff --git a/nodejs/automation/utils.js b/nodejs/automation/utils.js deleted file mode 100644 index 01ce1ad..0000000 --- a/nodejs/automation/utils.js +++ /dev/null @@ -1,3 +0,0 @@ -export function waypointToSystem(waypoint) { - return waypoint.split('-').slice(0,2).join('-'); -} diff --git a/nodejs/lib/systems.js b/nodejs/lib/systems.js index e03da6c..fefa5cd 100644 --- a/nodejs/lib/systems.js +++ b/nodejs/lib/systems.js @@ -1,9 +1,11 @@ import * as api from './api.js'; import * as db from '../database/systems.js'; +import * as utils from './utils.js'; + // Retrieves a shipyard's information for ctx.symbol export async function shipyard(ctx) { - const systemSymbol = ctx.symbol.match(/([^-]+-[^-]+)/)[1]; // TODO generalise this extraction + const systemSymbol = utils.systemFromWaypoint(ctx.symbol); console.log(systemSymbol); return await api.send({endpoint: `/systems/${systemSymbol}/waypoints/${ctx.symbol}/shipyard`}); } diff --git a/nodejs/lib/utils.js b/nodejs/lib/utils.js new file mode 100644 index 0000000..1d2e451 --- /dev/null +++ b/nodejs/lib/utils.js @@ -0,0 +1,3 @@ +export function systemFromWaypoint(waypoint) { + return waypoint.split('-').slice(0,2).join('-'); +} -- cgit v1.2.3