1
0
Fork 0

[node] fixed contracting and implemented renegotiation

This commit is contained in:
Julien Dessaux 2024-03-30 14:22:59 +01:00
parent 719a9c1a77
commit 8819cf9c67
Signed by: adyxax
GPG key ID: F92E51B86E07177E
8 changed files with 103 additions and 71 deletions

View file

@ -11,7 +11,7 @@ import * as systems from '../lib/systems.ts';
import * as utils from '../lib/utils.ts'; import * as utils from '../lib/utils.ts';
export async function init() { export async function init() {
const cs = dbContracts.getContracts(); const cs = dbContracts.getContracts().filter(c => !c.fulfilled);
cs.forEach(contract => run(contract)); cs.forEach(contract => run(contract));
} }
@ -37,8 +37,9 @@ async function runProcurement(contract: Contract, ships: Array<Ship>) {
const asteroids = await systems.type(ships[0].nav.systemSymbol, 'ENGINEERED_ASTEROID'); const asteroids = await systems.type(ships[0].nav.systemSymbol, 'ENGINEERED_ASTEROID');
const asteroidSymbol = asteroids[0].symbol; const asteroidSymbol = asteroids[0].symbol;
ships.forEach(async function(ship) { ships.forEach(async function(ship) {
while (true) {
while (!contract.fulfilled) { while (!contract.fulfilled) {
ship = dbShips.getShip(ship.symbol) as Ship; ship = dbShips.getShip(ship.symbol);
let goodCargo = ship.cargo.inventory.filter(i => i.symbol === wantedCargo)[0] let goodCargo = ship.cargo.inventory.filter(i => i.symbol === wantedCargo)[0]
// If we are in transit, we wait until we arrive // If we are in transit, we wait until we arrive
const delay = new Date(ship.nav.route.arrival).getTime() - new Date().getTime(); const delay = new Date(ship.nav.route.arrival).getTime() - new Date().getTime();
@ -46,25 +47,24 @@ async function runProcurement(contract: Contract, ships: Array<Ship>) {
// Then it depends on where we are // Then it depends on where we are
switch (ship.nav.waypointSymbol) { switch (ship.nav.waypointSymbol) {
case asteroidSymbol: case asteroidSymbol:
await mining.mineUntilFullOf(wantedCargo, ship, asteroidSymbol); await mining.mineUntilFullFor(contract, ship, asteroidSymbol);
await libShips.navigate(ship, deliveryPoint); await libShips.navigate(ship, deliveryPoint);
break; break;
case deliveryPoint: case deliveryPoint:
if (goodCargo !== undefined) { // we could be here if a client restart happens right after selling before we navigate away if (goodCargo !== undefined) { // we could be here if a client restart happens right after selling before we navigate away
console.log(`delivering ${goodCargo.units} of ${wantedCargo}`);
contract = await contracts.deliver(contract, ship); contract = await contracts.deliver(contract, ship);
if (contract.fulfilled) break; if (contract.fulfilled) break;
} }
await libShips.navigate(ship, asteroidSymbol); await libShips.navigate(ship, asteroidSymbol);
break; break;
default: default:
if (libShips.isFull(ship)) { if (await libShips.isFull(ship)) {
await selling.sell(ship, wantedCargo); await selling.sell(ship, wantedCargo);
} else { }
await libShips.navigate(ship, asteroidSymbol); await libShips.navigate(ship, asteroidSymbol);
} }
} }
contract = await libShips.negotiate(ship); // TODO that's not correct, the procurement type can change
} }
// TODO repurpose the ship
}); });
} }

View file

