From a34e4fbed4b6c2eb2440361178bc6cf213e6657c Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Sun, 31 Mar 2024 09:44:49 +0200 Subject: [node] implemented basic procurement trading loop --- nodejs/lib/ships.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'nodejs/lib/ships.ts') 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 { + 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 { ship = await getShip(ship); if (ship.nav.status === 'DOCKED') return; -- cgit v1.2.3