From 3c3d48c02730a826c7677dad2aaa01caf8e6de8b Mon Sep 17 00:00:00 2001 From: piernov Date: Wed, 27 Apr 2016 19:34:56 +0200 Subject: Also handle GET request for op in craftmine.php --- craftmine.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/craftmine.php b/craftmine.php index 78c5e37..dd2aa9e 100644 --- a/craftmine.php +++ b/craftmine.php @@ -7,6 +7,8 @@ require_once("inc/craftmine.inc"); session_start(); // Must be placed *BEFORE* $_SESSION is actually used and *AFTER* all classes are imported +$op = ""; + /** * Indique au client une message requete. */ @@ -27,14 +29,16 @@ function reportBadRequest() { exit(); } if (!isset($_POST["op"])) { - reportBadRequest(); -} + if(!isset($_GET["op"])) + reportBadRequest(); + else + $op = $_GET["op"]; +} else $op = $_POST["op"]; /** * On récupère l'opération à exécuter et on le fait. */ -$op = $_POST["op"]; switch($op) { case "withdrawMine": withdrawMine(); break; case "createGuild": createGuild(); break; -- cgit v1.2.3-70-g09d2 From 0133251e523b1c984b2b5cdb60c7392c18e6bd73 Mon Sep 17 00:00:00 2001 From: piernov Date: Wed, 27 Apr 2016 19:51:32 +0200 Subject: Add button for saving on server/downloading the game --- index.xhtml | 2 ++ js/craftmine.js | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/index.xhtml b/index.xhtml index aa29615..d12585f 100644 --- a/index.xhtml +++ b/index.xhtml @@ -32,6 +32,8 @@
+ +
diff --git a/js/craftmine.js b/js/craftmine.js index 92fa45a..13dd0a2 100644 --- a/js/craftmine.js +++ b/js/craftmine.js @@ -60,6 +60,14 @@ function updateMine() { updateData("mine"); } +function saveGame() { + sendRequest("craftmine.php", "op=saveGame"); +} + +function downGame() { + window.open("craftmine.php?op=downGame", "_blank"); +} + function init() { initCraftMine(); changeTab(); -- cgit v1.2.3-70-g09d2 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(-) 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-70-g09d2 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(+) 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-70-g09d2 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(-) 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-70-g09d2 From 043009630b775251ea20dcfb9316ac68585d1d3a Mon Sep 17 00:00:00 2001 From: piernov Date: Wed, 27 Apr 2016 20:39:23 +0200 Subject: Savegame PHP action handling --- craftmine.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/craftmine.php b/craftmine.php index dd2aa9e..ac187cc 100644 --- a/craftmine.php +++ b/craftmine.php @@ -4,6 +4,7 @@ require_once("inc/mine.inc"); require_once("inc/guild.inc"); require_once("inc/shop.inc"); require_once("inc/craftmine.inc"); +require_once("inc/savegame.inc"); session_start(); // Must be placed *BEFORE* $_SESSION is actually used and *AFTER* all classes are imported @@ -46,6 +47,8 @@ switch($op) { case "getCraftMine": sendCraftMine(); break; case "buildShop": buildShop(); break; case "buyItem": buyItem(); break; + case "saveGame": saveGame(); break; + case "downSave": downSave(); break; default: reportBadRequest(); } -- cgit v1.2.3-70-g09d2 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 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-70-g09d2 From caf62cfe0e439fc13f7c297954c4aeb21112a2ec Mon Sep 17 00:00:00 2001 From: piernov Date: Wed, 27 Apr 2016 20:46:27 +0200 Subject: Add data/save directory --- data/save/.gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 data/save/.gitignore diff --git a/data/save/.gitignore b/data/save/.gitignore new file mode 100644 index 0000000..4fcbff5 --- /dev/null +++ b/data/save/.gitignore @@ -0,0 +1 @@ +*.save.xml -- cgit v1.2.3-70-g09d2