summaryrefslogtreecommitdiff
path: root/nodejs/lib
diff options
context:
space:
mode:
authorJulien Dessaux2024-04-06 21:36:42 +0200
committerJulien Dessaux2024-04-07 23:01:52 +0200
commit3e80bc8a4d3127d17dbc3f52301d33a79e53a980 (patch)
tree41caed2075ed39f8eec5f3465aded203ae377c58 /nodejs/lib
parent[node] waypoints usage refactoring (diff)
downloadspacetraders-3e80bc8a4d3127d17dbc3f52301d33a79e53a980.tar.gz
spacetraders-3e80bc8a4d3127d17dbc3f52301d33a79e53a980.tar.bz2
spacetraders-3e80bc8a4d3127d17dbc3f52301d33a79e53a980.zip
[node] multiple contracting fixes and some more refactoring
Diffstat (limited to 'nodejs/lib')
-rw-r--r--nodejs/lib/contracts.ts4
-rw-r--r--nodejs/lib/errors.ts5
-rw-r--r--nodejs/lib/ships.ts3
-rw-r--r--nodejs/lib/utils.ts25
4 files changed, 37 insertions, 0 deletions
diff --git a/nodejs/lib/contracts.ts b/nodejs/lib/contracts.ts
index 833d434..0582cc7 100644
--- a/nodejs/lib/contracts.ts
+++ b/nodejs/lib/contracts.ts
@@ -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
diff --git a/nodejs/lib/errors.ts b/nodejs/lib/errors.ts
index f9dca89..c560ee8 100644
--- a/nodejs/lib/errors.ts
+++ b/nodejs/lib/errors.ts
@@ -1,5 +1,10 @@
import { Cooldown } from './types.ts';
+export type ContractDeadlineExpired = {
+ contractId: string;
+ deadline: Date;
+};
+
export type MarketTradeVolumeError = {
waypointSymbol: string;
tradeSymbol: string;
diff --git a/nodejs/lib/ships.ts b/nodejs/lib/ships.ts
index 187a87b..67dca00 100644
--- a/nodejs/lib/ships.ts
+++ b/nodejs/lib/ships.ts
@@ -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
diff --git a/nodejs/lib/utils.ts b/nodejs/lib/utils.ts
index 39378e9..480e7a4 100644
--- a/nodejs/lib/utils.ts
+++ b/nodejs/lib/utils.ts
@@ -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('-');
}