summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulien Dessaux2023-09-13 23:38:24 +0200
committerJulien Dessaux2023-09-13 23:38:24 +0200
commit14f81bcc1b50116118bd421e2cdea7050fc1a890 (patch)
tree4132f05e3909572cf9ffc3d5639b13125321f801
parent[javascript] added database function to fetch all the ships present in a spec... (diff)
downloadspacetraders-14f81bcc1b50116118bd421e2cdea7050fc1a890.tar.gz
spacetraders-14f81bcc1b50116118bd421e2cdea7050fc1a890.tar.bz2
spacetraders-14f81bcc1b50116118bd421e2cdea7050fc1a890.zip
[javascript] reworked basic exploration with systems fetch after registering
-rw-r--r--nodejs/automation/exploration.js22
-rw-r--r--nodejs/database/systems.js9
2 files changed, 15 insertions, 16 deletions
diff --git a/nodejs/automation/exploration.js b/nodejs/automation/exploration.js
index b35efe0..e4f24f1 100644
--- a/nodejs/automation/exploration.js
+++ b/nodejs/automation/exploration.js
@@ -1,21 +1,11 @@
-import * as db from '../database/systems.js';
+import db from '../database/db.js';
+import * as dbSystems from '../database/systems.js';
import * as api from '../lib/api.js';
-// Retrieves all systems information, should be called only once after registering
export async function init() {
- if (db.isInit()) {
- return;
+ const response = await api.send({endpoint: `/systems`, page: Math.max(1, Math.floor(dbSystems.getSystemsCount()/20)), priority: 100});
+ if (response.error !== undefined) {
+ throw response;
}
- for (let page=1; true; ++page) {
- const response = await api.send({endpoint: `/systems?limit=20&page=${page}`, priority:100});
- if (response.error !== undefined) {
- throw response;
- }
- response.data.forEach(system => db.setSystem(system));
- if (response.meta.total <= response.meta.limit * page) {
- break;
- }
- }
- console.log('Finished retrieving all systems information');
- db.init();
+ db.transaction(() => response.forEach(function(system) { try { dbSystems.addSystem(system); } catch {} }))();
}
diff --git a/nodejs/database/systems.js b/nodejs/database/systems.js
index 6cfc75a..dd70d33 100644
--- a/nodejs/database/systems.js
+++ b/nodejs/database/systems.js
@@ -3,6 +3,7 @@ import db from './db.js';
const addSystemStatement = db.prepare(`INSERT INTO systems(data) VALUES (json(?));`);
const getSystemStatement = db.prepare(`SELECT data FROM systems WHERE data->>'symbol' = ?;`);
const getSystemUpdatedStatement = db.prepare(`SELECT updated FROM systems WHERE data->>'symbol' = ?;`);
+const getSystemsCountStatement = db.prepare(`SELECT COUNT(data) as data FROM systems;`);
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 addSystem(data) {
@@ -17,6 +18,14 @@ export function getSystem(symbol) {
return JSON.parse(data.data);
}
+export function getSystemsCount() {
+ const data = getSystemsCountStatement.get();
+ if (data === undefined) {
+ return null;
+ }
+ return data.data;
+}
+
export function getSystemUpdated(symbol) {
const updated = getSystemUpdatedStatement.get(symbol);
if (updated === undefined) {