From a6768cc97e3b418e0df08bcc2b09d8ffb1c9daa2 Mon Sep 17 00:00:00 2001 From: alexichi Date: Sun, 24 Apr 2016 14:08:31 +0200 Subject: nothing special --- js/craftmine.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'js/craftmine.js') diff --git a/js/craftmine.js b/js/craftmine.js index 50d3a47..4ccdf2f 100644 --- a/js/craftmine.js +++ b/js/craftmine.js @@ -1,7 +1,7 @@ datas = { gold: 0, mine: 0, - miners: 1, + miners: 0, level: 1 } @@ -34,7 +34,7 @@ function withdrawMine() { function initCraftMine() { sendRequest("craftmine.php", "op=getCraftMine", function(xhr) { var ret = xhr.responseText; - datas.gold = ret; + datas.gold = parseInt(ret); updateDatas("gold"); }) } -- cgit v1.2.3-70-g09d2 From 7f345404a79e6e0f25bafa6f25c8ba1edf6aef73 Mon Sep 17 00:00:00 2001 From: piernov Date: Sun, 24 Apr 2016 22:23:04 +0200 Subject: Add JS debitAccount() helper --- js/craftmine.js | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'js/craftmine.js') diff --git a/js/craftmine.js b/js/craftmine.js index bbb1074..46c726a 100644 --- a/js/craftmine.js +++ b/js/craftmine.js @@ -24,6 +24,11 @@ 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 -- cgit v1.2.3-70-g09d2 From f2d53d539ee55e1b41cc74368ad5457da6e05cf3 Mon Sep 17 00:00:00 2001 From: piernov Date: Sun, 24 Apr 2016 22:25:17 +0200 Subject: Decode JSON before calling callback and prevent call in case of error --- js/craftmine.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'js/craftmine.js') diff --git a/js/craftmine.js b/js/craftmine.js index 46c726a..912d10c 100644 --- a/js/craftmine.js +++ b/js/craftmine.js @@ -11,7 +11,11 @@ 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) { + return; + } + if(callback) callback(data); } } xhr.send(params); @@ -31,7 +35,7 @@ function debitAccount(amount) { 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; @@ -41,7 +45,7 @@ function withdrawMine() { function initCraftMine() { sendRequest("craftmine.php", "op=getCraftMine", function(xhr) { - var ret = xhr.responseText; + var ret = xhr; data.gold = parseInt(ret); // Server's response is a string updateData("gold"); }) -- cgit v1.2.3-70-g09d2 From 5ff6c7051867ed14ecbbc391709f25106c1bef1f Mon Sep 17 00:00:00 2001 From: piernov Date: Sun, 24 Apr 2016 22:27:33 +0200 Subject: Add client side JS shop functions --- js/craftmine.js | 7 ++++--- js/shop.js | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 js/shop.js (limited to 'js/craftmine.js') diff --git a/js/craftmine.js b/js/craftmine.js index 912d10c..dc4ee9a 100644 --- a/js/craftmine.js +++ b/js/craftmine.js @@ -44,9 +44,10 @@ function withdrawMine() { } function initCraftMine() { - sendRequest("craftmine.php", "op=getCraftMine", function(xhr) { - var ret = xhr; - data.gold = parseInt(ret); // Server's response is a string + 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); updateData("gold"); }) } diff --git a/js/shop.js b/js/shop.js new file mode 100644 index 0000000..00636e1 --- /dev/null +++ b/js/shop.js @@ -0,0 +1,57 @@ +function displayShop(ret) { + var tmphtml = "

Select an item to buy it:

"; + tmphtml += "
    "; + for(var i=0; i < ret.items.length; i++) { + tmphtml += "
  • "; + tmphtml += ""; + tmphtml += "
  • "; + } + tmphtml += "
" + 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 = "
  • "; + itemhtml += ""; + itemhtml += "
  • "; + + var invcontent = document.getElementById("tab3"); + + if(invcontent.children.length <= 1) + { + var tmphtml = "

    Your bag contains the following items:

    "; + tmphtml += "
      "; + tmphtml += itemhtml; + tmphtml += "
    " + 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) { + }); +} + -- cgit v1.2.3-70-g09d2 From 9df3524395d661357483bcfcbf8962e57fe9bd6d Mon Sep 17 00:00:00 2001 From: piernov Date: Sun, 24 Apr 2016 22:29:27 +0200 Subject: Display error received from server + switch to specified tab on load --- js/craftmine.js | 2 ++ 1 file changed, 2 insertions(+) (limited to 'js/craftmine.js') diff --git a/js/craftmine.js b/js/craftmine.js index dc4ee9a..66c35fc 100644 --- a/js/craftmine.js +++ b/js/craftmine.js @@ -13,6 +13,7 @@ function sendRequest(url, params, callback) { if(xhr.readyState == XMLHttpRequest.DONE && xhr.status == "200") { var data = JSON.parse(xhr.responseText); if(data.error) { + showError(data.error); return; } if(callback) callback(data); @@ -60,5 +61,6 @@ function updateMine() { function init() { initCraftMine(); + changeTab(); window.setInterval(updateMine, 1000); } -- cgit v1.2.3-70-g09d2 From 29d23898c7ec5d3280e9a7f01a6209f8c8f2d5f8 Mon Sep 17 00:00:00 2001 From: alexichi Date: Mon, 25 Apr 2016 13:37:08 +0200 Subject: Mise en commun du shop et de la guilde --- inc/account.inc | 2 +- inc/craftmine.inc | 1 + inc/guild.inc | 29 +++++++++++++++-------------- inc/messages.inc | 2 ++ js/craftmine.js | 3 ++- js/guild.js | 27 +++++++++------------------ 6 files changed, 30 insertions(+), 34 deletions(-) (limited to 'js/craftmine.js') diff --git a/inc/account.inc b/inc/account.inc index 19f311d..6f398bb 100644 --- a/inc/account.inc +++ b/inc/account.inc @@ -2,7 +2,7 @@ require_once("messages.inc"); function debitAccount($amount) { - if($_SESSION["mine"]["gold"] <= $amount) { + if($_SESSION["mine"]["gold"] < $amount) { sendError("gold_insufficient"); return false; } diff --git a/inc/craftmine.inc b/inc/craftmine.inc index a30538c..33a28d7 100644 --- a/inc/craftmine.inc +++ b/inc/craftmine.inc @@ -7,6 +7,7 @@ function sendCraftMine() { $data = array("gold" => sendMine(), "shop" => sendShop(), "inventory" => Inventory::sendContent(), + "miners" => sendMiners() ); echo json_encode($data); } diff --git a/inc/guild.inc b/inc/guild.inc index 4ca0262..c0e8264 100644 --- a/inc/guild.inc +++ b/inc/guild.inc @@ -1,30 +1,31 @@ = 50 && !isset($_SESSION["guild"])){ - $_SESSION["guild"]= array("miners" => 1); - $_SESSION["mine"]["gold"] -= 50; - echo "Guild has been successfully created"; + if(!empty($_SESSION["guild"])) { + sendError("guild_already_built"); } - else{ - echo "g"; + elseif(debitAccount(GUILD_COST)) { + $_SESSION["guild"] = true; + echo json_encode(array("cost" => GUILD_COST)); } } function hireMiner(){ if(!isset($_SESSION["guild"])){ - echo "you need to create a guild first"; + sendError("guild_not_yet_created"); } - if($_SESSION["mine"]["gold"] >= 20 && isset($_SESSION["guild"])){ - $_SESSION["guild"]["miners"]++; - $_SESSION["mine"]["gold"] -= 20; - $_SESSION["mine"]["miners"] = $_SESSION["guild"]["miners"]; - $mine = $_SESSION["mine"]; - echo $mine["gold"] +","+ $mine["miners"]; + elseif(debitAccount(MINER_COST)){ + $_SESSION["mine"]["miners"]++; + echo json_encode(array("cost" => MINER_COST , "miners" => $_SESSION["mine"]["miners"])); } } -//function buy($obj,$prix){} +function sendMiners(){ + return $_SESSION["mine"]["miners"]; +} ?> diff --git a/inc/messages.inc b/inc/messages.inc index f1ca8b3..d6ea87e 100644 --- a/inc/messages.inc +++ b/inc/messages.inc @@ -4,6 +4,8 @@ $messages = array( "shop_already_built" => "You have already built a shop.", "gold_insufficient" => "You don't have enough gold.", "shop_missing_item" => "This item does not exist.", + "guild_not_yet_created" => "You need to create a guild first.", + "guild_already_built" => "You have aready built a guild." ); function sendError($msg) { diff --git a/js/craftmine.js b/js/craftmine.js index 000a8f2..92fa45a 100644 --- a/js/craftmine.js +++ b/js/craftmine.js @@ -49,7 +49,8 @@ function initCraftMine() { data.gold = parseInt(ret.gold); // Server's response is a string if(ret.shop) displayShop(ret.shop); displayInventory(ret.inventory); - updateData("gold"); + data.miners = parseInt(ret.miners); + updateData("gold","miners"); }) } diff --git a/js/guild.js b/js/guild.js index cb9e783..8d4d91b 100644 --- a/js/guild.js +++ b/js/guild.js @@ -1,26 +1,17 @@ function hireMiner(){ - sendRequest("craftmine.php", "op=hireMiner", function(xhr) { - //data.gold -= 40; - var ret = xhr.responseText; - if(ret != "you need to create a guild first" && ret!=""){ - console.log(ret); - var tmp = ret.split(","); - console.log(tmp); - data.gold = parseInt(tmp[0]); - data.miners = parseInt(tmp[1]); - updateData("gold","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(xhr) { - var ret = xhr.responseText; - if(ret != "g"){ - document.getElementById("guild").innerHTML = ret; - data.gold -= 50; - updateData("gold"); + sendRequest("craftmine.php", "op=createGuild", function(ret) { + showInfo("Your guild has been successfully created"); + debitAccount(parseInt(ret.cost)); } - }) + ) } -- cgit v1.2.3-70-g09d2