diff options
author | piernov <piernov@piernov.org> | 2016-04-27 20:39:59 +0200 |
---|---|---|
committer | piernov <piernov@piernov.org> | 2016-04-27 20:39:59 +0200 |
commit | 5f3b810b6a141cf70b575430d08c5a57e3acc5ce (patch) | |
tree | e13a90f6fcbfa7215e3cc7f88417cba67f5715f1 | |
parent | 043009630b775251ea20dcfb9316ac68585d1d3a (diff) | |
download | candybox-5f3b810b6a141cf70b575430d08c5a57e3acc5ce.tar.gz candybox-5f3b810b6a141cf70b575430d08c5a57e3acc5ce.tar.bz2 candybox-5f3b810b6a141cf70b575430d08c5a57e3acc5ce.tar.xz candybox-5f3b810b6a141cf70b575430d08c5a57e3acc5ce.zip |
Add savegame.inc with methods for saving the game
-rw-r--r-- | inc/savegame.inc | 44 |
1 files changed, 44 insertions, 0 deletions
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(); +} + +?> |