@ -29,8 +29,11 @@ export async function init(): Promise<void> {
if (json.error !== undefined) { if (json.error !== undefined) {
switch(json.error?.code) { switch(json.error?.code) {
case 4111: // 4111 means the agent symbol has already been claimed so no server reset happened case 4111: // 4111 means the agent symbol has already been claimed so no server reset happened
await libContracts.contracts(); // TODO await agents.agents();
await libShips.ships(); const contracts = await libContracts.getContracts();
const ongoing = contracts.filter(c => !c.fulfilled);
const ships = await libShips.getShips();
if (ongoing.length === 0) libShips.negotiate(ships[0]);
return; return;
default: default:
throw json; throw json;
@ -42,5 +45,5 @@ export async function init(): Promise<void> {
dbContracts.setContract(json.data.contract); dbContracts.setContract(json.data.contract);
dbShips.setShip(json.data.ship); dbShips.setShip(json.data.ship);
// Temporary fix to fetch the data on the startup probe // Temporary fix to fetch the data on the startup probe
await libShips.ships(); await libShips.getShips();
} }

View file

@ -1,21 +1,26 @@
import * as selling from './selling.js'; import * as selling from './selling.js';
import * as dbContracts from '../database/contracts.js';
import * as dbShips from '../database/ships.js'; import * as dbShips from '../database/ships.js';
import * as api from '../lib/api.js'; import * as api from '../lib/api.js';
import * as libShips from '../lib/ships.js'; import * as libShips from '../lib/ships.js';
import * as utils from '../lib/utils.js'; import * as utils from '../lib/utils.js';
import { Contract } from '../model/contract.ts';
import { Ship } from '../model/ship.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 // TODO find a good asteroid
while(true) { while(true) {
await mineUntilFull(ship); await mineUntilFull(ship);
ship = dbShips.getShip(ship.symbol) as Ship; contract = dbContracts.getContract(contract.id);
const cargo = utils.categorizeCargo(ship.cargo, good); 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); const wantedUnits = Object.values(cargo.wanted).reduce((acc, e) => acc += e, 0);
// > 90% full of the valuable goods ? // > 90% full of the valuable goods ? Or just have enough for the contract?
if (wantedUnits >= ship.cargo.capacity * 0.9) return ship; if (wantedUnits >= ship.cargo.capacity * 0.9
|| cargo.wanted[deliver.tradeSymbol] >= deliver.unitsRequired - deliver.unitsFulfilled) return;
// we are full but need to sell junk // we are full but need to sell junk
await selling.sell(ship, good); await selling.sell(ship, deliver.tradeSymbol);
await libShips.navigate(ship, asteroidSymbol); await libShips.navigate(ship, asteroidSymbol);
} }
} }

View file

@ -18,12 +18,9 @@ export function getContracts(): Array<Contract> {
} }
export function setContract(data: Contract): void { export function setContract(data: Contract): void {
if (getContract(data.id) === null) { const changes = updateContractStatement.run({
addContractStatement.run(JSON.stringify(data));
} else {
updateContractStatement.run({
data: JSON.stringify(data), data: JSON.stringify(data),
id: data.id, id: data.id,
}); }).changes;
} if (changes === 0) addContractStatement.run(JSON.stringify(data));
} }

View file

