aboutsummaryrefslogtreecommitdiffstats
path: root/inc
diff options
context:
space:
mode:
authorpiernov <piernov@piernov.org>2016-05-04 14:39:10 +0200
committerpiernov <piernov@piernov.org>2016-05-04 14:39:10 +0200
commit30e4f5f70aef5de687bc021f8f7a1b34f209063f (patch)
treeffa3187c93dabafdb5a4ce9c1159381b297e3c1a /inc
parent676ea92ed1c88b7490feb17f90978f613783293d (diff)
parentec7771705d62d09fcd142c87f4941604f31c1d76 (diff)
downloadcandybox-30e4f5f70aef5de687bc021f8f7a1b34f209063f.tar.gz
candybox-30e4f5f70aef5de687bc021f8f7a1b34f209063f.tar.bz2
candybox-30e4f5f70aef5de687bc021f8f7a1b34f209063f.tar.xz
candybox-30e4f5f70aef5de687bc021f8f7a1b34f209063f.zip
Merge branch 'feat/savegame' into piernov
Diffstat (limited to 'inc')
-rw-r--r--inc/messages.inc14
-rw-r--r--inc/savegame.inc20
2 files changed, 29 insertions, 5 deletions
diff --git a/inc/messages.inc b/inc/messages.inc
index 60ae678..3a7d9b1 100644
--- a/inc/messages.inc
+++ b/inc/messages.inc
@@ -13,20 +13,24 @@ $messages = array(
"gamesave_not_found" => "Couldn't find the specified save file.",
"gamesave_delete_fail" => "Couldn't delete the specified save file.",
"gamesave_delete_success" => "Game save successfully removed from server",
+
+ "upload_fail" => "Could not upload save file.",
+ "upload_success" => "Save file uploaded successfully: %s",
);
-function sendMessage($type, $msg) {
+function sendMessage($type, $msg, $fmt = null) {
global $messages;
$text = $messages[$msg];
+ if($fmt) $text = vsprintf($text, $fmt);
echo json_encode(array($type => $text));
}
-function sendError($msg) {
- sendMessage("error", $msg);
+function sendError($msg, $fmt = null) {
+ sendMessage("error", $msg, $fmt);
}
-function sendInfo($msg) {
- sendMessage("info", $msg);
+function sendInfo($msg, $fmt = null) {
+ sendMessage("info", $msg, $fmt);
}
?>
diff --git a/inc/savegame.inc b/inc/savegame.inc
index 1fa95b0..8772585 100644
--- a/inc/savegame.inc
+++ b/inc/savegame.inc
@@ -89,4 +89,24 @@ function loadSave() {
$_SESSION = array(); // drop current game
parseSave($xml, $_SESSION);
}
+
+function uploadSave() {
+ $fname = basename($_FILES['savefile']['name']);
+ $src = $_FILES['savefile']['tmp_name'];
+
+ libxml_use_internal_errors(true); // Ignore errors when loading the received file
+ $xml = simplexml_load_file($src);
+ libxml_use_internal_errors(false);
+ if(!$xml) {
+ sendError("upload_fail");
+ return;
+ }
+ $table = array();
+ parseSave($xml, $table); // Parse received file
+
+ $save = new SimpleXMLElement("<save/>");
+ genXML($table, $save); // Regenerate it
+ if($save->asXML(SAVEDIR."/".$fname)) sendInfo("upload_success", array($fname));
+ else sendError("upload_error");
+}
?>