rross46
06-16-2005, 07:59 PM
Hi guys. Ill cut right to the chase, i trying to develop a simple text based war game in php, for a college project. and i doing good, right up until implementation. Ive got my database set up, and parts of it working, like registration and login, and parts of the code i am going to post work too.
<?
// if "" (blank) isn't defined, do not let them see page (DIE)
(!defined('')) ? die : '';
// Require to be logged in
(!isset($_SESSION['user_id'])) ? redirect('index.php?mode=login') : '';
$user_id=$_SESSION['user_id'];
$tresult= mysql_query("SELECT * FROM user_training
WHERE user_id = $user_id");
$result = mysql_query("SELECT * FROM users
WHERE user_id = $user_id");
echo '<table width="95%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td align="center"><b>Rank</b></td>
<td align="center"><b>Username</b></td>
<td align="center"><b>Gold</b></td>
<td align="center"><b>Attack</b></td>
<td align="center"><b>Defence</b></td>
<td align="center"><b>Turns</b></td>
</tr>';
while ($member = mysql_query($result))
{
echo '<tr>
<td align="center">'.$member['user_rank'].'</td>
<td align="center">'.$member['username'].'</td>
<td align="center">'.$member['user_gold'].'</td>
<td align="center">'.$member['user_attack_rating'].'</td>
<td align="center">'.$member['user_defence_rating'].'</td>
<td align="center">'.$member['user_turns'].'</td>
</tr>';
}
echo '</table>';
echo '<table width="90%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td align="left"><b>Unit Production</b></td>
<td align="left"><b>Next Level</b></td>
<td align="left"><b>Cost</b></td>
</tr>';
while ($tmember = mysql_query($tresult))
{
echo '<tr>
<td align="left">'.$tmember['unit_production'].'</td>
<td align="left">'.$tmember['next_lvl'].'</td>
<td align="left">'.$tmember['production_cost'].'</td>
</tr>';
}
echo '</table>';
?>
This is the users home page. once they log in, this should display their stats, taken from the user table and training table. The training table holds the user id, unit production level, the next level of produciton and its cost (think koc lol). It is supposed to reference the users training details via the user_id and display them, along with attack ratings etc. It dont. Can someone please have a look at it please?
<?
// if "" (blank) isn't defined, do not let them see page (DIE)
(!defined('')) ? die : '';
// Require to be logged in
(!isset($_SESSION['user_id'])) ? redirect('index.php?mode=login') : '';
$set = (isset($HTTP_GET_VARS['set'])) ? $HTTP_GET_VARS['set'] : '';
$target_userid = (isset($HTTP_GET_VARS['uid'])) ? intval($HTTP_GET_VARS['uid']) : redirect('index.php?mode=members');
// Do not allow the user to attack him/her self
if ($target_userid == $_SESSION['user_id'])
{
echo 'You cannot attack yourself';
timed_redirect('index.php?mode=attack&set=no', 2);
die;
}
if ($set == 'no')
{
echo 'Maximum ammount: <b>15</b><form action="index.php?mode=attack&uid='.$target_userid.'" method="POST">
Turns: <input type="text" name="turns" size="10" />
<input type="submit" value="Attack" />';
} else {
$attack_turns = (isset($HTTP_POST_VARS['turns'])) ? intval($HTTP_POST_VARS['turns']) : '';
if ($user['user_turns'] >= $user_turns)
{
if ($user_turns > 0 && $user_turns <= 15)
{
$target = mysql_query("SELECT * FROM users
WHERE user_id = '".$target_userid."'");
$user = mysql_query("SELECT * FROM users
WHERE user_id = '".$_SESSION['user_id']."'");
$user = mysql_fetch_array($user);
$target = mysql_fetch_array($target);
if ($user['user_attack_rating'] > $target['user_defence_rating'])
{
$spoils = round(((4.3 * $target['user_gold']) * $user_turns));
$target_gold = $target['user_gold'] - ($spoils);
$user_gold = $user['user_gold']+$spoils;
$user_result = mysql_query("UPDATE users
SET user_gold = '".$user_gold."'
WHERE user_id = '".$_SESSION['user_id']."'");
$target_result = mysql_query("UPDATE users
SET user_gold = '".$target_gold."'
WHERE user_id = '".$target_userid."'");
$victory = true;
$outcome = 'Victory';
} else {
$victory = false;
$outcome = 'Defeat';
}
echo ($victory == true) ? 'Your troops demolish '.$target.'\'s army, victory is yours' : $target.' beats back your troops in a humilliating defeat';
echo '<br>You have gained:<br><b>'.$spoils.' Gold</b><br><b></b>';
$result = mysql_query("INSERT INTO `attack_log` VALUES ('".$_SESSION['user_id']."', '".$target."', '".$user['user_attack_rating']."', '".$target['user_def_rating']."', '".$outcome."', '".$spoils."')");
if ($result)
{
echo 'Attack Log Updated!';
timed_redirect('index.php?mode=main', 1);
} else {
echo 'Maximum ammount of turns: 15';
timed_redirect('index.php?mode=attack&uid='.$target_userid, 2);
}
}
}else{
echo 'You do not have '.$user_turns;
}
}
?>
This is the attack screen. It displays a list of the members and uses their names as a link to attack them. The user enters number of attack turns and attacks them, the result is to be stored in an attack log table. Nothing works.
<?
// if "" (blank) isn't defined, do not let them see page (DIE)
(!defined('')) ? die : '';
// Require to be logged in
(!isset($_SESSION['user_id'])) ? redirect('index.php?mode=login') : '';
$set = (isset($HTTP_GET_VARS['set'])) ? $HTTP_GET_VARS['set'] : '';
$result = mysql_query("SELECT * FROM armoury
ORDER BY weapon_type ASC");
echo '<table width="95%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td align="center"><b>Weapon</b></td>
<td align="center"><b>Type</b></td>
<td align="center"><b>Cost</b></td>
<td align="center"><b>Strength</b></td>
</tr>';
while ($armoury = mysql_fetch_array($result))
{
echo '<tr>
<td align="center">'.$armoury['weapon_name'].'</td>
<td align="center">'.$armoury['weapon_type'].'</td>
<td align="center">'.$armoury['weapon_cost'].'</td>
<td align="center">'.$armoury['weapon_strength'].'</td>
<td align="center">quantity: <input type="text" name="quantity" size="4" /><input type="submit" value="Buy" /></td>
</tr>';
}
$quantity = (isset($HTTP_POST_VARS['quantity'])) ? intval($HTTP_POST_VARS['quantity']) : '';
$user = mysql_query("SELECT * FROM users
WHERE user_id = '".$_SESSION['user_id']."'");
$user = mysql_fetch_array($user);
If( $quantity > 0)
{
$cost=($armoury['weapon_cost']*$armoury['quantity']);
if ($user['user_gold'] >= $cost)
{
$purchase = mysql_query("INSERT INTO `users` VALUES ('".$user_id."', '')");
if ($purchase)
{
if ($weapon_type=='attack')
{
$user_attack_rating= mysql_query("UPDATE users
SET user_attack_rating =$user_attack_rating+$weapon_strength*$quantity*$user_army_size;
WHERE user_id = '".$_SESSION['user_id']."'");
} else {
$user_defence_rating= mysql_query("UPDATE users
SET user_defence_rating =$user_defence_rating+$weapon_strength*$quantity*$user_army_size;
WHERE user_id = '".$_SESSION['user_id']."'");
}
$user_gold= mysql_query("UPDATE users
SET $user_gold=$user_gold-$cost;
WHERE user_id = '".$_SESSION['user_id']."'");
}
echo 'Purchase Successful!';
timed_redirect('index.php?mode=main', 1);
}else {
echo 'Not Enough Gold';
}
}
This is the armoury screen. it displays details of weapons stored in the armoury table, with input boxes next to them to enter the quantity you wish to buy. It does this but it dosent let you do anything else. Once the purchase has been made, the user_armoury table will store the details of the weapons along with the user_id to reference it in future, aswell as updating the user gold and user_attack/defence_ratings. Dont work.
I understand what i am asking is a lot. Please dont think me some lazy noob trying to get the work done for me. I have tried at this. I have fully designed the game, including algorithms. I just cant put it into php, i just not good enough. I will be eternally greatful if any of you could please find the time to help.
edit::
this aint a homework assignment either, its a project, and any help you give should be legit, it not like i claiming any help given as my own, a lot of the code up there was given to me by a friend, which i have tweaked and used parts to make other scripts. i noticed a thread about this sorta thing, i just want to e-itterate its a college project not homework, its legit as all persons who help me will be acknowledged in the project documentation. And it is only the coding part i have had help in lol, ive done all analysis, design myself. Sorry to go on there, just wanted to make that totally clear incase anyone thought i was trying it on.
<?
// if "" (blank) isn't defined, do not let them see page (DIE)
(!defined('')) ? die : '';
// Require to be logged in
(!isset($_SESSION['user_id'])) ? redirect('index.php?mode=login') : '';
$user_id=$_SESSION['user_id'];
$tresult= mysql_query("SELECT * FROM user_training
WHERE user_id = $user_id");
$result = mysql_query("SELECT * FROM users
WHERE user_id = $user_id");
echo '<table width="95%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td align="center"><b>Rank</b></td>
<td align="center"><b>Username</b></td>
<td align="center"><b>Gold</b></td>
<td align="center"><b>Attack</b></td>
<td align="center"><b>Defence</b></td>
<td align="center"><b>Turns</b></td>
</tr>';
while ($member = mysql_query($result))
{
echo '<tr>
<td align="center">'.$member['user_rank'].'</td>
<td align="center">'.$member['username'].'</td>
<td align="center">'.$member['user_gold'].'</td>
<td align="center">'.$member['user_attack_rating'].'</td>
<td align="center">'.$member['user_defence_rating'].'</td>
<td align="center">'.$member['user_turns'].'</td>
</tr>';
}
echo '</table>';
echo '<table width="90%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td align="left"><b>Unit Production</b></td>
<td align="left"><b>Next Level</b></td>
<td align="left"><b>Cost</b></td>
</tr>';
while ($tmember = mysql_query($tresult))
{
echo '<tr>
<td align="left">'.$tmember['unit_production'].'</td>
<td align="left">'.$tmember['next_lvl'].'</td>
<td align="left">'.$tmember['production_cost'].'</td>
</tr>';
}
echo '</table>';
?>
This is the users home page. once they log in, this should display their stats, taken from the user table and training table. The training table holds the user id, unit production level, the next level of produciton and its cost (think koc lol). It is supposed to reference the users training details via the user_id and display them, along with attack ratings etc. It dont. Can someone please have a look at it please?
<?
// if "" (blank) isn't defined, do not let them see page (DIE)
(!defined('')) ? die : '';
// Require to be logged in
(!isset($_SESSION['user_id'])) ? redirect('index.php?mode=login') : '';
$set = (isset($HTTP_GET_VARS['set'])) ? $HTTP_GET_VARS['set'] : '';
$target_userid = (isset($HTTP_GET_VARS['uid'])) ? intval($HTTP_GET_VARS['uid']) : redirect('index.php?mode=members');
// Do not allow the user to attack him/her self
if ($target_userid == $_SESSION['user_id'])
{
echo 'You cannot attack yourself';
timed_redirect('index.php?mode=attack&set=no', 2);
die;
}
if ($set == 'no')
{
echo 'Maximum ammount: <b>15</b><form action="index.php?mode=attack&uid='.$target_userid.'" method="POST">
Turns: <input type="text" name="turns" size="10" />
<input type="submit" value="Attack" />';
} else {
$attack_turns = (isset($HTTP_POST_VARS['turns'])) ? intval($HTTP_POST_VARS['turns']) : '';
if ($user['user_turns'] >= $user_turns)
{
if ($user_turns > 0 && $user_turns <= 15)
{
$target = mysql_query("SELECT * FROM users
WHERE user_id = '".$target_userid."'");
$user = mysql_query("SELECT * FROM users
WHERE user_id = '".$_SESSION['user_id']."'");
$user = mysql_fetch_array($user);
$target = mysql_fetch_array($target);
if ($user['user_attack_rating'] > $target['user_defence_rating'])
{
$spoils = round(((4.3 * $target['user_gold']) * $user_turns));
$target_gold = $target['user_gold'] - ($spoils);
$user_gold = $user['user_gold']+$spoils;
$user_result = mysql_query("UPDATE users
SET user_gold = '".$user_gold."'
WHERE user_id = '".$_SESSION['user_id']."'");
$target_result = mysql_query("UPDATE users
SET user_gold = '".$target_gold."'
WHERE user_id = '".$target_userid."'");
$victory = true;
$outcome = 'Victory';
} else {
$victory = false;
$outcome = 'Defeat';
}
echo ($victory == true) ? 'Your troops demolish '.$target.'\'s army, victory is yours' : $target.' beats back your troops in a humilliating defeat';
echo '<br>You have gained:<br><b>'.$spoils.' Gold</b><br><b></b>';
$result = mysql_query("INSERT INTO `attack_log` VALUES ('".$_SESSION['user_id']."', '".$target."', '".$user['user_attack_rating']."', '".$target['user_def_rating']."', '".$outcome."', '".$spoils."')");
if ($result)
{
echo 'Attack Log Updated!';
timed_redirect('index.php?mode=main', 1);
} else {
echo 'Maximum ammount of turns: 15';
timed_redirect('index.php?mode=attack&uid='.$target_userid, 2);
}
}
}else{
echo 'You do not have '.$user_turns;
}
}
?>
This is the attack screen. It displays a list of the members and uses their names as a link to attack them. The user enters number of attack turns and attacks them, the result is to be stored in an attack log table. Nothing works.
<?
// if "" (blank) isn't defined, do not let them see page (DIE)
(!defined('')) ? die : '';
// Require to be logged in
(!isset($_SESSION['user_id'])) ? redirect('index.php?mode=login') : '';
$set = (isset($HTTP_GET_VARS['set'])) ? $HTTP_GET_VARS['set'] : '';
$result = mysql_query("SELECT * FROM armoury
ORDER BY weapon_type ASC");
echo '<table width="95%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td align="center"><b>Weapon</b></td>
<td align="center"><b>Type</b></td>
<td align="center"><b>Cost</b></td>
<td align="center"><b>Strength</b></td>
</tr>';
while ($armoury = mysql_fetch_array($result))
{
echo '<tr>
<td align="center">'.$armoury['weapon_name'].'</td>
<td align="center">'.$armoury['weapon_type'].'</td>
<td align="center">'.$armoury['weapon_cost'].'</td>
<td align="center">'.$armoury['weapon_strength'].'</td>
<td align="center">quantity: <input type="text" name="quantity" size="4" /><input type="submit" value="Buy" /></td>
</tr>';
}
$quantity = (isset($HTTP_POST_VARS['quantity'])) ? intval($HTTP_POST_VARS['quantity']) : '';
$user = mysql_query("SELECT * FROM users
WHERE user_id = '".$_SESSION['user_id']."'");
$user = mysql_fetch_array($user);
If( $quantity > 0)
{
$cost=($armoury['weapon_cost']*$armoury['quantity']);
if ($user['user_gold'] >= $cost)
{
$purchase = mysql_query("INSERT INTO `users` VALUES ('".$user_id."', '')");
if ($purchase)
{
if ($weapon_type=='attack')
{
$user_attack_rating= mysql_query("UPDATE users
SET user_attack_rating =$user_attack_rating+$weapon_strength*$quantity*$user_army_size;
WHERE user_id = '".$_SESSION['user_id']."'");
} else {
$user_defence_rating= mysql_query("UPDATE users
SET user_defence_rating =$user_defence_rating+$weapon_strength*$quantity*$user_army_size;
WHERE user_id = '".$_SESSION['user_id']."'");
}
$user_gold= mysql_query("UPDATE users
SET $user_gold=$user_gold-$cost;
WHERE user_id = '".$_SESSION['user_id']."'");
}
echo 'Purchase Successful!';
timed_redirect('index.php?mode=main', 1);
}else {
echo 'Not Enough Gold';
}
}
This is the armoury screen. it displays details of weapons stored in the armoury table, with input boxes next to them to enter the quantity you wish to buy. It does this but it dosent let you do anything else. Once the purchase has been made, the user_armoury table will store the details of the weapons along with the user_id to reference it in future, aswell as updating the user gold and user_attack/defence_ratings. Dont work.
I understand what i am asking is a lot. Please dont think me some lazy noob trying to get the work done for me. I have tried at this. I have fully designed the game, including algorithms. I just cant put it into php, i just not good enough. I will be eternally greatful if any of you could please find the time to help.
edit::
this aint a homework assignment either, its a project, and any help you give should be legit, it not like i claiming any help given as my own, a lot of the code up there was given to me by a friend, which i have tweaked and used parts to make other scripts. i noticed a thread about this sorta thing, i just want to e-itterate its a college project not homework, its legit as all persons who help me will be acknowledged in the project documentation. And it is only the coding part i have had help in lol, ive done all analysis, design myself. Sorry to go on there, just wanted to make that totally clear incase anyone thought i was trying it on.