...

View Full Version : unwanted negative numbers



arne2
11-18-2006, 09:23 AM
Hi, i have a problem with my code. When the users weed & crack gets to very high numbers, the script returns negative numbers of sold weed and/or crack.
So if i have loads and loads of weed, and i use like 20turns to sell them, i sell negative weed amounts (so for example i sell -20000 weed).

Can anybody tell me how this can happen? and what can i do about it?




bcscale(1);
if($keyboard=="enter")
{
$cturns=$_POST['cturns'];
$wturns=$_POST['wturns'];
$turns=($cturns+$wturns);
if (($turns > $user["turn"])){
$error="You dont have enough turns!";
}
if((!preg_match('/^[0-9][0-9]*$/i', $turns))){
$error="Your invalid action is logged. Please only use numbers for turns.";
}elseif($turns == 0){$error="You can't deal without using turns!";
}elseif($turns > 30){$error="you can only use up to 30 turns!";
}elseif($user["crack"] < 2500 && $cturns >0){$error="You can't deal crack until you have at least 2500 crack stones!";
}elseif($user["weed"] < 2500 && $wturns >0){$error="You can't deal weed until you have at least 2500 grams of weed!";
}elseif($user["dealers"] <= 0){$error="You can't deal without dealers !!!"; $randif=1;//RANDIF = 1 TO PREVENT IT FROM DEALING WITH NO DEALERS!
}else {

//RANDOM EVENTS
$randomifturn_total = $user["turn"]-$turns;
$randevent=rand(1,20);
if($user["dealers"] <= 0){
$randevent=0; //RANDIF = 1 TO PREVENT IT FROM DEALING WITH NO DEALERS, AND PREVENT RANDOM EVENTS FROM OCCURING
}
if($randevent==1){
$randif=1;
$randmsg="Oh no ! Your dealers ran off with the drugs you gave them!";
$dealersgot=$user["dealers"];
$maxlost=(round((0.05*$dealersgot))+1);
$dealerslost=rand(1,$maxlost);
$dealersleft=$dealersgot-$dealerslost;
$crack=$user["crack"];
$weed=$user["weed"];
$weedleft=$user["weed"];
$crackleft=$user["crack"];
if($cturns > 0){
$cracklost=0.01*$crack;
$crackleft=$user["crack"]-$cracklost;
}
if($wturns > 0){
$weedlost=0.01*$weedlost;
$weedleft=$user["weed"]-$weedlost;
}
mysql_query("UPDATE $tab[pimp] SET dealers='$dealersleft',crack='$crackleft',weed='$weedleft',turn='$randomifturn_total' WHERE id='$id' AND rid='$rid'");

}elseif($randevent==5){
$randif=1;
$randmsg="FREEZE! The cops tried to arrest your dealers but fortunately they managed to escape!<br> All junkies were gone though so you sold nothing !";
}elseif($randevent==10){
$randif=1;
$bribe=losecash($id);
if($bribe>$user["cash"]){
$bribe=$user["cash"];
}
$bribec=commas($bribe);
if($bribe==0){
$randif=0;
//IF BRIBE IS 0 => DISPLAY NO RANDOM MESSAGE AND JUST EXECUTE THE REST OF THE CODE
}
$randmsg="FREEZE! The cops arrested your dealers and you had to bribe them with $$bribec!";
$cashgot=$user["cash"];
$cashleft=$cashgot-$bribe;
mysql_query("UPDATE $tab[pimp] SET cash='$cashleft',turn='$randomifturn_total' WHERE id='$id' AND rid='$rid'");
}
}

if($randif!=1){


$crack=$user["crack"];
$weed=$user["weed"];
$crackprofit=0;
$weedprofit=0;
$onecrackworth='5'; //in $ per piece
$oneweedworth='2.5';

if($cturns > 0){

if($crack < 10000){
$soldcrackvar1=bcmul('0.02',$crack);
}else{
$soldcrackvar1=bcmul('0.005',$crack);
}

$soldcrackvar2=bcmul('0.05',$crack);
$cracksold=mt_rand($soldcrackvar1,$soldcrackvar2);
$cracksold=bcmul($cracksold,$turns);


if($cracksold > $crack){
$nosold=mt_rand(1003,2003);
$cracksold=bcsub($crack,$nosold);
if($cracksold<0){
$cracksold=0;
}
}
$crackprofit=bcmul($onecrackworth,$cracksold);
}

if($wturns > 0){

if($weed < 10000){
$soldweedvar1=bcmul('0.02',$weed);
}else{
$soldweedvar1=bcmul('0.005',$weed);
}

$soldweedvar2=bcmul('0.05',$weed);
$weedsold=mt_rand($soldweedvar1,$soldweedvar2);
$weedsold=bcmul($weedsold,$turns);


if($weedsold > $weed){
$nosold=mt_rand(603,1803);
$weedsold=bcsub($weed,$nosold);
if($weedsold<0){
$cracksold=0;
}
}
$weedprofit=bcmul($oneweedworth,$weedsold);
}


$total_profit=bcadd($crackprofit,$weedprofit);

$dealerskeepvar1=bcmul('0.01',$total_profit);
$dealerskeepvar2=bcmul('0.06',$total_profit);
$dealerskeep=mt_rand($dealerskeepvar1,$dealerskeepvar2);

$profit_keep=bcsub($total_profit,$dealerskeep);
$bonus=(($user["dealers"]/1000)+1);
$dealersleft=unhappy("dealers",$turns,$bonus);

$user = mysql_fetch_array(mysql_query("SELECT dealers,cash,turn,crack,weed FROM $tab[pimp] WHERE id='$id' AND rid='$rid';"));

$dealers_total=bcsub($user["dealers"],$dealersleft);
$cash_total=bcadd($user["cash"],$profit_keep);
$turn_total = $user["turn"]-$turns;
$crack_total=bcsub($user["crack"],$cracksold);
$weed_total=bcsub($user["weed"],$weedsold);
mysql_query("UPDATE $tab[pimp] SET turn='$turn_total', cash='$cash_total', dealers='$dealers_total', weed='$weed_total', crack='$crack_total' WHERE id='$id' AND rid='$rid'");

$worth=worth($id);$dappy=dealerhappy($id);
mysql_query("UPDATE $tab[pimp] SET dealerhappy='$dappy',worth='$worth', online='$time' WHERE id='$id' AND rid='$rid'");
} //END RANDIF!=1

} //KEYBOARD ENTER
THis is not the full code but it's the calculating part of it!

arne2
11-18-2006, 12:07 PM
is it because of the mt_rand()? If so, is there a function similar to mt_rand that can handle big numbers?
Or is the problem caused by something else?
Thank you for your time and efforts!

marek_mar
11-18-2006, 12:26 PM
You're not using BC everywhere where needed.
For example:


if($weedsold > $weed){

will not work but


if(bccomp($weedslod, $weed) == 1)

Will give the correct result.

arne2
11-19-2006, 01:09 PM
so bccomp == 1
will replace >
and what about < or <= or what about == or something?

marek_mar
11-19-2006, 02:03 PM
bccomp() return 1 if 1st argument is greater than the second one, 0 if they're equal and -1 if the second one is greater.
Do read the manual.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum