[javascript] Completely reworked contracting
This commit is contained in:
parent
3be4f0e9b4
commit
1b9a55432a
6 changed files with 112 additions and 176 deletions
|
@ -1,26 +0,0 @@
|
|||
import * as dbConfig from '../database/config.js';
|
||||
import * as dbShips from '../database/ships.js';
|
||||
import * as exploration from './exploration.js';
|
||||
|
||||
// This function registers then inits the database
|
||||
export async function register(symbol, faction) {
|
||||
const response = await fetch('https://api.spacetraders.io/v2/register', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
symbol: symbol,
|
||||
faction: faction,
|
||||
}),
|
||||
});
|
||||
const json = await response.json();
|
||||
console.log(JSON.stringify(response, null, 2));
|
||||
if (response.error !== undefined) {
|
||||
throw response;
|
||||
}
|
||||
dbConfig.registerAgent(json.data);
|
||||
exploration.init();
|
||||
dbShips.setShip(json.data.ship);
|
||||
// TODO contract
|
||||
}
|
|
@ -1,55 +1,62 @@
|
|||
import * as mining from './mining.js';
|
||||
import * as dbContracts from '../database/contracts.js';
|
||||
import * as dbShips from '../database/ships.js';
|
||||
import * as api from '../lib/api.js';
|
||||
import * as contracts from '../lib/contracts.js';
|
||||
import * as ships from '../lib/ships.js';
|
||||
import * as libShips from '../lib/ships.js';
|
||||
import * as systems from '../lib/systems.js';
|
||||
import * as utils from './utils.js';
|
||||
|
||||
export async function auto(ctx) {
|
||||
let ship = dbShips.getShip(ctx.symbol);
|
||||
// Fetch our contracts in the system the ship currently is in
|
||||
let cs = await contracts.contracts();
|
||||
cs = cs.data.filter(c => c.terms.deliver[0].destinationSymbol.startsWith(ship.nav.systemSymbol));
|
||||
if (cs === []) throw `No contract at ${ctx.symbol}'s location`;
|
||||
let contract = cs[0];
|
||||
if (!contract.accepted) {
|
||||
console.log(new Date(), `accepting contract ${contract.id}`);
|
||||
await contracts.accept({contract: contract.id});
|
||||
}
|
||||
const good = contract.terms.deliver[0].tradeSymbol;
|
||||
const deliveryPoint = contract.terms.deliver[0].destinationSymbol;
|
||||
const asteroidFields = await systems.type({symbol: ship.nav.systemSymbol, type: 'ASTEROID_FIELD'});
|
||||
const asteroidField = asteroidFields[0].symbol;
|
||||
while (true) {
|
||||
ship = dbShips.getShip(ctx.symbol);
|
||||
// If we are in transit, we wait until we arrive
|
||||
const delay = new Date(ship.nav.route.arrival) - new Date();
|
||||
if (delay > 0) await api.sleep(delay);
|
||||
// Then it depends on where we are
|
||||
let goodCargo = ship.cargo.inventory.filter(i => i.symbol === good)[0];
|
||||
// the switch makes this 'resumable'
|
||||
switch (ship.nav.waypointSymbol) {
|
||||
case asteroidField:
|
||||
let response = await mining.mineUntilFullOf({good: good, symbol: ctx.symbol});
|
||||
await ships.navigate({symbol: ctx.symbol, waypoint: deliveryPoint});
|
||||
break;
|
||||
case deliveryPoint:
|
||||
await ships.dock({symbol: ctx.symbol});
|
||||
await ships.refuel({symbol: ctx.symbol});
|
||||
if (goodCargo !== undefined) {
|
||||
console.log(`delivering ${goodCargo.units} of ${good}`);
|
||||
await contracts.deliver({contract: contract.id, symbol: ctx.symbol, good: good, units: goodCargo.units });
|
||||
}
|
||||
await ships.navigate({symbol: ctx.symbol, waypoint: asteroidField});
|
||||
await ships.dock({symbol: ctx.symbol});
|
||||
await ships.refuel({symbol: ctx.symbol});
|
||||
await ships.orbit({symbol: ctx.symbol});
|
||||
break;
|
||||
default:
|
||||
await ships.navigate({symbol: ctx.symbol, waypoint: asteroidField});
|
||||
await ships.dock({symbol: ctx.symbol});
|
||||
await ships.refuel({symbol: ctx.symbol});
|
||||
await ships.orbit({symbol: ctx.symbol});
|
||||
}
|
||||
export async function init() {
|
||||
const cs = dbContracts.getContracts();
|
||||
cs.forEach(contract => run(contract));
|
||||
}
|
||||
|
||||
async function run(contract) {
|
||||
await contracts.accept({id: contract.id});
|
||||
const contractSystem = utils.waypointToSystem(contract.terms.deliver[0].destinationSymbol);
|
||||
let ships = dbShips.getShipsAt(contractSystem);
|
||||
ships = ships.filter(ship => ship.registration.role !== 'SATELLITE'); // filter out probes
|
||||
|
||||
switch(contract.type) {
|
||||
case 'PROCUREMENT':
|
||||
await runProcurement(contract, ships);
|
||||
break;
|
||||
default:
|
||||
throw `Handling of contract type ${contract.type} is not implemented yet`;
|
||||
}
|
||||
}
|
||||
|
||||
async function runProcurement(contract, ships) {
|
||||
// TODO check if contract is fulfilled!
|
||||
const wantedCargo = contract.terms.deliver[0].tradeSymbol;
|
||||
const deliveryPoint = contract.terms.deliver[0].destinationSymbol;
|
||||
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) {
|
||||
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
|
||||
const delay = new Date(ship.nav.route.arrival) - new Date();
|
||||
if (delay > 0) await api.sleep(delay);
|
||||
// Then it depends on where we are
|
||||
switch (ship.nav.waypointSymbol) {
|
||||
case asteroidField:
|
||||
let response = await mining.mineUntilFullOf({good: wantedCargo, symbol: ship.symbol});
|
||||
await libShips.navigate({symbol: ship.symbol, waypoint: deliveryPoint});
|
||||
break;
|
||||
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!
|
||||
}
|
||||
await libShips.navigate({symbol: ship.symbol, waypoint: asteroidField});
|
||||
break;
|
||||
default:
|
||||
await libShips.navigate({symbol: ship.symbol, waypoint: asteroidField});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
37
nodejs/automation/init.js
Normal file
37
nodejs/automation/init.js
Normal file
|
@ -0,0 +1,37 @@
|
|||
import * as dbAgents from '../database/agents.js';
|
||||
import * as db from '../database/db.js';
|
||||
import * as dbContracts from '../database/contracts.js';
|
||||
import * as dbShips from '../database/ships.js';
|
||||
import * as dbTokens from '../database/tokens.js';
|
||||
import * as api from '../lib/api.js';
|
||||
import * as ships from '../lib/ships.js';
|
||||
|
||||
// This function registers then inits the database
|
||||
export async function init() {
|
||||
const response = await fetch('https://api.spacetraders.io/v2/register', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
symbol: "ADYXAX",
|
||||
faction: "COSMIC",
|
||||
}),
|
||||
});
|
||||
const json = await response.json();
|
||||
if (json.error !== undefined) {
|
||||
switch(json.error?.code) {
|
||||
case 4111: // 4111 means the agent symbol has already been claimed so no server reset happened
|
||||
return;
|
||||
default:
|
||||
throw json;
|
||||
}
|
||||
}
|
||||
db.reset();
|
||||
dbAgents.addAgent(json.data.agent);
|
||||
dbContracts.setContract(json.data.contract);
|
||||
dbShips.setShip(json.data.ship);
|
||||
dbTokens.addToken(json.data.token);
|
||||
// Temporary fix to fetch the data on the startup probe
|
||||
ships.ships();
|
||||
}
|
3
nodejs/automation/utils.js
Normal file
3
nodejs/automation/utils.js
Normal file
|
@ -0,0 +1,3 @@
|
|||
export function waypointToSystem(waypoint) {
|
||||
return waypoint.split('-').slice(0,2).join('-');
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue