diff --git a/js/api/agent.js b/js/api/agent.js index e240afe..58d3430 100644 --- a/js/api/agent.js +++ b/js/api/agent.js @@ -19,9 +19,9 @@ export class Agent { } export class AgentBuilder { - static async create(symbol, faction, callback, error_handler, email = "") { - const url = `${SpaceTraders.host}register`; - await $.ajax({ + static create(symbol, faction, callback, error_handler, email = "") { + const url = `${SpaceTraders.host}/register`; + $.ajax({ url: url, method: "POST", headers: { "Content-Type": "application/json" }, @@ -37,9 +37,9 @@ export class AgentBuilder { }); } - static async get(token, callback, error_handler){ - const url = `${SpaceTraders.host}my/agent`; - await $.ajax({ + static get(token, callback, error_handler){ + const url = `${SpaceTraders.host}/my/agent`; + $.ajax({ url: url, method: "GET", headers: { @@ -56,9 +56,9 @@ export class AgentBuilder { }); } - static async get_public(symbol, callback) { - const url = `${SpaceTraders.host}agents/${symbol}`; - await $.ajax({ + static get_public(symbol, callback) { + const url = `${SpaceTraders.host}/agents/${symbol}`; + $.ajax({ url: url, method: "GET", headers: { @@ -71,10 +71,10 @@ export class AgentBuilder { }); } - static async list(limit, page, callback, agents = []) { + static list(limit, page, callback, agents = []) { const url = `${SpaceTraders.host}agents`; const data = { limit, page }; - await $.ajax({ + $.ajax({ url: url, method: "GET", headers: { @@ -90,14 +90,14 @@ export class AgentBuilder { }); } - static async list_all(callback) { - await this.list(1,1, (agents, meta) => { + static list_all(callback) { + this.list(1,1, (agents, meta) => { let maxPage = meta.total / 20; this.#r_listing(1, maxPage, [], callback); }); } - static async #r_listing(page, maxPage, agents, callback) { + static #r_listing(page, maxPage, agents, callback) { if (page < maxPage) { this.list(20, page++,() => { setTimeout(() => { diff --git a/js/api/config.js b/js/api/config.js index bb262fb..0b413af 100644 --- a/js/api/config.js +++ b/js/api/config.js @@ -1,5 +1,5 @@ 'use strict'; export const SpaceTraders = { - host: "https://api.spacetraders.io/v2/", + host: "https://api.spacetraders.io/v2", limit_max: 20, } \ No newline at end of file diff --git a/js/api/planet.js b/js/api/planet.js index b08c47c..55336dc 100644 --- a/js/api/planet.js +++ b/js/api/planet.js @@ -1,30 +1,103 @@ -class Position { - constructor(x, y) { - this.x = x; - this.y = y; +import { SpaceTraders } from "./config.js"; +import { Position } from "../commun/position.js"; + +class Market { + constructor(market) { + this.symbol = market.symbol; + this.exports = market.exports; + this.imports = market.imports; + this.exchange = market.exchange; + this.transctions = market.transctions; + this.trade_goods = market.tradeGoods; + } + + has_export(market_export) { + + } + + list_exports(callback) { + + } + + has_import(market_import) { + } - get_canvas_pos(w, h) { - return new Position(x - w/2, y - h/2) + list_import(callback) { + } } export class Planet { - constructor(waypoint) { - this.symbol = waypoint.symbol; - this.type = waypoint.type; - this.system = waypoint.systemSymbol; - this.position = new Position(x, y); - this.moons = waypoint.orbitals; - this.orbits = waypoint.orbits; - this.faction = waypoint.faction; - this.traits = waypoint.traits; - this.dangers = waypoint.modifiers; - this.discovery = waypoint.char; - this.is_under_construction = waypoint.isUnderConstruction; - } + constructor(waypoint) { + this.name = waypoint.symbol; + this.type = waypoint.type; + this.system = waypoint.systemSymbol; + this.position = new Position(waypoint.x, waypoint.y); + this.moons = waypoint.orbitals; + this.orbits = waypoint.orbits; + this.faction = waypoint.faction; + this.traits = waypoint.traits; + this.dangers = waypoint.modifiers; + this.discovery = waypoint.char; + this.is_under_construction = waypoint.isUnderConstruction; + } + + get_market() { + const url = `${SpaceTraders.host}/systems/${this.system}/waypoints/${this.name}/market`; + $.ajax({ + url: url, + method: "GET", + success: (reponse) => { + let market = new Market(reponse.data); + callback(market); + }, + error: (err) => { + error_handler("Planet not found"); + } + }); + } + + is_type(type) { + return this.type === type ? true : false; + } + + is_discovered() { + return this.discovery.length > 0 ? true : false; + } } export class PlanetBuilder { - + static get(system, name, callback, error_handler) { + const url = `${SpaceTraders.host}/systems/${system}/waypoints/${name}`; + $.ajax({ + url: url, + method: "GET", + success: (reponse) => { + let planet = new Planet(reponse.data); + callback(planet); + }, + error: (err) => { + error_handler("Planet not found"); + } + }); + } + + static list(system, limit, page, planets = []) { + const url = `${SpaceTraders.host}/systems/${system}/waypoints` + $.ajax({ + url: url, + method: "GET", + + }); + } + + static list_all(system, callback, error_handler) { + const url = `${SpaceTraders.host}/systems/${system}/waypoints`; + + } + + static #r_listing() { + + } } \ No newline at end of file diff --git a/js/api/ship.js b/js/api/ship.js deleted file mode 100644 index e69de29..0000000 diff --git a/js/commun/position.js b/js/commun/position.js new file mode 100644 index 0000000..fdfc929 --- /dev/null +++ b/js/commun/position.js @@ -0,0 +1,10 @@ +export class Position { + constructor(x, y) { + this.x = x; + this.y = y; + } + + get_canvas_pos(w, h) { + return new Position(x - w/2, y - h/2) + } +} \ No newline at end of file diff --git a/js/index.js b/js/index.js index 727e60b..18b9fd3 100644 --- a/js/index.js +++ b/js/index.js @@ -1,38 +1,3 @@ -// Copyright © 2023 Entreprise SkamKraft -'use strict'; -import { UIRenderer } from "./ui/templeting_engine.js"; -import { Initialzer } from "./commun/commun.js"; -import { AgentBuilder } from "./api/agent.js"; -import { Auth } from "./auth/auth.js"; -let store; -let token = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZGVudGlmaWVyIjoiSEFSRElDSyIsInZlcnNpb24iOiJ2Mi4xLjQiLCJyZXNldF9kYXRlIjoiMjAyMy0xMi0wMiIsImlhdCI6MTcwMjY2Mjc2Mywic3ViIjoiYWdlbnQtdG9rZW4ifQ.PrvaOz3W79acq6RoxryMW53PRRz824_AM0VGLwfXCOsGCOCAIY-rn6-bZTOnLAtp4xPSDqEk4c38oWYAWW59p0iMDDLpur6ONnjT0RjjsQS9zr5BByfBpP36CT23IZSSzk3XxGrFolHJAyU3K1liYfNbsPuNTXlkHGNHq6yMqH4ZQUPFsXEsCkg9cUynkdLw3C39SvWhtJ89oblj_8tQp2k8dxhZemepuXtiI51eFMpv8A0WRAi7pVYo_ajJujY9QDLYn_m5hDZWTlQMIstjPaDl99p2IMweIMO2Q2G-0lKiWQ4sl6VW5tuVrz1HLYU6kyMjFQWNn6kFDE7LWMTrfw"; -let UI = new UIRenderer("html"); -let initer = new Initialzer(UI); +import tests from "./test/tests.js"; -UI.render("templates/home.html"); -UI.frag_load("#test", "templates/login.html"); - -let auth = new Auth(store = true); -auth.done((agent) => { - console.log(agent); -}).fail((errs) => { - errs.forEach(err => { - console.log(err); - }); -}); -auth.login(token); -auth.relog(); - -//await auth.register({ -// symbol: "lkdsjfsjdlfjlk", -// faction: "COSMIC" -//}); - -$(document).ready(() => { - initer.init_menu_link("#contracts-link", "contracts.html"); - initer.init_menu_link("#ships-link", "ships.html"); - initer.init_menu_link("#systems-link", "systems.html"); - initer.init_menu_link("#signup-link", "register.html"); - initer.init_menu_link("#signin-link", "login.html"); - initer.init_menu_link(".nav-brand", "home.html"); -}); \ No newline at end of file +tests.modal(); \ No newline at end of file diff --git a/js/test/tests.js b/js/test/tests.js new file mode 100644 index 0000000..ac99d4a --- /dev/null +++ b/js/test/tests.js @@ -0,0 +1,93 @@ +import { Timer } from "../ui/timer.js"; +import { Modal } from "../ui/modal.js"; +import { TemplateEngine } from "../ui/templeting_engine.js"; +import { Initialzer } from "../commun/commun.js"; +import { AgentBuilder } from "../api/agent.js"; +import { Auth } from "../auth/auth.js"; +import { PlanetBuilder } from "../api/planet.js"; + +let temp_path = "html"; +let token = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZGVudGlmaWVyIjoiSEFSRElDSyIsInZlcnNpb24iOiJ2Mi4xLjQiLCJyZXNldF9kYXRlIjoiMjAyMy0xMi0wMiIsImlhdCI6MTcwMjY2Mjc2Mywic3ViIjoiYWdlbnQtdG9rZW4ifQ.PrvaOz3W79acq6RoxryMW53PRRz824_AM0VGLwfXCOsGCOCAIY-rn6-bZTOnLAtp4xPSDqEk4c38oWYAWW59p0iMDDLpur6ONnjT0RjjsQS9zr5BByfBpP36CT23IZSSzk3XxGrFolHJAyU3K1liYfNbsPuNTXlkHGNHq6yMqH4ZQUPFsXEsCkg9cUynkdLw3C39SvWhtJ89oblj_8tQp2k8dxhZemepuXtiI51eFMpv8A0WRAi7pVYo_ajJujY9QDLYn_m5hDZWTlQMIstjPaDl99p2IMweIMO2Q2G-0lKiWQ4sl6VW5tuVrz1HLYU6kyMjFQWNn6kFDE7LWMTrfw"; + +let tests = { + timer: function() { + let timer = new Timer(1, 0.1666, "m"); + timer.on("step", (time) => { + console.log(time); + }) + timer.on("end", () => { + console.log("pattes fini"); + }); + timer.start(); + }, + render_template: function() { + let UI = new TemplateEngine(temp_path); + let initer = new Initialzer(UI); + + UI.render("templates/home.html"); + UI.frag_load("#test", "templates/login.html"); + + $(document).ready(() => { + initer.init_menu_link("#contracts-link", "contracts.html"); + initer.init_menu_link("#ships-link", "ships.html"); + initer.init_menu_link("#systems-link", "systems.html"); + initer.init_menu_link("#signup-link", "register.html"); + initer.init_menu_link("#signin-link", "login.html"); + initer.init_menu_link(".nav-brand", "home.html"); + }); + }, + authentication: function() { + let auth = new Auth(true); + auth.done((agent) => { + console.log(agent); + }).fail((errs) => { + errs.forEach(err => { + console.log(err); + }); + }); + auth.login(token); + auth.relog(); + + //auth.register({ + // symbol: "lkdsjfsjdlfjlk", + // faction: "COSMIC" + //}); + }, + modal: function() { + let template_engine = new TemplateEngine(temp_path); + + let timer = new Timer(60, 1, "s"); + timer.on("step", (time) => { + $("#timer").html(` +
${time}
+ `); + }); + + template_engine.render("templates/login.html"); + + let modal = new Modal("test-modal",template_engine); + modal.enable = false; + + modal.render("templates/test_modal.html"); + + template_engine.add_event("#valid", "click", () => { + modal.show(); + timer.start(); + }); + + template_engine.add_event("#ok", "click", () => { + modal.close(); + timer.stop(); + }); + }, + get_planet: function() { + PlanetBuilder.get("X1-TT23", "X1-TT23-FF1E", (planet) => { + console.log(planet); + }, (err) => { + console.log(err); + }); + }, + +} + +export default tests; \ No newline at end of file diff --git a/js/ui/Menu.js b/js/ui/Menu.js deleted file mode 100644 index e69de29..0000000 diff --git a/js/ui/Modal.js b/js/ui/Modal.js deleted file mode 100644 index e69de29..0000000 diff --git a/js/api/api.js b/js/ui/menu.js similarity index 100% rename from js/api/api.js rename to js/ui/menu.js diff --git a/js/ui/modal.js b/js/ui/modal.js new file mode 100644 index 0000000..244ca3e --- /dev/null +++ b/js/ui/modal.js @@ -0,0 +1,30 @@ +export class Modal { + constructor(name, template_engine, tag = "#block-content") { + this.name = name; + this.template_engine = template_engine; + this.tag = tag; + this.modal_class = ""; + } + + render(template) { + this.template_engine.get_template((reponse) => { + $(this.tag).html(` + + `); + }, template); + } + + add_class(modal_class) { + this.modal_class = `${this.modal_class} ${modal_class}`; + } + + show() { + document.querySelector(`#${this.name}`).showModal(); + } + + close() { + document.querySelector(`#${this.name}`).close(); + } +} \ No newline at end of file diff --git a/js/ui/templeting_engine.js b/js/ui/templeting_engine.js index d51dcf1..15fb401 100644 --- a/js/ui/templeting_engine.js +++ b/js/ui/templeting_engine.js @@ -1,32 +1,32 @@ // Copyright © 2023 Entreprise SkamKraft 'use strict'; -export class UIRenderer { +export class TemplateEngine { constructor(path) { this.templatePath = path; } render(template) { - this.#get_template((reponse) => { + this.get_template((reponse) => { $('body').html(reponse); - this.#get_template((reponse) => { + this.get_template((reponse) => { $("#block-content").html(reponse); }, template) }); } frag_load(tag, template) { - this.#get_template((reponse) => { + this.get_template((reponse) => { $(tag).html(reponse); }, template); } - #get_template(callback, template = "") { + get_template(callback, template = "") { let url = template === "" ? `${this.templatePath}/template.html`: `${this.templatePath}/${template}`; - let data = $.ajax(url,{ + $.ajax(url,{ async: false, method: "GET", success: callback, - fail: (err) => { + error: (err) => { console.log(err); } }); diff --git a/js/ui/timer.js b/js/ui/timer.js new file mode 100644 index 0000000..0d6641e --- /dev/null +++ b/js/ui/timer.js @@ -0,0 +1,56 @@ +export class Timer { + constructor(time, step, unit = "s") { + this.passed_time = 0; + this.time = time; + this.step = step; + this.continue = true; + switch (unit) { + case "ms": + this.unit = 1; + break; + case "s": + this.unit = 1000; + break; + case "m": + this.unit = 60000; + break; + case "h": + this.unit = 3600000; + break; + default: + this.unit = 1; + break; + } + } + on(action, callback) { + switch(action) { + case "end": + this.end_callback = callback; + break; + case "step": + this.step_callback = callback; + break; + } + } + start() { + this.continue = true; + this.#time_step(); + } + + stop() { + this.continue = false; + this.passed_time = 0; + } + + #time_step() { + if (this.passed_time < this.time && this.continue) { + if (this.step_callback) this.step_callback(this.passed_time); + this.passed_time += this.step; + setTimeout(() => { + this.#time_step() + }, this.step*this.unit); + } else { + if (this.end_callback) this.end_callback(this.time); + } + } +} \ No newline at end of file