From 063b2c9951d568c43d4a3b37b2d075d1dff8ff3c Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Sat, 4 May 2024 00:13:32 +0200 Subject: [node] crudely handle tradevolume limit while selling --- nodejs/lib/ships.ts | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/nodejs/lib/ships.ts b/nodejs/lib/ships.ts index 4ae64c7..ff38164 100644 --- a/nodejs/lib/ships.ts +++ b/nodejs/lib/ships.ts @@ -204,15 +204,27 @@ export class Ship { this.fuel = response.data.fuel; dbAgents.setAgent(response.data.agent); } - async sell(tradeSymbol: string): Promise { + async sell(tradeSymbol: string, maybeUnits?: number): Promise { // TODO check if our current waypoint has a marketplace and buys tradeSymbol? await this.dock(); let units = 0; - this.cargo.inventory.forEach(i => {if (i.symbol === tradeSymbol) units = i.units; }); + if (maybeUnits !== undefined) { + units = maybeUnits; + } else { + this.cargo.inventory.forEach(i => {if (i.symbol === tradeSymbol) units = i.units; }); + } + // TODO take into account the tradevolume if we know it already, we might need to buy in multiple steps const response = await send<{agent: Agent, cargo: Cargo}>({endpoint: `/my/ships/${this.symbol}/sell`, method: 'POST', payload: { symbol: tradeSymbol, units: units }}); // TODO transaction field if (response.error) { - debugLog(response); - throw response; + switch(response.error.code) { + case 4604: // units per transaction limit exceeded + const mtve = response.error.data as MarketTradeVolumeError; + await this.sell(tradeSymbol, mtve.tradeVolume); // TODO cache this information + return await this.sell(tradeSymbol, units - mtve.tradeVolume); + default: + debugLog(response); + throw response; + } } this.cargo = response.data.cargo; dbAgents.setAgent(response.data.agent); -- cgit v1.2.3