skamkraft_proto
ambigipathyv ago%!(EXTRA string=1 year)
parent 81277ff164
commit ff3f7235f3
  1. 17
      js/skama_code/api/agent.js
  2. 2
      js/skama_code/api/config.js
  3. 56
      js/skama_code/api/system.js
  4. 5
      js/skama_code/commun/my.js

@ -4,6 +4,8 @@ import { SpaceTraders } from "./config.js"
export class My { export class My {
static agent = null; static agent = null;
static temp_engine = null;
static canvas_renderer = null;
} }
export class Agent { export class Agent {
@ -23,6 +25,11 @@ export class Agent {
} }
export class AgentBuilder { export class AgentBuilder {
constructor(end = false) {
this.stopped = false;
this.end = end;
}
static create(symbol, faction, callback, error_handler) { static create(symbol, faction, callback, error_handler) {
const url = `${SpaceTraders.host}/register`; const url = `${SpaceTraders.host}/register`;
$.ajax({ $.ajax({
@ -94,22 +101,22 @@ export class AgentBuilder {
}); });
} }
static list_all(callback, end = false) { static list_all(callback) {
this.list(20, 1, (agents, meta) => { this.list(20, 1, (agents, meta) => {
let maxPage = meta.total / 20; let maxPage = meta.total / 20;
this.#r_listing(2, maxPage, agents, callback, end); this.#r_listing(2, maxPage, agents, callback);
}); });
} }
static #r_listing(page, maxPage, agents, callback, end) { static #r_listing(page, maxPage, agents, callback) {
if (page < maxPage) { if (page < maxPage) {
this.list(20, page++,() => { this.list(20, page++,() => {
setTimeout(() => { setTimeout(() => {
if (!end) { if (!this.end) {
callback(agents); callback(agents);
agents = []; agents = [];
} }
this.#r_listing(page++, maxPage, agents, callback, end); if (!this.stopped) this.#r_listing(page++, maxPage, agents, callback, end);
}, 1000); }, 1000);
}, agents); }, agents);
} else { } else {

@ -3,5 +3,5 @@
export const SpaceTraders = { export const SpaceTraders = {
host: "https://api.spacetraders.io/v2", host: "https://api.spacetraders.io/v2",
limit_max: 20, timing: 1000,
} }

@ -12,9 +12,15 @@ export class System {
this.type = data.type; this.type = data.type;
this.position = new Position(data.x, data.y); this.position = new Position(data.x, data.y);
this.factions = data.factions; this.factions = data.factions;
this.stopped = false;
this.end = false;
} }
get(name, callback, error_handler) { when_end() {
this.end = true;
}
get_planet(name, callback, error_handler) {
const url = `${SpaceTraders.host}/systems/${this.name}/waypoints/${name}`; const url = `${SpaceTraders.host}/systems/${this.name}/waypoints/${name}`;
$.ajax({ $.ajax({
url: url, url: url,
@ -29,7 +35,7 @@ export class System {
}); });
} }
list(limit, page, callback, planets = []) { list_planets(limit, page, callback, planets = []) {
const url = `${SpaceTraders.host}/systems/${this.name}/waypoints` const url = `${SpaceTraders.host}/systems/${this.name}/waypoints`
$.ajax({ $.ajax({
url: url, url: url,
@ -47,23 +53,27 @@ export class System {
}); });
} }
list_all(callback, end = false) { stop() {
this.list(20, 1, (planets, meta) => { this.stopped = true;
}
list_all_planets(callback, end = false) {
this.list_planets(20, 1, (planets, meta) => {
let maxPage = meta.total / 20; let maxPage = meta.total / 20;
this.#r_listing(2, maxPage, planets, callback, end); this.#r_listing(2, maxPage, planets, callback, end);
}); });
} }
#r_listing(page, maxPage, planets, callback, end) { #r_listing(page, maxPage, planets, callback) {
if (page < maxPage) { if (page < maxPage) {
this.list(20, page++, () => { this.list_planets(20, page++, () => {
setTimeout(() => { setTimeout(() => {
if (!end) { if (!end) {
callback(planets); callback(planets);
planets = []; planets = [];
} }
this.#r_listing(page++, maxPage, planets, callback, end); if (!this.stopped) this.#r_listing(page++, maxPage, planets, callback, end);
}, 1000); }, SpaceTraders.timing);
}, planets); }, planets);
} else { } else {
callback(planets); callback(planets);
@ -72,6 +82,13 @@ export class System {
} }
export class SystemBuilder { export class SystemBuilder {
constructor(end = false) {
this.stopped = false;
this.end = end;
this.page = 1;
}
static parse_system_name(name) { static parse_system_name(name) {
return name.split("-").slice(-1, 2).join("-"); return name.split("-").slice(-1, 2).join("-");
} }
@ -109,24 +126,27 @@ export class SystemBuilder {
}); });
} }
static list_all(callback, end = false) { stop() {
this.list(20, 1, (systems, meta) => { this.stopped = true;
let maxPage = meta.total / 20; }
this.#r_listing(2, maxPage, systems, callback, end);
list_all(callback) {
SystemBuilder.list(20, this.page, (systems, meta) => {
this.max_page = meta.total / 20;
this.#r_listing(systems, callback);
}); });
} }
static #r_listing(page, maxPage, systems, callback, end) { #r_listing(systems, callback) {
if (page < maxPage) { if (page < maxPage) {
this.list(20, page++, () => { SystemBuilder.list(20, this.page++, () => {
setTimeout(() => { setTimeout(() => {
if (!end) { if (!this.end) {
console.log(systems);
callback(systems); callback(systems);
systems = []; systems = [];
} }
this.#r_listing(page++, maxPage, systems, callback, end); if (!this.stopped) this.#r_listing(page++, maxPage, systems, callback);
}, 1000); }, SpaceTraders.timing);
}, systems); }, systems);
} else { } else {
callback(systems); callback(systems);

@ -0,0 +1,5 @@
export class My {
static agent = null;
static temp_engine = null;
static canvas_renderer = null;
}
Loading…
Cancel
Save