diff options
Diffstat (limited to 'js')
-rw-r--r-- | js/craftmine.js | 26 | ||||
-rw-r--r-- | js/dungeon.js | 121 | ||||
-rw-r--r-- | js/perso.js | 5 | ||||
-rw-r--r-- | js/shop.js | 58 |
4 files changed, 193 insertions, 17 deletions
diff --git a/js/craftmine.js b/js/craftmine.js index 4e6490f..b87f946 100644 --- a/js/craftmine.js +++ b/js/craftmine.js @@ -1,8 +1,12 @@ data = { + name: "You", gold: 0, mine: 0, miners: 0, - level: 1 + level: 3, + hp: 5, + xp: 0, + icon : "H" } function sendRequest(url, params, callback, isFile) { @@ -11,7 +15,10 @@ function sendRequest(url, params, callback, isFile) { if(!isFile) xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xhr.onreadystatechange = function() { if(xhr.readyState == XMLHttpRequest.DONE && xhr.status == "200") { - var data = JSON.parse(xhr.responseText); + var data = ""; + if(xhr.responseText) data = JSON.parse(xhr.responseText); + if(data.info) + showInfo(data.info); if(data.error) { showError(data.error); return; @@ -46,11 +53,22 @@ function withdrawMine() { function initCraftMine() { sendRequest("craftmine.php", "op=getCraftMine", function(ret) { + console.log(ret.perso); data.gold = parseInt(ret.gold); // Server's response is a string + data.mine = 0; // Reset mine + if(ret.perso){ + data.hp = ret.perso.hp; + data.xp = ret.perso.xp; + data.level = ret.perso.lv; + displayPerso(ret.perso.hp,ret.perso.xp,ret.perso.lv); + } if(ret.shop) displayShop(ret.shop); - displayInventory(ret.inventory); + if(ret.inventory) displayInventory(ret.inventory); + if(ret.dungeon == false){}//if we have left the donjon + else if(typeof ret.dungeon.mob == "undefined") displayDungeon(0,1,true); //if we have reload just after buying the ticket + else displayDungeon(ret.dungeon.mob,ret.dungeon.flat,true);//if we have reload in the middle of the dungeon data.miners = parseInt(ret.miners); - updateData("gold","miners"); + updateData("gold", "mine", "miners"); }) } diff --git a/js/dungeon.js b/js/dungeon.js new file mode 100644 index 0000000..982dcf6 --- /dev/null +++ b/js/dungeon.js @@ -0,0 +1,121 @@ +var timeout; + +function buildDungeon(){ + sendRequest("craftmine.php", "op=buildDungeon", function(ret) { + displayDungeon(0,1,true);//mob 0 in the floor 1 and I access the dungeon for the first dungeon + debitAccount(ret.cost); + showInfo("You can acces the dungeon now. Good Luck."); + }); +} + +function displayDungeon(nb,f,firstTime){ + var tmphtml = "<br/>"; + tmphtml += "<button id=\"launch\" type=\"button\" class=\"btn btn-primary\" onclick=\"launchDungeon("+nb+","+f+","+firstTime+")\">Launch</button>"; + document.getElementById("tab4").innerHTML = tmphtml; +} + +function launchDungeon(nb,f,firstTime){ + sendRequest("craftmine.php", "op=launchDungeon&floor="+f, function(ret) { + if(nb==0 && firstTime){ + document.getElementById("launch").style.display = "none"; + } + displayBattle(ret,nb,f); + }); +} + +function displayBattle(ret,nb,f){ + var tmphtml = "<div class=\"row\">"; + tmphtml += "<h4>Battle floor "+ f +"</h4>"; + tmphtml += "<ul class=\"list-inline\">"; + tmphtml += "<li>"; + tmphtml += "<button type=\"button\" class=\"btn btn-primary\" id=\"perso\" style=\"margin-left:30px;\"><span class=\"item-icon\">" + data.icon + "</span><br />" + data.name + "<br/> lv: " + data.level + " hp: <span id=\"lifePerso\">" + data.hp + "</span></button>"; + tmphtml += "<button type=\"button\" class=\"btn btn-primary\" id=\"mob\" style=\"margin-left:30px;\">" + +"<span class=\"item-icon\">" + ret[nb].icon + "</span><br />" + ret[nb].name + "<br/>" + +" lv: " + ret[nb].level + " hp: <span id=\"lifeMob\">" + ret[nb].hp + "</span></button>"; + tmphtml += "</li>"; + tmphtml += "</ul>"; + tmphtml += "</div>"; + document.getElementById("tab4").innerHTML = tmphtml; + displayExit(); + battle(ret,nb,f); +} + +function battle(ret,nb,f){ + timeout=window.setTimeout(strike, 1000, ret, nb, f); +} + +function strike(ret,nb, f){ + var lvDiff = data.level-parseInt(ret[0].level); + var hitRate = Math.floor((Math.random() * 100) + 1); + var mobLife = document.getElementById("lifeMob").innerHTML; + var persoLife = document.getElementById("lifePerso").innerHTML; + if(mobLife == 0){ + data.hp = persoLife; + endBattle("perso",nb,f,ret); + return; + } + else if(persoLife == 0){ + data.hp = 1; + endBattle("mob",nb,f,ret); + return; + } + if(hitRate<50+10*lvDiff){ + mobLife--; + document.getElementById("lifeMob").innerHTML = parseInt(mobLife); + } + else{ + persoLife--; + document.getElementById("lifePerso").innerHTML = parseInt(persoLife); + } + battle(ret,nb,f); +} + +function endBattle(v,nb, f, ret){ + window.clearTimeout(timeout); + if(v=="mob")exitDungeon(false);//if you die in the dungeon, you are immediately sent out of the dungeon + //To level up you have to obtain 2 xp to go to lv 3, 3 to go to lv 4, etc + //A mob level 2 , if defeated gives you 2 xp, ...etc + else{ + data.xp += parseInt(ret[nb].xp); + if(data.xp>=data.level){ + data.xp-=data.level; + data.level++; + //need to send the xp to the server + sendRequest("craftmine.php", "op=updatePerso&hp="+data.hp+"&xp="+data.xp+"&lv="+data.level, function(){ + displayPerso(data.hp,data.xp,data.level); + }); + } + nb++;//go to the next mob in the same floor + if(nb>=3){//floor changing + nb=0;//reset the number of the mob + f++;//increment the number of the floor + if(f>=4){ + exitDungeon(true);//true means that you have completed the dungeon and not just die or exit by yourself + return; + } + } + var tmphtml = "Le " + v + " a gagné."; + tmphtml += "<button type=\"button\" class=\"btn btn-primary\" onclick=\"launchDungeon("+nb+","+ f +",false)\">Next Battle</button>" + document.getElementById("tab4").innerHTML = tmphtml; + displayExit(); + sendRequest("craftmine.php", "op=sendDungeonProgress&floor="+f+"&mob="+nb); + } + sendRequest("craftmine.php", "op=updatePerso&hp="+data.hp+"&xp="+data.xp+"&lv="+data.level, function(){ + displayPerso(data.hp,parseInt(data.xp),data.level); + }); +} + +function displayExit(){ + var tmphtml = "<br/><br/>"; + tmphtml += "<button type=\"button\" class=\"btn btn-primary\" onclick=\"exitDungeon(false)\">Exit</button>"; + document.getElementById("tab4").innerHTML += tmphtml; +} + +function exitDungeon(boss){ + sendRequest("craftmine.php", "op=exitDungeon", function() { + window.clearTimeout(timeout); + document.getElementById("tab4").innerHTML = "<h4>Not available, you have to buy a ticket in the build section.</h4>"; + if(boss) showInfo("You have beaten the final boss! CONGRATULATIONS!"); + showInfo("You have left the dungeon"); + }); +} diff --git a/js/perso.js b/js/perso.js new file mode 100644 index 0000000..365cf54 --- /dev/null +++ b/js/perso.js @@ -0,0 +1,5 @@ +function displayPerso(hp,xp,lv){ + document.getElementById("hp").innerHTML = hp; + document.getElementById("lv").innerHTML = lv; + document.getElementById("xp").innerHTML = xp; +} @@ -1,12 +1,20 @@ function displayShop(ret) { - var tmphtml = "<h4> Select an item to buy it:</h4>"; - tmphtml += "<ul class=\"list-inline\">"; - for(var i=0; i < ret.items.length; i++) { - tmphtml += "<li>"; - tmphtml += "<button type=\"submit\" class=\"btn btn-primary\" onclick=\"buyItem('" + ret.items[i].name + "')\"><span class=\"item-icon\">" + ret.items[i].icon + "</span><br />" + ret.items[i].name + "</button>"; - tmphtml += "</li>"; + var tmphtml = "<h3> Select an item to buy it:</h3>"; + for(var key in ret.items){ + if(ret.items.hasOwnProperty(key)){ + var category = ret.items[key]; + tmphtml += "<div class=\"row\">"; + tmphtml += "<h4>"+key+"</h4>"; + tmphtml += "<ul class=\"list-inline\">"; + for(var i=0; i < category.length; i++) { + tmphtml += "<li>"; + tmphtml += "<button type=\"button\" class=\"btn btn-primary\" onclick=\"buyItem('" + category[i].name + "')\"><span class=\"item-icon\">" + category[i].icon + "</span><br />" + category[i].name + "</button>"; + tmphtml += "</li>"; + } + tmphtml += "</ul>"; + tmphtml += "</div>"; + } } - tmphtml += "</ul>" document.getElementById("tab2").innerHTML = tmphtml; } @@ -20,14 +28,22 @@ function buildShop() { sendRequest("craftmine.php", "op=buildShop", function(ret) { displayShop(ret); debitAccount(ret.cost); + showInfo("Your shop has been successfully created"); }); } function addItem(ret) { - var itemhtml = "<li>"; - itemhtml += "<button type=\"button\" class=\"btn btn-primary\" onclick=\"useItem('" + ret.name + "')\"><span class=\"item-icon\">" + ret.icon + "</span><br />" + ret.name + "</button>"; - itemhtml += "</li>"; - + var itemhtml = ""; + var itemtag = document.querySelector("[data-name=\""+ret[0].name+"\"]"); + if(!itemtag){ //si c'est la première itération de l'objet + itemhtml += "<li>"; + itemhtml += "<button type=\"button\" class=\"btn btn-primary\" onclick=\"useItem('" + ret[0].name + "')\"><span class=\"item-icon\">" + ret[0].icon + "</span><br />" + ret[0].name + "(<span id=\"nbItem\" data-name=\""+ ret[0].name + "\">"+ret[1]+"</span>)</button>"; + itemhtml += "</li>"; + } + else{ // si c'est une n-ième itération + itemtag.innerHTML=ret[1]; + } + var invcontent = document.getElementById("tab3"); if(invcontent.children.length <= 1) @@ -40,18 +56,34 @@ function addItem(ret) { } else invcontent.getElementsByTagName('ul')[0].innerHTML += itemhtml; - showInfo(ret.desc); + showInfo("The "+ ret[0].name + " has been successfully purchased"); } function buyItem(name) { sendRequest("craftmine.php", "op=buyItem&item="+name, function(ret) { addItem(ret); - debitAccount(ret.cost); + debitAccount(ret[0].cost); }); } function useItem(name) { sendRequest("craftmine.php", "op=useItem&item="+name, function(ret) { + if(parseInt(ret[1])>=0){ + var nb = parseInt(document.getElementById("nbItem").innerHTML); + if(nb>0){ + nb--; + switch(ret[0].name){ + case "Life Bottle": data.hp = parseInt(data.hp) + 3; break; + case "Strength Bottle" : break; // to do + case "Wooden Sword" : break; //to do + case "Metal Sword" : break; //to do + } + } + document.getElementById("nbItem").innerHTML = nb; + sendRequest("craftmine.php", "op=updatePerso&hp="+data.hp+"&xp="+data.xp+"&lv="+data.level, function(){ + displayPerso(data.hp,data.xp,data.level); + }); + } }); } |