Jump to content

Simple Battle Script


beansandsausages

Recommended Posts

Good morning freaks.

 

I have this simple script :

 


<?
include("connect");
//this file will be all in php
//if user did not select who to attack, we show him list of users
if (!$_GET[attack]) {
//we select id and username of everyone except us
$battleQ = mysql_query("SELECT id,username FROM users WHERE id<>'$user[id]'");
while ($battle = mysql_fetch_array($battleQ)) {
//we show all selected users as well as link to their "attack" subpage
echo "$battle[username] => <a href=\"battle.php?attack=".$battle[id]."\">attack</a> <br>";
}
}

//if user selected link to attack player
if ($_GET[attack]) {
$attack_id = $_GET[attack];
//we check if ID submited is numerical
if (!is_numeric($attack_id)) { die("Bad target ID!"); }
//we select user with that ID
$attackQ = mysql_query("SELECT * FROM users WHERE id='$user[id]' LIMIT 1");
$attackR = mysql_num_rows($attackQ);
//we check that rows are 1. if there are zero rows this means user is modifying link so we close our script!
if ($attackR <> 1) { die("Bad target ID!"); }
//all is fine, we select attacked user
$attack = mysql_fetch_array($attackQ);

//now we divide weapons to our people
if ($user[weapon3]<=$user[units]) {
//if weapon3 is more or the same of all people, then all people have it
$user_w3 = $user[units];
}else{
//only some users get weapon3
$user_w3 = $user[weapon3];
}

if ($user[weapon2]<=($user[units]-$user_w3)) {
//if weapon2 is more or the same of our people without any weapons, then they have it
$user_w2 = $user[units]-$user_w3;
}else{
//only some users get weapon3
$user_w2 = $user[weapon2];
}

if ($user[weapon1]<=($user[units]-$user_w3-$user_w2)) {
//if weapon1 is more or the same of our people without any weapons, then they have it
$user_w1 = $user[units]-$user_w3-$user_w2;
}else{
//only some users get weapon3
$user_w1 = $user[weapon1];
}

//those are our people without weapons
$user_w0 = $user[units]-$user_w3-$user_w2-$user_w1;

//now we calculate our strength
$user_strength = $user_w0*100 + $user_w1*200 + $user_w2*400 + $user_w3*800;

//We do the same thing for attacked player

//now we divide weapons to attacker
if ($attack[weapon3]<=$attack[units]) {
$target_w3 = $attack[units];
}else{
$target_w3 = $attack[weapon3];
}

if ($attack[weapon2]<=($attack[units]-$target_w3)) {
$target_w2 = $attack[units]-$target_w3;
}else{
$target_w2 = $attack[weapon2];
}

if ($attack[weapon1]<=($attack[units]-$target_w3-$target_w2)) {
$target_w1 = $attack[units]-$target_w3-$target_w2;
}else{
$target_w1 = $attack[weapon1];
}

//those are people who we attacked without weapons
$target_w0 = $attack[units]-$target_w3-$target_w2-$target_w1;

//now we calculate defender strength
$target_strength = $target_w0*100 + $target_w1*200 + $target_w2*400 + $target_w3*800;

//now to calculate casualties; we would divide strength with four (4) and then check the percentage of troops died
$att_cas = $target_strength/4;
$def_cas = $user_strength/4;

//we create formula but see that we have $target_w0 on both sides of / so we can use simple formula
$target_w0_die = ($def_cas/$target_w0*100)*$target_w0;
$target_w0_die = ($def_cas/100);
//we cannot have more death people then we have
if ($target_w0_die >) { $target_w0_die=$target_w0; }
$target_w1_die = ($def_cas/200);
if ($target_w1_die >) { $target_w1_die=$target_w1; }
$target_w2_die = ($def_cas/400);
if ($target_w2_die >) { $target_w2_die=$target_w2; }
$target_w3_die = ($def_cas/800);
if ($target_w3_die >) { $target_w3_die=$target_w3; }
$target_killed = $target_w0_die+$target_w1_die+$target_w2_die+$target_w3_die;

//the same for our troops
$user_w0_die = ($att_cas/100);
if ($user_w0_die >) { $user_w0_die=$user_w0; }
$user_w1_die = ($att_cas/200);
if ($user_w1_die >) { $user_w1_die=$user_w1; }
$user_w2_die = ($att_cas/400);
if ($user_w2_die >) { $user_w2_die=$user_w2; }
$user_w3_die = ($att_cas/800);
if ($user_w3_die >) { $user_w3_die=$user_w3; }
$user_killed = $user_w0_die+$user_w1_die+$user_w2_die+$user_w3_die;

//we have two options
//attacker won
if ($user_strength > $target_strength) {
echo "Your troops won. You lost $user_killed units! You managed to plunder $attack[money] money.";

//we update attacker and defender, add money to attacker
mysql_query("UPDATE users SET money=money+'$attack[money]', units=units-'$user_killed' WHERE id='$user[id]'");
mysql_query("UPDATE users SET money='0', units=units-'$target_killed' WHERE id='$attack[id]'");
}

//defender won
if ($user_strength <= $target_strength) {
echo "Enemy troops were too strong. You lost $user_killed units!";

//we update attacker and defender
mysql_query("UPDATE users SET units=units-'$user_killed' WHERE id='$user[id]'");
mysql_query("UPDATE users SET units=units-'$target_killed' WHERE id='$attack[id]'");
}

}

?>

 

The proble is i am getting a error on line 93 and i cant seem to see it any were. The error is :

Parse error: parse error in c:\phpdev\www\public\bored_stuff\battle.php on line 93

 

Line 93 is :

 

if ($target_w0_die >) { $target_w0_die=$target_w0; }

 

Just incase its not here ill post a few lines befor and after line 93 :

 


$att_cas = $target_strength/4;
$def_cas = $user_strength/4;

//we create formula but see that we have $target_w0 on both sides of / so we can use simple formula
$target_w0_die = ($def_cas/$target_w0*100)*$target_w0; 
$target_w0_die = ($def_cas/100);
//we cannot have more death people then we have
if ($target_w0_die >) { $target_w0_die=$target_w0; }
$target_w1_die = ($def_cas/200);
if ($target_w1_die >) { $target_w1_die=$target_w1; }
$target_w2_die = ($def_cas/400);
if ($target_w2_die >) { $target_w2_die=$target_w2; }
$target_w3_die = ($def_cas/800);
if ($target_w3_die >) { $target_w3_die=$target_w3; }
$target_killed = $target_w0_die+$target_w1_die+$target_w2_die+$target_w3_die;

 

Thank you.

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/87341-simple-battle-script/
Share on other sites

In these lines:

if ($target_w0_die >) { $target_w0_die=$target_w0; }
$target_w1_die = ($def_cas/200);
if ($target_w1_die >) { $target_w1_die=$target_w1; }
$target_w2_die = ($def_cas/400);
if ($target_w2_die >) { $target_w2_die=$target_w2; }
$target_w3_die = ($def_cas/800);
if ($target_w3_die >) { $target_w3_die=$target_w3; }

there is no right side operand.

Link to comment
https://forums.phpfreaks.com/topic/87341-simple-battle-script/#findComment-446719
Share on other sites

Well, for instance you write

$target_w0_die >

You are checking to see if $target_w0_die is higher, but you do not specify what it should be higher than. You need something on the right side of the operator seeing as "greater than" is a binary operator and not a unary operator (like ! ("not") is for instance).

Link to comment
https://forums.phpfreaks.com/topic/87341-simple-battle-script/#findComment-446728
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.