From f171811a44364f605712aff1ca0808bfe570ef6e Mon Sep 17 00:00:00 2001 From: alexichi Date: Thu, 5 May 2016 23:06:29 +0200 Subject: add xp system and perso server side --- inc/perso.inc | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 inc/perso.inc (limited to 'inc/perso.inc') diff --git a/inc/perso.inc b/inc/perso.inc new file mode 100644 index 0000000..075f8ae --- /dev/null +++ b/inc/perso.inc @@ -0,0 +1,17 @@ + -- cgit v1.2.3-54-g00ecf From ee02581b7fabd087fc4056bda739c88656fbca14 Mon Sep 17 00:00:00 2001 From: alexichi Date: Sat, 7 May 2016 10:13:20 +0200 Subject: add the display of the perso characteristics below the title --- inc/perso.inc | 12 +++++++++--- js/craftmine.js | 2 +- js/dungeon.js | 18 +++++++++++++----- 3 files changed, 23 insertions(+), 9 deletions(-) (limited to 'inc/perso.inc') diff --git a/inc/perso.inc b/inc/perso.inc index 075f8ae..4fb05ef 100644 --- a/inc/perso.inc +++ b/inc/perso.inc @@ -1,8 +1,8 @@ diff --git a/js/craftmine.js b/js/craftmine.js index c7a396f..a8619df 100644 --- a/js/craftmine.js +++ b/js/craftmine.js @@ -3,7 +3,7 @@ data = { gold: 0, mine: 0, miners: 0, - level: 4, + level: 3, hp: 5, xp: 0, icon : "H" diff --git a/js/dungeon.js b/js/dungeon.js index 65d1081..30808ff 100644 --- a/js/dungeon.js +++ b/js/dungeon.js @@ -71,11 +71,11 @@ function strike(ret,nb, f){ function endBattle(v,nb, f, ret){ window.clearTimeout(timeout); - if(v=="mob")exitDungeon();//if you die in the dungeon, you are immediately sent out of the dungeon + 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 += ret[nb].xp; + data.xp += parseInt(ret[nb].xp); if(data.xp>=data.level){ data.xp-=data.level; data.level++; @@ -88,6 +88,10 @@ function endBattle(v,nb, f, ret){ 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 += "" @@ -95,18 +99,22 @@ function endBattle(v,nb, f, ret){ 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,data.xp,data.level); + }); } function displayExit(){ var tmphtml = "

"; - tmphtml += ""; + tmphtml += ""; document.getElementById("tab4").innerHTML += tmphtml; } -function exitDungeon(){ +function exitDungeon(boss){ sendRequest("craftmine.php", "op=exitDungeon", function() { window.clearTimeout(timeout); - document.getElementById("tab4").innerHTML = "

Look at how poor you are! You can't access the dungeon, it is only for the elite.

"; + document.getElementById("tab4").innerHTML = "

Not available, you have to buy a ticket in the build section.

"; + if(boss) showInfo("You have beaten the final boss! CONGRATULATIONS!"); showInfo("You have left the dungeon"); }); } -- cgit v1.2.3-54-g00ecf From 79cfbb29042fd60dfbc76a6810f75cce21d3ffe0 Mon Sep 17 00:00:00 2001 From: alexichi Date: Sat, 7 May 2016 12:08:36 +0200 Subject: add the use of the life bottle --- craftmine.php | 1 + inc/Inventory.inc | 17 +++++++++++++++++ inc/perso.inc | 2 +- inc/shop.inc | 6 ++++++ js/craftmine.js | 2 +- js/dungeon.js | 3 ++- js/shop.js | 18 +++++++++++++++++- 7 files changed, 45 insertions(+), 4 deletions(-) (limited to 'inc/perso.inc') diff --git a/craftmine.php b/craftmine.php index d47989c..9a8806b 100644 --- a/craftmine.php +++ b/craftmine.php @@ -49,6 +49,7 @@ 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; 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/perso.inc b/inc/perso.inc index 4fb05ef..0362d8c 100644 --- a/inc/perso.inc +++ b/inc/perso.inc @@ -10,7 +10,7 @@ function updatePerso(){ $xp = $_POST["xp"]; $lv = $_POST["lv"]; $_SESSION["perso"]["hp"] = $hp; - $_SESSION["perso"]["xp"] = $xp; + $_SESSION["perso"]["xp"] = intval($xp); $_SESSION["perso"]["lv"] = $lv; } 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/js/craftmine.js b/js/craftmine.js index a8619df..70becec 100644 --- a/js/craftmine.js +++ b/js/craftmine.js @@ -63,7 +63,7 @@ function initCraftMine() { displayPerso(ret.perso.hp,ret.perso.xp,ret.perso.lv); } if(ret.shop) displayShop(ret.shop); - displayInventory(ret.inventory); + 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 diff --git a/js/dungeon.js b/js/dungeon.js index 30808ff..982dcf6 100644 --- a/js/dungeon.js +++ b/js/dungeon.js @@ -55,6 +55,7 @@ function strike(ret,nb, f){ return; } else if(persoLife == 0){ + data.hp = 1; endBattle("mob",nb,f,ret); return; } @@ -100,7 +101,7 @@ function endBattle(v,nb, f, ret){ 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,data.xp,data.level); + displayPerso(data.hp,parseInt(data.xp),data.level); }); } diff --git a/js/shop.js b/js/shop.js index bda3885..efb347a 100644 --- a/js/shop.js +++ b/js/shop.js @@ -37,7 +37,7 @@ function addItem(ret) { var itemtag = document.querySelector("[data-name=\""+ret[0].name+"\"]"); if(!itemtag){ //si c'est la première itération de l'objet itemhtml += "
  • "; - itemhtml += ""; + itemhtml += ""; itemhtml += "
  • "; } else{ // si c'est une n-ième itération @@ -68,6 +68,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); + }); + } }); } -- cgit v1.2.3-54-g00ecf From f734770dd4fe464a8c263218efa90fd487c6a608 Mon Sep 17 00:00:00 2001 From: piernov Date: Sun, 8 May 2016 14:41:25 +0200 Subject: Add feature attribute to Item class + add consume() method to use those features + use coercion on updatePerso() values --- inc/Item.inc | 15 ++++++++++++++- inc/perso.inc | 13 ++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) (limited to 'inc/perso.inc') diff --git a/inc/Item.inc b/inc/Item.inc index 907872e..f01b709 100644 --- a/inc/Item.inc +++ b/inc/Item.inc @@ -1,16 +1,29 @@ name = $name; $this->cost = $cost; $this->icon = $icon; $this->desc = $desc; + $this->feat = $feat; + } + + function consume() { + foreach($this->feat as $k => $v) { + switch($k) { + case "hp": increasePerso("hp", +$v); break; + case "power": break; + } + } } function addToXML($root, $count) { diff --git a/inc/perso.inc b/inc/perso.inc index 0362d8c..f455173 100644 --- a/inc/perso.inc +++ b/inc/perso.inc @@ -1,17 +1,24 @@ Date: Mon, 9 May 2016 20:26:51 +0200 Subject: add the use of the objects and the feature power and maxHP --- data/items.xml | 9 ++++++--- data/monsters.xml | 33 +++++++++++++++++++++------------ inc/Inventory.inc | 14 +++++++++----- inc/Item.inc | 2 +- inc/Monster.inc | 4 +++- inc/dungeon.inc | 3 ++- inc/perso.inc | 33 +++++++++++++++++++++++++++++++++ index.xhtml | 10 +++++++++- js/craftmine.js | 17 +++++++++++++++-- js/dungeon.js | 35 +++++++++++++++++++---------------- js/perso.js | 24 ++++++++++++++++++++++-- js/shop.js | 2 -- 12 files changed, 140 insertions(+), 46 deletions(-) (limited to 'inc/perso.inc') diff --git a/data/items.xml b/data/items.xml index 9489b1a..4448b1c 100644 --- a/data/items.xml +++ b/data/items.xml @@ -5,7 +5,8 @@ Wooden Sword 10 - 10 + +1 + 1 A sword that beginners need to use to improve their skills @@ -14,7 +15,8 @@ Metal Sword 15 - 30 + +3 + 1 A sword which are often use buy skilled knight. The material is very good and the sword is very powerful @@ -34,7 +36,8 @@ Strength Bottle 10 - 20 + +2 + 3 🍶 If used, you have 20% more chance to hit your enemy during the battle diff --git a/data/monsters.xml b/data/monsters.xml index ab29364..a8dc2f7 100644 --- a/data/monsters.xml +++ b/data/monsters.xml @@ -6,21 +6,24 @@ 1 3 1 - m + 1 + 👾 monster2 2 3 2 - m + 2 + 👾 monster3 3 4 3 - m + 3 + 👾 @@ -29,44 +32,50 @@ 4 5 4 - m + 4 + 👾 monster5 5 6 5 - m + 5 + 👾 - monster5 + monster6 6 7 6 - m + 6 + 👾 - monster6 + monster7 8 9 8 - m + 8 + 👾 - monster7 + monster8 9 10 9 - m + 9 + 👾 Boss 10 12 10 - m + 14 + 👾 diff --git a/inc/Inventory.inc b/inc/Inventory.inc index 2be48f8..3e0137d 100644 --- a/inc/Inventory.inc +++ b/inc/Inventory.inc @@ -1,5 +1,7 @@ items as $k => $object){ if($object[0] == $item) { $nb = $this->items[$k][1]; - if($nb > 0) { - $this->items[$k][0]->consume(); - $this->items[$k][1]--; + if(limitUse($this->items[$k][0])){ + if($nb > 0) { + $this->items[$k][0]->consume(); + $this->items[$k][1]--; + } + if($nb-1 <= 0) $this->_removeItem($item); + return array($object[0], $nb-1); } - if($nb-1 <= 0) $this->_removeItem($item); - return array($object[0], $nb-1); } } return false; diff --git a/inc/Item.inc b/inc/Item.inc index 8eebbf8..a8ee302 100644 --- a/inc/Item.inc +++ b/inc/Item.inc @@ -21,7 +21,7 @@ class Item { foreach($this->feat as $k => $v) { switch($k) { case "hp": increasePerso("hp", +$v); break; - case "power": break; + case "power": increasePerso("bonusPower", +$v); break; } } } diff --git a/inc/Monster.inc b/inc/Monster.inc index c7063ec..ae48691 100644 --- a/inc/Monster.inc +++ b/inc/Monster.inc @@ -7,12 +7,14 @@ class Monster { public $hp = 1; public $xp = 0; public $level = 1; + public $power = 1; - function __construct($name, $level, $hp, $xp, $icon) { + function __construct($name, $level, $hp, $xp, $power, $icon) { $this->name = $name; $this->level = $level; $this->hp = $hp; $this->xp = $xp; + $this->power = $power; $this->icon = $icon; } diff --git a/inc/dungeon.inc b/inc/dungeon.inc index c023cf8..521303e 100644 --- a/inc/dungeon.inc +++ b/inc/dungeon.inc @@ -14,7 +14,8 @@ function generateMonster(){ $dungeon["monsters"][$floor][] = new Monster((string)$monster->name, intval($monster->level), intval($monster->hp), - intval($monster->xp), + intval($monster->xp), + intval($monster->power), (string)$monster->icon); } } diff --git a/inc/perso.inc b/inc/perso.inc index f455173..0a32b64 100644 --- a/inc/perso.inc +++ b/inc/perso.inc @@ -10,21 +10,54 @@ function increasePerso($prop, $num) { if(empty($_SESSION["perso"])) initPerso(); $_SESSION["perso"][$prop] += $num; + if($_SESSION["perso"]["hp"] > $_SESSION["perso"]["maxHP"]){//if you want to heal even if you have less than 3 hp to heal, heal until the max is attained + $diff = $_SESSION["perso"]["hp"] - $_SESSION["perso"]["maxHP"]; + $_SESSION["perso"]["hp"] -= $diff; + } +} + +/** + *traite le fait que wooden sword n'est pas cumulable + *metal sword non plus + *life bottle cumulable 3 fois + *si on clique sur wooden sword alors que on avait une metal sword, le bonusPower passe de +3 à +1 + */ +function limitUse($item){ + $n = $item->name; + if($n =="Life Bottle")return true; + if(empty($_SESSION[$n])){ + $_SESSION[$n]=1; + return true; + } + else{ + $_SESSION[$n]++; + if($_SESSION[$n] >= $item->feat["limit"])return false; + else return true; + } } function updatePerso(){ $hp = $_POST["hp"]; + $maxHP = $_POST["maxHP"]; $xp = $_POST["xp"]; $lv = $_POST["lv"]; + $power = $_POST["power"]; + $bonusPower = $_POST["bonusPower"]; $_SESSION["perso"]["hp"] = +$hp; + $_SESSION["perso"]["maxHP"] = +$maxHP; $_SESSION["perso"]["xp"] = +$xp; $_SESSION["perso"]["lv"] = +$lv; + $_SESSION["perso"]["power"] = +$power; + $_SESSION["perso"]["bonusPower"] = +$bonusPower; } function initPerso(){ $_SESSION["perso"]["hp"] = 5; + $_SESSION["perso"]["maxHP"] = 5; $_SESSION["perso"]["xp"] = 0; $_SESSION["perso"]["lv"] = 3; + $_SESSION["perso"]["power"] = 3; + $_SESSION["perso"]["bonusPower"] = 0; } ?> diff --git a/index.xhtml b/index.xhtml index 1ea1bfe..eba0f43 100644 --- a/index.xhtml +++ b/index.xhtml @@ -30,9 +30,17 @@
      -
    • HP5
    • +
    • HP + 5 / + 5 + +
    • LV1
    • Exp0
    • +
    • Power + 1 + + 0 +
      diff --git a/js/craftmine.js b/js/craftmine.js index 77cafee..fa70809 100644 --- a/js/craftmine.js +++ b/js/craftmine.js @@ -5,8 +5,11 @@ data = { miners: 0, level: 3, hp: 5, + maxHP: 5, xp: 0, - icon : "H" + power: 3, + bonusPower: 0, + icon : "💪" } function sendRequest(url, params, callback, isFile) { @@ -41,6 +44,16 @@ function debitAccount(amount) { updateData("gold"); } +function creditAccount(amount){ + data.gold += amount; + sendRequest("craftmine.php", "op=withdrawMine&amount="+amount, function(xhr) { + var gold = parseInt(xhr); + if(isNaN(gold)) return; + data.gold = gold; + updateData("gold"); + }); +} + function withdrawMine() { sendRequest("craftmine.php", "op=withdrawMine&amount="+data.mine, function(xhr) { var gold = parseInt(xhr); // Server's response is a string @@ -53,7 +66,7 @@ function withdrawMine() { function initCraftMine() { sendRequest("craftmine.php", "op=getCraftMine", function(ret) { - console.log(ret.perso); + //console.log(ret.perso); data.gold = parseInt(ret.gold); // Server's response is a string data.mine = 0; // Reset mine if(ret.perso) updatePerso(ret.perso); diff --git a/js/dungeon.js b/js/dungeon.js index 6ebf4dd..2921c7d 100644 --- a/js/dungeon.js +++ b/js/dungeon.js @@ -2,7 +2,7 @@ var timeout; function buildDungeon(){ sendRequest("craftmine.php", "op=buildDungeon", function(ret) { - displayDungeon(0,1,true);//mob 0 in the floor 1 and I access the dungeon for the first dungeon + displayDungeon(0,1,true);//mob 0 in the floor 1 and I access the dungeon for the first time debitAccount(ret.cost); showInfo("You can acces the dungeon now. Good Luck."); }); @@ -45,21 +45,23 @@ function battle(ret,nb,f){ } function strike(ret,nb, f){ - var lvDiff = data.level-parseInt(ret[0].level); + var powerDiff = (data.power+data.bonusPower)-parseInt(ret[nb].power); 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; + data.bonusPower = 0; //à revoir car l'épée doit continuer à apporter un bonus endBattle("perso",nb,f,ret); return; } else if(persoLife == 0){ data.hp = 1; + data.bonusPower = 0;//when you die you lose your sword and all your power bonus endBattle("mob",nb,f,ret); return; } - if(hitRate<50+10*lvDiff){ + if(hitRate<50+10*powerDiff){ mobLife--; document.getElementById("lifeMob").innerHTML = parseInt(mobLife); } @@ -76,15 +78,7 @@ function endBattle(v,nb, f, ret){ //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); - }); - } + levelUp(ret[nb].xp); nb++;//go to the next mob in the same floor if(nb>=3){//floor changing nb=0;//reset the number of the mob @@ -100,8 +94,8 @@ function endBattle(v,nb, f, ret){ 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); + sendRequest("craftmine.php", "op=updatePerso&hp="+data.hp+"&maxHP="+data.maxHP+"&xp="+data.xp+"&lv="+data.level+"&power="+data.power+"&bonusPower="+data.bonusPower, function(){ + displayPerso(data.hp,data.maxHP,data.xp,data.level,data.power,data.bonusPower); }); } @@ -114,8 +108,17 @@ function displayExit(){ function exitDungeon(boss){ sendRequest("craftmine.php", "op=exitDungeon", function() { window.clearTimeout(timeout); - document.getElementById("tab4").innerHTML = "

      Not available, you have to buy a ticket in the build section.

      "; - if(boss) showInfo("You have beaten the final boss! CONGRATULATIONS!"); + if(boss){//if the boss is beaten + levelUp(20);//you earn 20 xp + creditAccount(1000);//you earn 1000 gold coins + var tmphtml = "

      You have completed the dungeon! CONGRATULATIONS!

      "; + tmphmtl += "
        "; + tmphtml += "
      • You have earned 1000 gold coins

      • " + tmphmtl += "
      • You have earned 20 xp

      • "; + tmphmtl += "
      "; + document.getElementById("tab4").innerHTML = tmphtml; + } + else document.getElementById("tab4").innerHTML = "

      Not available, you have to buy a ticket in the build section.

      "; showInfo("You have left the dungeon"); }); } diff --git a/js/perso.js b/js/perso.js index 02a8892..44b82eb 100644 --- a/js/perso.js +++ b/js/perso.js @@ -1,12 +1,32 @@ -function displayPerso(hp,xp,lv){ +function displayPerso(hp,maxHP,xp,lv,power,bonusPower){ document.getElementById("hp").innerHTML = hp; + document.getElementById("maxHP").innerHTML = maxHP; document.getElementById("lv").innerHTML = lv; document.getElementById("xp").innerHTML = xp; + document.getElementById("power").innerHTML = power; + document.getElementById("bonusPower").innerHTML = bonusPower; } function updatePerso(perso) { data.hp = perso.hp; + data.maxHP = perso.maxHP; data.xp = perso.xp; data.level = perso.lv; - displayPerso(perso.hp, perso.xp, perso.lv); + data.power = perso.power; + data.bonusPower = perso.bonusPower; + displayPerso(perso.hp, perso.maxHP, perso.xp, perso.lv, perso.power, perso.bonusPower); +} + +function levelUp(xp){ + data.xp += parseInt(xp); + while(data.xp>=data.level){ + data.xp-=data.level; + data.level++; + data.power++; + data.maxHP+=2; + } + //need to send the xp to the server + sendRequest("craftmine.php", "op=updatePerso&hp="+data.hp+"&maxHP="+data.maxHP+"&xp="+data.xp+"&lv="+data.level+"&power="+data.power+"&bonusPower="+bonusPower, function(){ + displayPerso(data.hp,data.maxHP,data.maxHP,data.xp,data.level,data.power,data.bonusPower); + }); } diff --git a/js/shop.js b/js/shop.js index a520a38..b6d4cee 100644 --- a/js/shop.js +++ b/js/shop.js @@ -69,10 +69,8 @@ function useItem(name) { sendRequest("craftmine.php", "op=useItem&item="+name, function(ret) { if(!ret.item) return; if(ret.perso) updatePerso(ret.perso); - var item = document.querySelector("[data-name=\""+ret.item[0].name+"\"]"); var nb = ret.item[1]; - if(nb>=1) item.innerHTML = nb; else -- cgit v1.2.3-54-g00ecf