aboutsummaryrefslogtreecommitdiffstats
path: root/js
diff options
context:
space:
mode:
authorpiernov <piernov@piernov.org>2016-04-25 22:36:33 +0200
committerpiernov <piernov@piernov.org>2016-04-25 22:36:33 +0200
commit54635d17eef27eb2546d69599e4107b242509ced (patch)
tree86d18cbb6e021b78ba1ce87307447d71d1802ad3 /js
parentf98b06a25d71cc02bf29d7c525da0095688ea872 (diff)
parent8140617aeb2f32f7095a443ca743c6d6915739c6 (diff)
downloadcandybox-54635d17eef27eb2546d69599e4107b242509ced.tar.gz
candybox-54635d17eef27eb2546d69599e4107b242509ced.tar.bz2
candybox-54635d17eef27eb2546d69599e4107b242509ced.tar.xz
candybox-54635d17eef27eb2546d69599e4107b242509ced.zip
Merge branch 'alexichi' of ssh://piernov.org/srv/git/candybox into alexichi
Diffstat (limited to 'js')
-rw-r--r--js/craftmine.js27
-rw-r--r--js/craftmine.js~51
-rw-r--r--js/gui.js42
-rw-r--r--js/guild.js23
-rw-r--r--js/shop.js57
5 files changed, 130 insertions, 70 deletions
diff --git a/js/craftmine.js b/js/craftmine.js
index bbb1074..92fa45a 100644
--- a/js/craftmine.js
+++ b/js/craftmine.js
@@ -1,7 +1,7 @@
data = {
gold: 0,
mine: 0,
- miners: 1,
+ miners: 0,
level: 1
}
@@ -11,7 +11,12 @@ function sendRequest(url, params, callback) {
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
if(xhr.readyState == XMLHttpRequest.DONE && xhr.status == "200") {
- callback(xhr);
+ var data = JSON.parse(xhr.responseText);
+ if(data.error) {
+ showError(data.error);
+ return;
+ }
+ if(callback) callback(data);
}
}
xhr.send(params);
@@ -24,9 +29,14 @@ function updateData() {
}
}
+function debitAccount(amount) {
+ data.gold -= amount;
+ updateData("gold");
+}
+
function withdrawMine() {
sendRequest("craftmine.php", "op=withdrawMine&amount="+data.mine, function(xhr) {
- var gold = parseInt(xhr.responseText); // Server's response is a string
+ var gold = parseInt(xhr); // Server's response is a string
if(isNaN(gold)) return;
data.gold = gold;
data.mine = 0;
@@ -35,10 +45,12 @@ function withdrawMine() {
}
function initCraftMine() {
- sendRequest("craftmine.php", "op=getCraftMine", function(xhr) {
- var ret = xhr.responseText;
- data.gold = parseInt(ret); // Server's response is a string
- updateData("gold");
+ sendRequest("craftmine.php", "op=getCraftMine", function(ret) {
+ data.gold = parseInt(ret.gold); // Server's response is a string
+ if(ret.shop) displayShop(ret.shop);
+ displayInventory(ret.inventory);
+ data.miners = parseInt(ret.miners);
+ updateData("gold","miners");
})
}
@@ -50,5 +62,6 @@ function updateMine() {
function init() {
initCraftMine();
+ changeTab();
window.setInterval(updateMine, 1000);
}
diff --git a/js/craftmine.js~ b/js/craftmine.js~
deleted file mode 100644
index bc03d6c..0000000
--- a/js/craftmine.js~
+++ /dev/null
@@ -1,51 +0,0 @@
-datas = {
- gold: 0,
- mine: 0,
- miners: 1,
- level: 1
-}
-
-function sendRequest(url, params, callback) {
- var xhr = new XMLHttpRequest();
- xhr.open("POST", url);
- xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
- xhr.onreadystatechange = function() {
- if(xhr.readyState == XMLHttpRequest.DONE && xhr.status == "200") {
- callback(xhr);
- }
- }
- xhr.send(params);
-}
-
-
-function updateDatas(t) {
- document.getElementById(t).innerHTML = datas[t];
-}
-
-function withdrawMine() {
- sendRequest("craftmine.php", "op=withdrawMine", function() {
- datas.gold += parseInt(datas.mine);
- datas.mine = 0;
- updateDatas("gold");
- updateDatas("mine");
- })
-}
-
-function initCraftMine() {
- sendRequest("craftmine.php", "op=getCraftMine", function(xhr) {
- var ret = xhr.responseText;
- datas.gold = ret;
- updateDatas("gold");
- })
-}
-
-
-function updateMine() {
- datas.mine += (datas.mineurs+1);
- updateDatas("mine");
-}
-
-function init() {
- initCraftMine();
- window.setInterval(updateMine, 1000);
-}
diff --git a/js/gui.js b/js/gui.js
new file mode 100644
index 0000000..bdf1710
--- /dev/null
+++ b/js/gui.js
@@ -0,0 +1,42 @@
+var MESSAGE_TIMEOUT = 4000;
+
+function changeTab() {
+ var hashtype = window.location.hash.substr(1,3);
+ if(hashtype != "tab") return;
+ var id = window.location.hash.substr(4);
+ var tabs = document.querySelectorAll("#tabs-panel > ul > li");
+ for(var i=0; i < tabs.length; i++) {
+ if(i == id-1)
+ tabs[i].className = "active";
+ else
+ tabs[i].className = "";
+ }
+
+}
+
+function showMessage(type, msg) {
+ var msg_box = document.getElementById(type+"-box");
+ msg_box.style.display = "initial";
+ var msg_list = msg_box.firstElementChild.firstElementChild.firstElementChild;
+ msg_list.innerHTML = "<li>" + msg + "</li>\n" + msg_list.innerHTML;
+
+ window.setTimeout(hideMessage.bind(null, type), MESSAGE_TIMEOUT);
+}
+
+function hideMessage(type) {
+ var msg_box = document.getElementById(type+"-box");
+ var msg_list = msg_box.firstElementChild.firstElementChild.firstElementChild;
+ var item = msg_list.lastElementChild;
+ msg_list.removeChild(item);
+
+ if(msg_list.children.length <= 0)
+ msg_box.style.display = "none";
+}
+
+function showError(msg) {
+ showMessage("error", msg);
+}
+
+function showInfo(msg) {
+ showMessage("info", msg);
+}
diff --git a/js/guild.js b/js/guild.js
index 28f58b8..8d4d91b 100644
--- a/js/guild.js
+++ b/js/guild.js
@@ -1,18 +1,17 @@
function hireMiner(){
- sendRequest("craftmine.php", "op=hireMiner", function() {
- //datas.gold -= parseInt(datas.mine);
- var ret = xhr.responseText;
- console.log(ret);
- //datas.gold = ret;
- //updateDatas("gold");
- //updateDatas("miners");
- })
+ sendRequest("craftmine.php", "op=hireMiner", function(ret) {
+ debitAccount(parseInt(ret.cost));
+ data.miners = parseInt(ret.miners);
+ updateData("miners");
+ }
+ )
}
function createGuild(){
- sendRequest("craftmine.php", "op=createGuild", function() {
- var ret = xhr.responseText;
- console.log(ret);
- })
+ sendRequest("craftmine.php", "op=createGuild", function(ret) {
+ showInfo("Your guild has been successfully created");
+ debitAccount(parseInt(ret.cost));
+ }
+ )
}
diff --git a/js/shop.js b/js/shop.js
new file mode 100644
index 0000000..22ca85f
--- /dev/null
+++ b/js/shop.js
@@ -0,0 +1,57 @@
+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>";
+ }
+ 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);
+ });
+}
+
+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 invcontent = document.getElementById("tab3");
+
+ if(invcontent.children.length <= 1)
+ {
+ var tmphtml = "<h4>Your bag contains the following items:</h4>";
+ tmphtml += "<ul class=\"list-inline\">";
+ tmphtml += itemhtml;
+ tmphtml += "</ul>"
+ invcontent.innerHTML = tmphtml;
+ } else
+ invcontent.getElementsByTagName('ul')[0].innerHTML += itemhtml;
+
+ showInfo(ret.desc);
+}
+
+function buyItem(name) {
+ sendRequest("craftmine.php", "op=buyItem&item="+name, function(ret) {
+ addItem(ret);
+ debitAccount(ret.cost);
+ });
+}
+
+function useItem(name) {
+ sendRequest("craftmine.php", "op=useItem&item="+name, function(ret) {
+ });
+}
+