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') 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') 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') 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') 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 96b345cdd68e0d5f434cd1bd25417e0b9eb62ba6 Mon Sep 17 00:00:00 2001 From: piernov Date: Sun, 24 Apr 2016 22:28:22 +0200 Subject: Add JS GUI helpers --- js/gui.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 js/gui.js (limited to 'js') 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 = "
  • " + msg + "
  • \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); +} -- 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') 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 8984aa2ecca7feacec93db35aee1570e40fa5123 Mon Sep 17 00:00:00 2001 From: alexichi Date: Mon, 25 Apr 2016 10:18:41 +0200 Subject: Add the guild add buttons to hireMiner and purchase the guild on xhtml javascript send request to the server php -> update $_SESSION for miner --- inc/guild.inc | 19 ++++++++++--------- index.xhtml | 26 +++++++++++++++++++++++--- js/guild.js | 24 ++++++++++++++++-------- 3 files changed, 49 insertions(+), 20 deletions(-) (limited to 'js') diff --git a/inc/guild.inc b/inc/guild.inc index 5ede1f6..4ca0262 100644 --- a/inc/guild.inc +++ b/inc/guild.inc @@ -1,29 +1,30 @@ = 150){ + if($_SESSION["mine"]["gold"] >= 50 && !isset($_SESSION["guild"])){ $_SESSION["guild"]= array("miners" => 1); - $_SESSION["mine"]["gold"] -= 150; + $_SESSION["mine"]["gold"] -= 50; echo "Guild has been successfully created"; } else{ - echo "Not enough money"; + echo "g"; } } function hireMiner(){ if(!isset($_SESSION["guild"])){ - echo "

    you need to create a guild first"; + echo "you need to create a guild first"; } - if($_SESSION["mine"]["gold"] >= 40){ + if($_SESSION["mine"]["gold"] >= 20 && isset($_SESSION["guild"])){ $_SESSION["guild"]["miners"]++; - $_SESSION["mine"]["gold"] -= 40; + $_SESSION["mine"]["gold"] -= 20; $_SESSION["mine"]["miners"] = $_SESSION["guild"]["miners"]; $mine = $_SESSION["mine"]; - $guild = $_SESSION["guild"]; - echo $mine["gold"]; - echo $mine["miners"]; + echo $mine["gold"] +","+ $mine["miners"]; } } +//function buy($obj,$prix){} + + ?> diff --git a/index.xhtml b/index.xhtml index 30352bf..495d6f4 100644 --- a/index.xhtml +++ b/index.xhtml @@ -21,8 +21,28 @@

    - - -
    + + + + + + +
    +

    Items Shop

    +
    wooden sword + +
    +
    golden sword + +
    +
    life bottle + +
    +
    strength bottle + +
    +
    + + diff --git a/js/guild.js b/js/guild.js index 28f58b8..cb9e783 100644 --- a/js/guild.js +++ b/js/guild.js @@ -1,18 +1,26 @@ function hireMiner(){ - sendRequest("craftmine.php", "op=hireMiner", function() { - //datas.gold -= parseInt(datas.mine); + sendRequest("craftmine.php", "op=hireMiner", function(xhr) { + //data.gold -= 40; var ret = xhr.responseText; - console.log(ret); - //datas.gold = ret; - //updateDatas("gold"); - //updateDatas("miners"); + 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"); + } }) } function createGuild(){ - sendRequest("craftmine.php", "op=createGuild", function() { + sendRequest("craftmine.php", "op=createGuild", function(xhr) { var ret = xhr.responseText; - console.log(ret); + if(ret != "g"){ + document.getElementById("guild").innerHTML = ret; + data.gold -= 50; + updateData("gold"); + } }) } -- cgit v1.2.3-70-g09d2 From 3ece645cc83e36aaa36c0258afa0f1b36eb13ca2 Mon Sep 17 00:00:00 2001 From: alexichi Date: Mon, 25 Apr 2016 10:25:46 +0200 Subject: remove useless files and add .gitignore to not see them again --- .gitignore | 1 + inc/mine.inc~ | 20 -------------------- js/craftmine.js~ | 51 --------------------------------------------------- 3 files changed, 1 insertion(+), 71 deletions(-) create mode 100644 .gitignore delete mode 100644 inc/mine.inc~ delete mode 100644 js/craftmine.js~ (limited to 'js') diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b25c15b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*~ diff --git a/inc/mine.inc~ b/inc/mine.inc~ deleted file mode 100644 index a01d768..0000000 --- a/inc/mine.inc~ +++ /dev/null @@ -1,20 +0,0 @@ - 0, "or" => 0, "miners" => 1); -} - -function withdrawMine() { - $mine = $_SESSION["mine"]; - $_SESSION["mine"]["gold"] += $mine["mine"]; - $_SESSION["mine"]["mine"] = 0; -} - -function sendMine() { - if(empty($_SESSION["mine"])) initMine(); - $mine = $_SESSION["mine"]; - echo $mine["gold"]; -} - - -?> 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); -} -- 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') 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 From 8140617aeb2f32f7095a443ca743c6d6915739c6 Mon Sep 17 00:00:00 2001 From: alexichi Date: Mon, 25 Apr 2016 13:45:05 +0200 Subject: change class of Hire a Miner, and change type submit to button for items --- index.xhtml | 2 +- js/shop.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'js') diff --git a/index.xhtml b/index.xhtml index 1685eb9..aa29615 100644 --- a/index.xhtml +++ b/index.xhtml @@ -31,7 +31,7 @@
    - +
    diff --git a/js/shop.js b/js/shop.js index 00636e1..22ca85f 100644 --- a/js/shop.js +++ b/js/shop.js @@ -25,7 +25,7 @@ function buildShop() { function addItem(ret) { var itemhtml = "
  • "; - itemhtml += ""; + itemhtml += ""; itemhtml += "
  • "; var invcontent = document.getElementById("tab3"); -- cgit v1.2.3-70-g09d2