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(-) (limited to 'craftmine.php') 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 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(+) (limited to 'craftmine.php') 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 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 'craftmine.php') 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-70-g09d2 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 'craftmine.php') 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-70-g09d2