summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulien Dessaux2024-05-04 00:13:32 +0200
committerJulien Dessaux2024-05-04 00:13:32 +0200
commit063b2c9951d568c43d4a3b37b2d075d1dff8ff3c (patch)
treebc1902e902b71e412b788300b1a78c35d0d5ba37
parent[node] updated dependencies (diff)
downloadspacetraders-063b2c9951d568c43d4a3b37b2d075d1dff8ff3c.tar.gz
spacetraders-063b2c9951d568c43d4a3b37b2d075d1dff8ff3c.tar.bz2
spacetraders-063b2c9951d568c43d4a3b37b2d075d1dff8ff3c.zip
[node] crudely handle tradevolume limit while selling
Diffstat (limited to '')
-rw-r--r--nodejs/lib/ships.ts20
1 files 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<Cargo> {
+ async sell(tradeSymbol: string, maybeUnits?: number): Promise<Cargo> {
// 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);