From 4cb5c7853c75eb3d9e99a525065f0816ebb5dd62 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Mon, 20 May 2024 22:50:33 +0200 Subject: [node] implement shipyard info retrieval and database caching --- nodejs/database/005_shipyards.sql | 6 ++++++ nodejs/database/shipyards.ts | 27 +++++++++++++++++++++++++++ nodejs/lib/systems.ts | 26 +++++++++++++++++++------- nodejs/lib/types.ts | 23 +++++++++++++++++++++++ nodejs/lib/utils.ts | 9 ++++++--- 5 files changed, 81 insertions(+), 10 deletions(-) create mode 100644 nodejs/database/005_shipyards.sql create mode 100644 nodejs/database/shipyards.ts (limited to 'nodejs') diff --git a/nodejs/database/005_shipyards.sql b/nodejs/database/005_shipyards.sql new file mode 100644 index 0000000..e4d3f28 --- /dev/null +++ b/nodejs/database/005_shipyards.sql @@ -0,0 +1,6 @@ +CREATE TABLE shipyards ( + id INTEGER PRIMARY KEY, + data JSON NOT NULL, + updated DATE DEFAULT NULL +); +CREATE UNIQUE INDEX shipyards_data_symbol on shipyards (json_extract(data, '$.symbol')); diff --git a/nodejs/database/shipyards.ts b/nodejs/database/shipyards.ts new file mode 100644 index 0000000..4c243f9 --- /dev/null +++ b/nodejs/database/shipyards.ts @@ -0,0 +1,27 @@ +import { DbData, db } from './db.ts'; +import { Shipyard } from '../lib/types'; + +const addStatement = db.prepare(`INSERT INTO shipyards(data, updated) VALUES (json(:data), :date);`); +const getStatement = db.prepare(`SELECT data FROM shipyards WHERE data->>'symbol' = ?;`); +const updateStatement = db.prepare(`UPDATE shipyards SET data = json(:data), updated = :date WHERE data->>'symbol' = :symbol;`); + +export function get(symbol: string): Shipyard|null { + const data = getStatement.get(symbol) as DbData|undefined; + if (!data) return null; + return JSON.parse(data.data); +} + +export function set(data: Shipyard): void { + if (get(data.symbol) === null) { + addStatement.run({ + data: JSON.stringify(data), + date: new Date().toISOString(), + }); + } else { + updateStatement.run({ + data: JSON.stringify(data), + date: new Date().toISOString(), + symbol: data.symbol, + }); + } +} diff --git a/nodejs/lib/systems.ts b/nodejs/lib/systems.ts index d4b3be5..6f0a830 100644 --- a/nodejs/lib/systems.ts +++ b/nodejs/lib/systems.ts @@ -4,17 +4,22 @@ import { sendPaginated, } from './api.ts'; import * as dbMarkets from '../database/markets.ts'; +import * as dbShipyards from '../database/shipyards.ts'; import * as dbSystems from '../database/systems.ts'; import { Market, + Shipyard, System, Waypoint, } from './types.ts' -import { systemFromWaypoint } from './utils.ts'; +import { + isThereAShipAtThisWaypoint, + systemFromWaypoint, +} from './utils.ts'; export async function market(waypoint: Waypoint): Promise { const data = dbMarkets.getMarketAtWaypoint(waypoint.symbol); - if (data) { return data; } + if (data && (data.tradeGoods || !isThereAShipAtThisWaypoint(waypoint))) { return data; } const systemSymbol = systemFromWaypoint(waypoint.symbol); let response = await send({endpoint: `/systems/${systemSymbol}/waypoints/${waypoint.symbol}/market`}); if (response.error) { @@ -25,11 +30,18 @@ export async function market(waypoint: Waypoint): Promise { return response.data; } -//export async function shipyard(waypoint: string): Promise { -// // TODO database caching -// const systemSymbol = systemFromWaypoint(waypoint); -// return await send({endpoint: `/systems/${systemSymbol}/waypoints/${waypoint}/shipyard`}); -//} +export async function shipyard(waypoint: Waypoint): Promise { + const data = dbShipyards.get(waypoint.symbol); + if (data && (data.ships || !isThereAShipAtThisWaypoint(waypoint))) { return data; } + const systemSymbol = systemFromWaypoint(waypoint.symbol); + const response = await send({endpoint: `/systems/${systemSymbol}/waypoints/${waypoint.symbol}/shipyard`}); + if (response.error) { + debugLog(response); + throw response; + } + dbShipyards.set(response.data); + return response.data; +} export async function system(symbol: string): Promise { let data = dbSystems.getSystem(symbol); diff --git a/nodejs/lib/types.ts b/nodejs/lib/types.ts index 4f15d70..e03e5a7 100644 --- a/nodejs/lib/types.ts +++ b/nodejs/lib/types.ts @@ -80,6 +80,29 @@ export type RouteEndpoint = { y: number; }; +export type Shipyard = { + modificationFee: number; + ships: Array; + shipTypes: Array<{type: string}>; + symbol: string; + //transactions: Array; +}; + +export type ShipyardShip = { + activity: string; + // crew + description: string; + // engine + // frame + // modules + // mounts + name: string; + purchasePrice: number; + // reactor + supply: string; + type: string; +}; + export type System = { symbol: string; sectorSymbol: string; diff --git a/nodejs/lib/utils.ts b/nodejs/lib/utils.ts index db39349..ec3ab09 100644 --- a/nodejs/lib/utils.ts +++ b/nodejs/lib/utils.ts @@ -2,9 +2,8 @@ import { debugLog, } from './api.ts'; import { PriorityQueue } from './priority_queue.ts'; -import { - market, -} from './systems.ts'; +import { getShips } from './ships.ts'; +import { market } from './systems.ts'; import { Cargo, CargoManifest, @@ -41,6 +40,10 @@ export function distance(a: Point, b: Point) { return Math.sqrt((a.x-b.x)**2 + (a.y-b.y)**2); } +export function isThereAShipAtThisWaypoint(waypoint: Waypoint): boolean { + return getShips().some(s => s.nav.waypointSymbol === waypoint.symbol); +} + export function sortByDistanceFrom(a: Point, points: Array): Array<{data: T, distance: number}>{ let result = points.map(function (m) { return { data: m, -- cgit v1.2.3