1
0
Fork 0

[node] multiple contracting fixes and some more refactoring

This commit is contained in:
Julien Dessaux 2024-04-06 21:36:42 +02:00
parent eeaa64b5ed
commit 3e80bc8a4d
Signed by: adyxax
GPG key ID: F92E51B86E07177E
6 changed files with 65 additions and 46 deletions

View file

@ -61,6 +61,10 @@ export async function deliver(contract: Contract, ship: Ship): Promise<Contract>
}});
if (response.error) {
switch(response.error.code) {
case 4503: // contract has expired
// TODO sell cargo? the next trading loop should take care of it by itself
contract.fulfilled = true;
return contract;
case 4509: // contract delivery terms have been met
return await fulfill(contract);
default: // yet unhandled error

View file

@ -1,5 +1,10 @@
import { Cooldown } from './types.ts';
export type ContractDeadlineExpired = {
contractId: string;
deadline: Date;
};
export type MarketTradeVolumeError = {
waypointSymbol: string;
tradeSymbol: string;

View file

@ -130,6 +130,7 @@ export class Ship {
this.nav.status = 'IN_ORBIT'; // we arrive in orbit
}
async negotiate(): Promise<Contract> {
await this.dock();
const response = await send<{contract: Contract}>({endpoint: `/my/ships/${this.symbol}/negotiate/contract`, method: 'POST'});
if (response.error) {
switch(response.error.code) {
@ -142,6 +143,7 @@ export class Ship {
throw response;
}
}
dbContracts.setContract(response.data.contract);
return response.data.contract;
}
async orbit(): Promise<void> {
@ -180,6 +182,7 @@ export class Ship {
dbAgents.setAgent(response.data.agent);
}
async refuel(): Promise<void> {
if (this.fuel.current === this.fuel.capacity) return;
// TODO check if our current waypoint has a marketplace (and sells fuel)?
await this.dock();
const response = await send<{agent: Agent, fuel: Fuel}>({endpoint: `/my/ships/${this.symbol}/refuel`, method: 'POST'}); // TODO transaction field

View file

@ -5,6 +5,11 @@ export type CategorizedCargo = {
goods: CargoManifest;
};
type Point = {
x: number;
y: number;
};
// cargo is a ship.cargo object, want is an optional symbol
export function categorizeCargo(cargo: Cargo, want?: string): CategorizedCargo {
const wanted = cargo.inventory.filter(i => i.symbol === want || i.symbol === 'ANTIMATTER');
@ -20,6 +25,26 @@ export function categorizeCargo(cargo: Cargo, want?: string): CategorizedCargo {
return {wanted: wobj, goods: gobj};
}
export function distance(a: Point, b: Point) {
return Math.sqrt((a.x-b.x)**2 + (a.y-b.y)**2);
}
export function sortByDistanceFrom<T extends Point>(a: Point, points: Array<T>): Array<{data: T, distance: number}>{
let result = points.map(function (m) { return {
data: m,
distance: distance(a, m),
}});
result.sort(function(a, b) {
if (a.distance < b.distance) {
return -1;
} else if (a.distance > b.distance) {
return 1;
}
return 0;
});
return result;
}
export function systemFromWaypoint(waypoint: string): string {
return waypoint.split('-').slice(0,2).join('-');
}