1
0
Fork 0

[node] finished the great typescript rewrite

This commit is contained in:
Julien Dessaux 2024-03-27 15:20:14 +01:00
parent 8107afbd90
commit a1d6b03ec9
Signed by: adyxax
GPG key ID: F92E51B86E07177E
30 changed files with 1019 additions and 359 deletions

View file

@ -34,7 +34,7 @@ async function runProcurement(contract: Contract, ships: Array<Ship>) {
// TODO check if contract is fulfilled!
const wantedCargo = contract.terms.deliver[0].tradeSymbol;
const deliveryPoint = contract.terms.deliver[0].destinationSymbol;
const asteroids = await systems.type({symbol: ships[0].nav.systemSymbol, type: 'ENGINEERED_ASTEROID'});
const asteroids = await systems.type(ships[0].nav.systemSymbol, 'ENGINEERED_ASTEROID');
const asteroidSymbol = asteroids[0].symbol;
ships.forEach(async function(ship) {
while (!contract.fulfilled) {
@ -46,12 +46,8 @@ async function runProcurement(contract: Contract, ships: Array<Ship>) {
// Then it depends on where we are
switch (ship.nav.waypointSymbol) {
case asteroidSymbol:
await mining.mineUntilFullOf({
asteroidSymbol: asteroidSymbol,
good: wantedCargo,
symbol: ship.symbol
});
await libShips.navigate(ship, deliveryPoint);
ship = await mining.mineUntilFullOf(wantedCargo, ship, asteroidSymbol);
ship = await libShips.navigate(ship, deliveryPoint);
break;
case deliveryPoint:
if (goodCargo !== undefined) { // we could be here if a client restart happens right after selling before we navigate away
@ -59,12 +55,12 @@ async function runProcurement(contract: Contract, ships: Array<Ship>) {
contract = await contracts.deliver(contract, ship);
if (contract.fulfilled) break;
}
await libShips.navigate(ship, asteroidSymbol);
ship = await libShips.navigate(ship, asteroidSymbol);
break;
default:
// we were either selling or started contracting
await selling.sell(ship, wantedCargo);
await libShips.navigate(ship, asteroidSymbol);
ship = await selling.sell(ship, wantedCargo);
ship = await libShips.navigate(ship, asteroidSymbol);
}
}
// TODO repurpose the ship

View file

@ -1,11 +0,0 @@
import db from '../database/db.js';
import * as dbSystems from '../database/systems.js';
import * as api from '../lib/api.js';
export async function init() {
const response = await api.send({endpoint: `/systems`, page: Math.max(1, Math.floor(dbSystems.getSystemsCount()/20)), priority: 100});
if (response.error !== undefined) {
throw response;
}
db.transaction(() => response.forEach(function(system) { try { dbSystems.addSystem(system); } catch {} }))();
}

View file

@ -0,0 +1,9 @@
import db from '../database/db.ts';
import * as dbSystems from '../database/systems.ts';
import { System } from '../model/system.ts';
import * as api from '../lib/api.ts';
export async function init(): Promise<void> {
const response = await api.sendPaginated<System>({endpoint: `/systems`, page: Math.max(1, Math.floor(dbSystems.getSystemsCount()/20)), priority: 100});
db.transaction(() => response.forEach(function(system) { try { dbSystems.addSystem(system); } catch {} }))();
}

View file

@ -1,15 +1,19 @@
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';
import * as dbAgents from '../database/agents.ts';
import * as db from '../database/db.ts';
import * as dbContracts from '../database/contracts.ts';
import * as dbShips from '../database/ships.ts';
import * as dbTokens from '../database/tokens.ts';
import { Agent } from '../model/agent.ts';
import { Response } from '../model/api.ts';
import { Contract } from '../model/contract.ts';
import { Ship } from '../model/ship.ts';
import * as api from '../lib/api.ts';
import * as ships from '../lib/ships.ts';
const symbol = process.env.NODE_ENV === 'test' ? 'ADYXAX-0' : 'ADYXAX-TS';
const symbol = process.env.NODE_ENV === 'test' ? 'ADYXAX-0' : 'ADYXAX-JS';
// This function registers then inits the database
export async function init() {
export async function init(): Promise<void> {
const response = await fetch('https://api.spacetraders.io/v2/register', {
method: 'POST',
headers: {
@ -20,7 +24,7 @@ export async function init() {
faction: "COSMIC",
}),
});
const json = await response.json();
const json = await response.json() as Response<{agent: Agent, contract: Contract, ship: Ship, token: string}>;
if (json.error !== undefined) {
switch(json.error?.code) {
case 4111: // 4111 means the agent symbol has already been claimed so no server reset happened

View file

@ -1,33 +0,0 @@
import * as selling from './selling.js';
import * as dbShips from '../database/ships.js';
import * as api from '../lib/api.js';
import * as libShips from '../lib/ships.js';
import * as utils from '../lib/utils.js';
// example ctx { asteroidSymbol: XXXXX, good: 'SILVER_ORE', symbol: 'ADYXAX-2' }
// returns the number of units of the good the ship holds
export async function mineUntilFullOf(ctx) {
while(true) {
const ship = dbShips.getShip(ctx.symbol);
const cargo = utils.categorizeCargo(await mineUntilFull({symbol: ctx.symbol}), ctx.good);
const wantedUnits = Object.values(cargo.wanted).reduce((acc, e) => acc += e, 0);
// > 90% full of the valuable goods ?
if (wantedUnits >= ship.cargo.capacity * 0.9) return;
// we are full but need to sell junk
await selling.sell(ship, ctx.good);
await libShips.navigate({symbol: ship.symbol, waypoint: ctx.asteroidSymbol});
}
}
// 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 libShips.extract({symbol: ctx.symbol}) === null)
await ship(ctx); // refresh the ships status from the server just in case
}
}
// TODO surveying the asteroid field

View file

@ -0,0 +1,29 @@
import * as selling from './selling.js';
import * as dbShips from '../database/ships.js';
import * as api from '../lib/api.js';
import * as libShips from '../lib/ships.js';
import * as utils from '../lib/utils.js';
import { Ship } from '../model/ship.ts';
export async function mineUntilFullOf(good: string, ship: Ship, asteroidSymbol: string): Promise<Ship> {
// TODO find a good asteroid
while(true) {
ship = await mineUntilFull(ship);
const cargo = utils.categorizeCargo(ship.cargo, good);
const wantedUnits = Object.values(cargo.wanted).reduce((acc, e) => acc += e, 0);
// > 90% full of the valuable goods ?
if (wantedUnits >= ship.cargo.capacity * 0.9) return ship;
// we are full but need to sell junk
await selling.sell(ship, good);
await libShips.navigate(ship, asteroidSymbol);
}
}
// 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(ship: Ship): Promise<Ship> {
for (;ship.cargo.units <= ship.cargo.capacity * 0.9; ship = await libShips.extract(ship)) {}
return ship;
}
// TODO surveying the asteroid field

View file

@ -1,37 +1,34 @@
import * as dbMarkets from '../database/markets.js';
import * as dbShips from '../database/ships.js';
import * as api from '../lib/api.js';
import * as libShips from '../lib/ships.js';
import * as libSystems from '../lib/systems.js';
import * as utils from '../lib/utils.js';
import * as dbMarkets from '../database/markets.ts';
import * as dbShips from '../database/ships.ts';
import * as api from '../lib/api.ts';
import * as libShips from '../lib/ships.ts';
import * as libSystems from '../lib/systems.ts';
import * as utils from '../lib/utils.ts';
import { CargoManifest } from '../model/cargo.ts';
import { CommonThing } from '../model/common.ts';
import { Ship } from '../model/ship.ts';
// example ctx { ship: {XXX}, keep: 'SILVER_ORE' }
export async function sell(ship, keep) {
export async function sell(ship: Ship, good: string): Promise<Ship> {
outer: while(true) {
// first lets see what we want to sell
let cargo = utils.categorizeCargo(ship.cargo, keep);
let cargo = utils.categorizeCargo(ship.cargo, good);
// get the marketdata from our location
const market = await libSystems.market(ship.nav.waypointSymbol);
// can we sell anything here?
const goods = whatCanBeTradedAt(cargo.goods, market.imports.concat(market.exchange));
for (let i = 0; i < goods.length; i++) {
const symbol = goods[i].symbol;
await libShips.sell({
good: symbol,
symbol: ship.symbol,
units: cargo.goods[symbol],
});
delete cargo.goods[symbol];
ship = await libShips.sell(ship, good);
};
// are we done selling everything we can?
ship = dbShips.getShip(ship.symbol);
cargo = utils.categorizeCargo(ship.cargo, keep);
cargo = utils.categorizeCargo(ship.cargo, good);
if (Object.keys(cargo.goods).length === 0) {
return;
return ship;
}
// we need to move somewhere else to sell our remaining goods
// first we look into markets in our system
const rawMarkets = await libSystems.trait({symbol: ship.nav.systemSymbol, trait: 'MARKETPLACE'});
const rawMarkets = await libSystems.trait(ship.nav.systemSymbol, 'MARKETPLACE');
// sorted by distance from where we are
const markets = rawMarkets.map(function (m) { return {
data: m,
@ -52,7 +49,7 @@ export async function sell(ship, keep) {
// if we have no data on the market we need to go there and see
// and if we have data and can sell there we need to go too
if (market === null || whatCanBeTradedAt(cargo.goods, market.imports).length > 0) {
await libShips.navigate({symbol: ship.symbol, waypoint: waypointSymbol});
ship = await libShips.navigate(ship, waypointSymbol);
continue outer;
}
}
@ -62,7 +59,7 @@ export async function sell(ship, keep) {
const market = await libSystems.market(waypointSymbol);
// if we can sell there we need to go
if (whatCanBeTradedAt(cargo.goods, market.exchange).length > 0) {
await libShips.navigate({symbol: ship.symbol, waypoint: waypointSymbol});
ship = await libShips.navigate(ship, waypointSymbol);
continue outer;
}
}
@ -70,7 +67,6 @@ export async function sell(ship, keep) {
}
}
function whatCanBeTradedAt(cargo, goods) {
if (goods === undefined) return [];
function whatCanBeTradedAt(cargo: CargoManifest, goods: Array<CommonThing>): Array<CommonThing> {
return goods.filter(g => cargo[g.symbol] !== undefined );
}