summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--nodejs/automation/contracting.js8
-rw-r--r--nodejs/lib/contracts.js35
-rwxr-xr-xnodejs/main.js1
3 files changed, 36 insertions, 8 deletions
diff --git a/nodejs/automation/contracting.js b/nodejs/automation/contracting.js
index c051b03..a02c1c6 100644
--- a/nodejs/automation/contracting.js
+++ b/nodejs/automation/contracting.js
@@ -34,7 +34,7 @@ async function runProcurement(contract, ships) {
const asteroidFields = await systems.type({symbol: ships[0].nav.systemSymbol, type: 'ASTEROID_FIELD'});
const asteroidField = asteroidFields[0].symbol;
ships.forEach(async function(ship) {
- while (!contract.fulfilled) {
+ while (!dbContracts.getContract(contract.id).fulfilled) {
ship = dbShips.getShip(ship.symbol);
let goodCargo = ship.cargo.inventory.filter(i => i.symbol === wantedCargo)[0];
// If we are in transit, we wait until we arrive
@@ -49,8 +49,9 @@ async function runProcurement(contract, ships) {
case deliveryPoint:
if (goodCargo !== undefined) { // we could be here if a client restart happens right after selling before we navigate away
console.log(`delivering ${goodCargo.units} of ${wantedCargo}`);
- await contracts.deliver({contract: contract.id, symbol: ship.symbol, good: wantedCargo, units: goodCargo.units });
- // TODO check if contract is fulfilled!
+ if (await contracts.deliver({id: contract.id, symbol: ship.symbol, good: wantedCargo, units: goodCargo.units })) {
+ break;
+ }
}
await libShips.navigate({symbol: ship.symbol, waypoint: asteroidField});
break;
@@ -58,5 +59,6 @@ async function runProcurement(contract, ships) {
await libShips.navigate({symbol: ship.symbol, waypoint: asteroidField});
}
}
+ // TODO repurpose the ship
});
}
diff --git a/nodejs/lib/contracts.js b/nodejs/lib/contracts.js
index 5c2e970..a009889 100644
--- a/nodejs/lib/contracts.js
+++ b/nodejs/lib/contracts.js
@@ -19,21 +19,48 @@ export async function contracts() {
return contracts;
}
+// returns true if the contract has been fulfilled
export async function deliver(ctx) {
+ const contract = dbContracts.getContract(ctx.id);
+ if (contract.terms.deliver[0].unitsRequired === contract.terms.deliver[0].unitsFulfilled) {
+ await fulfill(ctx);
+ return true;
+ }
await libShips.dock(ctx);
- const response = await api.send({ endpoint: `/my/contracts/${ctx.contract}/deliver`, method: 'POST', payload: {
+ const response = await api.send({ endpoint: `/my/contracts/${ctx.id}/deliver`, method: 'POST', payload: {
shipSymbol: ctx.symbol,
tradeSymbol: ctx.good,
units: ctx.units,
}});
if (response.error !== undefined) {
- throw response;
+ switch(response.error.code) {
+ case 4509: // contract delivery terms have been met
+ await fulfill(ctx);
+ return true;
+ default: // yet unhandled error
+ api.debugLog(response);
+ throw response;
+ }
}
+ dbContracts.setContract(response.data.contract);
dbShips.setShipCargo(ctx.symbol, response.data.cargo);
- // TODO update contract delivered units
// TODO track credits
+ if(response.data.contract.terms.deliver[0].unitsRequired === response.data.contract.terms.deliver[0].unitsFulfilled) {
+ await fulfill(ctx);
+ return true;
+ }
+ return false;
}
export async function fulfill(ctx) {
- return await api.send({ endpoint: `/my/contracts/${ctx.contract}/fulfill`, method: 'POST'});
+ const contract = dbContracts.getContract(ctx.id);
+ if (contract.fulfilled) {
+ return;
+ }
+ const response = await api.send({ endpoint: `/my/contracts/${ctx.id}/fulfill`, method: 'POST'});
+ if (response.error !== undefined) {
+ api.debugLog(response);
+ throw response;
+ }
+ dbContracts.setContract(response.data.contract);
}
diff --git a/nodejs/main.js b/nodejs/main.js
index ef05bc7..a811b6a 100755
--- a/nodejs/main.js
+++ b/nodejs/main.js
@@ -3,7 +3,6 @@ import * as autoExploring from './automation/exploration.js';
import * as autoInit from './automation/init.js';
import * as api from './lib/api.js';
import * as contracts from './lib/contracts.js';
-import * as ships from './lib/ships.js';
await autoInit.init();
autoContracting.init();