Jump to content

Inventory system, cant sell weapons, or equip them.


MasterACE14

Recommended Posts

Hi guys,

 

I have a Armoury and Inventory system for my game. The Armoury is working fine. Players can buy weapons at the Armoury, and thats put in the MySQL database. Now, when they go to equip, or sell there weapon in the Inventory page. It doesn't want to work.

 

When a player presses the "Equip" link next to there weapon, it goes to Inventory.php with the $_GET['id'] but nothing happens.

 

and When a player presses the "Sell" link next to there weapon, it goes to Armoury.php with the $_GET[]'s but it returns this message:

Sorry, that item does not exist!

 

here is the relevent code to "Equip":

<?php
player_session();

$player = player_table($_SESSION['playerid']);


if ($_GET['id'])
{
$query = @mysql_query("SELECT `status`, `item_id` FROM `items` WHERE `id`='" . $_GET['id'] . "' AND `player_id`='" . $player['id'] . "'");
$query2 = @mysql_query("SELECT * FROM `items`, `blueprint_items` WHERE items.item_id = blueprint_items.id AND blueprint_items.type=(SELECT `type` FROM `blueprint_items` WHERE `id`='" . $item['item_id'] . "') AND items.player_id='" . $player['id'] . "' AND `status`='equipped'");
if (mysql_num_rows($query) == 1)
{

$item = mysql_fetch_array($query);
$bpi = mysql_fetch_array($query2);

switch($bpi['type'])
{
	// weapon
	case "weapon":
	$type = "weapon";
	$action = "strikeaction";
	break;
	// armour
	case "armour":
	$type = "armour";
	$action = "defenceaction";
	break;
	// vehicle
	case "vehicle":
	$type = "vehicle";
	$action = "covertaction";
	break;
}

	switch($item['status'])
	{
		case "unequipped": //User wants to equip item
			$itemtype = @mysql_query("SELECT `type` FROM `blueprint_items` WHERE `id`='" . $item['item_id'] . "'");

			//Check if another item is already equipped
			$unequip = @mysql_query("SELECT items.id FROM `items`, `blueprint_items` WHERE items.item_id = blueprint_items.id AND blueprint_items.type=(SELECT `type` FROM `blueprint_items` WHERE `id`='" . $item['item_id'] . "') AND items.player_id='" . $player['id'] . "' AND `status`='equipped'");
			if ($unequip) //If so, then unequip it (only one item may be equipped at any one time)
			{
				$query = @mysql_query("UPDATE `items` SET `status`='unequipped' WHERE `id`='" . $unequip . "'");
				$query2 = @mysql_query("UPDATE `cf_users` SET `" . $action . "`=`" . $action . "`-'" . $bpi['effectiveness'] . "' WHERE `id`='" . $unequip . "'");  # take away the strike/defence or covert action
			}
			//Equip the selected item
			$query = @mysql_query("UPDATE `items` SET `status`='equipped' WHERE `id`='" . $_GET['id'] . "'");
			$query2 = @mysql_query("UPDATE `cf_users` SET `" . $action . "`=`" . $action . "`+'" . $bpi['effectiveness'] . "' WHERE `id`='" . $_GET['id'] . "'"); # add the strike/defence or covert action
			break;
		case "equipped": //User wants to unequip item
			$query = @mysql_query("UPDATE `items` SET `status`='unequipped' WHERE `id`='" . $_GET['id'] . "'");
			$query2 = @mysql_query("UPDATE `cf_users` SET `" . $action . "`=`" . $action . "`-'" . $bpi['effectiveness'] . "' WHERE `id`='" . $unequip . "'");  # take away the strike/defence or covert action
			break;
		default: //Set status to unequipped, in case the item had no status when it was inserted into db
			$query = @mysql_query("UPDATE `items` SET `status`='unequipped' WHERE `id`='" . $_GET['id'] . "'");
			break;
	}
}
}


?>

<b>Weapons:</b>
<br /><br />
<?php
$query = @mysql_query("SELECT * FROM `items`, `blueprint_items` WHERE blueprint_items.id=items.item_id AND items.player_id='" . $player['id'] . "' AND blueprint_items.type='weapon' ORDER BY items.status ASC");
if (mysql_num_rows($query) == 0)
{
echo "<br /><b>You have no weapons.</b>";
}
else
{
while($item = mysql_fetch_array($query))
{
	echo "<fieldset>\n<legend>";
	echo "<b>" . $item['name'] . "</b></legend>\n";
	echo "<table width=\"100%\">\n";
	echo "<tr><td width=\"85%\">";
	echo $item['description'] . "\n<br />";
	echo "<b>Effectiveness:</b> " . $item['effectiveness'] . "\n";
	echo "</td><td width=\"15%\">";
	echo "<a href=\"index.php?page=armoury&act=sell&id=" . $item['id'] . "\">Sell</a><br />";
	echo "<a href=\"index.php?page=inventory&id=" . $item['id'] . "\">";
	echo ($item['status'] == "equipped")?"Unequip":"Equip";
	echo "</a>";
	echo "</td></tr>\n";
	echo "</table>";
	echo "</fieldset>\n<br />";
}
}
?>

 

