Senin, 23 Maret 2009

Basic Strategy game - train units

Today I will try to explain how to train and untrain units, which user will use to attack others or defend himself.

Theory is simple:

each unit cost 100 money
if you untrain unit it refunds you with 50 money
Now lets create new php file like train.php (don't forget to include config.php in beginning of file).

//we need 2 form parts (html)


Number:




Number:



//now for the PHP part
//first if train button is pressed
if ($_POST[train]) {
$train_s = strip_tags(addslashes($_POST[train_s])); //number of soldiers trained

//we check if it is numeric and positive
if (!is_numeric($train_s)) { die("Wrong input!"); }
if ($train_s < 0) { die("Wrong input!"); }

//we check if we have enough money
$train_money = $train_s * 100;
if ($user[money] < $train_money) { die("Not enough money to train so many units!"); }

//if script got it to this point it was all ok so we can update
mysql_query("UPDATE users SET money=money-'$train_money', units=units+'$train_s' WHERE id='$user[id]'");
}

//we now make script which will run if we press Untrain button
if ($_POST[untrain]) {
$untrain_s = strip_tags(addslashes($_POST[untrain_s])); //number of soldiers trained

//we check if it is numeric and positive
if (!is_numeric($untrain_s)) { die("Wrong input!"); }
if ($untrain_s < 0) { die("Wrong input!"); }

//we check if user has enough units to untrain
if ($user[units] < $untrain_s) { die("You cannot untrain so many units!"); }

//if script got it to this point it was all ok so we can update, but first calculate amount of gained money
$gain_money = $untrain_s * 50;
//we update, so we add money gained because of untraining units, and change number of units
mysql_query("UPDATE users SET money=money+'$gain_money', units=units-'$train_s' WHERE id='$user[id]'");
}

This is all for now. Soon we will create script that would allow us to buy weapons.
Bye - until next time
POSTED BY SALJUTIN AT 3:18 AM

Tidak ada komentar: