summaryrefslogtreecommitdiff
path: root/nodejs/lib
diff options
context:
space:
mode:
authorJulien Dessaux2024-03-31 09:44:49 +0200
committerJulien Dessaux2024-03-31 09:44:49 +0200
commita34e4fbed4b6c2eb2440361178bc6cf213e6657c (patch)
tree258ee8b997bc22f75a13a1d3fac8b1c6e0bd82c9 /nodejs/lib
parent[node] fixed contracting and implemented renegotiation (diff)
downloadspacetraders-a34e4fbed4b6c2eb2440361178bc6cf213e6657c.tar.gz
spacetraders-a34e4fbed4b6c2eb2440361178bc6cf213e6657c.tar.bz2
spacetraders-a34e4fbed4b6c2eb2440361178bc6cf213e6657c.zip
[node] implemented basic procurement trading loop
Diffstat (limited to '')
-rw-r--r--nodejs/lib/ships.ts14
1 files changed, 14 insertions, 0 deletions
diff --git a/nodejs/lib/ships.ts b/nodejs/lib/ships.ts
index 8fd3a6f..83b2e86 100644
--- a/nodejs/lib/ships.ts
+++ b/nodejs/lib/ships.ts
@@ -9,6 +9,20 @@ import * as dbShips from '../database/ships.ts';
//import * as dbSurveys from '../database/surveys.ts';
import * as systems from '../lib/systems.ts';
+export async function buy(ship: Ship, tradeSymbol: string, units: number): Promise<void> {
+ if (units <= 0) return;
+ ship = await getShip(ship);
+ await dock(ship);
+ // TODO take into account the tradevolume, we might need to buy in multiple steps
+ const response = await api.send<{agent: Agent, cargo: Cargo}>({endpoint: `/my/ships/${ship.symbol}/purchase`, method: 'POST', payload: { symbol: tradeSymbol, units: units }}); // TODO transaction field
+ if (response.error) {
+ api.debugLog(response);
+ throw response;
+ }
+ dbShips.setShipCargo(ship.symbol, response.data.cargo);
+ dbAgents.setAgent(response.data.agent);
+}
+
export async function dock(ship: Ship): Promise<void> {
ship = await getShip(ship);
if (ship.nav.status === 'DOCKED') return;