In this tutorial I will try to explain how to create system which would allow you to buy weapons for your units.
Again theory:
- weapon1 doubles (2x) efficiency of soldier with it - costs 1000
- weapon2 quadruples (4x) efficiency of soldier - cost 2000
- weapon3 gives 8x bonus to soldier - cost 4000
- by selling weapon you gain 75% of price back
//we need 2 form parts (html) - one for buying, one for selling
<form>
Weapon1: <input size="3" maxlength="4" value="0" name="buy1"> <br>
Weapon2: <input size="3" maxlength="4" value="0" name="buy2"> <br>
Weapon3: <input size="3" maxlength="4" value="0" name="buy3"> <br>
<input name="buy" type="submit" value="Buy">
</form>
<form>
Weapon1: <input size="3" maxlength="4" value="0" name="sell1"> <br>
Weapon2: <input size="3" maxlength="4" value="0" name="sell2"> <br>
Weapon3: <input size="3" maxlength="4" value="0" name="sell3"> <br>
<input name="sell" type="submit" value="Sell">
</form>
//now the PHP part
//first if buy button is pressed
if ($_POST[buy]) {
$buy1 = strip_tags(addslashes($_POST[buy1]));
$buy2 = strip_tags(addslashes($_POST[buy2]));
$buy3 = strip_tags(addslashes($_POST[buy3]));
//we check if it is numeric and positive
if (!is_numeric($buy1) OR !is_numeric($buy2) OR !is_numeric($buy3)) { die("Wrong input!"); }
if (($buy1 < 0) OR ($buy2 < 0) OR ($buy3 < 0)) { die("Wrong input!"); }
//we check if we have enough money
$buy_money = 1000*$buy1+2000*$buy2+4000*$buy3;
if ($user[money] < $buy_money) { die("Not enough money to buy so many weapons!"); }
//if script got it to this point it was all ok so we can update
mysql_query("UPDATE users SET money=money-'$buy_money', weapon1=weapon1+'$buy1', weapon2=weapon2+'$buy2', weapon3=weapon3+'$buy3' WHERE id='$user[id]'");
}
//we now make script which will run if we press Sell button
if ($_POST[sell]) {
$sell1 = strip_tags(addslashes($_POST[sell1]));
$sell2 = strip_tags(addslashes($_POST[sell2]));
$sell3 = strip_tags(addslashes($_POST[sell3]));
//we check if it is numeric and positive
if (!is_numeric($sell1) OR !is_numeric($sell2) OR !is_numeric($sell3)) { die("Wrong input!"); }
if (($sell1 < 0) OR ($sell2 < 0) OR ($sell3 < 0)) { die("Wrong input!"); }
//we check if user wants to sell more weapons he has
if (($sell1 > $user[weapon1]) OR ($sell2 > $user[weapon2]) OR ($sell3 > $user[weapon3])) { die("You cannot sell more weapons then you have!"); }
//we calculate how much money we will get (75%)
$sell_money = round((1000*$sell1+2000*$sell2+4000*$sell3)*0.75);
//if script got it to this point it was all ok so we can update
mysql_query("UPDATE users SET money=money+'$sell_money', weapon1=weapon1-'$sell1', weapon2=weapon2-'$sell2', weapon3=weapon3-'$sell3' WHERE id='$user[id]'");
}
This concludes our tutorial. You can see training units and buying weapons scripts are quite the same. Those are basics, but it is up to you and your game logic to create different, better and more complicated systems. (You can make use previous tutorials and add that user gets each hour 1 unit, which he can train to soldier).
In next tutorial we will create basic attack system, which would allow users to attack each other to gain money.