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/messages.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-54-g00ecf 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/messages.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-54-g00ecf From 31866085151518f468794dc71b541543e6b2f691 Mon Sep 17 00:00:00 2001 From: alexichi Date: Sun, 1 May 2016 11:32:08 +0200 Subject: add the beginning part of the dungeon When you have enough money you can buy a ticket and access the dungeon I made a file monsters.xml . It's a bit like the code with the shop but modified When you press the button launch in the onglet dungeon , it display "Battle!" in the console --- craftmine.php | 3 ++- data/monsters.xml | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ inc/Monster.inc | 19 +++++++++++++++++ inc/dungeon.inc | 39 ++++++++++++++++++++++++++++++++++ inc/messages.inc | 3 ++- index.xhtml | 3 +++ js/dungeon.js | 18 ++++++++++++++++ 7 files changed, 146 insertions(+), 2 deletions(-) create mode 100644 data/monsters.xml create mode 100644 inc/Monster.inc create mode 100644 inc/dungeon.inc create mode 100644 js/dungeon.js (limited to 'inc/messages.inc') diff --git a/craftmine.php b/craftmine.php index 78c5e37..4cd6b00 100644 --- a/craftmine.php +++ b/craftmine.php @@ -4,7 +4,7 @@ require_once("inc/mine.inc"); require_once("inc/guild.inc"); require_once("inc/shop.inc"); require_once("inc/craftmine.inc"); - +require_once("inc/dungeon.inc"); session_start(); // Must be placed *BEFORE* $_SESSION is actually used and *AFTER* all classes are imported /** @@ -42,6 +42,7 @@ switch($op) { case "getCraftMine": sendCraftMine(); break; case "buildShop": buildShop(); break; case "buyItem": buyItem(); break; + case "buildDungeon" : buildDungeon(); break; default: reportBadRequest(); } diff --git a/data/monsters.xml b/data/monsters.xml new file mode 100644 index 0000000..86cf422 --- /dev/null +++ b/data/monsters.xml @@ -0,0 +1,63 @@ + + + + + monster1 + 1 + 2 + m + + + monster2 + 2 + 3 + m + + + monster3 + 3 + 4 + m + + + + + monster4 + 4 + 5 + m + + + monster5 + 5 + 6 + m + + + monster5 + 6 + 7 + m + + + + + monster6 + 8 + 9 + m + + + monster7 + 9 + 10 + m + + + Boss + 10 + 12 + m + + + diff --git a/inc/Monster.inc b/inc/Monster.inc new file mode 100644 index 0000000..fbb0fe9 --- /dev/null +++ b/inc/Monster.inc @@ -0,0 +1,19 @@ +name = $name; + $this->level = $level; + $this->hp = $hp; + $this->icon = $icon; + + } +} + +?> diff --git a/inc/dungeon.inc b/inc/dungeon.inc new file mode 100644 index 0000000..b2277f0 --- /dev/null +++ b/inc/dungeon.inc @@ -0,0 +1,39 @@ +(string)$monsters["cost"],"monsters"=>array()); + foreach($monsters as $f){ + $floor = (string)$f["name"]; + $dungeon["monsters"][$floor] = array(); + foreach($f as $monster){ + $dungeon["monsters"][$floor][] = new Monster((string)$monster->name, + intval($monster->level), + intval($monster->hp), + (string)$monster->icon); + } + } + return $dungeon; +} + +function initDungeon() { + $_SESSION["dungeon"] = true; +} + +function buildDungeon() { + $dungeon=generateMonster(); + if(!empty($_SESSION["dungeon"])) { + sendError("dungeon_already_available"); + } + elseif(debitAccount($dungeon["cost"])) { + initDungeon(); + $_SESSION["mine"]["gold"] -= $dungeon["cost"]; + echo json_encode($dungeon); + } +} + +?> diff --git a/inc/messages.inc b/inc/messages.inc index d6ea87e..c24bd18 100644 --- a/inc/messages.inc +++ b/inc/messages.inc @@ -5,7 +5,8 @@ $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.", + "dungeon_already_available" => "You can already access the dungeon" ); function sendError($msg) { diff --git a/index.xhtml b/index.xhtml index aa29615..2745c8a 100644 --- a/index.xhtml +++ b/index.xhtml @@ -15,6 +15,8 @@ + +
@@ -69,6 +71,7 @@
  • +
