aboutsummaryrefslogtreecommitdiffstats
path: root/js/craftmine.js
blob: fa70809f39655dbf00d5d98d51a02d7be57f698c (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
data = {
	name: "You",
	gold: 0,
	mine: 0,
	miners: 0,
	level: 3,
	hp: 5,
	maxHP: 5,
	xp: 0,
	power: 3,
	bonusPower: 0,
	icon : "💪"
}

function sendRequest(url, params, callback, isFile) {
	var xhr = new XMLHttpRequest();
	xhr.open("POST", url);
	if(!isFile) xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr.onreadystatechange = function() {
		if(xhr.readyState == XMLHttpRequest.DONE && xhr.status == "200") {
			var data = "";
			if(xhr.responseText) data = JSON.parse(xhr.responseText);
			if(data.info)
				showInfo(data.info);
			if(data.error) {
				showError(data.error);
				return;
			}
			if(callback) callback(data);
		}
	}
	xhr.send(params);
}


function updateData() {
	for (var i = 0; i < arguments.length; i++) {
		document.getElementById(arguments[i]).innerHTML = data[arguments[i]];
	}
}

function debitAccount(amount) {
	data.gold -= amount;
	updateData("gold");
}

function creditAccount(amount){
	data.gold += amount;
	sendRequest("craftmine.php", "op=withdrawMine&amount="+amount, function(xhr) {
		var gold = parseInt(xhr);
		if(isNaN(gold)) return;
		data.gold = gold;
		updateData("gold");
	});
}

function withdrawMine() {
	sendRequest("craftmine.php", "op=withdrawMine&amount="+data.mine, function(xhr) {
		var gold = parseInt(xhr); // Server's response is a string
		if(isNaN(gold)) return;
		data.gold = gold;
		data.mine = 0;
		updateData("gold", "mine");
	})
}

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) updatePerso(ret.perso);
		if(ret.shop) displayShop(ret.shop);
		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", "mine", "miners");
	})
}


function updateMine() {
	data.mine += (data.miners+1);
	updateData("mine");
}

function init() {
	initCraftMine();
	changeTab(); // Switch to tab specified in URL
	listSaves(); // Update save list on page load
	window.setInterval(updateMine, 1000); // Increase mine amount every 1 second
	window.onhashchange = changeTab; // Hook changeTab from js/gui.js to hashchange event
}