Jump to content

[SOLVED] in_array return values, or coding logic wrong?


DeanWhitehouse

Recommended Posts

Hiya,

Long time since i have posted a coding problem, but here it goes.

 

I have created this code to work out what a user can steal from another user, but it seems to allow the user to try and steal anything even if it is on the list of unstealable items, any ideas as to why?

 

Does in_array return a different value than i thought?

Code Snippet

			function StealType($extort)
			{
				$car = mysql_query("SELECT id FROM user_cars WHERE user_id = '".$extort['id']."' AND up_for_sale = '0' ORDER BY RAND() LIMIT 1");

				$cash = mysql_query("SELECT money FROM user_stats WHERE user_id = '".$extort['id']."' LIMIT 1");
				$c_check = mysql_fetch_assoc($cash);

				$bullets = mysql_query("SELECT bullets FROM user_stats WHERE user_id = '".$extort['id']."' LIMIT 1");
				$b_check = mysql_fetch_assoc($bullets);

				$Not = array();

				if(mysql_num_rows($car) == 0)
				{
					$Not[] = 1;
				}

				if($c_check['money'] < 100)
				{
					$Not[] = 2;
				}				

				if($b_check['bullets'] == 0)
				{
					$Not[] = 3;
				}

				print_r($Not);

				if(in_array(array(1,2,3),$Not))
				{
					echo "S1";
					return false;
				}
				elseif(!in_array(array(1,2,3),$Not))
				{
					echo "S2";
					return rand(1,3);
				}
				elseif(!in_array(array(1),$Not) && in_array(array(2,3),$Not))
				{
					echo "S3";
					return 1;
				}
				elseif(!in_array(array(2),$Not) && in_array(array(1,3),$Not))
				{
					echo "S4";
					return 2;
				}
				elseif(!in_array(array(3),$Not) && in_array(array(1,2),$Not))
				{
					echo "S5";
					return 3;
				}
				elseif(!in_array(array(1,2),$Not) && in_array(array(3),$Not))
				{
					echo "S6";
					return rand(1,2);
				}
				elseif(!in_array(array(1,3),$Not) && in_array(array(2),$Not))
				{
					echo "S7";
					return 1;
				}
				elseif(!in_array(array(2,3),$Not) && in_array(array(1),$Not))
				{
					echo "S8";
					return rand(2,3);
				}
			}

			$steal = StealType($extort);
			if($steal == false)
			{
				echo "Failed! This user has nothing!";
			}
			elseif($steal == 1)
			{
				echo "To come car";
			}
			elseif($steal == 2)
			{
				echo "To come cash";
			}
			elseif($steal == 3)
			{
				echo "To come bullets";
			}
			else
				echo "Error! What the fuck?";

Link to comment
Share on other sites

also, I was looking at your SQL: wouldn't this be cleaner:

$car = mysql_query("SELECT id FROM user_cars WHERE user_id = '".$extort['id']."' AND up_for_sale = '0' ORDER BY RAND() LIMIT 1");

$cash = mysql_query("SELECT money FROM user_stats WHERE user_id = '".$extort['id']."' AND `money` > 100 LIMIT 1");
$c_check = mysql_fetch_assoc($cash);

$bullets = mysql_query("SELECT bullets FROM user_stats WHERE user_id = '".$extort['id']."' AND `bullets` > 1 LIMIT 1");
$b_check = mysql_fetch_assoc($bullets);

?

Link to comment
Share on other sites

For in_array() to work in this case, you need to redefine $Not

if(mysql_num_rows($car) == 0) {
$Not[0][] = 1;
}
if($c_check['money'] < 100) {
$Not[0][] = 2;
}				
if($b_check['bullets'] == 0) {
$Not[0][] = 3;
}

 

However, it's an incredibly complicated mechanism you're trying to use.

Straight variable comparison would be easier; or if you have to use arrays you could use array_intersect()

Link to comment
Share on other sites

cleaner version:

<?php
function StealType($extort)
{
$car = mysql_query("SELECT id FROM user_cars WHERE user_id = '".$extort['id']."' AND up_for_sale = '0' ORDER BY RAND() LIMIT 1");

$cash = mysql_query("SELECT money FROM user_stats WHERE user_id = '".$extort['id']."' AND `money` > 100 LIMIT 1");
$c_check = mysql_fetch_assoc($cash);

$bullets = mysql_query("SELECT bullets FROM user_stats WHERE user_id = '".$extort['id']."' AND `bullets` > 1 LIMIT 1");
$b_check = mysql_fetch_assoc($bullets);
$total = 0;
if(mysql_num_rows($car) == 0)
{
	$total = $total + 1;
}

if($c_check['money'] < 100)
{
	$total = $total + 2;
}

if($b_check['bullets'] == 0)
{
	$total = $total + 4;
}
switch ($total){
	case 0:
		echo "S2";
		return rand(1,3);
		break;
	case 1:
		echo "S8";
		return rand(2,3);
		break;
	case 2:
		echo "S7";
		return 1;
		break;
	case 3:
		echo "S5";
		return 3;
		break;
	case 4:
		echo "S6";
		return rand(1,2);
		break;
	case 5:
		echo "S4";
		return 2;
		break;
	case 6:
		echo "S3";
		return 1;
		break;
	case 7:
		echo "S1";
		return false;
		break;
}
}

$steal = StealType($extort);
if($steal == false)
{
echo "Failed! This user has nothing!";
}
elseif($steal == 1)
{
echo "To come car";
}
elseif($steal == 2)
{
echo "To come cash";
}
elseif($steal == 3)
{
echo "To come bullets";
}
else
echo "Error! What the fuck?";
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.