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 = " ";
tmphtml += "Launch ";
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 = "
";
tmphtml += "
Battle floor "+ f +" ";
tmphtml += "
";
tmphtml += "";
tmphtml += "" + data.icon + " " + data.name + " lv: " + data.level + " hp: " + data.hp + " ";
tmphtml += ""
+"" + ret[nb].icon + " " + ret[nb].name + " "
+" lv: " + ret[nb].level + " hp: " + ret[nb].hp + " ";
tmphtml += " ";
tmphtml += " ";
tmphtml += "
";
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 += "Next Battle "
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 = " ";
tmphtml += "Exit ";
document.getElementById("tab4").innerHTML += tmphtml;
}
function exitDungeon(boss){
sendRequest("craftmine.php", "op=exitDungeon", function() {
window.clearTimeout(timeout);
document.getElementById("tab4").innerHTML = "Not available, you have to buy a ticket in the build section. ";
if(boss) showInfo("You have beaten the final boss! CONGRATULATIONS!");
showInfo("You have left the dungeon");
});
}