aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpiernov <piernov@piernov.org>2016-05-07 13:48:28 +0200
committerpiernov <piernov@piernov.org>2016-05-07 13:48:28 +0200
commitb91588134f3067c2771011c10af77cda5f9031e8 (patch)
treea8e5f1bde0bfe9539bb5dd29e249ee8c6b1f36d6
parent8621eae6aa5fcdba0a60f0a5f1136412c61999ec (diff)
parent80f5826d1ac8f6d42866d5133c6f463943b96bab (diff)
downloadcandybox-b91588134f3067c2771011c10af77cda5f9031e8.tar.gz
candybox-b91588134f3067c2771011c10af77cda5f9031e8.tar.bz2
candybox-b91588134f3067c2771011c10af77cda5f9031e8.tar.xz
candybox-b91588134f3067c2771011c10af77cda5f9031e8.zip
Merge branch 'alexichi' into feat/gui
-rw-r--r--craftmine.php5
-rw-r--r--data/items.xml4
-rw-r--r--data/monsters.xml11
-rw-r--r--inc/Inventory.inc17
-rw-r--r--inc/Monster.inc4
-rw-r--r--inc/craftmine.inc2
-rw-r--r--inc/dungeon.inc21
-rw-r--r--inc/perso.inc23
-rw-r--r--inc/shop.inc6
-rw-r--r--index.xhtml4
-rw-r--r--js/craftmine.js18
-rw-r--r--js/dungeon.js109
-rw-r--r--js/perso.js5
-rw-r--r--js/shop.js16
14 files changed, 216 insertions, 29 deletions
diff --git a/craftmine.php b/craftmine.php
index 242e1fd..9a8806b 100644
--- a/craftmine.php
+++ b/craftmine.php
@@ -6,6 +6,7 @@ require_once("inc/shop.inc");
require_once("inc/craftmine.inc");
require_once("inc/dungeon.inc");
require_once("inc/savegame.inc");
+require_once("inc/perso.inc");
session_start(); // Must be placed *BEFORE* $_SESSION is actually used and *AFTER* all classes are imported
@@ -48,9 +49,13 @@ switch($op) {
case "getCraftMine": sendCraftMine(); break;
case "buildShop": buildShop(); break;
case "buyItem": buyItem(); break;
+ case "useItem": useItem(); break;
case "buildDungeon" : buildDungeon(); break;
case "launchDungeon" : launchDungeon(); break;
case "exitDungeon" : exitDungeon(); break;
+ case "sendDungeonProgress" : sendDungeonProgress(); break;
+ case "updatePerso" : updatePerso(); break;
+ case "sendPerso" : sendPerso(); break;
case "saveGame": saveGame(); break;
case "downSave": downSave(); break;
case "listSaves": listSaves(); break;
diff --git a/data/items.xml b/data/items.xml
index 8888796..6c19b84 100644
--- a/data/items.xml
+++ b/data/items.xml
@@ -25,7 +25,7 @@
<name>Life Bottle</name>
<cost>5</cost>
<features></features>
- <icon>💧</icon>
+ <icon>🍶</icon>
<description>A bottle which can heal you by regaining hp</description>
</item>
<item>
@@ -34,7 +34,7 @@
<features>
<power>20</power>
</features>
- <icon>💧</icon>
+ <icon>🍶</icon>
<description>If used, you have 20% more chance to hit your enemy during the battle</description>
</item>
</category>
diff --git a/data/monsters.xml b/data/monsters.xml
index 86cf422..ab29364 100644
--- a/data/monsters.xml
+++ b/data/monsters.xml
@@ -4,19 +4,22 @@
<monster>
<name>monster1</name>
<level>1</level>
- <hp>2</hp>
+ <hp>3</hp>
+ <xp>1</xp>
<icon>m</icon>
</monster>
<monster >
<name>monster2</name>
<level>2</level>
<hp>3</hp>
+ <xp>2</xp>
<icon>m</icon>
</monster>
<monster>
<name>monster3</name>
<level>3</level>
<hp>4</hp>
+ <xp>3</xp>
<icon>m</icon>
</monster>
</floor>
@@ -25,18 +28,21 @@
<name>monster4</name>
<level>4</level>
<hp>5</hp>
+ <xp>4</xp>
<icon>m</icon>
</monster>
<monster>
<name>monster5</name>
<level>5</level>
<hp>6</hp>
+ <xp>5</xp>
<icon>m</icon>
</monster>
<monster>
<name>monster5</name>
<level>6</level>
<hp>7</hp>
+ <xp>6</xp>
<icon>m</icon>
</monster>
</floor>
@@ -45,18 +51,21 @@
<name>monster6</name>
<level>8</level>
<hp>9</hp>
+ <xp>8</xp>
<icon>m</icon>
</monster>
<monster>
<name>monster7</name>
<level>9</level>
<hp>10</hp>
+ <xp>9</xp>
<icon>m</icon>
</monster>
<monster>
<name>Boss</name>
<level>10</level>
<hp>12</hp>
+ <xp>10</xp>
<icon>m</icon>
</monster>
</floor>
diff --git a/inc/Inventory.inc b/inc/Inventory.inc
index 024a4fe..747c4db 100644
--- a/inc/Inventory.inc
+++ b/inc/Inventory.inc
@@ -46,6 +46,23 @@ class Inventory {
$inv->_removeItem($item);
}
+ private function _useItem($item) {
+ foreach($this->items as $k => $object){
+ if($object[0] == $item){
+ if($this->items[$k][1]>0)$this->items[$k][1]--;
+ //if($this->items[$k][1] == 0) _removeItem($item);
+ return $this->items[$k];
+ }
+ }
+ return false;
+ }
+
+ public static function useItem($item) {
+ $inv = self::get();
+ $it = $inv->_useItem($item);
+ return $it;
+ }
+
public function addToXML($root) {
foreach($this->items as $item)
$item->addToXML($root);
diff --git a/inc/Monster.inc b/inc/Monster.inc
index fbb0fe9..c7063ec 100644
--- a/inc/Monster.inc
+++ b/inc/Monster.inc
@@ -5,12 +5,14 @@ class Monster {
public $icon = "";
public $desc = "";
public $hp = 1;
+ public $xp = 0;
public $level = 1;
- function __construct($name, $level, $hp, $icon) {
+ function __construct($name, $level, $hp, $xp, $icon) {
$this->name = $name;
$this->level = $level;
$this->hp = $hp;
+ $this->xp = $xp;
$this->icon = $icon;
}
diff --git a/inc/craftmine.inc b/inc/craftmine.inc
index 0d6a445..f5dbbb1 100644
--- a/inc/craftmine.inc
+++ b/inc/craftmine.inc
@@ -3,6 +3,7 @@
require_once("mine.inc");
require_once("shop.inc");
require_once("dungeon.inc");
+require_once("perso.inc");
function sendCraftMine() {
$data = array("gold" => sendMine(),
@@ -10,6 +11,7 @@ function sendCraftMine() {
"inventory" => Inventory::sendContent(),
"miners" => sendMiners(),
"dungeon" => sendDungeon(),
+ "perso" => sendPerso(),
);
echo json_encode($data);
}
diff --git a/inc/dungeon.inc b/inc/dungeon.inc
index 81e2ba7..c023cf8 100644
--- a/inc/dungeon.inc
+++ b/inc/dungeon.inc
@@ -12,8 +12,9 @@ function generateMonster(){
$dungeon["monsters"][$floor] = array();
foreach($f as $monster){
$dungeon["monsters"][$floor][] = new Monster((string)$monster->name,
- intval($monster->level),
- intval($monster->hp),
+ intval($monster->level),
+ intval($monster->hp),
+ intval($monster->xp),
(string)$monster->icon);
}
}
@@ -21,12 +22,12 @@ function generateMonster(){
}
function initDungeon() {
- $_SESSION["dungeon"] = true;
+ $_SESSION["dungeon"]["access"] = true;
}
function sendDungeon() {
if(!empty($_SESSION["dungeon"]))
- return generateMonster();
+ return $_SESSION["dungeon"];
else return false;
}
@@ -43,14 +44,22 @@ function buildDungeon() {
}
function launchDungeon(){
+ $f= $_POST["floor"];
$dungeon=generateMonster();
- $opponent = $dungeon["monsters"]["floor1"];
+ $opponent = $dungeon["monsters"]["floor".$f];
echo json_encode($opponent);
}
+function sendDungeonProgress(){
+ $f= $_POST["floor"];
+ $nb=$_POST["mob"];
+ $_SESSION["dungeon"]["flat"] = $f;
+ $_SESSION["dungeon"]["mob"] = $nb;
+}
+
+
function exitDungeon(){
$_SESSION["dungeon"] = false;
- echo json_encode(array("a" => 7));//Cette ligne ne sert qu'à contourner l'erreur créé à cause de sendRequest. A revoir.
}
?>
diff --git a/inc/perso.inc b/inc/perso.inc
new file mode 100644
index 0000000..0362d8c
--- /dev/null
+++ b/inc/perso.inc
@@ -0,0 +1,23 @@
+<?php
+function sendPerso() {
+ if(empty($_SESSION["perso"]))
+ initPerso();
+ return $_SESSION["perso"];
+}
+
+function updatePerso(){
+ $hp = $_POST["hp"];
+ $xp = $_POST["xp"];
+ $lv = $_POST["lv"];
+ $_SESSION["perso"]["hp"] = $hp;
+ $_SESSION["perso"]["xp"] = intval($xp);
+ $_SESSION["perso"]["lv"] = $lv;
+}
+
+function initPerso(){
+ $_SESSION["perso"]["hp"] = 5;
+ $_SESSION["perso"]["xp"] = 0;
+ $_SESSION["perso"]["lv"] = 3;
+}
+
+?>
diff --git a/inc/shop.inc b/inc/shop.inc
index 49ee20b..32ecea2 100644
--- a/inc/shop.inc
+++ b/inc/shop.inc
@@ -60,5 +60,11 @@ function buyItem() {
}
}
+function useItem(){
+ $item = getItem($_POST["item"]);
+ $it = Inventory::useItem($item);
+ echo json_encode($it);
+}
+
?>
diff --git a/index.xhtml b/index.xhtml
index dcfdc69..c67ee28 100644
--- a/index.xhtml
+++ b/index.xhtml
@@ -15,6 +15,7 @@
<script type="text/javascript" charset="utf-8" src="js/gui.js"></script>
<script type="text/javascript" charset="utf-8" src="js/dungeon.js"></script>
<script type="text/javascript" charset="utf-8" src="js/savegame.js"></script>
+ <script type="text/javascript" charset="utf-8" src="js/perso.js"></script>
</head>
<body onload="init()">
<div class="container-fluid">
@@ -22,6 +23,9 @@
<div class="col-md-3">
<div class="row">
+ <span id="perso">HP : <span id="hp">5</span>/ LV : <span id="lv">1</span>/ exp : <span id="xp">0</span></span>
+ </div>
+ <div class="row">
<div class="col-md-12">
<div class="panel panel-success">
<div class="panel-heading">
diff --git a/js/craftmine.js b/js/craftmine.js
index 0e95aa3..347ce65 100644
--- a/js/craftmine.js
+++ b/js/craftmine.js
@@ -3,8 +3,9 @@ data = {
gold: 0,
mine: 0,
miners: 0,
- level: 1,
- hp: 1,
+ level: 3,
+ hp: 5,
+ xp: 0,
icon : "H"
}
@@ -52,11 +53,20 @@ function withdrawMine() {
function initCraftMine() {
sendRequest("craftmine.php", "op=getCraftMine", function(ret) {
+ console.log(ret.perso);
data.gold = parseInt(ret.gold); // Server's response is a string
data.mine = 0; // Reset mine
+ if(ret.perso){
+ data.hp = ret.perso.hp;
+ data.xp = ret.perso.xp;
+ data.level = ret.perso.lv;
+ displayPerso(ret.perso.hp,ret.perso.xp,ret.perso.lv);
+ }
if(ret.shop) displayShop(ret.shop);
- displayInventory(ret.inventory);
- if(ret.dungeon) displayDungeon();
+ if(ret.inventory) displayInventory(ret.inventory);
+ if(ret.dungeon == false){}//if we have left the donjon
+ else if(typeof ret.dungeon.mob == "undefined") displayDungeon(0,1,true); //if we have reload just after buying the ticket
+ else displayDungeon(ret.dungeon.mob,ret.dungeon.flat,true);//if we have reload in the middle of the dungeon
data.miners = parseInt(ret.miners);
updateData("gold", "mine", "miners");
})
diff --git a/js/dungeon.js b/js/dungeon.js
index 4fb226d..c23b549 100644
--- a/js/dungeon.js
+++ b/js/dungeon.js
@@ -1,42 +1,121 @@
+var timeout;
+
function buildDungeon(){
sendRequest("craftmine.php", "op=buildDungeon", function(ret) {
- displayDungeon();
+ displayDungeon(0,1,true);//mob 0 in the floor 1 and I access the dungeon for the first dungeon
debitAccount(ret.cost);
showInfo("You can acces the dungeon now. Good Luck.");
});
}
-function displayDungeon(){
+function displayDungeon(nb,f,firstTime){
var tmphtml = "<br/>";
- tmphtml += "<button id=\"launch\" type=\"button\" class=\"btn btn-success\" onclick=\"launchDungeon()\">Launch</button>";
+ tmphtml += "<button id=\"launch\" type=\"button\" class=\"btn btn-success\" onclick=\"launchDungeon("+nb+","+f+","+firstTime+")\">Launch</button>";
document.getElementById("tab4").innerHTML = tmphtml;
}
-function launchDungeon(){
- sendRequest("craftmine.php", "op=launchDungeon", function(ret) {
- document.getElementById("launch").style.display = "none";
- displayBattle(ret);
+function launchDungeon(nb,f,firstTime){
+ sendRequest("craftmine.php", "op=launchDungeon&floor="+f, function(ret) {
+ if(nb==0 && firstTime){
+ document.getElementById("launch").style.display = "none";
+ }
+ displayBattle(ret,nb,f);
});
}
-function displayBattle(ret){
+function displayBattle(ret,nb,f){
var tmphtml = "<div class=\"row\">";
- tmphtml += "<h4>Battle</h4>";
+ tmphtml += "<h4>Battle floor "+ f +"</h4>";
tmphtml += "<ul class=\"list-inline\">";
tmphtml += "<li>";
- tmphtml += "<button type=\"button\" class=\"btn btn-success\" style=\"margin-left:30px;\"><span class=\"item-icon\">" + data.icon + "</span><br />" + data.name + "<br/> lv: " + data.level + " <span id=\"lifeMob\">hp: " + data.hp + "</span></button>";
- tmphtml += "<button type=\"button\" class=\"btn btn-success\" style=\"margin-left:30px;\"><span class=\"item-icon\">" + ret[0].icon + "</span><br />" + ret[0].name + "<br/> lv: " + ret[0].level + " <span id=\"lifeMob\">hp: " + ret[0].hp + "</span></button>";
+ tmphtml += "<button type=\"button\" class=\"btn btn-success\" id=\"perso\" style=\"margin-left:30px;\"><span class=\"item-icon\">" + data.icon + "</span><br />" + data.name + "<br/> lv: " + data.level + " hp: <span id=\"lifePerso\">" + data.hp + "</span></button>";
+ tmphtml += "<button type=\"button\" class=\"btn btn-success\" id=\"mob\" style=\"margin-left:30px;\">"
+ +"<span class=\"item-icon\">" + ret[nb].icon + "</span><br />" + ret[nb].name + "<br/>"
+ +" lv: " + ret[nb].level + " hp: <span id=\"lifeMob\">" + ret[nb].hp + "</span></button>";
tmphtml += "</li>";
tmphtml += "</ul>";
tmphtml += "</div>";
- tmphtml += "<br/><br/>";
- tmphtml += "<button type=\"button\" class=\"btn btn-success\" onclick=\"exitDungeon()\">Exit</button>";
document.getElementById("tab4").innerHTML = tmphtml;
+ displayExit();
+ battle(ret,nb,f);
+}
+
+function battle(ret,nb,f){
+ timeout=window.setTimeout(strike, 1000, ret, nb, f);
+}
+
+function strike(ret,nb, f){
+ var lvDiff = data.level-parseInt(ret[0].level);
+ var hitRate = Math.floor((Math.random() * 100) + 1);
+ var mobLife = document.getElementById("lifeMob").innerHTML;
+ var persoLife = document.getElementById("lifePerso").innerHTML;
+ if(mobLife == 0){
+ data.hp = persoLife;
+ endBattle("perso",nb,f,ret);
+ return;
+ }
+ else if(persoLife == 0){
+ data.hp = 1;
+ endBattle("mob",nb,f,ret);
+ return;
+ }
+ if(hitRate<50+10*lvDiff){
+ mobLife--;
+ document.getElementById("lifeMob").innerHTML = parseInt(mobLife);
+ }
+ else{
+ persoLife--;
+ document.getElementById("lifePerso").innerHTML = parseInt(persoLife);
+ }
+ battle(ret,nb,f);
+}
+
+function endBattle(v,nb, f, ret){
+ window.clearTimeout(timeout);
+ if(v=="mob")exitDungeon(false);//if you die in the dungeon, you are immediately sent out of the dungeon
+ //To level up you have to obtain 2 xp to go to lv 3, 3 to go to lv 4, etc
+ //A mob level 2 , if defeated gives you 2 xp, ...etc
+ else{
+ data.xp += parseInt(ret[nb].xp);
+ if(data.xp>=data.level){
+ data.xp-=data.level;
+ data.level++;
+ //need to send the xp to the server
+ sendRequest("craftmine.php", "op=updatePerso&hp="+data.hp+"&xp="+data.xp+"&lv="+data.level, function(){
+ displayPerso(data.hp,data.xp,data.level);
+ });
+ }
+ nb++;//go to the next mob in the same floor
+ if(nb>=3){//floor changing
+ nb=0;//reset the number of the mob
+ f++;//increment the number of the floor
+ if(f>=4){
+ exitDungeon(true);//true means that you have completed the dungeon and not just die or exit by yourself
+ return;
+ }
+ }
+ var tmphtml = "Le " + v + " a gagné.";
+ tmphtml += "<button type=\"button\" class=\"btn btn-primary\" onclick=\"launchDungeon("+nb+","+ f +",false)\">Next Battle</button>"
+ document.getElementById("tab4").innerHTML = tmphtml;
+ displayExit();
+ sendRequest("craftmine.php", "op=sendDungeonProgress&floor="+f+"&mob="+nb);
+ }
+ sendRequest("craftmine.php", "op=updatePerso&hp="+data.hp+"&xp="+data.xp+"&lv="+data.level, function(){
+ displayPerso(data.hp,parseInt(data.xp),data.level);
+ });
+}
+
+function displayExit(){
+ var tmphtml = "<br/><br/>";
+ tmphtml += "<button type=\"button\" class=\"btn btn-primary\" onclick=\"exitDungeon(false)\">Exit</button>";
+ document.getElementById("tab4").innerHTML += tmphtml;
}
-function exitDungeon(){
+function exitDungeon(boss){
sendRequest("craftmine.php", "op=exitDungeon", function() {
- document.getElementById("tab4").innerHTML = "<h4>Look at how poor you are! You can't access the dungeon, it is only for the elite.</h4>";
+ window.clearTimeout(timeout);
+ document.getElementById("tab4").innerHTML = "<h4>Not available, you have to buy a ticket in the build section.</h4>";
+ if(boss) showInfo("You have beaten the final boss! CONGRATULATIONS!");
showInfo("You have left the dungeon");
});
}
diff --git a/js/perso.js b/js/perso.js
new file mode 100644
index 0000000..365cf54
--- /dev/null
+++ b/js/perso.js
@@ -0,0 +1,5 @@
+function displayPerso(hp,xp,lv){
+ document.getElementById("hp").innerHTML = hp;
+ document.getElementById("lv").innerHTML = lv;
+ document.getElementById("xp").innerHTML = xp;
+}
diff --git a/js/shop.js b/js/shop.js
index a2f14ad..ce0ecd8 100644
--- a/js/shop.js
+++ b/js/shop.js
@@ -66,6 +66,22 @@ function buyItem(name) {
function useItem(name) {
sendRequest("craftmine.php", "op=useItem&item="+name, function(ret) {
+ if(parseInt(ret[1])>=0){
+ var nb = parseInt(document.getElementById("nbItem").innerHTML);
+ if(nb>0){
+ nb--;
+ switch(ret[0].name){
+ case "Life Bottle": data.hp = parseInt(data.hp) + 3; break;
+ case "Strength Bottle" : break; // to do
+ case "Wooden Sword" : break; //to do
+ case "Metal Sword" : break; //to do
+ }
+ }
+ document.getElementById("nbItem").innerHTML = nb;
+ sendRequest("craftmine.php", "op=updatePerso&hp="+data.hp+"&xp="+data.xp+"&lv="+data.level, function(){
+ displayPerso(data.hp,data.xp,data.level);
+ });
+ }
});
}