Improved ships lib with ships db caching
This commit is contained in:
parent
68c457964a
commit
7008c91c6f
10 changed files with 170 additions and 32 deletions
|
@ -21,6 +21,6 @@ export async function register(symbol, faction) {
|
|||
}
|
||||
dbConfig.registerAgent(json.data);
|
||||
exploration.init();
|
||||
// TODO ship
|
||||
dbShips.setShip(json.data.ship);
|
||||
// TODO contract
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ import * as ships from '../lib/ships.js';
|
|||
import * as systems from '../lib/systems.js';
|
||||
|
||||
export async function auto(ctx) {
|
||||
let ship = await ships.ship({ship: ctx.ship});
|
||||
let ship = await ships.ship({symbol: ctx.ship});
|
||||
// Fetch our contracts in the system the ship currently is in
|
||||
let cs = await contracts.contracts();
|
||||
cs = cs.data.filter(c => c.terms.deliver[0].destinationSymbol.startsWith(ship.data.nav.systemSymbol));
|
||||
|
@ -20,7 +20,7 @@ export async function auto(ctx) {
|
|||
const asteroidFields = await systems.type({symbol: ship.data.nav.systemSymbol, type: 'ASTEROID_FIELD'});
|
||||
const asteroidField = asteroidFields[0].symbol;
|
||||
while (true) {
|
||||
ship = await ships.ship({ship: ctx.ship}); // TODO we should not need to fetch this
|
||||
ship = await ships.ship({symbol: ctx.ship}); // TODO we should not need to fetch this
|
||||
// If we are in transit, we wait until we arrive
|
||||
const delay = new Date(ship.data.nav.route.arrival) - new Date();
|
||||
if (delay > 0) await api.sleep(delay);
|
||||
|
@ -31,16 +31,16 @@ export async function auto(ctx) {
|
|||
case asteroidField:
|
||||
let response = await mining.mineUntilFullOf({good: good, ship: ctx.ship});
|
||||
//console.log(`${ctx.ship}'s cargo is full with ${response.units} of ${good}!`);
|
||||
await ships.navigate({ship: ctx.ship, waypoint: deliveryPoint});
|
||||
await ships.navigate({symbol: ctx.ship, waypoint: deliveryPoint});
|
||||
break;
|
||||
case deliveryPoint:
|
||||
await ships.dock({symbol: ctx.ship});
|
||||
await ships.refuel({ship: ctx.ship});
|
||||
await ships.refuel({symbol: ctx.ship});
|
||||
console.log(`delivering ${goodCargo.units} of ${good}`);
|
||||
await contracts.deliver({contract: contract.id, ship: ctx.ship, good: good, units: goodCargo.units });
|
||||
await ships.navigate({ship: ctx.ship, waypoint: asteroidField});
|
||||
await ships.navigate({symbol: ctx.ship, waypoint: asteroidField});
|
||||
await ships.dock({symbol: ctx.ship});
|
||||
await ships.refuel({ship: ctx.ship});
|
||||
await ships.refuel({symbol: ctx.ship});
|
||||
await ships.orbit({symbol: ctx.ship});
|
||||
break;
|
||||
default:
|
||||
|
|
|
@ -6,7 +6,7 @@ import * as ships from '../lib/ships.js';
|
|||
export async function mineUntilFullOf(ctx) {
|
||||
while(true) {
|
||||
let response = await mineUntilFull({ship: ctx.ship});
|
||||
if (response === null) response = await ships.ship({ship: ctx.ship}); // TODO we should not need to fetch this
|
||||
if (response === null) response = await ships.ship({symbol: ctx.ship}); // TODO we should not need to fetch this
|
||||
let good = response.data.cargo.inventory.filter(i => i.symbol === ctx.good)[0];
|
||||
const inventory = response.data.cargo.inventory.filter(i => i.symbol !== ctx.good);
|
||||
const antimatter = response.data.cargo.inventory.filter(i => i.symbol === 'ANTIMATTER')[0];
|
||||
|
@ -17,7 +17,7 @@ export async function mineUntilFullOf(ctx) {
|
|||
for (let i=0; i<inventory.length; ++i) {
|
||||
if (inventory[i].symbol === 'ANTIMATTER') continue;
|
||||
//console.log(`selling ${inventory[i].units} of ${inventory[i].symbol}`);
|
||||
await ships.sell({ship: ctx.ship, good: inventory[i].symbol, units: inventory[i].units});
|
||||
await ships.sell({symbol: ctx.ship, good: inventory[i].symbol, units: inventory[i].units});
|
||||
}
|
||||
await ships.orbit({symbol: ctx.ship});
|
||||
}
|
||||
|
@ -28,7 +28,7 @@ export async function mineUntilFullOf(ctx) {
|
|||
// returns the last ship's extract api response
|
||||
async function mineUntilFull(ctx) {
|
||||
while(true) {
|
||||
const response = await ships.extract(ctx);
|
||||
const response = await ships.extract({symbol: ctx.ship});
|
||||
if (response === null) return null;
|
||||
//console.log(`${ctx.ship}: extracted ${response.data.extraction.yield.units} of ${response.data.extraction.yield.symbol}`);
|
||||
if (response.data.cargo.units >= response.data.cargo.capacity * 0.9) return response;
|
||||
|
|
6
database/002_ships.sql
Normal file
6
database/002_ships.sql
Normal file
|
@ -0,0 +1,6 @@
|
|||
CREATE TABLE ships (
|
||||
id INTEGER PRIMARY KEY,
|
||||
data JSON NOT NULL,
|
||||
updated DATE DEFAULT NULL
|
||||
);
|
||||
CREATE UNIQUE INDEX ships_data_symbol on ships (json_extract(data, '$.symbol'));
|
|
@ -1,6 +1,6 @@
|
|||
import db from './db.js';
|
||||
|
||||
const getTokenStatement = db.prepare(`SELECT json_extract(value, '$.token') as token from config where key = 'register_data';`);
|
||||
const getTokenStatement = db.prepare(`SELECT value->>'token' as token from config where key = 'register_data';`);
|
||||
const registerAgentStatement = db.prepare(`INSERT INTO config(key, value) VALUES ('register_data', json(?));`);
|
||||
|
||||
export function getToken() {
|
||||
|
|
|
@ -4,6 +4,7 @@ import Database from 'better-sqlite3';
|
|||
const allMigrations = [
|
||||
'database/000_init.sql',
|
||||
'database/001_systems.sql',
|
||||
'database/002_ships.sql',
|
||||
];
|
||||
|
||||
const db = new Database(
|
||||
|
|
82
database/ships.js
Normal file
82
database/ships.js
Normal file
|
@ -0,0 +1,82 @@
|
|||
import db from './db.js';
|
||||
|
||||
const getShipStatement = db.prepare(`SELECT data FROM ships WHERE data->>'symbol' = ?;`);
|
||||
const setShipStatement = db.prepare(`INSERT INTO ships(data, updated) VALUES (json(?), ?);`);
|
||||
const setShipCargoStatement = db.prepare(`UPDATE ships SET data = (SELECT json_set(data, '$.cargo', json(:cargo)) FROM ships WHERE data->>'symbol' = :symbol), updated = :date WHERE data->>'symbol' = :symbol;`);
|
||||
const setShipFuelStatement = db.prepare(`UPDATE ships SET data = (SELECT json_set(data, '$.fuel', json(:fuel)) FROM ships WHERE data->>'symbol' = :symbol), updated = :date WHERE data->>'symbol' = :symbol;`);
|
||||
const setShipNavStatement = db.prepare(`UPDATE ships SET data = (SELECT json_set(data, '$.nav', json(:nav)) FROM ships WHERE data->>'symbol' = :symbol), updated = :date WHERE data->>'symbol' = :symbol;`);
|
||||
const updateShipStatement = db.prepare(`UPDATE ships SET data = json(:data), updated = :date WHERE data->>'symbol' = :symbol;`);
|
||||
|
||||
export function getShip(symbol) {
|
||||
try {
|
||||
const data = getShipStatement.get(symbol);
|
||||
if (data === undefined) {
|
||||
return null;
|
||||
}
|
||||
return JSON.parse(data.data);
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function setShip(data) {
|
||||
if (getShip(data.symbol) === null) {
|
||||
try {
|
||||
return setShipStatement.run(JSON.stringify(data), new Date().toISOString()).lastInsertRowid;
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
return updateShipStatement.run({
|
||||
data: JSON.stringify(data),
|
||||
date: new Date().toISOString(),
|
||||
symbol: data.symbol,
|
||||
}).changes;
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function setShipCargo(symbol, cargo) {
|
||||
try {
|
||||
setShipCargoStatement.run({
|
||||
cargo: JSON.stringify(cargo),
|
||||
date: new Date().toISOString(),
|
||||
symbol: symbol,
|
||||
}).changes;
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function setShipFuel(symbol, fuel) {
|
||||
try {
|
||||
setShipFuelStatement.run({
|
||||
date: new Date().toISOString(),
|
||||
fuel: JSON.stringify(fuel),
|
||||
symbol: symbol,
|
||||
}).changes;
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function setShipNav(symbol, nav) {
|
||||
try {
|
||||
setShipNavStatement.run({
|
||||
date: new Date().toISOString(),
|
||||
nav: JSON.stringify(nav),
|
||||
symbol: symbol,
|
||||
}).changes;
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -1,9 +1,9 @@
|
|||
import db from './db.js';
|
||||
|
||||
const getSystemStatement = db.prepare(`SELECT data FROM systems WHERE json_extract(data, '$.symbol') = ?;`);
|
||||
const getSystemUpdatedStatement = db.prepare(`SELECT updated FROM systems WHERE json_extract(data, '$.symbol') = ?;`);
|
||||
const getSystemStatement = db.prepare(`SELECT data FROM systems WHERE data->>'symbol' = ?;`);
|
||||
const getSystemUpdatedStatement = db.prepare(`SELECT updated FROM systems WHERE data->>'symbol' = ?;`);
|
||||
const setSystemStatement = db.prepare(`INSERT INTO systems(data) VALUES (json(?));`);
|
||||
const setSystemWaypointsStatement = db.prepare(`UPDATE systems SET data = (SELECT json_set(data, '$.waypoints', json(:waypoints)) FROM systems WHERE json_extract(data, '$.symbol') = :symbol), updated = :date WHERE json_extract(data, '$.symbol') = :symbol;`);
|
||||
const setSystemWaypointsStatement = db.prepare(`UPDATE systems SET data = (SELECT json_set(data, '$.waypoints', json(:waypoints)) FROM systems WHERE data->>'symbol' = :symbol), updated = :date WHERE data->>'symbol' = :symbol;`);
|
||||
|
||||
export function init() {
|
||||
try {
|
||||
|
|
69
lib/ships.js
69
lib/ships.js
|
@ -1,7 +1,12 @@
|
|||
import * as api from './api.js';
|
||||
import * as dbConfig from '../database/config.js';
|
||||
import * as dbShips from '../database/ships.js';
|
||||
|
||||
export async function extract(ctx) {
|
||||
const response = await api.send({endpoint: `/my/ships/${ctx.ship}/extract`, method: 'POST'});
|
||||
// TODO check if our current waypoint has an asteroid field?
|
||||
await orbit(ctx);
|
||||
const ship = dbShips.getShip(ctx.symbol);
|
||||
const response = await api.send({endpoint: `/my/ships/${ctx.symbol}/extract`, method: 'POST'});
|
||||
if (response.error !== undefined) {
|
||||
switch(response.error.code) {
|
||||
case 4000: // ship is on cooldown
|
||||
|
@ -13,12 +18,17 @@ export async function extract(ctx) {
|
|||
throw response;
|
||||
}
|
||||
} else {
|
||||
dbShips.setShipCargo(ctx.symbol, response.data.cargo);
|
||||
await api.sleep(response.data.cooldown.remainingSeconds*1000);
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
export async function dock(ctx) {
|
||||
const ship = dbShips.getShip(ctx.symbol);
|
||||
if (ship.nav.status === 'DOCKED') {
|
||||
return null;
|
||||
}
|
||||
const response = await api.send({endpoint: `/my/ships/${ctx.symbol}/dock`, method: 'POST'});
|
||||
if (response.error !== undefined) {
|
||||
switch(response.error.code) {
|
||||
|
@ -29,27 +39,41 @@ export async function dock(ctx) {
|
|||
throw response;
|
||||
}
|
||||
}
|
||||
dbShips.setShipNav(ctx.symbol, response.data.nav);
|
||||
return response;
|
||||
}
|
||||
|
||||
export async function jump(ctx) {
|
||||
// TODO
|
||||
const response = await api.send({endpoint: `/my/ships/${ctx.ship}/jump`, method: 'POST', payload: { systemSymbol: ctx.system }});
|
||||
await api.sleep(response.data.cooldown.remainingSeconds*1000);
|
||||
return response;
|
||||
}
|
||||
|
||||
export async function navigate(ctx) {
|
||||
const response = await api.send({endpoint: `/my/ships/${ctx.ship}/navigate`, method: 'POST', payload: { waypointSymbol: ctx.waypoint }});
|
||||
const ship = dbShips.getShip(ctx.symbol);
|
||||
if (ship.nav.waypointSymbol === ctx.waypoint) {
|
||||
return null;
|
||||
}
|
||||
await orbit(ctx);
|
||||
const response = await api.send({endpoint: `/my/ships/${ctx.symbol}/navigate`, method: 'POST', payload: { waypointSymbol: ctx.waypoint }});
|
||||
dbShips.setShipFuel(ctx.symbol, response.data.fuel);
|
||||
dbShips.setShipNav(ctx.symbol, response.data.nav);
|
||||
const delay = new Date(response.data.nav.route.arrival) - new Date();
|
||||
await api.sleep(delay);
|
||||
return response;
|
||||
}
|
||||
|
||||
export async function negotiate(ctx) {
|
||||
// TODO
|
||||
return await api.send({endpoint: `/my/ships/${ctx.ship}/negotiate/contract`, method: 'POST'});
|
||||
}
|
||||
|
||||
export async function orbit(ctx) {
|
||||
const ship = dbShips.getShip(ctx.symbol);
|
||||
if (ship.nav.status === 'IN_ORBIT') {
|
||||
return null;
|
||||
}
|
||||
const response = await api.send({endpoint: `/my/ships/${ctx.symbol}/orbit`, method: 'POST'});
|
||||
if (response.error !== undefined) {
|
||||
switch(response.error.code) {
|
||||
|
@ -60,28 +84,59 @@ export async function orbit(ctx) {
|
|||
throw response;
|
||||
}
|
||||
}
|
||||
dbShips.setShipNav(ctx.symbol, response.data.nav);
|
||||
return response;
|
||||
}
|
||||
|
||||
export async function purchase(ctx) {
|
||||
return await api.send({endpoint: '/my/ships', method: 'POST', payload: {
|
||||
const response = await api.send({endpoint: '/my/ships', method: 'POST', payload: {
|
||||
shipType: ctx.shipType,
|
||||
waypointSymbol: ctx.waypoint,
|
||||
}});
|
||||
if (response.error !== undefined) {
|
||||
throw response;
|
||||
}
|
||||
dbShips.setShip(response.data.ship);
|
||||
}
|
||||
|
||||
export async function refuel(ctx) {
|
||||
return await api.send({endpoint: `/my/ships/${ctx.ship}/refuel`, method: 'POST'});
|
||||
// TODO check if our current waypoint has a marketplace (and sells fuel)?
|
||||
const ship = dbShips.getShip(ctx.symbol);
|
||||
if (ship.fuel.current >= ship.fuel.capacity * 0.9) {
|
||||
return null;
|
||||
}
|
||||
await dock(ctx);
|
||||
const response = await api.send({endpoint: `/my/ships/${ctx.symbol}/refuel`, method: 'POST'});
|
||||
if (response.error !== undefined) {
|
||||
throw response;
|
||||
}
|
||||
dbShips.setShipFuel(ctx.symbol, response.data.fuel);
|
||||
// TODO track credits
|
||||
return response;
|
||||
}
|
||||
|
||||
export async function sell(ctx) {
|
||||
return await api.send({endpoint: `/my/ships/${ctx.ship}/sell`, method: 'POST', payload: { symbol: ctx.good, units: ctx.units }});
|
||||
await dock(ctx);
|
||||
const ship = dbShips.getShip(ctx.symbol);
|
||||
const response = await api.send({endpoint: `/my/ships/${ctx.symbol}/sell`, method: 'POST', payload: { symbol: ctx.good, units: ctx.units }});
|
||||
if (response.error !== undefined) {
|
||||
throw response;
|
||||
}
|
||||
dbShips.setShipCargo(ctx.symbol, response.data.cargo);
|
||||
// TODO track credits
|
||||
return response;
|
||||
}
|
||||
|
||||
export async function ship(ctx) {
|
||||
return await api.send({endpoint: `/my/ships/${ctx.ship}`});
|
||||
const response = await api.send({endpoint: `/my/ships/${ctx.symbol}`});
|
||||
if (response.error !== undefined) {
|
||||
throw response;
|
||||
}
|
||||
dbShips.setShip(response.data);
|
||||
return response;
|
||||
}
|
||||
|
||||
export async function survey(ctx) {
|
||||
return await api.send({endpoint: `/my/ships/${ctx.ship}/survey`, method: 'POST'});
|
||||
// TODO
|
||||
return await api.send({endpoint: `/my/ships/${ctx.symbol}/survey`, method: 'POST'});
|
||||
}
|
||||
|
|
16
main.js
16
main.js
|
@ -54,9 +54,6 @@ default:
|
|||
case 'ships.dock':
|
||||
api.debugLog(await ships.dock({symbol: process.argv[3]}));
|
||||
break;
|
||||
case 'ships.extract':
|
||||
api.debugLog(await ships.extract({ship: process.argv[3]}));
|
||||
break;
|
||||
case 'ships.jump':
|
||||
api.debugLog(await ships.jump({ship: process.argv[3], system: process.argv[4]}));
|
||||
break;
|
||||
|
@ -64,14 +61,11 @@ default:
|
|||
// api.send({endpoint: `/systems/${process.argv[3]}/waypoints/${process.argv[4]}/market`});
|
||||
// break;
|
||||
case 'ships.navigate':
|
||||
api.debugLog(await ships.navigate({ship: process.argv[3], waypoint: process.argv[4]}));
|
||||
api.debugLog(await ships.navigate({symbol: process.argv[3], waypoint: process.argv[4]}));
|
||||
break;
|
||||
case 'ships.negotiate':
|
||||
api.debugLog(await ships.negotiate({ship: process.argv[3]}));
|
||||
break;
|
||||
case 'ships.navigate':
|
||||
api.debugLog(await ships.navigate({ship: process.argv[3], waypoint: process.argv[4]}));
|
||||
break;
|
||||
case 'ships.orbit':
|
||||
api.debugLog(await ships.orbit({symbol: process.argv[3]}));
|
||||
break;
|
||||
|
@ -79,16 +73,16 @@ default:
|
|||
api.debugLog(await ships.purchase({shipType: process.argv[3], waypoint: process.argv[4]}));
|
||||
break;
|
||||
case 'ships.refuel':
|
||||
api.debugLog(await ships.refuel({ship: process.argv[3]}));
|
||||
api.debugLog(await ships.refuel({symbol: process.argv[3]}));
|
||||
break;
|
||||
case 'ships.sell':
|
||||
api.debugLog(await ships.sell({ship: process.argv[3], good: process.argv[4], units: process.argv[5]}));
|
||||
api.debugLog(await ships.sell({symbol: process.argv[3], good: process.argv[4], units: process.argv[5]}));
|
||||
break;
|
||||
case 'ships.ship':
|
||||
api.debugLog(await ships.ship({ship: process.argv[3]}));
|
||||
api.debugLog(await ships.ship({symbol: process.argv[3]}));
|
||||
break;
|
||||
case 'ships.survey':
|
||||
api.debugLog(await ships.survey({ship: process.argv[3]}));
|
||||
api.debugLog(await ships.survey({symbol: process.argv[3]}));
|
||||
break;
|
||||
case 'systems.asteroids':
|
||||
api.debugLog(await systems.type({symbol: process.argv[3], type: 'ASTEROID_FIELD'}));
|
||||
|
|
Loading…
Add table
Reference in a new issue