diff --git a/js/element.js b/js/element.js index 5da32af..44c0caa 100644 --- a/js/element.js +++ b/js/element.js @@ -4,16 +4,14 @@ class Element{ this.techno = techno } - chargerDepuisBdd(){ - fetch("../bdd/arbre.json").then((data)=>{ - data.json().then((data)=>{ - this.props = data["technologies"][this.techno]["elements"][this.tag] - this.cout = this.props.cout - this.nom = this.props.nom - this.boost = this.props.boost - }) + async chargerDepuisBdd(){ + let data = await fetch("../bdd/arbre.json") + data = await data.json() - }) + this.props = data["technologies"][this.techno]["elements"][this.tag] + this.cout = this.props.cout + this.nom = this.props.nom + this.boost = this.props.boost } afficherDropsDansHtml(){ diff --git a/js/intro.js b/js/intro.js index 02131cd..ef9fe9f 100644 --- a/js/intro.js +++ b/js/intro.js @@ -32,4 +32,7 @@ function scoreCount(){ const score_aff = document.getElementById("score_affichage") score += 1 score_aff.innerText="SCORE : " + score + + // check refresh la table magasin pour afficher une amélioration si un palier est atteint + remplirMagasin() } diff --git a/js/main.js b/js/main.js index f249d8e..5b43aac 100644 --- a/js/main.js +++ b/js/main.js @@ -1,50 +1,86 @@ -function remplirMagasin(){ + +// stoque les ameliorations deja disponibles dans le magasin pour ne pas le faire clignoter +let magasin = {} + +// stoquer les ameliorations deja achetees pour ne pas les afficher dans le magasin +let sacado = {} + +async function remplirMagasin(){ - const table= document.getElementById("table_magasin_body") - + const table= document.getElementById("table_magasin_body") - fetch("../bdd/arbre.json").then((data)=>{ - data.json().then((data)=>{ - console.log(data) - let tech = data["technologies"] + let data = await fetch("../bdd/arbre.json") + data = await data.json() - Object.keys(tech).forEach(key => { - el = tech[key] - const tr = document.createElement("tr") - const nom = document.createElement("td") - nom.innerText = el.nom - const boost = document.createElement("td") - boost.innerText = el.boost - const cout = document.createElement("td") - cout.innerText = el.cout + let tech = data["technologies"] + + for(const key in tech){ + console.log(sacado) + console.log(magasin) + // ne va pas s'embeter à proecess si on a deja acheté l'amelioration + if(!sacado[key] && !magasin[key]){ + el = tech[key] - const test = document.createElement("td") - const btn_acheter = document.createElement("button") - let tmp = key - btn_acheter.addEventListener("click", (event) =>{ - console.log("clé : ",tmp) - let t = new Technologie(tmp) - t.chargerDepuisBdd(true) - }) + + const tr = document.createElement("tr") + const nom = document.createElement("td") + nom.innerText = el.nom + const boost = document.createElement("td") + boost.innerText = el.boost + const cout = document.createElement("td") + cout.innerText = el.cout + + const test = document.createElement("td") + const btn_acheter = document.createElement("button") + + // stoquage temporaire de la clé JSON correspondant à la technologie à débloquer + // pour éviter de référencer key, qui va sauter sur sa valeur finale + let tmp = key + + + let t = new Technologie(tmp) + // charge les propriétés de la technologie depuis la bdd sans mettre à jour le style directement après + await t.chargerDepuisBdd(false) + + /* fonction qui va s'activer lorsqu'on achète l'objet*/ + btn_acheter.addEventListener("click", (event) =>{ + // on améliore le style de la page :) + t.appliquerAmeliorationStyle() + + // et on ajouter l'amélioration dans l'abre des compétences débloquées + sacado[key] = true - btn_acheter.innerText = "Acheter" + // on refresh le magasin pour enlever l'amelioration de la liste + // vide le ventre du magasin + table.innerHTML = "" + magasin = {} + remplirMagasin() + + }) + + btn_acheter.innerText = "Acheter" + + // n'afficher l'amélioration que si le score est assez élevé + if(t.props.borne <= score){ + + // met à jour le contenu du magasin + // pour ne pas re-afficher cette ligne et éviter un clignotement + magasin[key] = true test.appendChild(btn_acheter) - - + + tr.appendChild(nom) tr.appendChild(boost) tr.appendChild(cout) tr.appendChild(test) table.appendChild(tr) + } + } + - }); - }) - - }) - - + } } diff --git a/js/technologie.js b/js/technologie.js index d007127..06e5777 100644 --- a/js/technologie.js +++ b/js/technologie.js @@ -3,21 +3,16 @@ class Technologie{ this.nom = nom } - chargerDepuisBdd( style){ - fetch("../bdd/arbre.json").then((data)=>{ - data.json().then((data)=>{ - console.log("clé dans technologie :",this.nom) - this.props = data["technologies"][this.nom] - this.cout = this.props.cout - this.boost = this.props.boost - if(style){ - this.appliquerAmeliorationStyle() - } - }) - - - - }) + async chargerDepuisBdd(style) { + let data = await fetch("../bdd/arbre.json") + data = await data.json() + console.log("clé dans technologie :", this.nom); + this.props = data["technologies"][this.nom]; + this.cout = this.props.cout; + this.boost = this.props.boost; + if (style) { + this.appliquerAmeliorationStyle(); + } } appliquerAmeliorationStyle(){