aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpiernov <piernov@piernov.org>2016-04-25 19:10:53 +0200
committerpiernov <piernov@piernov.org>2016-04-25 19:10:53 +0200
commit8770f9ddccfafaad935f66ccbf254624b41545f3 (patch)
treee07d2e93e95a34e9f70f22f52aafa82c09682e4b
parentb141eb3d70b3dc2d75ee8f2bf2edba1fed71f5b6 (diff)
parent8140617aeb2f32f7095a443ca743c6d6915739c6 (diff)
downloadcandybox-8770f9ddccfafaad935f66ccbf254624b41545f3.tar.gz
candybox-8770f9ddccfafaad935f66ccbf254624b41545f3.tar.bz2
candybox-8770f9ddccfafaad935f66ccbf254624b41545f3.tar.xz
candybox-8770f9ddccfafaad935f66ccbf254624b41545f3.zip
Merge remote-tracking branch 'origin/alexichi' into piernov
-rw-r--r--.gitignore1
-rw-r--r--craftmine.php3
-rw-r--r--inc/account.inc2
-rw-r--r--inc/craftmine.inc1
-rw-r--r--inc/guild.inc31
-rw-r--r--inc/messages.inc2
-rw-r--r--inc/mine.inc2
-rw-r--r--inc/mine.inc~20
-rw-r--r--index.xhtml6
-rw-r--r--js/craftmine.js5
-rw-r--r--js/craftmine.js~51
-rw-r--r--js/guild.js17
-rw-r--r--js/shop.js2
13 files changed, 66 insertions, 77 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..b25c15b
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+*~
diff --git a/craftmine.php b/craftmine.php
index 4d08ffd..78c5e37 100644
--- a/craftmine.php
+++ b/craftmine.php
@@ -1,6 +1,7 @@
<?php
require_once("inc/mine.inc");
+require_once("inc/guild.inc");
require_once("inc/shop.inc");
require_once("inc/craftmine.inc");
@@ -36,6 +37,8 @@ if (!isset($_POST["op"])) {
$op = $_POST["op"];
switch($op) {
case "withdrawMine": withdrawMine(); break;
+ case "createGuild": createGuild(); break;
+ case "hireMiner": hireMiner(); break;
case "getCraftMine": sendCraftMine(); break;
case "buildShop": buildShop(); break;
case "buyItem": buyItem(); break;
diff --git a/inc/account.inc b/inc/account.inc
index 19f311d..6f398bb 100644
--- a/inc/account.inc
+++ b/inc/account.inc
@@ -2,7 +2,7 @@
require_once("messages.inc");
function debitAccount($amount) {
- if($_SESSION["mine"]["gold"] <= $amount) {
+ if($_SESSION["mine"]["gold"] < $amount) {
sendError("gold_insufficient");
return false;
}
diff --git a/inc/craftmine.inc b/inc/craftmine.inc
index a30538c..33a28d7 100644
--- a/inc/craftmine.inc
+++ b/inc/craftmine.inc
@@ -7,6 +7,7 @@ function sendCraftMine() {
$data = array("gold" => sendMine(),
"shop" => sendShop(),
"inventory" => Inventory::sendContent(),
+ "miners" => sendMiners()
);
echo json_encode($data);
}
diff --git a/inc/guild.inc b/inc/guild.inc
new file mode 100644
index 0000000..c0e8264
--- /dev/null
+++ b/inc/guild.inc
@@ -0,0 +1,31 @@
+<?php
+
+define("GUILD_COST",10);
+define("MINER_COST",5);
+
+function createGuild(){
+ if(!empty($_SESSION["guild"])) {
+ sendError("guild_already_built");
+ }
+ elseif(debitAccount(GUILD_COST)) {
+ $_SESSION["guild"] = true;
+ echo json_encode(array("cost" => GUILD_COST));
+ }
+}
+
+function hireMiner(){
+ if(!isset($_SESSION["guild"])){
+ sendError("guild_not_yet_created");
+ }
+ elseif(debitAccount(MINER_COST)){
+ $_SESSION["mine"]["miners"]++;
+ echo json_encode(array("cost" => MINER_COST , "miners" => $_SESSION["mine"]["miners"]));
+ }
+}
+
+function sendMiners(){
+ return $_SESSION["mine"]["miners"];
+}
+
+
+?>
diff --git a/inc/messages.inc b/inc/messages.inc
index f1ca8b3..d6ea87e 100644
--- a/inc/messages.inc
+++ b/inc/messages.inc
@@ -4,6 +4,8 @@ $messages = array(
"shop_already_built" => "You have already built a shop.",
"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."
);
function sendError($msg) {
diff --git a/inc/mine.inc b/inc/mine.inc
index 6a04cc4..94a2c66 100644
--- a/inc/mine.inc
+++ b/inc/mine.inc
@@ -1,7 +1,7 @@
<?php
function initCraftMine() {
- $_SESSION["mine"] = array("mine" => 0, "gold" => 0, "miners" => 1);
+ $_SESSION["mine"] = array("mine" => 0, "gold" => 0, "miners" => 0);
}
function withdrawMine() {
diff --git a/inc/mine.inc~ b/inc/mine.inc~
deleted file mode 100644
index a01d768..0000000
--- a/inc/mine.inc~
+++ /dev/null
@@ -1,20 +0,0 @@
-<?php
-
-function initCraftMine() {
- $_SESSION["mine"] = array("mine" => 0, "or" => 0, "miners" => 1);
-}
-
-function withdrawMine() {
- $mine = $_SESSION["mine"];
- $_SESSION["mine"]["gold"] += $mine["mine"];
- $_SESSION["mine"]["mine"] = 0;
-}
-
-function sendMine() {
- if(empty($_SESSION["mine"])) initMine();
- $mine = $_SESSION["mine"];
- echo $mine["gold"];
-}
-
-
-?>
diff --git a/index.xhtml b/index.xhtml
index 6ec45ef..aa29615 100644
--- a/index.xhtml
+++ b/index.xhtml
@@ -12,6 +12,7 @@
<meta name="author" content="Alexandre Renoux,Pierre-Emmanuel Novac"/>
<title>CraftMine</title>
<script type="text/javascript" charset="utf-8" src="js/craftmine.js"></script>
+ <script type="text/javascript" charset="utf-8" src="js/guild.js"></script>
<script type="text/javascript" charset="utf-8" src="js/shop.js"></script>
<script type="text/javascript" charset="utf-8" src="js/gui.js"></script>
</head>
@@ -26,8 +27,11 @@
<div class="col-md-10">
<span id="gold">0</span>
<span id="mine">0</span>
+ <p>Miners : <span id="miners">0</span></p>
+ <span id="guild"></span>
<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>
</form>
</div>
</div>
@@ -63,7 +67,7 @@
<div class="tab-pane" id="tab1">
<h4>Select a building to build:</h4>
<ul class="list-inline">
- <li><button class="btn btn-default" type="button" onclick="">Miner's guild</button></li>
+ <li><button class="btn btn-default" type="button" onclick="createGuild()">Miner's guild</button></li>
<li><button class="btn btn-default" type="button" onclick="buildShop()">Shop</button></li>
</ul>
</div>
diff --git a/js/craftmine.js b/js/craftmine.js
index 66c35fc..92fa45a 100644
--- a/js/craftmine.js
+++ b/js/craftmine.js
@@ -1,7 +1,7 @@
data = {
gold: 0,
mine: 0,
- miners: 1,
+ miners: 0,
level: 1
}
@@ -49,7 +49,8 @@ function initCraftMine() {
data.gold = parseInt(ret.gold); // Server's response is a string
if(ret.shop) displayShop(ret.shop);
displayInventory(ret.inventory);
- updateData("gold");
+ data.miners = parseInt(ret.miners);
+ updateData("gold","miners");
})
}
diff --git a/js/craftmine.js~ b/js/craftmine.js~
deleted file mode 100644
index bc03d6c..0000000
--- a/js/craftmine.js~
+++ /dev/null
@@ -1,51 +0,0 @@
-datas = {
- gold: 0,
- mine: 0,
- miners: 1,
- level: 1
-}
-
-function sendRequest(url, params, callback) {
- var xhr = new XMLHttpRequest();
- xhr.open("POST", url);
- xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
- xhr.onreadystatechange = function() {
- if(xhr.readyState == XMLHttpRequest.DONE && xhr.status == "200") {
- callback(xhr);
- }
- }
- xhr.send(params);
-}
-
-
-function updateDatas(t) {
- document.getElementById(t).innerHTML = datas[t];
-}
-
-function withdrawMine() {
- sendRequest("craftmine.php", "op=withdrawMine", function() {
- datas.gold += parseInt(datas.mine);
- datas.mine = 0;
- updateDatas("gold");
- updateDatas("mine");
- })
-}
-
-function initCraftMine() {
- sendRequest("craftmine.php", "op=getCraftMine", function(xhr) {
- var ret = xhr.responseText;
- datas.gold = ret;
- updateDatas("gold");
- })
-}
-
-
-function updateMine() {
- datas.mine += (datas.mineurs+1);
- updateDatas("mine");
-}
-
-function init() {
- initCraftMine();
- window.setInterval(updateMine, 1000);
-}
diff --git a/js/guild.js b/js/guild.js
new file mode 100644
index 0000000..8d4d91b
--- /dev/null
+++ b/js/guild.js
@@ -0,0 +1,17 @@
+function hireMiner(){
+ sendRequest("craftmine.php", "op=hireMiner", function(ret) {
+ debitAccount(parseInt(ret.cost));
+ data.miners = parseInt(ret.miners);
+ updateData("miners");
+ }
+ )
+}
+
+function createGuild(){
+ sendRequest("craftmine.php", "op=createGuild", function(ret) {
+ showInfo("Your guild has been successfully created");
+ debitAccount(parseInt(ret.cost));
+ }
+ )
+}
+
diff --git a/js/shop.js b/js/shop.js
index 00636e1..22ca85f 100644
--- a/js/shop.js
+++ b/js/shop.js
@@ -25,7 +25,7 @@ function buildShop() {
function addItem(ret) {
var itemhtml = "<li>";
- itemhtml += "<button type=\"submit\" class=\"btn btn-primary\" onclick=\"useItem('" + ret.name + "')\"><span class=\"item-icon\">" + ret.icon + "</span><br />" + ret.name + "</button>";
+ itemhtml += "<button type=\"button\" class=\"btn btn-primary\" onclick=\"useItem('" + ret.name + "')\"><span class=\"item-icon\">" + ret.icon + "</span><br />" + ret.name + "</button>";
itemhtml += "</li>";
var invcontent = document.getElementById("tab3");