Added modal timer position and planet class

vinayak
anulax1225 ago%!(EXTRA string=1 year)
parent 7dfe56b4a2
commit 5174ee36a8
  1. 28
      js/api/agent.js
  2. 2
      js/api/config.js
  3. 113
      js/api/planet.js
  4. 0
      js/api/ship.js
  5. 10
      js/commun/position.js
  6. 39
      js/index.js
  7. 93
      js/test/tests.js
  8. 0
      js/ui/Menu.js
  9. 0
      js/ui/Modal.js
  10. 0
      js/ui/menu.js
  11. 30
      js/ui/modal.js
  12. 14
      js/ui/templeting_engine.js
  13. 56
      js/ui/timer.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(() => {

@ -1,5 +1,5 @@
'use strict';
export const SpaceTraders = {
host: "https://api.spacetraders.io/v2/",
host: "https://api.spacetraders.io/v2",
limit_max: 20,
}

@ -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() {
}
}

@ -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)
}
}

@ -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");
});
tests.modal();

@ -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(`
<p>${time}</p>
`);
});
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;

@ -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(`
<dialog id="${this.name}" class="${this.modal_class} modal-disable">
${reponse}
</dialog>
`);
}, 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();
}
}

@ -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);
}
});

@ -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);
}
}
}
Loading…
Cancel
Save