blob: df9a92b312ac66c0a04a56cdf0009effa23c16b5 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
import * as dbMarkets from '../database/markets.ts';
import * as libSystems from '../lib/systems.ts';
import {
categorizeCargo,
sortByDistanceFrom,
whatCanBeTradedAt,
} from '../lib/utils.ts';
import { Ship } from '../lib/ships.ts';
// example ctx { ship: {XXX}, good: 'SILVER_ORE' }
export async function sell(ship: Ship, good: string): Promise<Ship> {
outer: while(true) {
const waypoint = await libSystems.waypoint(ship.nav.waypointSymbol);
// first lets see what we want to sell
let cargo = categorizeCargo(ship.cargo, good);
// get the marketdata from our location
const market = await libSystems.market(waypoint);
// can we sell anything here?
const goods = whatCanBeTradedAt(cargo.goods, market.imports.concat(market.exchange));
for (let i = 0; i < goods.length; i++) {
await ship.sell(goods[i].symbol);
};
// are we done selling everything we can?
cargo = categorizeCargo(ship.cargo, good);
if (Object.keys(cargo.goods).length === 0) {
return ship;
}
// we need to move somewhere else to sell our remaining goods
const markets = sortByDistanceFrom(ship.nav.route.destination, await libSystems.trait(ship.nav.systemSymbol, 'MARKETPLACE'));
// check from the closest one if they import what we need to sell
for (let i = 0; i < markets.length; i++) {
const waypoint = await libSystems.waypoint(markets[i].data.symbol);
const market = await libSystems.market(waypoint);
// if we have no data on the market we need to go there and see
// and if we have data and can sell there we need to go too
if (market === null || whatCanBeTradedAt(cargo.goods, market.imports).length > 0) {
await ship.navigate(waypoint);
continue outer;
}
}
// check from the closest one if they exchange what we need to sell
for (let i = 0; i < markets.length; i++) {
const waypoint = await libSystems.waypoint(markets[i].data.symbol);
const market = await libSystems.market(waypoint);
// if we can sell there we need to go
if (whatCanBeTradedAt(cargo.goods, market.exchange).length > 0) {
await ship.navigate(waypoint);
continue outer;
}
}
throw new Error(`Ship {ship.symbol} has found no importing or exchanging market for its cargo in the system`);
}
}
|