diff --git a/js/dungeon.js b/js/dungeon.js new file mode 100644 index 0000000..1760afa --- /dev/null +++ b/js/dungeon.js @@ -0,0 +1,18 @@ +function buildDungeon(){ + sendRequest("craftmine.php", "op=buildDungeon", function(ret) { + displayDungeon(); + debitAccount(ret.cost); + showInfo("You can acces the dungeon now. Good Luck."); + }); +} + +function displayDungeon(){ + var tmphtml = "
"; + tmphtml += ""; + //tmphtml += ""; + document.getElementById("tab4").innerHTML = tmphtml; +} + +function launchDungeon(){ + console.log("Battle!"); +} -- cgit v1.2.3-54-g00ecf From 76df4eb85cadf9cf548b4aa1cc95970e1d53f48d Mon Sep 17 00:00:00 2001 From: piernov Date: Sun, 1 May 2016 14:52:43 +0200 Subject: Implement loading saved games on server --- craftmine.php | 2 ++ inc/Item.inc | 4 ++++ inc/messages.inc | 1 + inc/savegame.inc | 30 ++++++++++++++++++++++++++++++ 4 files changed, 37 insertions(+) (limited to 'inc/messages.inc') diff --git a/craftmine.php b/craftmine.php index ac187cc..28bb49d 100644 --- a/craftmine.php +++ b/craftmine.php @@ -49,6 +49,8 @@ switch($op) { case "buyItem": buyItem(); break; case "saveGame": saveGame(); break; case "downSave": downSave(); break; + case "listSaves": listSaves(); break; + case "loadSave": loadSave(); break; default: reportBadRequest(); } diff --git a/inc/Item.inc b/inc/Item.inc index bef6d00..8e90998 100644 --- a/inc/Item.inc +++ b/inc/Item.inc @@ -20,6 +20,10 @@ class Item { $item->addChild("icon", $this->icon); $item->addChild("desc", $this->desc); } + + public static function fromXML($xml) { + return new static((string)$xml->name, +(string)$xml->cost /* convert to number */, (string)$xml->icon, (string)$xml->desc); + } } ?> diff --git a/inc/messages.inc b/inc/messages.inc index 9f3a09d..955c0a0 100644 --- a/inc/messages.inc +++ b/inc/messages.inc @@ -9,6 +9,7 @@ $messages = array( "gamesave_ok" => "Game saved.", "gamesave_error" => "An error occured when trying to save the game.", + "gamesave_not_found" => "Couldn't find the specified save file.", ); function sendMessage($type, $msg) { diff --git a/inc/savegame.inc b/inc/savegame.inc index f3b6da6..4646601 100644 --- a/inc/savegame.inc +++ b/inc/savegame.inc @@ -41,4 +41,34 @@ function downSave() { echo $save->asXML(); } +function listSaves() { + chdir(SAVEDIR); // Go to SAVEDIR folder, avoiding leading folder name in file list + echo json_encode(glob("*.save.xml")); +} + +function parseSave($xml, &$table) { // Passing $table by reference + foreach($xml as $k => $v) { + if($v->count() == 0) { // No child, treat as string + $v = (string)$v; + if(is_numeric($v)) $v = +$v; // If it is in fact a number, treat it that way using PHP unary '+' coercion + $table[$k] = $v; + } elseif($k == "inventory") { // Special case for inventory: objects need to be created + foreach($v as $item) Inventory::addItem(Item::fromXML($item)); + } else { // If nested array + $table[$k] = array(); + parseXML($v, $table[$k]); + } // Other types unsupported (unused) + } +} + +function loadSave() { + if(empty($_POST["filename"])) return; + $xml = simplexml_load_file(SAVEDIR . "/" . $_POST["filename"]); + if(empty($xml)) { + sendError("gamesave_not_found"); + return; + } + $_SESSION = array(); // drop current game + parseSave($xml, $_SESSION); +} ?> -- cgit v1.2.3-54-g00ecf From 0b72114561a685b247922a93c576edabd4d3b09e Mon Sep 17 00:00:00 2001 From: piernov Date: Mon, 2 May 2016 15:59:16 +0200 Subject: Add ability to delete a save Doesn't work because idk which method to use and I don't have the PHP doc right now --- craftmine.php | 1 + inc/messages.inc | 2 ++ inc/savegame.inc | 7 +++++++ js/savegame.js | 3 +++ 4 files changed, 13 insertions(+) (limited to 'inc/messages.inc') diff --git a/craftmine.php b/craftmine.php index 28bb49d..9a5d3d7 100644 --- a/craftmine.php +++ b/craftmine.php @@ -51,6 +51,7 @@ switch($op) { case "downSave": downSave(); break; case "listSaves": listSaves(); break; case "loadSave": loadSave(); break; + case "deleteSave": deleteSave(); break; default: reportBadRequest(); } diff --git a/inc/messages.inc b/inc/messages.inc index 955c0a0..5574bcf 100644 --- a/inc/messages.inc +++ b/inc/messages.inc @@ -10,6 +10,8 @@ $messages = array( "gamesave_ok" => "Game saved.", "gamesave_error" => "An error occured when trying to save the game.", "gamesave_not_found" => "Couldn't find the specified save file.", + "gamesave_delete_error" => "Couldn't delete the specified save file.", + "gamesave_delete_success" => "Game save successfully removed from server", ); function sendMessage($type, $msg) { diff --git a/inc/savegame.inc b/inc/savegame.inc index 79b1dae..eb4134f 100644 --- a/inc/savegame.inc +++ b/inc/savegame.inc @@ -71,6 +71,13 @@ function parseSave($xml, &$table) { // Passing $table by reference } } +function deleteSave() { + if(empty($_POST["filename"])) return; + $filename = $_POST["filename"]; + if(rm_file(SAVEDIR + "/" + $filename)) sendError("gamesave_delete_failed"); // TODO: find the correct method for removing a fileā€¦ + else sendInfo("gamesave_delete_success"); +} + function loadSave() { if(empty($_POST["filename"])) return; $xml = simplexml_load_file(SAVEDIR . "/" . $_POST["filename"]); diff --git a/js/savegame.js b/js/savegame.js index 438be0a..4a08f91 100644 --- a/js/savegame.js +++ b/js/savegame.js @@ -28,6 +28,9 @@ function downloadSave() { } function deleteSave() { + sendRequest("craftmine.php", "op=deleteSave&filename="+getCheckedSave(), function(ret) { + listSaves(); + }); } function saveGame() { -- cgit v1.2.3-54-g00ecf From bb029a5feff5c3d284ef8d509c116eee345c13b9 Mon Sep 17 00:00:00 2001 From: piernov Date: Mon, 2 May 2016 20:04:15 +0200 Subject: Better when error/info messages aren't swapped --- inc/messages.inc | 2 +- inc/savegame.inc | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'inc/messages.inc') diff --git a/inc/messages.inc b/inc/messages.inc index 5574bcf..4938fa1 100644 --- a/inc/messages.inc +++ b/inc/messages.inc @@ -10,7 +10,7 @@ $messages = array( "gamesave_ok" => "Game saved.", "gamesave_error" => "An error occured when trying to save the game.", "gamesave_not_found" => "Couldn't find the specified save file.", - "gamesave_delete_error" => "Couldn't delete the specified save file.", + "gamesave_delete_fail" => "Couldn't delete the specified save file.", "gamesave_delete_success" => "Game save successfully removed from server", ); diff --git a/inc/savegame.inc b/inc/savegame.inc index 0dfb1c8..1fa95b0 100644 --- a/inc/savegame.inc +++ b/inc/savegame.inc @@ -75,8 +75,8 @@ function deleteSave() { if(empty($_POST["filename"])) return; $path = SAVEDIR . "/" . basename($_POST["filename"]); // remove any leading directory if(file_exists($path) && unlink($path)) - sendError("gamesave_delete_failed"); - else sendInfo("gamesave_delete_success"); + sendInfo("gamesave_delete_success"); + else sendError("gamesave_delete_fail"); } function loadSave() { -- cgit v1.2.3-54-g00ecf From 40b0d42dee76bc62517801aa746371a9ce2895a1 Mon Sep 17 00:00:00 2001 From: piernov Date: Wed, 4 May 2016 14:26:57 +0200 Subject: Now possible to add format to sendMessage() strings --- inc/messages.inc | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'inc/messages.inc') diff --git a/inc/messages.inc b/inc/messages.inc index 4938fa1..7b952b8 100644 --- a/inc/messages.inc +++ b/inc/messages.inc @@ -14,18 +14,19 @@ $messages = array( "gamesave_delete_success" => "Game save successfully removed from server", ); -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); } ?> -- cgit v1.2.3-54-g00ecf From 9ce1da5147b3df01812687562faca80623b27a38 Mon Sep 17 00:00:00 2001 From: piernov Date: Wed, 4 May 2016 14:27:35 +0200 Subject: Receive uploaded savegame from PHP --- inc/messages.inc | 3 +++ inc/savegame.inc | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+) (limited to 'inc/messages.inc') diff --git a/inc/messages.inc b/inc/messages.inc index 7b952b8..d08331a 100644 --- a/inc/messages.inc +++ b/inc/messages.inc @@ -12,6 +12,9 @@ $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, $fmt = null) { 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(""); + genXML($table, $save); // Regenerate it + if($save->asXML(SAVEDIR."/".$fname)) sendInfo("upload_success", array($fname)); + else sendError("upload_error"); +} ?> -- cgit v1.2.3-54-g00ecf From 676ea92ed1c88b7490feb17f90978f613783293d Mon Sep 17 00:00:00 2001 From: piernov Date: Wed, 4 May 2016 14:37:33 +0200 Subject: Missing comma --- inc/messages.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'inc/messages.inc') diff --git a/inc/messages.inc b/inc/messages.inc index c51adba..60ae678 100644 --- a/inc/messages.inc +++ b/inc/messages.inc @@ -6,7 +6,7 @@ $messages = array( "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.", - "dungeon_already_available" => "You can already access the dungeon" + "dungeon_already_available" => "You can already access the dungeon", "gamesave_ok" => "Game saved.", "gamesave_error" => "An error occured when trying to save the game.", -- cgit v1.2.3-54-g00ecf