aboutsummaryrefslogtreecommitdiffstats
path: root/js/shop.js
blob: 053f7c8938e01e4fb92f368b9ae6d5b78a2467a6 (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
function displayShop(ret) {
	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 += "<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-success\" onclick=\"buyItem('" + category[i].name + "')\"><span class=\"item-icon\">" + category[i].icon + "</span><br />" + category[i].name + "</button>";
				tmphtml += "</li>";
			}
			tmphtml += "</ul>";
		}
	}
	document.getElementById("tab2").innerHTML = tmphtml;
}

function displayInventory(items) {
	for(var i=0; i < items.length; i++) {
		addItem(items[i]);
	}
}

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 = "";
	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-success\" 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)
	{
		var tmphtml = "<h3>Your bag contains the following items:</h3>";
		tmphtml += "<ul class=\"list-inline\">";
		tmphtml += itemhtml;
		tmphtml += "</ul>"
		invcontent.innerHTML = tmphtml;
	} else
		invcontent.getElementsByTagName('ul')[0].innerHTML += itemhtml;

	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[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);
			});
		}
	});
}