From 8a3610d9ac8349877b7a023a417288c2daee14ba Mon Sep 17 00:00:00 2001 From: piernov Date: Sun, 24 Apr 2016 22:05:25 +0200 Subject: Add sendError() PHP error message helper --- inc/messages.inc | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 inc/messages.inc diff --git a/inc/messages.inc b/inc/messages.inc new file mode 100644 index 0000000..f1ca8b3 --- /dev/null +++ b/inc/messages.inc @@ -0,0 +1,15 @@ + "You have already built a shop.", + "gold_insufficient" => "You don't have enough gold.", + "shop_missing_item" => "This item does not exist.", +); + +function sendError($msg) { + global $messages; + $text = $messages[$msg]; + echo json_encode(array("error" => $text)); +} + +?> -- cgit v1.2.3-70-g09d2 From 6dea6c0fbbc748e417076e7ca0d7773452b1bb57 Mon Sep 17 00:00:00 2001 From: piernov Date: Sun, 24 Apr 2016 22:06:16 +0200 Subject: Add PHP Item class --- inc/Item.inc | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 inc/Item.inc diff --git a/inc/Item.inc b/inc/Item.inc new file mode 100644 index 0000000..bf77818 --- /dev/null +++ b/inc/Item.inc @@ -0,0 +1,17 @@ +name = $name; + $this->cost = $cost; + $this->icon = $icon; + $this->desc = $desc; + } +} + +?> -- cgit v1.2.3-70-g09d2 From 38a80dc6468c7e7cfaa4e22d4e4b8e3b374cf388 Mon Sep 17 00:00:00 2001 From: piernov Date: Sun, 24 Apr 2016 22:06:40 +0200 Subject: Add PHP Inventory class --- inc/Inventory.inc | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 inc/Inventory.inc diff --git a/inc/Inventory.inc b/inc/Inventory.inc new file mode 100644 index 0000000..efe54f2 --- /dev/null +++ b/inc/Inventory.inc @@ -0,0 +1,41 @@ +items; + } + + public static function get() { + if(!self::created()) { + $_SESSION["inventory"] = new Inventory(); + } + return $_SESSION["inventory"]; + } + + private function _addItem($item) { + $this->items[] = $item; + } + + public static function addItem($item) { + $inv = self::get(); + $inv->_addItem($item); + } + + private function _removeItem($item) { + unset($this->items[array_search($item, $this->items)]); + } + + public static function removeItem($item) { + $inv = self::get(); + $inv->_removeItem($item); + } +} + +?> -- cgit v1.2.3-70-g09d2 From c8688e50d613b540913de570f3b64fd5ffcec161 Mon Sep 17 00:00:00 2001 From: piernov Date: Sun, 24 Apr 2016 22:08:10 +0200 Subject: Add debit/creditAccount() PHP account management helpers --- inc/account.inc | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 inc/account.inc diff --git a/inc/account.inc b/inc/account.inc new file mode 100644 index 0000000..19f311d --- /dev/null +++ b/inc/account.inc @@ -0,0 +1,18 @@ + -- cgit v1.2.3-70-g09d2 From 5a688c8c35d4a2bcca716aa7da1379c0abd48d3f Mon Sep 17 00:00:00 2001 From: piernov Date: Sun, 24 Apr 2016 22:08:58 +0200 Subject: Add PHP shop requests handlers --- inc/shop.inc | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 inc/shop.inc diff --git a/inc/shop.inc b/inc/shop.inc new file mode 100644 index 0000000..11cf97b --- /dev/null +++ b/inc/shop.inc @@ -0,0 +1,59 @@ + 3, + "items" => array( + new Item("cat", 6, "🐈", "Nyan!"), + new Item("torch", 3, "🔦", "Electric torch"), + ), +); + +function getItem($name) { + global $shop; + foreach($shop["items"] as $item) { + if($name == $item->name) { + return $item; + } + } + sendError("shop_missing_item"); + return false; +} + +function initShop() { + $_SESSION["shop"] = true; +} + +function sendShop() { + global $shop; + if(!empty($_SESSION["shop"])) + return $shop; + else return false; +} + +function buildShop() { + global $shop; + if(!empty($_SESSION["shop"])) { + sendError("shop_already_built"); + } + elseif(debitAccount($shop["cost"])) { + initShop(); + $_SESSION["mine"]["gold"] -= $shop["cost"]; + echo json_encode(sendShop()); + } +} + +function buyItem() { + $item = getItem($_POST["item"]); + if($item && debitAccount($item->cost)) { + Inventory::addItem($item); + echo json_encode($item); + } +} + + +?> -- cgit v1.2.3-70-g09d2 From e82fa2d62a1f0a6d0496c54425633042d972eb83 Mon Sep 17 00:00:00 2001 From: piernov Date: Sun, 24 Apr 2016 22:12:56 +0200 Subject: Add shop related PHP request handling --- craftmine.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/craftmine.php b/craftmine.php index 5df926f..4582f4e 100644 --- a/craftmine.php +++ b/craftmine.php @@ -2,6 +2,8 @@ session_start(); require_once("inc/mine.inc"); +require_once("inc/shop.inc"); + /** * Indique au client une message requete. */ @@ -33,6 +35,8 @@ $op = $_POST["op"]; switch($op) { case "withdrawMine": withdrawMine(); break; case "getCraftMine": sendMine(); break; + case "buildShop": buildShop(); break; + case "buyItem": buyItem(); break; default: reportBadRequest(); } -- cgit v1.2.3-70-g09d2 From f2c007680664004a0745d14ee375958ce4794330 Mon Sep 17 00:00:00 2001 From: piernov Date: Sun, 24 Apr 2016 22:13:44 +0200 Subject: Move session_start() after all requires (needed for classes to work correctly in $_SESSION) --- craftmine.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/craftmine.php b/craftmine.php index 4582f4e..c5f09be 100644 --- a/craftmine.php +++ b/craftmine.php @@ -1,9 +1,10 @@ Date: Sun, 24 Apr 2016 22:15:52 +0200 Subject: json_encode() PHP gold amount sent to client --- inc/mine.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/mine.inc b/inc/mine.inc index aea04ad..4293243 100644 --- a/inc/mine.inc +++ b/inc/mine.inc @@ -8,7 +8,7 @@ function withdrawMine() { $amount = intval($_POST["amount"]); if($amount == 0) return; $_SESSION["mine"]["gold"] += $amount; - echo $_SESSION["mine"]["gold"]; + echo json_encode($_SESSION["mine"]["gold"]); } function sendMine() { -- cgit v1.2.3-70-g09d2 From bd688fc969dfdda43e180db17095733d6f63da56 Mon Sep 17 00:00:00 2001 From: piernov Date: Sun, 24 Apr 2016 22:17:13 +0200 Subject: Send gold, shop and inventory infos from PHP all at once when requested on loading by client --- craftmine.php | 2 +- inc/craftmine.inc | 14 ++++++++++++++ inc/mine.inc | 2 +- 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 inc/craftmine.inc diff --git a/craftmine.php b/craftmine.php index c5f09be..869c83a 100644 --- a/craftmine.php +++ b/craftmine.php @@ -35,7 +35,7 @@ if (!isset($_POST["op"])) { $op = $_POST["op"]; switch($op) { case "withdrawMine": withdrawMine(); break; - case "getCraftMine": sendMine(); break; + case "getCraftMine": sendCraftMine(); break; case "buildShop": buildShop(); break; case "buyItem": buyItem(); break; default: reportBadRequest(); diff --git a/inc/craftmine.inc b/inc/craftmine.inc new file mode 100644 index 0000000..a30538c --- /dev/null +++ b/inc/craftmine.inc @@ -0,0 +1,14 @@ + sendMine(), + "shop" => sendShop(), + "inventory" => Inventory::sendContent(), + ); + echo json_encode($data); +} + +?> diff --git a/inc/mine.inc b/inc/mine.inc index 4293243..6a04cc4 100644 --- a/inc/mine.inc +++ b/inc/mine.inc @@ -14,7 +14,7 @@ function withdrawMine() { function sendMine() { if(empty($_SESSION["mine"])) initCraftMine(); $mine = $_SESSION["mine"]; - echo $mine["gold"]; + return $mine["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(+) 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(-) 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 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 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(+) 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 fde52230626138437e5836bf419ad3028ec58d94 Mon Sep 17 00:00:00 2001 From: piernov Date: Sun, 24 Apr 2016 22:29:57 +0200 Subject: Rewrote index.xhtml: GUI rework --- index.xhtml | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 72 insertions(+), 6 deletions(-) diff --git a/index.xhtml b/index.xhtml index 5b38b69..6ec45ef 100644 --- a/index.xhtml +++ b/index.xhtml @@ -1,5 +1,6 @@ + @@ -11,13 +12,78 @@ CraftMine + + - + +
    +

    CraftMine

    - 0 - 0 -
    - -
    +
    +
    +
    +
    +
    + 0 + 0 +
    + +
    +
    +
    + + + +
    +
    +
    + +
    + +
    +
    +

    Select a building to build:

    +
      +
    • +
    • +
    +
    +
    +

    There's no shop in this city.

    +
    +
    +

    Your inventory is empty.

    +
    +
    +

    Look at how poor you are! You can't access the dungeon, it is only for the elite.

    +
    +
    +
    + + + +
    +
    +
    +
    -- cgit v1.2.3-70-g09d2 From bc8e1117591e22a2ec850ce34358abadf950d87e Mon Sep 17 00:00:00 2001 From: piernov Date: Sun, 24 Apr 2016 22:30:11 +0200 Subject: Add css/craftmine.css --- css/craftmine.css | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 css/craftmine.css diff --git a/css/craftmine.css b/css/craftmine.css new file mode 100644 index 0000000..2a51ff7 --- /dev/null +++ b/css/craftmine.css @@ -0,0 +1,9 @@ +.tab-pane { + display: none; +} +.tab-pane:target { + display: block; +} +.item-icon { + font-size: 3em; +} -- cgit v1.2.3-70-g09d2