aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--inc/messages.inc14
-rw-r--r--inc/savegame.inc20
-rw-r--r--index.xhtml6
-rw-r--r--js/craftmine.js5
-rw-r--r--js/savegame.js19
-rw-r--r--upload.php6
6 files changed, 60 insertions, 10 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");
+}
?>
diff --git a/index.xhtml b/index.xhtml
index ed3e7a6..9260134 100644
--- a/index.xhtml
+++ b/index.xhtml
@@ -34,8 +34,6 @@
<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>
@@ -92,7 +90,11 @@
</div>
<ul class="list-inline">
<li><button class="btn btn-default" type="button" onclick="loadSave()">Load</button></li>
+ <li><button class="btn btn-default" type="button" onclick="saveGame()">Save game</button></li>
+ <li><button class="btn btn-default" type="button" onclick="downGame()">Download current game</button></li>
<li><button class="btn btn-default" type="button" onclick="downloadSave()">Download</button></li>
+ <li><input type="file" id="selectedFile" style="display: none;" accept=".save.xml" onchange="uploadSave()" />
+ <button class="btn btn-default" type="button" onclick="document.getElementById('selectedFile').click()">Upload</button></li>
<li><button class="btn btn-default" type="button" onclick="deleteSave()">Delete</button></li>
</ul>
</div>
diff --git a/js/craftmine.js b/js/craftmine.js
index ffc9b5e..61abbf7 100644
--- a/js/craftmine.js
+++ b/js/craftmine.js
@@ -8,10 +8,10 @@ data = {
icon : "H"
}
-function sendRequest(url, params, callback) {
+function sendRequest(url, params, callback, isFile) {
var xhr = new XMLHttpRequest();
xhr.open("POST", url);
- xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
+ if(!isFile) xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
if(xhr.readyState == XMLHttpRequest.DONE && xhr.status == "200") {
var data = "";
@@ -71,5 +71,6 @@ function updateMine() {
function init() {
initCraftMine();
changeTab();
+ listSaves();
window.setInterval(updateMine, 1000);
}
diff --git a/js/savegame.js b/js/savegame.js
index 4a08f91..d5a9ba1 100644
--- a/js/savegame.js
+++ b/js/savegame.js
@@ -24,7 +24,9 @@ function loadSave() {
}
function downloadSave() {
- window.open("craftmine.php?op=downSave&filename="+getCheckedSave(), "_blank");
+ var filename = getCheckedSave();
+ if(filename == -1) downGame();
+ else window.open("craftmine.php?op=downSave&filename="+getCheckedSave(), "_blank");
}
function deleteSave() {
@@ -40,3 +42,18 @@ function saveGame() {
function downGame() {
window.open("craftmine.php?op=downSave", "_blank");
}
+
+function uploadSave() {
+ var selectedFile = document.getElementById("selectedFile");
+ if(selectedFile.files[0].size > 2000000) {
+ showError("File is too big.");
+ return;
+ }
+
+ var form = new FormData(); // Doesn't work with IE < 10 (and Opera Mini), but, as always, who cares?
+ form.append("savefile", selectedFile.files[0]);
+
+ sendRequest("upload.php", form, function(ret) {
+ console.log(ret);
+ }, true);
+}
diff --git a/upload.php b/upload.php
new file mode 100644
index 0000000..a6e1637
--- /dev/null
+++ b/upload.php
@@ -0,0 +1,6 @@
+<?php
+require_once("inc/savegame.inc");
+
+uploadSave();
+
+?>