diff options
author | piernov <piernov@piernov.org> | 2016-04-27 20:53:43 +0200 |
---|---|---|
committer | piernov <piernov@piernov.org> | 2016-04-27 20:53:43 +0200 |
commit | 807e528d6a1ae40ab118f711fe2e8c2bb7df915a (patch) | |
tree | 1d4fbde95ff281d5bb628dcc545401146e26ff15 /inc | |
parent | 2b0a84a41eeb90094d42c3c516bf99837d08a745 (diff) | |
parent | caf62cfe0e439fc13f7c297954c4aeb21112a2ec (diff) | |
download | candybox-807e528d6a1ae40ab118f711fe2e8c2bb7df915a.tar.gz candybox-807e528d6a1ae40ab118f711fe2e8c2bb7df915a.tar.bz2 candybox-807e528d6a1ae40ab118f711fe2e8c2bb7df915a.tar.xz candybox-807e528d6a1ae40ab118f711fe2e8c2bb7df915a.zip |
Merge branch 'feat/savegame' into piernov
Diffstat (limited to 'inc')
-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 |
4 files changed, 71 insertions, 3 deletions
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(); +} + +?> |