@ -8,7 +8,7 @@ const getMarketAtWaypointStatement = db.prepare(`SELECT data FROM markets WHERE
const updateMarketStatement = db.prepare(`UPDATE markets SET data = json(:data) WHERE data->>'symbol' = :symbol;`); const updateMarketStatement = db.prepare(`UPDATE markets SET data = json(:data) WHERE data->>'symbol' = :symbol;`);
export function getMarketAtWaypoint(symbol: string): Market|null { export function getMarketAtWaypoint(symbol: string): Market|null {
const data = getMarketAtWaypointStatement.get(symbol) as DbData; const data = getMarketAtWaypointStatement.get(symbol) as DbData|undefined;
if (!data) return null; if (!data) return null;
return JSON.parse(data.data); return JSON.parse(data.data);
} }

View file

@ -23,14 +23,11 @@ export function getShipsAt(symbol: string): Array<Ship> {
export function setShip(data: Ship): void { export function setShip(data: Ship): void {
if (getShip(data.symbol) === null) { const changes = updateShipStatement.run({
addShipStatement.run(JSON.stringify(data));
} else {
updateShipStatement.run({
data: JSON.stringify(data), data: JSON.stringify(data),
symbol: data.symbol, symbol: data.symbol,
}); }).changes;
} if (changes === 0) addShipStatement.run(JSON.stringify(data));
} }
export function setShipCargo(symbol: string, cargo: Cargo): void { export function setShipCargo(symbol: string, cargo: Cargo): void {

View file

@ -22,16 +22,29 @@ export async function accept(contract: Contract): Promise<Contract> {
return response.data.contract; return response.data.contract;
} }
export async function contracts(): Promise<Array<Contract>> { export async function getContracts(): Promise<Array<Contract>> {
const response = await api.sendPaginated<Contract>({endpoint: '/my/contracts'}); const response = await api.sendPaginated<Contract>({endpoint: '/my/contracts'});
response.forEach(contract => dbContracts.setContract(contract)); response.forEach(contract => dbContracts.setContract(contract));
return response; return response;
} }
export async function getContract(contract: Contract): Promise<Contract> {
try {
return dbContracts.getContract(contract.id);
} catch {}
const response = await api.send<Contract>({endpoint: `/my/contracts/${contract.id}`});
if (response.error) {
api.debugLog(response);
throw response;
}
dbContracts.setContract(response.data);
return response.data;
}
export async function deliver(contract: Contract, ship: Ship): Promise<Contract> { export async function deliver(contract: Contract, ship: Ship): Promise<Contract> {
contract = dbContracts.getContract(contract.id); contract = dbContracts.getContract(contract.id);
ship = dbShips.getShip(ship.symbol); ship = dbShips.getShip(ship.symbol);
if (contract.terms.deliver[0].unitsRequired >= contract.terms.deliver[0].unitsFulfilled) { if (contract.terms.deliver[0].unitsRequired <= contract.terms.deliver[0].unitsFulfilled) {
return await fulfill(contract); return await fulfill(contract);
} }
const tradeSymbol = contract.terms.deliver[0].tradeSymbol; const tradeSymbol = contract.terms.deliver[0].tradeSymbol;
@ -54,7 +67,7 @@ export async function deliver(contract: Contract, ship: Ship): Promise<Contract>
} }
dbContracts.setContract(response.data.contract); dbContracts.setContract(response.data.contract);
dbShips.setShipCargo(ship.symbol, response.data.cargo); dbShips.setShipCargo(ship.symbol, response.data.cargo);
if(response.data.contract.terms.deliver[0].unitsRequired >= response.data.contract.terms.deliver[0].unitsFulfilled) { if(response.data.contract.terms.deliver[0].unitsRequired <= response.data.contract.terms.deliver[0].unitsFulfilled) {
return await fulfill(response.data.contract); return await fulfill(response.data.contract);
} }
return response.data.contract; return response.data.contract;

View file

@ -1,6 +1,7 @@
import { Response } from '../model/api.ts'; import { Response } from '../model/api.ts';
import { Agent } from '../model/agent.ts'; import { Agent } from '../model/agent.ts';
import { Cargo } from '../model/cargo.ts'; import { Cargo } from '../model/cargo.ts';
import { Contract } from '../model/contract.ts';
import { Cooldown, Fuel, Nav, Ship } from '../model/ship.ts'; import { Cooldown, Fuel, Nav, Ship } from '../model/ship.ts';
import * as api from './api.ts'; import * as api from './api.ts';
import * as dbAgents from '../database/agents.ts'; import * as dbAgents from '../database/agents.ts';
@ -9,7 +10,7 @@ import * as dbShips from '../database/ships.ts';
import * as systems from '../lib/systems.ts'; import * as systems from '../lib/systems.ts';
export async function dock(ship: Ship): Promise<void> { export async function dock(ship: Ship): Promise<void> {
ship = dbShips.getShip(ship.symbol); ship = await getShip(ship);
if (ship.nav.status === 'DOCKED') return; if (ship.nav.status === 'DOCKED') return;
const response = await api.send<{nav: Nav}>({endpoint: `/my/ships/${ship.symbol}/dock`, method: 'POST'}); const response = await api.send<{nav: Nav}>({endpoint: `/my/ships/${ship.symbol}/dock`, method: 'POST'});
if (response.error) { if (response.error) {
@ -27,8 +28,8 @@ export async function dock(ship: Ship): Promise<void> {
} }
export async function extract(ship: Ship): Promise<Cargo> { export async function extract(ship: Ship): Promise<Cargo> {
ship = dbShips.getShip(ship.symbol); ship = await getShip(ship);
if (isFull(ship)) return ship.cargo; if (await isFull(ship)) return ship.cargo;
// TODO move to a suitable asteroid? // TODO move to a suitable asteroid?
// const asteroidFields = await systems.type({symbol: ship.nav.systemSymbol, type: 'ENGINEERED_ASTEROID'}); // const asteroidFields = await systems.type({symbol: ship.nav.systemSymbol, type: 'ENGINEERED_ASTEROID'});
// TODO if there are multiple fields, find the closest one? // TODO if there are multiple fields, find the closest one?
@ -55,8 +56,8 @@ export async function extract(ship: Ship): Promise<Cargo> {
return response.data.cargo return response.data.cargo
} }
export function isFull(ship: Ship): boolean { export async function isFull(ship: Ship): Promise<boolean> {
ship = dbShips.getShip(ship.symbol); ship = await getShip(ship);
return ship.cargo.units >= ship.cargo.capacity * 0.9; return ship.cargo.units >= ship.cargo.capacity * 0.9;
} }
@ -73,7 +74,7 @@ export function isFull(ship: Ship): boolean {
//} //}
export async function navigate(ship: Ship, waypoint: string): Promise<void> { export async function navigate(ship: Ship, waypoint: string): Promise<void> {
ship = dbShips.getShip(ship.symbol); ship = await getShip(ship);
if (ship.nav.waypointSymbol === waypoint) return; if (ship.nav.waypointSymbol === waypoint) return;
await orbit(ship); await orbit(ship);
// TODO if we do not have enough fuel, make a stop to refuel along the way or drift to the destination // TODO if we do not have enough fuel, make a stop to refuel along the way or drift to the destination
@ -99,13 +100,25 @@ export async function navigate(ship: Ship, waypoint: string): Promise<void> {
await refuel(ship); await refuel(ship);
} }
//export async function negotiate(ctx) { export async function negotiate(ship: Ship): Promise<Contract> {
// // TODO ship = await getShip(ship);
// return await api.send({endpoint: `/my/ships/${ctx.ship}/negotiate/contract`, method: 'POST'}); const response = await api.send<{contract: Contract}>({endpoint: `/my/ships/${ship.symbol}/negotiate/contract`, method: 'POST'});
//} if (response.error) {
switch(response.error.code) {
case 4214: // ship is in transit
const errorData = response.error.data as { secondsToArrival: number};
await api.sleep(errorData.secondsToArrival * 1000);
return await negotiate(ship);
default: // yet unhandled error
api.debugLog(response);
throw response;
}
}
return response.data.contract;
}
export async function orbit(ship: Ship): Promise<void> { export async function orbit(ship: Ship): Promise<void> {
ship = dbShips.getShip(ship.symbol); ship = await getShip(ship);
if (ship.nav.status === 'IN_ORBIT') return; if (ship.nav.status === 'IN_ORBIT') return;
const response = await api.send<{nav: Nav}>({endpoint: `/my/ships/${ship.symbol}/orbit`, method: 'POST'}); const response = await api.send<{nav: Nav}>({endpoint: `/my/ships/${ship.symbol}/orbit`, method: 'POST'});
if (response.error) { if (response.error) {
@ -115,6 +128,7 @@ export async function orbit(ship: Ship): Promise<void> {
await api.sleep(errorData.secondsToArrival * 1000); await api.sleep(errorData.secondsToArrival * 1000);
return await orbit(ship); return await orbit(ship);
default: // yet unhandled error default: // yet unhandled error
api.debugLog(response);
throw response; throw response;
} }
} }
@ -134,7 +148,7 @@ export async function orbit(ship: Ship): Promise<void> {
//} //}
export async function refuel(ship: Ship): Promise<void> { export async function refuel(ship: Ship): Promise<void> {
ship = dbShips.getShip(ship.symbol); ship = await getShip(ship);
if (ship.fuel.current >= ship.fuel.capacity * 0.9) return; if (ship.fuel.current >= ship.fuel.capacity * 0.9) return;
// TODO check if our current waypoint has a marketplace (and sells fuel)? // TODO check if our current waypoint has a marketplace (and sells fuel)?
await dock(ship); await dock(ship);
@ -148,7 +162,7 @@ export async function refuel(ship: Ship): Promise<void> {
} }
export async function sell(ship: Ship, tradeSymbol: string): Promise<Cargo> { export async function sell(ship: Ship, tradeSymbol: string): Promise<Cargo> {
ship = dbShips.getShip(ship.symbol); ship = await getShip(ship);
// TODO check if our current waypoint has a marketplace and buys tradeSymbol? // TODO check if our current waypoint has a marketplace and buys tradeSymbol?
await dock(ship); await dock(ship);
let units = 0; let units = 0;
@ -163,7 +177,7 @@ export async function sell(ship: Ship, tradeSymbol: string): Promise<Cargo> {
return response.data.cargo; return response.data.cargo;
} }
export async function ships(): Promise<Array<Ship>> { export async function getShips(): Promise<Array<Ship>> {
const response = await api.send<Array<Ship>>({endpoint: `/my/ships`, page: 1}); const response = await api.send<Array<Ship>>({endpoint: `/my/ships`, page: 1});
if (response.error) { if (response.error) {
api.debugLog(response); api.debugLog(response);
@ -173,7 +187,10 @@ export async function ships(): Promise<Array<Ship>> {
return response.data; return response.data;
} }
export async function ship(ship: Ship): Promise<Ship> { export async function getShip(ship: Ship): Promise<Ship> {
try {
return dbShips.getShip(ship.symbol);
} catch {}
const response = await api.send<Ship>({endpoint: `/my/ships/${ship.symbol}`}); const response = await api.send<Ship>({endpoint: `/my/ships/${ship.symbol}`});
if (response.error) { if (response.error) {
api.debugLog(response); api.debugLog(response);