diff options
-rw-r--r-- | craftmine.php | 13 | ||||
-rw-r--r-- | data/save/.gitignore | 1 | ||||
-rw-r--r-- | inc/Inventory.inc | 5 | ||||
-rw-r--r-- | inc/Item.inc | 8 | ||||
-rw-r--r-- | inc/messages.inc | 17 | ||||
-rw-r--r-- | inc/savegame.inc | 44 | ||||
-rw-r--r-- | index.xhtml | 2 | ||||
-rw-r--r-- | js/craftmine.js | 8 |
8 files changed, 92 insertions, 6 deletions
diff --git a/craftmine.php b/craftmine.php index 78c5e37..ac187cc 100644 --- a/craftmine.php +++ b/craftmine.php @@ -4,9 +4,12 @@ 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 +$op = ""; + /** * Indique au client une message requete. */ @@ -27,14 +30,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; @@ -42,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(); } 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 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); + } } ?> diff --git a/inc/messages.inc b/inc/messages.inc index d6ea87e..9f3a09d 100644 --- a/inc/messages.inc +++ b/inc/messages.inc @@ -5,13 +5,24 @@ $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 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); } ?> 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 @@ +<?php + +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)); + } + else + $xml->addChild($k, $v); +} + +function genSave() { + header("Content-Type: application/xml"); + $save = new SimpleXMLElement("<save/>"); + + 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(); +} + +?> diff --git a/index.xhtml b/index.xhtml index aa29615..d12585f 100644 --- a/index.xhtml +++ b/index.xhtml @@ -32,6 +32,8 @@ <form class="form-horizontal" method="post" action="craftmine.php"> <button class="btn btn-default" type="button" name="withdraw" onclick="withdrawMine()">Withdraw</button> <button class="btn btn-default" type="button" name="HireMiner" onclick="hireMiner()">Hire one miner</button> + <button class="btn btn-default" type="button" onclick="saveGame()">Save game</button> + <button class="btn btn-default" type="button" onclick="downGame()">Download game</button> </form> </div> </div> diff --git a/js/craftmine.js b/js/craftmine.js index 28583dd..8af8296 100644 --- a/js/craftmine.js +++ b/js/craftmine.js @@ -62,6 +62,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(); |