aboutsummaryrefslogtreecommitdiffstats
path: root/js/dungeon.js
blob: 8b0c0c2edbd8a3707bc3abda0b0135a1e2e72fe5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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 time
		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-success\" 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-success\" 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-success\" 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 powerDiff = (data.power+data.bonusPower)-parseInt(ret[nb].power);
	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;
		data.bonusPower = 0; //à revoir car l'épée doit continuer à apporter un bonus
		endBattle("perso",nb,f,ret);
		return;
	}
	else if(persoLife == 0){
		data.hp = 1;
		data.bonusPower = 0;//when you die you lose your sword and all your power bonus
		endBattle("mob",nb,f,ret);
		return;
	}
	if(hitRate<50+10*powerDiff){
		mobLife--;
		document.getElementById("lifeMob").innerHTML = parseInt(mobLife);
	}
	else{
		persoLife--;
		document.getElementById("lifePerso").innerHTML = parseInt(persoLife);
	}
	data.hp=persoLife;
	sendRequest("craftmine.php", "op=updatePerso&hp="+data.hp+"&maxHP="+data.maxHP+"&xp="+data.xp+"&lv="+data.level+"&power="+data.power
				+"&bonusPower="+data.bonusPower, function(){
				displayPerso(data.hp,data.maxHP,data.xp,data.level,data.power,data.bonusPower);
	});
	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{
		levelUp(ret[nb].xp);
		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 w = "player";
		if(v == "mob") w = "monster";
		var tmphtml = "<h4>The " + w + " won.</h4>"
//		var tmphtml = "Le " + v + " a gagné.";
		tmphtml += "<button type=\"button\" class=\"btn btn-success\" 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+"&maxHP="+data.maxHP+"&xp="+data.xp+"&lv="+data.level+"&power="+data.power
				+"&bonusPower="+data.bonusPower, function(){
				displayPerso(data.hp,data.maxHP,data.xp,data.level,data.power,data.bonusPower);
	});
}

function displayExit(){
	var tmphtml = "<br/><br/>";
	tmphtml += "<button type=\"button\" class=\"btn btn-success\" onclick=\"exitDungeon(false)\">Exit</button>";
	document.getElementById("tab4").innerHTML += tmphtml;
}

function exitDungeon(boss){
	sendRequest("craftmine.php", "op=exitDungeon", function() {
		window.clearTimeout(timeout);
		if(boss){//if the boss is beaten
			levelUp(20);//you earn 20 xp
			creditAccount(1000);//you earn 1000 gold coins
			var tmphtml = "<h3>You have completed the dungeon! CONGRATULATIONS!</h3>";
			tmphtml += "<ul>";
			tmphtml += "<li><h4>You have earned 1000 gold coins</h4></li>"
			tmphtml += "<li><h4>You have earned 20 xp</h4></li>";
			tmphtml += "</ul>";
			document.getElementById("tab4").innerHTML = tmphtml;
		}
		else document.getElementById("tab4").innerHTML = "<h4>Not available, you have to buy a ticket in the build section.</h4>";
		showInfo("You have left the dungeon");
	});
}