[node] fixed contracting and implemented renegotiation
This commit is contained in:
parent
719a9c1a77
commit
8819cf9c67
8 changed files with 103 additions and 71 deletions
|
@ -1,21 +1,26 @@
|
|||
import * as selling from './selling.js';
|
||||
import * as dbContracts from '../database/contracts.js';
|
||||
import * as dbShips from '../database/ships.js';
|
||||
import * as api from '../lib/api.js';
|
||||
import * as libShips from '../lib/ships.js';
|
||||
import * as utils from '../lib/utils.js';
|
||||
import { Contract } from '../model/contract.ts';
|
||||
import { Ship } from '../model/ship.ts';
|
||||
|
||||
export async function mineUntilFullOf(good: string, ship: Ship, asteroidSymbol: string): Promise<void> {
|
||||
export async function mineUntilFullFor(contract: Contract, ship: Ship, asteroidSymbol: string): Promise<void> {
|
||||
// TODO find a good asteroid
|
||||
while(true) {
|
||||
await mineUntilFull(ship);
|
||||
ship = dbShips.getShip(ship.symbol) as Ship;
|
||||
const cargo = utils.categorizeCargo(ship.cargo, good);
|
||||
contract = dbContracts.getContract(contract.id);
|
||||
const deliver = contract.terms.deliver[0];
|
||||
ship = dbShips.getShip(ship.symbol);
|
||||
const cargo = utils.categorizeCargo(ship.cargo, deliver.tradeSymbol);
|
||||
const wantedUnits = Object.values(cargo.wanted).reduce((acc, e) => acc += e, 0);
|
||||
// > 90% full of the valuable goods ?
|
||||
if (wantedUnits >= ship.cargo.capacity * 0.9) return ship;
|
||||
// > 90% full of the valuable goods ? Or just have enough for the contract?
|
||||
if (wantedUnits >= ship.cargo.capacity * 0.9
|
||||
|| cargo.wanted[deliver.tradeSymbol] >= deliver.unitsRequired - deliver.unitsFulfilled) return;
|
||||
// we are full but need to sell junk
|
||||
await selling.sell(ship, good);
|
||||
await selling.sell(ship, deliver.tradeSymbol);
|
||||
await libShips.navigate(ship, asteroidSymbol);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue