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-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(+) (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-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(-) (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-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 (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-70-g09d2