Moved the nodejs agent to its own subfolder to make room for my haskell agent
This commit is contained in:
parent
0bc0df0891
commit
36cc33f9e9
22 changed files with 0 additions and 0 deletions
26
nodejs/automation/automation.js
Normal file
26
nodejs/automation/automation.js
Normal file
|
@ -0,0 +1,26 @@
|
|||
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
|
||||
}
|
55
nodejs/automation/contracting.js
Normal file
55
nodejs/automation/contracting.js
Normal file
|
@ -0,0 +1,55 @@
|
|||
import * as mining from './mining.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 systems from '../lib/systems.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});
|
||||
}
|
||||
}
|
||||
}
|
21
nodejs/automation/exploration.js
Normal file
21
nodejs/automation/exploration.js
Normal file
|
@ -0,0 +1,21 @@
|
|||
import * as db from '../database/systems.js';
|
||||
import * as api from '../lib/api.js';
|
||||
|
||||
// Retrieves all systems information, should be called only once after registering
|
||||
export async function init() {
|
||||
if (db.isInit()) {
|
||||
return;
|
||||
}
|
||||
for (let page=1; true; ++page) {
|
||||
const response = await api.send({endpoint: `/systems?limit=20&page=${page}`, priority:100});
|
||||
if (response.error !== undefined) {
|
||||
throw response;
|
||||
}
|
||||
response.data.forEach(system => db.setSystem(system));
|
||||
if (response.meta.total <= response.meta.limit * page) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
console.log('Finished retrieving all systems information');
|
||||
db.init();
|
||||
}
|
34
nodejs/automation/mining.js
Normal file
34
nodejs/automation/mining.js
Normal file
|
@ -0,0 +1,34 @@
|
|||
import * as dbShips from '../database/ships.js';
|
||||
import * as api from '../lib/api.js';
|
||||
import * as ships from '../lib/ships.js';
|
||||
|
||||
// example ctx { good: 'SILVER_ORE', symbol: 'ADYXAX-2' }
|
||||
// returns the number of units of the good the ship holds
|
||||
export async function mineUntilFullOf(ctx) {
|
||||
while(true) {
|
||||
let cargo = await mineUntilFull({symbol: ctx.symbol});
|
||||
let good = cargo.inventory.filter(i => i.symbol === ctx.good)[0];
|
||||
const antimatter = cargo.inventory.filter(i => i.symbol === 'ANTIMATTER')[0];
|
||||
const junk = cargo.inventory.filter(i => i.symbol !== ctx.good && i.symbol !== 'ANTIMATTER');
|
||||
if ((good?.units ?? 0) + (antimatter?.units ?? 0) >= cargo.capacity * 0.9) { // > 90% full of the valuable goods
|
||||
return good.units;
|
||||
} else { // we are full but need to sell junk
|
||||
for (let i=0; i<junk.length; ++i) {
|
||||
await ships.sell({symbol: ctx.symbol, good: junk[i].symbol, units: junk[i].units});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// example ctx { symbol: 'ADYXAX-2' }
|
||||
// extract the ship's cargo contents when more than 80% full then returns the ships cargo object
|
||||
async function mineUntilFull(ctx) {
|
||||
while(true) {
|
||||
const ship = dbShips.getShip(ctx.symbol);
|
||||
if (ship.cargo.units >= ship.cargo.capacity * 0.9) return ship.cargo;
|
||||
if (await ships.extract({symbol: ctx.symbol}) === null)
|
||||
ship = await ship(ctx); // refresh the ships status from the server just in case
|
||||
}
|
||||
}
|
||||
|
||||
// TODO surveying the asteroid field
|
Loading…
Add table
Add a link
Reference in a new issue