parent
cb2c307696
commit
54cfb1e136
13 changed files with 0 additions and 773 deletions
@ -1,122 +0,0 @@ |
||||
// Copyright © 2023 Entreprise SkamKraft
|
||||
'use strict'; |
||||
import { SpaceTraders } from "./config.js" |
||||
|
||||
export class My { |
||||
static agent = null; |
||||
} |
||||
|
||||
export class Agent { |
||||
constructor(agent, token = "") { |
||||
this.token = token; |
||||
this.name = agent.symbol; |
||||
this.credits = agent.credits; |
||||
this.faction = agent.startingFaction; |
||||
this.hq = agent.headquarters; |
||||
this.ships_cpt = agent.shipCount;
|
||||
} |
||||
|
||||
get_agent_system() { |
||||
let metaSystem = this.hq.split("-"); |
||||
return metaSystem[0] + "-" + metaSystem[1];
|
||||
} |
||||
} |
||||
|
||||
export class AgentBuilder { |
||||
static create(symbol, faction, callback, error_handler) { |
||||
const url = `${SpaceTraders.host}/register`; |
||||
$.ajax({ |
||||
url: url, |
||||
method: "POST", |
||||
headers: { "Content-Type": "application/json" }, |
||||
processData: false, |
||||
data: `{\n "faction": "${faction}",\n "symbol": "${symbol}"}`, |
||||
success: (reponse) => { |
||||
let agent = new Agent(reponse.data.agent, reponse.data.token) |
||||
callback(agent); |
||||
}, |
||||
error: (err) => { |
||||
error_handler(["Name already took."]) |
||||
} |
||||
}); |
||||
} |
||||
|
||||
static get(token, callback, error_handler){ |
||||
const url = `${SpaceTraders.host}/my/agent`; |
||||
$.ajax({ |
||||
url: url, |
||||
method: "GET", |
||||
headers: { |
||||
Accept: "application/json", |
||||
Authorization: `Bearer ${token}`, |
||||
}, |
||||
success: (reponse) => { |
||||
let agent = new Agent(reponse.data, token); |
||||
callback(agent); |
||||
}, |
||||
error: (err) => { |
||||
error_handler(["Token invalide."]); |
||||
} |
||||
}); |
||||
} |
||||
|
||||
static get_public(symbol, callback) { |
||||
const url = `${SpaceTraders.host}/agents/${symbol}`; |
||||
$.ajax({ |
||||
url: url, |
||||
method: "GET", |
||||
headers: { |
||||
Accept: "application/json"
|
||||
}, |
||||
success: (reponse) => { |
||||
let agent = new Agent(reponse.data); |
||||
callback(agent); |
||||
}, |
||||
}); |
||||
} |
||||
|
||||
static list(limit, page, callback, agents = []) { |
||||
const url = `${SpaceTraders.host}/agents`; |
||||
const data = { limit, page }; |
||||
$.ajax({ |
||||
url: url, |
||||
method: "GET", |
||||
headers: { |
||||
Accept: "application/json"
|
||||
}, |
||||
data: data, |
||||
success: (reponse) => { |
||||
reponse.data.forEach(agent => { |
||||
agents.push(new Agent(agent)); |
||||
}); |
||||
callback(agents, reponse.meta); |
||||
}, |
||||
}); |
||||
} |
||||
|
||||
static list_all(callback, end = false) { |
||||
this.list(20, 1, (agents, meta) => { |
||||
let maxPage = meta.total / 20; |
||||
this.#r_listing(2, maxPage, agents, callback, end); |
||||
}); |
||||
} |
||||
|
||||
static #r_listing(page, maxPage, agents, callback, end) { |
||||
if (page < maxPage) { |
||||
this.list(20, page++,() => { |
||||
setTimeout(() => { |
||||
if (!end) { |
||||
callback(agents); |
||||
agents = []; |
||||
} |
||||
this.#r_listing(page++, maxPage, agents, callback, end);
|
||||
}, 1000); |
||||
}, agents); |
||||
} else { |
||||
callback(agents); |
||||
} |
||||
} |
||||
} |
||||
|
||||
|
||||
|
@ -1,7 +0,0 @@ |
||||
// Copyright © 2023 Entreprise SkamKraft
|
||||
'use strict'; |
||||
|
||||
export const SpaceTraders = { |
||||
host: "https://api.spacetraders.io/v2", |
||||
limit_max: 20, |
||||
} |
@ -1,135 +0,0 @@ |
||||
// Copyright © 2023 Entreprise SkamKraft
|
||||
'use strict'; |
||||
import contracts from "../../controllers/contracts.js"; |
||||
import { My } from "../api/agent.js"; |
||||
import { SpaceTraders } from "./config.js"; |
||||
|
||||
|
||||
export class Contract { |
||||
constructor(data) { |
||||
this.id = data.id; |
||||
this.faction = data.factionSymbol; |
||||
this.type = data.type; |
||||
this.accepted = data.accepted; |
||||
this.expiration = data.expiration; |
||||
this.deadline = data.deadlineToAccept; |
||||
this.terms = data.terms; |
||||
this.paymentAccepted = data.terms.payment.onAccepted; |
||||
this.paymentFulfill = data.terms.payment.onFulfilled; |
||||
this.tradeSymbol = data.terms.deliver[0].tradeSymbol; |
||||
this.destination = data.terms.deliver[0].destinationSymbol; |
||||
|
||||
|
||||
} |
||||
|
||||
static get(id, callback, error_handler) { |
||||
const url = `${SpaceTraders.host}/my/contracts/${id}`; |
||||
$.ajax({ |
||||
url: url, |
||||
method: "GET", |
||||
headers: { |
||||
Accept: "application/json", |
||||
Authorization: `Bearer ${My.agent.token}`, |
||||
}, |
||||
success: (reponse) => { |
||||
callback(new Contract(reponse.data)); |
||||
}, |
||||
error: (err) => { |
||||
error_handler("Contract not found"); |
||||
} |
||||
}); |
||||
} |
||||
|
||||
static list(limit, page, callback) { |
||||
const url = `${SpaceTraders.host}/my/contracts` |
||||
$.ajax({ |
||||
url: url, |
||||
method: "GET", |
||||
data: { |
||||
limit: limit, |
||||
page: page, |
||||
}, |
||||
headers: { |
||||
Accept: "application/json", |
||||
Authorization: `Bearer ${My.agent.token}`, |
||||
}, |
||||
success: (reponse) => { |
||||
let contracts = []; |
||||
reponse.data.forEach(contract => { |
||||
contracts.push(new Contract(contract)); |
||||
}); |
||||
callback(contracts); |
||||
}, |
||||
error: (err) => { |
||||
error_handler("Contract not found"); |
||||
} |
||||
}); |
||||
} |
||||
|
||||
accept(callback) { |
||||
console.log("Access"); |
||||
const url = `${SpaceTraders.host}/my/contracts/${this.id}/accept` |
||||
$.ajax({ |
||||
url: url, |
||||
method: "POST", |
||||
headers: { |
||||
'Content-Type': 'application/json', |
||||
Accept: "application/json", |
||||
Authorization: `Bearer ${My.agent.token}`, |
||||
}, |
||||
success: (reponse) => { |
||||
callback(reponse); |
||||
}, |
||||
error: (err) => { |
||||
//error_handler("Contract not found");
|
||||
} |
||||
}); |
||||
|
||||
|
||||
} |
||||
|
||||
static deliver(contractId, token) { |
||||
|
||||
const url = `${SpaceTraders.host}/my/contracts/${contractId}/deliver` |
||||
$.ajax({ |
||||
url: url, |
||||
method: "POST", |
||||
headers: { |
||||
'Content-Type': 'application/json', |
||||
Accept: "application/json", |
||||
Authorization: `Bearer ${My.agent.token}`, |
||||
}, |
||||
success: (reponse) => { |
||||
callback(reponse); |
||||
}, |
||||
error: (err) => { |
||||
error_handler("Contract not found"); |
||||
} |
||||
}); |
||||
|
||||
|
||||
} |
||||
|
||||
static fulfill(contractId, token) { |
||||
|
||||
const url = `${SpaceTraders.host}/my/contracts/${contractId}/fulfill` |
||||
$.ajax({ |
||||
url: url, |
||||
method: "POST", |
||||
headers: { |
||||
'Content-Type': 'application/json', |
||||
Accept: "application/json", |
||||
Authorization: `Bearer ${My.agent.token}`, |
||||
}, |
||||
success: (reponse) => { |
||||
callback(reponse); |
||||
}, |
||||
error: (err) => { |
||||
error_handler("Contract not found"); |
||||
} |
||||
}); |
||||
|
||||
|
||||
} |
||||
|
||||
} |
@ -1,71 +0,0 @@ |
||||
// Copyright © 2023 Entreprise SkamKraft
|
||||
'use strict'; |
||||
|
||||
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) { |
||||
|
||||
} |
||||
|
||||
list_import(callback) { |
||||
|
||||
} |
||||
} |
||||
|
||||
export class Planet { |
||||
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(callback, error_handler) { |
||||
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("Market not found"); |
||||
} |
||||
});
|
||||
} |
||||
|
||||
is_type(type) { |
||||
return this.type === type ? true : false; |
||||
} |
||||
|
||||
is_discovered() { |
||||
return this.discovery.length > 0 ? true : false; |
||||
}
|
||||
} |
@ -1,135 +0,0 @@ |
||||
// Copyright © 2023 Entreprise SkamKraft
|
||||
'use strict'; |
||||
|
||||
import { SpaceTraders } from "./config.js"; |
||||
import { Position } from "../commun/position.js"; |
||||
import { Planet } from "./planet.js"; |
||||
|
||||
export class System { |
||||
constructor(data) { |
||||
this.name = data.symbol; |
||||
this.sector = data.sectorSymbol; |
||||
this.type = data.type; |
||||
this.position = new Position(data.x, data.y); |
||||
this.factions = data.factions; |
||||
} |
||||
|
||||
get(name, callback, error_handler) { |
||||
const url = `${SpaceTraders.host}/systems/${this.name}/waypoints/${name}`; |
||||
$.ajax({ |
||||
url: url, |
||||
method: "GET", |
||||
success: (reponse) => { |
||||
let planet = new Planet(reponse.data); |
||||
callback(planet); |
||||
}, |
||||
error: (err) => { |
||||
error_handler("Planet not found"); |
||||
} |
||||
}); |
||||
} |
||||
|
||||
list(limit, page, callback, planets = []) { |
||||
const url = `${SpaceTraders.host}/systems/${this.name}/waypoints` |
||||
$.ajax({ |
||||
url: url, |
||||
method: "GET", |
||||
data: { |
||||
limit: limit, |
||||
page: page |
||||
}, |
||||
success: (reponse) => { |
||||
reponse.data.forEach(planet => { |
||||
planets.push(new Planet(planet)); |
||||
}); |
||||
callback(planets, reponse.meta); |
||||
} |
||||
}); |
||||
} |
||||
|
||||
list_all(callback, end = false) { |
||||
this.list(20, 1, (planets, meta) => { |
||||
let maxPage = meta.total / 20; |
||||
this.#r_listing(2, maxPage, planets, callback, end); |
||||
}); |
||||
} |
||||
|
||||
#r_listing(page, maxPage, planets, callback, end) { |
||||
if (page < maxPage) { |
||||
this.list(20, page++, () => { |
||||
setTimeout(() => { |
||||
if (!end) { |
||||
callback(planets); |
||||
planets = []; |
||||
} |
||||
this.#r_listing(page++, maxPage, planets, callback, end); |
||||
}, 1000); |
||||
}, planets); |
||||
} else { |
||||
callback(planets); |
||||
} |
||||
} |
||||
} |
||||
|
||||
export class SystemBuilder { |
||||
static parse_system_name(name) { |
||||
return name.split("-").slice(-1, 2).join("-"); |
||||
} |
||||
|
||||
static get(name, callback, error_handler) { |
||||
const url = `${SpaceTraders.host}/systems/${name}/`; |
||||
$.ajax({ |
||||
url: url, |
||||
method: "GET", |
||||
success: (reponse) => { |
||||
let system = new System(reponse.data); |
||||
callback(system); |
||||
}, |
||||
error: (err) => { |
||||
error_handler("System not found"); |
||||
} |
||||
}); |
||||
} |
||||
|
||||
static list(limit, page, callback, systems = []) { |
||||
const url = `${SpaceTraders.host}/systems/` |
||||
$.ajax({ |
||||
url: url, |
||||
method: "GET", |
||||
data: { |
||||
limit: limit, |
||||
page: page |
||||
}, |
||||
success: (reponse) => { |
||||
reponse.data.forEach(system => { |
||||
systems.push(new System(system)); |
||||
}); |
||||
callback(systems, reponse.meta); |
||||
} |
||||
}); |
||||
} |
||||
|
||||
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); |
||||
}); |
||||
} |
||||
|
||||
static #r_listing(page, maxPage, systems, callback, end) { |
||||
if (page < maxPage) { |
||||
this.list(20, page++, () => { |
||||
setTimeout(() => { |
||||
if (!end) { |
||||
console.log(systems); |
||||
callback(systems); |
||||
systems = []; |
||||
} |
||||
this.#r_listing(page++, maxPage, systems, callback, end); |
||||
}, 1000); |
||||
}, systems); |
||||
} else { |
||||
callback(systems); |
||||
} |
||||
} |
||||
} |
@ -1,89 +0,0 @@ |
||||
// Copyright © 2023 Entreprise SkamKraft
|
||||
'use strict'; |
||||
import { AgentBuilder } from '../api/agent.js' |
||||
import Strategie from '../commun/strategie.js'; |
||||
|
||||
let strategies = { |
||||
register: [ |
||||
{ |
||||
name: "name", |
||||
validations: [ |
||||
"required", |
||||
"max_length|14" |
||||
] |
||||
}, |
||||
{ |
||||
name: "faction", |
||||
validations: [ |
||||
"required" |
||||
] |
||||
} |
||||
], |
||||
login: [ |
||||
{ |
||||
name: "token", |
||||
validations: [ |
||||
"required" |
||||
] |
||||
} |
||||
] |
||||
} |
||||
|
||||
export class Auth { |
||||
constructor(store = false) { |
||||
this.store = store; |
||||
this.validated = () => {}; |
||||
this.error_handler = () => {}; |
||||
this.strategies = strategies; |
||||
} |
||||
|
||||
done(validated) { |
||||
this.validated = validated; |
||||
return this; |
||||
} |
||||
|
||||
fail(error_handler) { |
||||
this.error_handler = error_handler; |
||||
return this; |
||||
} |
||||
|
||||
login(token) { |
||||
let validateur = new Strategie(this.strategies.login); |
||||
validateur.validate("token", token); |
||||
if (validateur.errors.length > 0) this.error_handler(validateur.errors); |
||||
else { |
||||
if (this.store) localStorage.setItem("token", token); |
||||
AgentBuilder.get(token, this.validated, this.error_handler); |
||||
} |
||||
} |
||||
|
||||
relog() { |
||||
if(this.#is_login()) { |
||||
AgentBuilder.get(localStorage.getItem("token"), this.validated, this.error_handler);
|
||||
return true; |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
register(new_agent) { |
||||
let validateur = new Strategie(this.strategies.register); |
||||
validateur.validate("name", new_agent.name); |
||||
validateur.validate("faction", new_agent.faction); |
||||
if (validateur.errors.length > 0) this.error_handler(validateur.errors); |
||||
else {
|
||||
AgentBuilder.create(new_agent.name, new_agent.faction, (agent) => { |
||||
if (this.store) localStorage.setItem("token", agent.token); |
||||
this.validated(agent); |
||||
}, this.error_handler); |
||||
} |
||||
} |
||||
|
||||
unload_token() { |
||||
if(this.#is_login()) localStorage.removeItem("token"); |
||||
} |
||||
|
||||
#is_login() { |
||||
if (localStorage.getItem("token")) return true |
||||
return false |
||||
} |
||||
} |
@ -1,18 +0,0 @@ |
||||
// Copyright © 2023 Entreprise SkamKraft
|
||||
'use strict'; |
||||
|
||||
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) |
||||
} |
||||
|
||||
move(position) { |
||||
this.x += position.x; |
||||
this.y += position.y; |
||||
} |
||||
} |
@ -1,51 +0,0 @@ |
||||
// Copyright © 2023 Entreprise SkamKraft
|
||||
'use strict'; |
||||
|
||||
export default class Strategie { |
||||
constructor(strategie) { |
||||
this.strategie = strategie; |
||||
this.errors = []; |
||||
} |
||||
|
||||
validate(name, input) { |
||||
this.strategie.forEach(input_strat => { |
||||
if(input_strat.name === name) input_strat.validations.forEach((validation) => { |
||||
let args = validation.split("|"); |
||||
switch (args[0]) { |
||||
case "required":
|
||||
this.#test(Strategie.#required(input), `${name} is required.`); |
||||
break; |
||||
case "max_length": |
||||
this.#test(Strategie.#max_length(input, parseInt(args[1])), `${name} must have a max lenght of ${args[1]}.`); |
||||
break; |
||||
case "min_length": |
||||
this.#test(Strategie.#min_length(input, parseInt(args[1])), `${name} must have a min lenght of ${args[1]}`); |
||||
break; |
||||
} |
||||
}); |
||||
}); |
||||
} |
||||
|
||||
#test(test, error) { |
||||
if(!test) this.errors.push(error);
|
||||
} |
||||
|
||||
static #valide_email(input) { |
||||
|
||||
} |
||||
|
||||
static #min_length(input, length) { |
||||
if(input.length < length) return false; |
||||
return true; |
||||
} |
||||
|
||||
static #max_length(input, length) { |
||||
if(input.length > length) return false; |
||||
return true; |
||||
} |
||||
|
||||
static #required(input) { |
||||
if (input === undefined || input === null || input === "") return false; |
||||
return true; |
||||
} |
||||
} |
@ -1,59 +0,0 @@ |
||||
// Copyright © 2023 Entreprise SkamKraft
|
||||
'use strict'; |
||||
|
||||
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); |
||||
} |
||||
} |
||||
} |
@ -1,39 +0,0 @@ |
||||
// Copyright © 2023 Entreprise SkamKraft
|
||||
'use strict'; |
||||
|
||||
export class Modal { |
||||
constructor(name, template_engine, tag = "#block-content") { |
||||
this.name = name; |
||||
this.template_engine = template_engine; |
||||
this.tag = tag; |
||||
this.modal_class = ""; |
||||
} |
||||
|
||||
load(template) { |
||||
this.template_engine.get_template((reponse) => { |
||||
$(this.tag).prepend(` |
||||
<dialog id="${this.name}" class="${this.modal_class} modal-disable"> |
||||
${reponse} |
||||
</dialog> |
||||
`);
|
||||
}, template); |
||||
} |
||||
|
||||
on_close(callback) { |
||||
document.querySelector(`#${this.name}`).addEventListener("close", callback); |
||||
} |
||||
|
||||
add_class(modal_class) { |
||||
let modal; |
||||
if(modal = $(`#${this.name}`)) modal.addClass(modal_class); |
||||
this.modal_class = `${this.modal_class} ${modal_class}`; |
||||
} |
||||
|
||||
show() { |
||||
document.querySelector(`#${this.name}`).showModal();
|
||||
} |
||||
|
||||
close() { |
||||
document.querySelector(`#${this.name}`).close(); |
||||
} |
||||
} |
@ -1,47 +0,0 @@ |
||||
// Copyright © 2023 Entreprise SkamKraft
|
||||
'use strict'; |
||||
export class TemplateEngine { |
||||
constructor(path) { |
||||
this.templatePath = path; |
||||
} |
||||
|
||||
render(template) { |
||||
this.get_template((reponse) => { |
||||
$('body').html(reponse); |
||||
this.get_template((reponse) => { |
||||
$("#block-content").html(reponse); |
||||
if (this.after_render_callback) this.#flush_events().after_render_callback(this); |
||||
}, template) |
||||
}); |
||||
} |
||||
|
||||
frag_load(tag, template) { |
||||
this.get_template((reponse) => { |
||||
$(tag).html(reponse); |
||||
}, template); |
||||
} |
||||
|
||||
get_template(callback, template = "") { |
||||
let url = template === "" ? `${this.templatePath}/template.html` : `${this.templatePath}/${template}`; |
||||
$.ajax(url, { |
||||
method: "GET", |
||||
success: callback, |
||||
error: (err) => { |
||||
console.log(err); |
||||
} |
||||
}); |
||||
} |
||||
|
||||
add_event(tag, action, callback) { |
||||
$("body").on(action, tag, callback); |
||||
} |
||||
|
||||
#flush_events() { |
||||
$("body").unbind(); |
||||
return this; |
||||
} |
||||
|
||||
after_render(callback) { |
||||
this.after_render_callback = callback; |
||||
} |
||||
} |
Loading…
Reference in New Issue