From 1c8629abd2bd8fdcc29fe50d358c176bbfb2e3a3 Mon Sep 17 00:00:00 2001 From: piernov Date: Mon, 25 Apr 2016 12:31:00 +0200 Subject: Account already debited --- inc/shop.inc | 1 - 1 file changed, 1 deletion(-) (limited to 'inc') diff --git a/inc/shop.inc b/inc/shop.inc index 11cf97b..a0f9a8a 100644 --- a/inc/shop.inc +++ b/inc/shop.inc @@ -42,7 +42,6 @@ function buildShop() { } elseif(debitAccount($shop["cost"])) { initShop(); - $_SESSION["mine"]["gold"] -= $shop["cost"]; echo json_encode(sendShop()); } } -- cgit v1.2.3-54-g00ecf From a639278fcf395dd7349886785d56dee63c3a8774 Mon Sep 17 00:00:00 2001 From: piernov Date: Wed, 27 Apr 2016 20:00:13 +0200 Subject: Add sendInfo() from PHP --- inc/messages.inc | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'inc') diff --git a/inc/messages.inc b/inc/messages.inc index d6ea87e..5b1631f 100644 --- a/inc/messages.inc +++ b/inc/messages.inc @@ -8,10 +8,18 @@ $messages = array( "guild_already_built" => "You have aready built a guild." ); -function sendError($msg) { +function sendMessage($type, $msg) { global $messages; $text = $messages[$msg]; - echo json_encode(array("error" => $text)); + echo json_encode(array($type => $text)); +} + +function sendError($msg) { + sendMessage("error", $msg); +} + +function sendInfo($msg) { + sendMessage("info", $msg); } ?> -- cgit v1.2.3-54-g00ecf From 3d59c6ceca1c5b05b304e1f8146e32d701a17acc Mon Sep 17 00:00:00 2001 From: piernov Date: Wed, 27 Apr 2016 20:02:28 +0200 Subject: Add addToXML() methods on Inventory and Item objects --- inc/Inventory.inc | 5 +++++ inc/Item.inc | 8 ++++++++ 2 files changed, 13 insertions(+) (limited to 'inc') diff --git a/inc/Inventory.inc b/inc/Inventory.inc index efe54f2..0a93d7f 100644 --- a/inc/Inventory.inc +++ b/inc/Inventory.inc @@ -36,6 +36,11 @@ class Inventory { $inv = self::get(); $inv->_removeItem($item); } + + public function addToXML($root) { + foreach($this->items as $item) + $item->addToXML($root); + } } ?> diff --git a/inc/Item.inc b/inc/Item.inc index bf77818..bef6d00 100644 --- a/inc/Item.inc +++ b/inc/Item.inc @@ -12,6 +12,14 @@ class Item { $this->icon = $icon; $this->desc = $desc; } + + function addToXML($root) { + $item = $root->addChild("item"); + $item->addChild("name", $this->name); + $item->addChild("cost", $this->cost); + $item->addChild("icon", $this->icon); + $item->addChild("desc", $this->desc); + } } ?> -- cgit v1.2.3-54-g00ecf From 5cba16a79d041000fe482ae69e36577220d63d56 Mon Sep 17 00:00:00 2001 From: piernov Date: Wed, 27 Apr 2016 20:39:02 +0200 Subject: Add messages for savegame --- inc/messages.inc | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'inc') diff --git a/inc/messages.inc b/inc/messages.inc index 5b1631f..9f3a09d 100644 --- a/inc/messages.inc +++ b/inc/messages.inc @@ -5,7 +5,10 @@ $messages = array( "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." + "guild_already_built" => "You have aready built a guild.", + + "gamesave_ok" => "Game saved.", + "gamesave_error" => "An error occured when trying to save the game.", ); function sendMessage($type, $msg) { -- cgit v1.2.3-54-g00ecf From 5f3b810b6a141cf70b575430d08c5a57e3acc5ce Mon Sep 17 00:00:00 2001 From: piernov Date: Wed, 27 Apr 2016 20:39:59 +0200 Subject: Add savegame.inc with methods for saving the game --- inc/savegame.inc | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 inc/savegame.inc (limited to 'inc') diff --git a/inc/savegame.inc b/inc/savegame.inc new file mode 100644 index 0000000..f3b6da6 --- /dev/null +++ b/inc/savegame.inc @@ -0,0 +1,44 @@ +addToXML($xml->addChild($k)); + } + else + $xml->addChild($k, $v); +} + +function genSave() { + header("Content-Type: application/xml"); + $save = new SimpleXMLElement(""); + + array_walk_recursive($_SESSION, "genXML", $save); + return $save; + echo $save->asXML(); +} + +function genFilename() { + return "craftmine-".date("d-m-Y_H-i-s").".save.xml"; +} + +function saveGame() { + $save = genSave(); + if($save->asXML(SAVEDIR."/".genFilename())) sendInfo("gamesave_ok"); + else sendError("gamesave_error"); +} + +function downSave() { + $save = genSave(); + header("Content-Type: application/xml"); + header("Content-Disposition: attachment; filename=".genFilename()); + header("Pragma: no-cache"); + echo $save->asXML(); +} + +?> -- cgit v1.2.3-54-g00ecf From 29a1138f7d0c0e3b5ca785aef381bae96d282777 Mon Sep 17 00:00:00 2001 From: piernov Date: Wed, 27 Apr 2016 20:56:22 +0200 Subject: Remove useless mine.mine in --- inc/mine.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'inc') diff --git a/inc/mine.inc b/inc/mine.inc index 94a2c66..752fc69 100644 --- a/inc/mine.inc +++ b/inc/mine.inc @@ -1,7 +1,7 @@ 0, "gold" => 0, "miners" => 0); + $_SESSION["mine"] = array("gold" => 0, "miners" => 0); } function withdrawMine() { -- cgit v1.2.3-54-g00ecf From 76df4eb85cadf9cf548b4aa1cc95970e1d53f48d Mon Sep 17 00:00:00 2001 From: piernov Date: Sun, 1 May 2016 14:52:43 +0200 Subject: Implement loading saved games on server --- craftmine.php | 2 ++ inc/Item.inc | 4 ++++ inc/messages.inc | 1 + inc/savegame.inc | 30 ++++++++++++++++++++++++++++++ 4 files changed, 37 insertions(+) (limited to 'inc') diff --git a/craftmine.php b/craftmine.php index ac187cc..28bb49d 100644 --- a/craftmine.php +++ b/craftmine.php @@ -49,6 +49,8 @@ switch($op) { case "buyItem": buyItem(); break; case "saveGame": saveGame(); break; case "downSave": downSave(); break; + case "listSaves": listSaves(); break; + case "loadSave": loadSave(); break; default: reportBadRequest(); } diff --git a/inc/Item.inc b/inc/Item.inc index bef6d00..8e90998 100644 --- a/inc/Item.inc +++ b/inc/Item.inc @@ -20,6 +20,10 @@ class Item { $item->addChild("icon", $this->icon); $item->addChild("desc", $this->desc); } + + public static function fromXML($xml) { + return new static((string)$xml->name, +(string)$xml->cost /* convert to number */, (string)$xml->icon, (string)$xml->desc); + } } ?> diff --git a/inc/messages.inc b/inc/messages.inc index 9f3a09d..955c0a0 100644 --- a/inc/messages.inc +++ b/inc/messages.inc @@ -9,6 +9,7 @@ $messages = array( "gamesave_ok" => "Game saved.", "gamesave_error" => "An error occured when trying to save the game.", + "gamesave_not_found" => "Couldn't find the specified save file.", ); function sendMessage($type, $msg) { diff --git a/inc/savegame.inc b/inc/savegame.inc index f3b6da6..4646601 100644 --- a/inc/savegame.inc +++ b/inc/savegame.inc @@ -41,4 +41,34 @@ function downSave() { echo $save->asXML(); } +function listSaves() { + chdir(SAVEDIR); // Go to SAVEDIR folder, avoiding leading folder name in file list + echo json_encode(glob("*.save.xml")); +} + +function parseSave($xml, &$table) { // Passing $table by reference + foreach($xml as $k => $v) { + if($v->count() == 0) { // No child, treat as string + $v = (string)$v; + if(is_numeric($v)) $v = +$v; // If it is in fact a number, treat it that way using PHP unary '+' coercion + $table[$k] = $v; + } elseif($k == "inventory") { // Special case for inventory: objects need to be created + foreach($v as $item) Inventory::addItem(Item::fromXML($item)); + } else { // If nested array + $table[$k] = array(); + parseXML($v, $table[$k]); + } // Other types unsupported (unused) + } +} + +function loadSave() { + if(empty($_POST["filename"])) return; + $xml = simplexml_load_file(SAVEDIR . "/" . $_POST["filename"]); + if(empty($xml)) { + sendError("gamesave_not_found"); + return; + } + $_SESSION = array(); // drop current game + parseSave($xml, $_SESSION); +} ?> -- cgit v1.2.3-54-g00ecf From 888afbab390f227bb89c98847b516897886df3d3 Mon Sep 17 00:00:00 2001 From: piernov Date: Mon, 2 May 2016 15:48:37 +0200 Subject: Fix game save XML generation + add ability to send a previously generated game save --- inc/savegame.inc | 44 +++++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 17 deletions(-) (limited to 'inc') diff --git a/inc/savegame.inc b/inc/savegame.inc index 4646601..79b1dae 100644 --- a/inc/savegame.inc +++ b/inc/savegame.inc @@ -4,23 +4,22 @@ require_once("inc/messages.inc"); define("SAVEDIR", "data/save"); -function genXML($v, $k, $xml) { - if(is_object($v)) - { - if(is_callable(array($v, "addToXML"))) - $v->addToXML($xml->addChild($k)); +function genXML($table, $xml) { + foreach($table as $k => $v) { + if(is_object($v)) { // Object: either Item or Inventory + if(is_callable(array($v, "addToXML"))) // Check if the object has a addToXML method + $v->addToXML($xml->addChild($k)); + } elseif(is_array($v)) // Nested array + genXML($v, $xml->addChild($k)); + else // Single value (numeric or string) + $xml->addChild($k, $v); } - else - $xml->addChild($k, $v); } function genSave() { - header("Content-Type: application/xml"); $save = new SimpleXMLElement(""); - - array_walk_recursive($_SESSION, "genXML", $save); + genXML($_SESSION, $save); return $save; - echo $save->asXML(); } function genFilename() { @@ -34,11 +33,22 @@ function saveGame() { } function downSave() { - $save = genSave(); - header("Content-Type: application/xml"); - header("Content-Disposition: attachment; filename=".genFilename()); - header("Pragma: no-cache"); - echo $save->asXML(); + $save = ""; + $filename = ""; + + if(empty($_GET["filename"])) { + $filename = genFilename(); + $save = genSave()->asXML(); // Send current game save file if no file specified + } else { + $filename = $_GET["filename"]; + $save = file_get_contents(SAVEDIR . "/" . $filename); + if(empty($save)) return; // Probably file not found + } + + header("Content-Type: application/xml"); // Force browser to intepret the file as XML + header("Content-Disposition: attachment; filename=".$filename); // Force download with specific filename + header("Cache-Control: no-cache"); // Avoid storing save file in proxy/browser cache + echo $save; } function listSaves() { @@ -56,7 +66,7 @@ function parseSave($xml, &$table) { // Passing $table by reference foreach($v as $item) Inventory::addItem(Item::fromXML($item)); } else { // If nested array $table[$k] = array(); - parseXML($v, $table[$k]); + parseSave($v, $table[$k]); } // Other types unsupported (unused) } } -- cgit v1.2.3-54-g00ecf From 0b72114561a685b247922a93c576edabd4d3b09e Mon Sep 17 00:00:00 2001 From: piernov Date: Mon, 2 May 2016 15:59:16 +0200 Subject: Add ability to delete a save Doesn't work because idk which method to use and I don't have the PHP doc right now --- craftmine.php | 1 + inc/messages.inc | 2 ++ inc/savegame.inc | 7 +++++++ js/savegame.js | 3 +++ 4 files changed, 13 insertions(+) (limited to 'inc') diff --git a/craftmine.php b/craftmine.php index 28bb49d..9a5d3d7 100644 --- a/craftmine.php +++ b/craftmine.php @@ -51,6 +51,7 @@ switch($op) { case "downSave": downSave(); break; case "listSaves": listSaves(); break; case "loadSave": loadSave(); break; + case "deleteSave": deleteSave(); break; default: reportBadRequest(); } diff --git a/inc/messages.inc b/inc/messages.inc index 955c0a0..5574bcf 100644 --- a/inc/messages.inc +++ b/inc/messages.inc @@ -10,6 +10,8 @@ $messages = array( "gamesave_ok" => "Game saved.", "gamesave_error" => "An error occured when trying to save the game.", "gamesave_not_found" => "Couldn't find the specified save file.", + "gamesave_delete_error" => "Couldn't delete the specified save file.", + "gamesave_delete_success" => "Game save successfully removed from server", ); function sendMessage($type, $msg) { diff --git a/inc/savegame.inc b/inc/savegame.inc index 79b1dae..eb4134f 100644 --- a/inc/savegame.inc +++ b/inc/savegame.inc @@ -71,6 +71,13 @@ function parseSave($xml, &$table) { // Passing $table by reference } } +function deleteSave() { + if(empty($_POST["filename"])) return; + $filename = $_POST["filename"]; + if(rm_file(SAVEDIR + "/" + $filename)) sendError("gamesave_delete_failed"); // TODO: find the correct method for removing a file… + else sendInfo("gamesave_delete_success"); +} + function loadSave() { if(empty($_POST["filename"])) return; $xml = simplexml_load_file(SAVEDIR . "/" . $_POST["filename"]); diff --git a/js/savegame.js b/js/savegame.js index 438be0a..4a08f91 100644 --- a/js/savegame.js +++ b/js/savegame.js @@ -28,6 +28,9 @@ function downloadSave() { } function deleteSave() { + sendRequest("craftmine.php", "op=deleteSave&filename="+getCheckedSave(), function(ret) { + listSaves(); + }); } function saveGame() { -- cgit v1.2.3-54-g00ecf From 49aaf3ec355f783507875381a426ea350f0cdea1 Mon Sep 17 00:00:00 2001 From: piernov Date: Mon, 2 May 2016 19:22:05 +0200 Subject: Fix delete save game Yep, it was called unlink(). Who would've thought of that? Also, let's use the right operator and not fall for Javascript's silliness --- inc/savegame.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'inc') diff --git a/inc/savegame.inc b/inc/savegame.inc index eb4134f..2f8c70a 100644 --- a/inc/savegame.inc +++ b/inc/savegame.inc @@ -74,7 +74,7 @@ function parseSave($xml, &$table) { // Passing $table by reference function deleteSave() { if(empty($_POST["filename"])) return; $filename = $_POST["filename"]; - if(rm_file(SAVEDIR + "/" + $filename)) sendError("gamesave_delete_failed"); // TODO: find the correct method for removing a file… + if(unlink(SAVEDIR + "/" + $filename)) sendError("gamesave_delete_failed"); else sendInfo("gamesave_delete_success"); } -- cgit v1.2.3-54-g00ecf From 035a477c4f30180edecead29e8bcda34a0725881 Mon Sep 17 00:00:00 2001 From: piernov Date: Mon, 2 May 2016 19:58:11 +0200 Subject: Better when the user isn't allowed of deleting arbitrary files… Thanks PHP for not highligthing the problem and not providing simple solution… MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/savegame.inc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'inc') diff --git a/inc/savegame.inc b/inc/savegame.inc index 2f8c70a..0dfb1c8 100644 --- a/inc/savegame.inc +++ b/inc/savegame.inc @@ -73,8 +73,9 @@ function parseSave($xml, &$table) { // Passing $table by reference function deleteSave() { if(empty($_POST["filename"])) return; - $filename = $_POST["filename"]; - if(unlink(SAVEDIR + "/" + $filename)) sendError("gamesave_delete_failed"); + $path = SAVEDIR . "/" . basename($_POST["filename"]); // remove any leading directory + if(file_exists($path) && unlink($path)) + sendError("gamesave_delete_failed"); else sendInfo("gamesave_delete_success"); } -- cgit v1.2.3-54-g00ecf From bb029a5feff5c3d284ef8d509c116eee345c13b9 Mon Sep 17 00:00:00 2001 From: piernov Date: Mon, 2 May 2016 20:04:15 +0200 Subject: Better when error/info messages aren't swapped --- inc/messages.inc | 2 +- inc/savegame.inc | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'inc') diff --git a/inc/messages.inc b/inc/messages.inc index 5574bcf..4938fa1 100644 --- a/inc/messages.inc +++ b/inc/messages.inc @@ -10,7 +10,7 @@ $messages = array( "gamesave_ok" => "Game saved.", "gamesave_error" => "An error occured when trying to save the game.", "gamesave_not_found" => "Couldn't find the specified save file.", - "gamesave_delete_error" => "Couldn't delete the specified save file.", + "gamesave_delete_fail" => "Couldn't delete the specified save file.", "gamesave_delete_success" => "Game save successfully removed from server", ); diff --git a/inc/savegame.inc b/inc/savegame.inc index 0dfb1c8..1fa95b0 100644 --- a/inc/savegame.inc +++ b/inc/savegame.inc @@ -75,8 +75,8 @@ function deleteSave() { if(empty($_POST["filename"])) return; $path = SAVEDIR . "/" . basename($_POST["filename"]); // remove any leading directory if(file_exists($path) && unlink($path)) - sendError("gamesave_delete_failed"); - else sendInfo("gamesave_delete_success"); + sendInfo("gamesave_delete_success"); + else sendError("gamesave_delete_fail"); } function loadSave() { -- cgit v1.2.3-54-g00ecf