diff --git a/js/skama_code/api/agent.js b/js/skama_code/api/agent.js index f3d379a..67cf836 100644 --- a/js/skama_code/api/agent.js +++ b/js/skama_code/api/agent.js @@ -4,6 +4,8 @@ import { SpaceTraders } from "./config.js" export class My { static agent = null; + static temp_engine = null; + static canvas_renderer = null; } export class Agent { @@ -23,6 +25,11 @@ export class Agent { } export class AgentBuilder { + constructor(end = false) { + this.stopped = false; + this.end = end; + } + static create(symbol, faction, callback, error_handler) { const url = `${SpaceTraders.host}/register`; $.ajax({ @@ -94,22 +101,22 @@ export class AgentBuilder { }); } - static list_all(callback, end = false) { + static list_all(callback) { this.list(20, 1, (agents, meta) => { 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) { this.list(20, page++,() => { setTimeout(() => { - if (!end) { + if (!this.end) { callback(agents); agents = []; } - this.#r_listing(page++, maxPage, agents, callback, end); + if (!this.stopped) this.#r_listing(page++, maxPage, agents, callback, end); }, 1000); }, agents); } else { diff --git a/js/skama_code/api/config.js b/js/skama_code/api/config.js index 8658ac2..322ae37 100644 --- a/js/skama_code/api/config.js +++ b/js/skama_code/api/config.js @@ -3,5 +3,5 @@ export const SpaceTraders = { host: "https://api.spacetraders.io/v2", - limit_max: 20, + timing: 1000, } \ No newline at end of file diff --git a/js/skama_code/api/system.js b/js/skama_code/api/system.js index 6f9ad8a..14053af 100644 --- a/js/skama_code/api/system.js +++ b/js/skama_code/api/system.js @@ -12,9 +12,15 @@ export class System { this.type = data.type; this.position = new Position(data.x, data.y); 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}`; $.ajax({ 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` $.ajax({ url: url, @@ -47,23 +53,27 @@ export class System { }); } - list_all(callback, end = false) { - this.list(20, 1, (planets, meta) => { + stop() { + this.stopped = true; + } + + list_all_planets(callback, end = false) { + this.list_planets(20, 1, (planets, meta) => { let maxPage = meta.total / 20; this.#r_listing(2, maxPage, planets, callback, end); }); } - #r_listing(page, maxPage, planets, callback, end) { + #r_listing(page, maxPage, planets, callback) { if (page < maxPage) { - this.list(20, page++, () => { + this.list_planets(20, page++, () => { setTimeout(() => { if (!end) { callback(planets); planets = []; } - this.#r_listing(page++, maxPage, planets, callback, end); - }, 1000); + if (!this.stopped) this.#r_listing(page++, maxPage, planets, callback, end); + }, SpaceTraders.timing); }, planets); } else { callback(planets); @@ -72,6 +82,13 @@ export class System { } export class SystemBuilder { + constructor(end = false) { + this.stopped = false; + this.end = end; + this.page = 1; + + } + static parse_system_name(name) { return name.split("-").slice(-1, 2).join("-"); } @@ -109,24 +126,27 @@ export class SystemBuilder { }); } - static list_all(callback, end = false) { - this.list(20, 1, (systems, meta) => { - let maxPage = meta.total / 20; - this.#r_listing(2, maxPage, systems, callback, end); + stop() { + this.stopped = true; + } + + 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) { - this.list(20, page++, () => { + SystemBuilder.list(20, this.page++, () => { setTimeout(() => { - if (!end) { - console.log(systems); + if (!this.end) { callback(systems); systems = []; } - this.#r_listing(page++, maxPage, systems, callback, end); - }, 1000); + if (!this.stopped) this.#r_listing(page++, maxPage, systems, callback); + }, SpaceTraders.timing); }, systems); } else { callback(systems); diff --git a/js/skama_code/commun/my.js b/js/skama_code/commun/my.js new file mode 100644 index 0000000..00770e2 --- /dev/null +++ b/js/skama_code/commun/my.js @@ -0,0 +1,5 @@ +export class My { + static agent = null; + static temp_engine = null; + static canvas_renderer = null; +} \ No newline at end of file