summaryrefslogtreecommitdiff
path: root/lib/systems.js
diff options
context:
space:
mode:
authorJulien Dessaux2023-05-24 23:03:19 +0200
committerJulien Dessaux2023-05-24 23:03:19 +0200
commit9963ab79b7c7392d5f87c98cd55953ffa004efd9 (patch)
treeefcd42cbe0f2614c863a05c77d797c7a4645b003 /lib/systems.js
parentImplemented a basic extraction loop (diff)
downloadspacetraders-9963ab79b7c7392d5f87c98cd55953ffa004efd9.tar.gz
spacetraders-9963ab79b7c7392d5f87c98cd55953ffa004efd9.tar.bz2
spacetraders-9963ab79b7c7392d5f87c98cd55953ffa004efd9.zip
Rewrote the api rate limiter with promises instead of callbacks
Diffstat (limited to '')
-rw-r--r--lib/systems.js36
1 files changed, 36 insertions, 0 deletions
diff --git a/lib/systems.js b/lib/systems.js
new file mode 100644
index 0000000..ad354e4
--- /dev/null
+++ b/lib/systems.js
@@ -0,0 +1,36 @@
+import * as api from './api.js';
+import * as db from '../database/systems.js';
+
+// Retrieves a list of waypoints that have a specific ctx.trait like a SHIPYARD or a MARKETPLACE in the system ctx.symbol
+export async function trait(ctx) {
+ const s = await getSystem(ctx);
+ return s.filter(s => s.traits.some(t => t.symbol === ctx.trait));
+}
+
+// Retrieves a list of waypoints that have a specific ctx.type like ASTEROID_FIELD in the system ctx.symbol
+export async function type(ctx, response) {
+ const s = await getSystem(ctx);
+ return s.filter(s => s.type === ctx.type);
+}
+
+// Retrieves the system's information for ctx.symbol and cache it in the database
+async function getSystem(ctx) {
+ let s = db.getSystem(ctx.symbol);
+ if (s === null) {
+ const response = await api.send({endpoint: `/systems/${ctx.symbol}/waypoints?limit=20&page=1`});
+ if (response.error !== undefined) {
+ switch(response.error.code) {
+ case 404:
+ throw `Error retrieving waypoints for system ${ctx.symbol}: ${response.error.message}`;
+ default: // yet unhandled error
+ throw response;
+ }
+ }
+ if (response.meta !== undefined && response.meta.total > response.meta.limit) {
+ throw `Error retrieving waypoints for system ${ctx.symbol}: Pagination is not implemented yet!`;
+ }
+ s = response.data;
+ db.setSystem(ctx.symbol, s);
+ }
+ return s;
+}