import { Response, debugLog, send, sleep, } from './api.ts'; import { MarketTradeVolumeError, ShipIsCurrentlyInTransitError, ShipIsStillOnCooldownError, ShipRequiresMoreFuelForNavigationError, } from './errors.ts'; import { Agent, setAgent } from './agent.ts'; import { Contract } from './contracts.ts'; import * as libSystems from './systems.ts'; import { Cargo, Cooldown, Fuel, Nav, Registration, Waypoint, } from './types.ts'; import { is_there_a_ship_at_this_waypoint, shortestPath, sortByPrice, } from './utils.ts'; export class Ship { cargo: Cargo; cooldown: Cooldown; // crew // engine // frame fuel: Fuel; // modules // mounts nav: Nav; // reactor registration: Registration; symbol: string; constructor(ship: Ship) { this.cargo = ship.cargo; this.cooldown = ship.cooldown; this.fuel = ship.fuel; this.nav = ship.nav; this.registration = ship.registration; this.symbol = ship.symbol; } async dock(): Promise { if (this.nav.status === 'DOCKED') return; const response = await send<{nav: Nav}>({endpoint: `/my/ships/${this.symbol}/dock`, method: 'POST'}); if (response.error) { switch(response.error.code) { case 4214: const sicite = response.error.data as ShipIsCurrentlyInTransitError; await sleep(sicite.secondsToArrival * 1000); return await this.dock(); default: // yet unhandled error debugLog(response); throw response; } } this.nav = response.data.nav; } async extract(): Promise { if (this.isFull()) return this.cargo; // TODO move to a suitable asteroid? // const asteroidFields = await systems.type({symbol: this.nav.systemSymbol, type: 'ENGINEERED_ASTEROID'}); // TODO if there are multiple fields, find the closest one? //await navigate({symbol: ctx.symbol, waypoint: asteroidFields[0].symbol}); await this.orbit(); // TODO handle surveying? const response = await send<{cooldown: Cooldown, cargo: Cargo}>({endpoint: `/my/ships/${this.symbol}/extract`, method: 'POST'}); // TODO extraction and events api response fields cf https://spacetraders.stoplight.io/docs/spacetraders/b3931d097608d-extract-resources if (response.error) { switch(response.error.code) { case 4000: const sisoce = response.error.data as ShipIsStillOnCooldownError; await sleep(sisoce.cooldown.remainingSeconds * 1000); return await this.extract(); case 4228: // ship is full return this.cargo; default: // yet unhandled error debugLog(response); throw response; } } this.cargo = response.data.cargo; await sleep(response.data.cooldown.remainingSeconds*1000); return this.cargo; } //async flightMode(mode: string): Promise { // if (this.nav.flightMode === mode) return; // const response = await send