[javascript] Implementing market data gathering and caching
This commit is contained in:
parent
0377c99a54
commit
1b1df83ffd
4 changed files with 65 additions and 7 deletions
7
nodejs/database/004_markets.sql
Normal file
7
nodejs/database/004_markets.sql
Normal file
|
@ -0,0 +1,7 @@
|
|||
CREATE TABLE markets (
|
||||
id INTEGER PRIMARY KEY,
|
||||
system TEXT NOT NULL,
|
||||
data JSON NOT NULL
|
||||
);
|
||||
CREATE INDEX markets_system on markets (system);
|
||||
CREATE UNIQUE INDEX markets_symbol on markets(json_extract(data, '$.symbol'));
|
|
@ -6,6 +6,7 @@ const allMigrations = [
|
|||
'database/001_systems.sql',
|
||||
'database/002_ships.sql',
|
||||
'database/003_surveys.sql',
|
||||
'database/004_markets.sql',
|
||||
];
|
||||
|
||||
const db = new Database(
|
||||
|
|
34
nodejs/database/markets.js
Normal file
34
nodejs/database/markets.js
Normal file
|
@ -0,0 +1,34 @@
|
|||
import db from './db.js';
|
||||
import * as utils from '../lib/utils.js';
|
||||
|
||||
const addMarketStatement = db.prepare(`INSERT INTO markets(system, data) VALUES (?, json(?));`);
|
||||
const getMarketAtWaypointStatement = db.prepare(`SELECT data FROM markets WHERE data->>'symbol' = ?;`);
|
||||
const getMarketsInSystemStatement = db.prepare(`SELECT data FROM markets WHERE system = ?;`);
|
||||
const updateMarketStatement = db.prepare(`UPDATE markets SET data = json(:data) WHERE data->>'symbol' = :symbol;`);
|
||||
|
||||
export function getMarketAtWaypoint(symbol) {
|
||||
const data = getMarketAtWaypointStatement.get(symbol);
|
||||
if (data === undefined) {
|
||||
return null;
|
||||
}
|
||||
return JSON.parse(data.data);
|
||||
}
|
||||
|
||||
export function getMarketsInSystem(symbol) {
|
||||
const data = getMarketsInSystemStatement.get(symbol);
|
||||
if (data === undefined) {
|
||||
return null;
|
||||
}
|
||||
return JSON.parse(data.data);
|
||||
}
|
||||
|
||||
export function setMarket(data) {
|
||||
if (getMarketAtWaypoint(data.symbol) === null) {
|
||||
const system = utils.systemFromWaypoint(data.symbol);
|
||||
return addMarketStatement.run(system, JSON.stringify(data)).lastInsertRowid;
|
||||
}
|
||||
return updateMarketStatement.run({
|
||||
data: JSON.stringify(data),
|
||||
symbol: data.symbol,
|
||||
}).changes;
|
||||
}
|
|
@ -1,18 +1,34 @@
|
|||
import * as api from './api.js';
|
||||
import * as db from '../database/systems.js';
|
||||
import * as dbMarkets from '../database/markets.js';
|
||||
import * as dbShips from '../database/ships.js';
|
||||
import * as dbSystems from '../database/systems.js';
|
||||
import * as utils from './utils.js';
|
||||
|
||||
// Retrieves a marketplace's market data for waypointSymbol
|
||||
export async function market(waypointSymbol) {
|
||||
const data = dbMarkets.getMarketAtWaypoint(waypointSymbol);
|
||||
if (data === null) {
|
||||
if (dbShips.getShipsAt(waypointSymbol) === null) {
|
||||
return null;
|
||||
}
|
||||
const systemSymbol = utils.systemFromWaypoint(waypointSymbol);
|
||||
let d = await api.send({endpoint: `/systems/${systemSymbol}/waypoints/${waypointSymbol}/market`});
|
||||
delete d.data.transactions;
|
||||
dbMarkets.setMarket(d.data);
|
||||
return d;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
// Retrieves a shipyard's information for ctx.symbol
|
||||
export async function shipyard(ctx) {
|
||||
const systemSymbol = utils.systemFromWaypoint(ctx.symbol);
|
||||
console.log(systemSymbol);
|
||||
return await api.send({endpoint: `/systems/${systemSymbol}/waypoints/${ctx.symbol}/shipyard`});
|
||||
}
|
||||
|
||||
// Retrieves the system's information for ctx.symbol and caches it in the database
|
||||
export async function system(ctx) {
|
||||
let s = db.getSystem(ctx.symbol);
|
||||
let s = dbSystems.getSystem(ctx.symbol);
|
||||
if (s === null) {
|
||||
const response = await api.send({endpoint: `/systems/${ctx.symbol}`});
|
||||
if (response.error !== undefined) {
|
||||
|
@ -24,7 +40,7 @@ export async function system(ctx) {
|
|||
}
|
||||
}
|
||||
s = response.data;
|
||||
db.setSystem(s);
|
||||
dbSystems.setSystem(s);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
@ -44,7 +60,7 @@ export async function type(ctx, response) {
|
|||
// Retrieves the system's information for ctx.symbol and caches it in the database
|
||||
export async function waypoints(ctx) {
|
||||
await system(ctx);
|
||||
let updated = db.getSystemUpdated(ctx.symbol);
|
||||
let updated = dbSystems.getSystemUpdated(ctx.symbol);
|
||||
// TODO handle uncharted systems
|
||||
if (updated === null) {
|
||||
let waypoints = [];
|
||||
|
@ -63,8 +79,8 @@ export async function waypoints(ctx) {
|
|||
break;
|
||||
}
|
||||
}
|
||||
db.setSystemWaypoints(ctx.symbol, waypoints);
|
||||
dbSystems.setSystemWaypoints(ctx.symbol, waypoints);
|
||||
return waypoints;
|
||||
}
|
||||
return db.getSystem(ctx.symbol).waypoints;
|
||||
return dbSystems.getSystem(ctx.symbol).waypoints;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue