[node] implement shipyard info retrieval and database caching
This commit is contained in:
parent
ccbfd9deb9
commit
4cb5c7853c
5 changed files with 81 additions and 10 deletions
6
nodejs/database/005_shipyards.sql
Normal file
6
nodejs/database/005_shipyards.sql
Normal file
|
@ -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'));
|
27
nodejs/database/shipyards.ts
Normal file
27
nodejs/database/shipyards.ts
Normal file
|
@ -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,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,17 +4,22 @@ import {
|
||||||
sendPaginated,
|
sendPaginated,
|
||||||
} from './api.ts';
|
} from './api.ts';
|
||||||
import * as dbMarkets from '../database/markets.ts';
|
import * as dbMarkets from '../database/markets.ts';
|
||||||
|
import * as dbShipyards from '../database/shipyards.ts';
|
||||||
import * as dbSystems from '../database/systems.ts';
|
import * as dbSystems from '../database/systems.ts';
|
||||||
import {
|
import {
|
||||||
Market,
|
Market,
|
||||||
|
Shipyard,
|
||||||
System,
|
System,
|
||||||
Waypoint,
|
Waypoint,
|
||||||
} from './types.ts'
|
} from './types.ts'
|
||||||
import { systemFromWaypoint } from './utils.ts';
|
import {
|
||||||
|
isThereAShipAtThisWaypoint,
|
||||||
|
systemFromWaypoint,
|
||||||
|
} from './utils.ts';
|
||||||
|
|
||||||
export async function market(waypoint: Waypoint): Promise<Market> {
|
export async function market(waypoint: Waypoint): Promise<Market> {
|
||||||
const data = dbMarkets.getMarketAtWaypoint(waypoint.symbol);
|
const data = dbMarkets.getMarketAtWaypoint(waypoint.symbol);
|
||||||
if (data) { return data; }
|
if (data && (data.tradeGoods || !isThereAShipAtThisWaypoint(waypoint))) { return data; }
|
||||||
const systemSymbol = systemFromWaypoint(waypoint.symbol);
|
const systemSymbol = systemFromWaypoint(waypoint.symbol);
|
||||||
let response = await send<Market>({endpoint: `/systems/${systemSymbol}/waypoints/${waypoint.symbol}/market`});
|
let response = await send<Market>({endpoint: `/systems/${systemSymbol}/waypoints/${waypoint.symbol}/market`});
|
||||||
if (response.error) {
|
if (response.error) {
|
||||||
|
@ -25,11 +30,18 @@ export async function market(waypoint: Waypoint): Promise<Market> {
|
||||||
return response.data;
|
return response.data;
|
||||||
}
|
}
|
||||||
|
|
||||||
//export async function shipyard(waypoint: string): Promise<unknown> {
|
export async function shipyard(waypoint: Waypoint): Promise<Shipyard> {
|
||||||
// // TODO database caching
|
const data = dbShipyards.get(waypoint.symbol);
|
||||||
// const systemSymbol = systemFromWaypoint(waypoint);
|
if (data && (data.ships || !isThereAShipAtThisWaypoint(waypoint))) { return data; }
|
||||||
// return await send({endpoint: `/systems/${systemSymbol}/waypoints/${waypoint}/shipyard`});
|
const systemSymbol = systemFromWaypoint(waypoint.symbol);
|
||||||
//}
|
const response = await send<Shipyard>({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<System> {
|
export async function system(symbol: string): Promise<System> {
|
||||||
let data = dbSystems.getSystem(symbol);
|
let data = dbSystems.getSystem(symbol);
|
||||||
|
|
|
@ -80,6 +80,29 @@ export type RouteEndpoint = {
|
||||||
y: number;
|
y: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type Shipyard = {
|
||||||
|
modificationFee: number;
|
||||||
|
ships: Array<ShipyardShip>;
|
||||||
|
shipTypes: Array<{type: string}>;
|
||||||
|
symbol: string;
|
||||||
|
//transactions: Array<Transaction>;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type ShipyardShip = {
|
||||||
|
activity: string;
|
||||||
|
// crew
|
||||||
|
description: string;
|
||||||
|
// engine
|
||||||
|
// frame
|
||||||
|
// modules
|
||||||
|
// mounts
|
||||||
|
name: string;
|
||||||
|
purchasePrice: number;
|
||||||
|
// reactor
|
||||||
|
supply: string;
|
||||||
|
type: string;
|
||||||
|
};
|
||||||
|
|
||||||
export type System = {
|
export type System = {
|
||||||
symbol: string;
|
symbol: string;
|
||||||
sectorSymbol: string;
|
sectorSymbol: string;
|
||||||
|
|
|
@ -2,9 +2,8 @@ import {
|
||||||
debugLog,
|
debugLog,
|
||||||
} from './api.ts';
|
} from './api.ts';
|
||||||
import { PriorityQueue } from './priority_queue.ts';
|
import { PriorityQueue } from './priority_queue.ts';
|
||||||
import {
|
import { getShips } from './ships.ts';
|
||||||
market,
|
import { market } from './systems.ts';
|
||||||
} from './systems.ts';
|
|
||||||
import {
|
import {
|
||||||
Cargo,
|
Cargo,
|
||||||
CargoManifest,
|
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);
|
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<T extends Point>(a: Point, points: Array<T>): Array<{data: T, distance: number}>{
|
export function sortByDistanceFrom<T extends Point>(a: Point, points: Array<T>): Array<{data: T, distance: number}>{
|
||||||
let result = points.map(function (m) { return {
|
let result = points.map(function (m) { return {
|
||||||
data: m,
|
data: m,
|
||||||
|
|
Loading…
Add table
Reference in a new issue