sorry there is a fair bit of code relevant to the problem  :-\

 

and here is the code related to "Sell" and the relevant code in Armoury.php:

<?php
case "sell":
	if (!$_GET['id']) //No item ID
	{
		header("Location: index.php?page=armoury");
		break;
	}

	//Select the item from the database
	$query = @mysql_query("SELECT * FROM `blueprint_items`, `items` WHERE items.item_id=blueprint_items.id AND items.player_id='" . $player['id'] . "' and items.id='" . $_GET['id'] . "'");

	//Either item doesn't exist, or item doesn't belong to user
	if (mysql_num_rows($query) == 0)
	{
		echo "Sorry, that item does not exist!";
		break;
	}

	$sell = mysql_fetch_array($query); //Get item info

	//Check to make sure clicking Sell wasn't an accident
	if (!$_POST['sure'])
	{
		echo "Are you sure you want to sell your <b>" . $sell['name'] . "</b> for <b>" . floor($sell['price']/2) . "</b> money?<br /><br />\n";
		echo "<form method=\"post\" action=\"index.php?page=armoury&act=sell&id=" . $sell['id'] . "\">\n";
		echo "<input type=\"submit\" name=\"sure\" value=\"Yes, I am sure!\" />\n";
		echo "</form>\n";
		break;
	}

	//Delete item from database, add money to player's account
	$query = @mysql_query("DELETE FROM `items` WHERE `id`='" . $sell['id'] . "'");
	$query = @mysql_query("UPDATE `cf_users` SET `money`='" . $player['money'] + floor($sell['price']/2) . "' WHERE `id`='" . $player['id'] . "'");

 

Any help is greatly appreciated.

 

Regards ACE

Link to comment
Share on other sites

I have a quick question, sorry.  Does this:

$player = player_table($_SESSION['playerid']);

Do what I think it does?  Are you reloading all of the person's info on every single page when it can be just as easily put into the session to save load times?

Link to comment
Share on other sites

this is the function:

<?php
// Select fields from other users table
function player_table($id=null){
    if($id == null) { $id = $_SESSION['playerid']; }
    $sql = "SELECT * FROM cf_users,cf_users2 WHERE cf_users.id= '$id' && cf_users2.id= '$id' LIMIT 1";
    $rs = @mysql_query($sql) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error());
    $row = mysql_fetch_assoc($rs);
return $row;
    
};

Link to comment
Share on other sites

<?php
// Select fields from other users table
function player_table($id=NULL;){
    if($id == NULL) { $id = $_SESSION['playerid']; }
    $sql = "SELECT * FROM cf_users,cf_users2 WHERE cf_users.id= '$id' && cf_users2.id= '$id' LIMIT 1";
    $rs = mysql_query($sql) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error());
    $row = mysql_fetch_assoc($rs);
return $row;
    
};?>

Link to comment
Share on other sites

The function isn't the problem lol. I was just showing DarkWater.

 

these are the 2 problems relating to the other 2 codes:

When a player presses the "Equip" link next to there weapon, it goes to Inventory.php with the $_GET['id'] but nothing happens.

 

and When a player presses the "Sell" link next to there weapon, it goes to Armoury.php with the $_GET[]'s but it returns this message:

Sorry, that item does not exist!

Link to comment
Share on other sites

I just put error reporting on(E_ALL).

 

In armoury.php it has this:

Notice: Undefined index: act in /home/ace/public_html/conflictingforces/lib/armoury.php on line 7

 

in inventory.php is has this:

Notice: Undefined index: id in /home/ace/public_html/conflictingforces/lib/inventory.php on line 8

Link to comment
Share on other sites

I've put isset() around the $_GET's in armoury.php and inventory.php.

 

Now when I press "Sell" , it actually buys another weapon. Instead of Selling one I already have.

 

and when I press "Equip" it gives me this message:

Notice: Undefined variable: item in /home/ace/public_html/conflictingforces/lib/inventory.php on line 11

 

Link to comment
Share on other sites

Try this with your query

 

if (mysql_query($query)) {

    blah blah

} else {

    print "There was an error with MySQL, the error was " . mysql_error() . " The query was " . $query;

}

 

 

Thats a great debugging script for MySQL. IF you are using PHPmyadmin, then you can past that query that it submitted into the SQL section of PHPmyAdmin, and it will show you the results, if any. That way you can see if it is a q

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.