Jump to content

HELP PLEASE!!


SieRobin

Recommended Posts

Ok so I'm running a Browser Based RPG, where players play amongst each other, whatever.. not the point. If anyone knows anything about RPGs you know you have an inventory, and well obviously I'm trying to make an inventory, EVERYTHING is set up except when they go an equipped a weapon, but HAVE more than one item in their inventory, it pulls the first item that player owns and puts the effects into the database, rather than the one you actually equipped. I'll be posting a series of pictures, just so you know what's going on.

[code]<?php
session_start();
include 'connect.php'; 
?>
<title>Feudal Age - Inventory</title>
<link rel="stylesheet" href="style.css" type="text/css">
<?php
include "navigate.php";
if (isset($_SESSION['player']))
  {
$player=$_SESSION['player'];
    $userstats="SELECT * from users where playername='$player'";
    $userstats2=mysql_query($userstats) or die("Could not retrieve the users stats.");
    $userstats3=mysql_fetch_array($userstats2);
$inventory="SELECT * from inventory where UID='$userstats3[ID]' order by IID";
$inventory2=mysql_query($inventory) or die("Could not get inventory.");

print "<table class='table'><tr class='headline'><td colspan='4'><center>Inventory</center></td></tr>
<tr><td class='mainrowb' width='40px'>Type</td><td class='mainrowb'>Item Name</td><td class='mainrowb' width='100px'>Sell</td><td class='mainrowb' width='100px'>Status</td></tr>";

//Displays what's in your inventory, just a couple of quirks that add some effect to the inventory, instead of it being so dull.
while ($inventory3=mysql_fetch_array($inventory2)) {
$uinventory="SELECT * from market where ID='$inventory3[IID]'";
$uinventory2=mysql_query($uinventory) or die("Could not get inventory.");
$uinventory3=mysql_fetch_array($uinventory2);
print "<tr class='mainrow'>";
if ($uinventory3['type']=="weapon") {
print "<td><font color='red' face='webdings' size='2'>•</font></td>";
}
elseif ($uinventory3['type']=="shield") {
print "<td><font color='white' face='webdings' size='2'>d</font></td>";
}
elseif ($uinventory3['type']=="armor") {
print "<td><font color='green' face='webdings' size='2'>€</font></td>";
}

//Scripting to sell your items for a portion of the cost you bought it for.
$sellp=$uinventory3['cost']*.6;
$sell=$_GET['sell'];
$item=$_GET['item'];
print "<td>$uinventory3[name]</td>";
print "<td><a href='inventory.php?sell=$sellp&item=$inventory3[IID]'>$sellp</a> Gold</td>";
if (isset($sell)) {
mysql_query("UPDATE users set gold=gold+'$sell' where ID='$userstats3[ID]'");
mysql_query("DELETE from inventory where UID='$userstats3[ID]' AND IID='$item'");
print "<script type='text/javascript'>
onload = function () {location.reload('inventory.php')}
</script>";
}
if ($inventory3['status']==0) {
print "<td><a href='inventory.php?equip=$inventory3[IID]&type=$uinventory3[type]&user=$userstats3[ID]'>Equip</a> | Unequip</td></tr>";
} else {
print "<td>Equip | <a href='inventory.php?unequip=$inventory3[IID]&type=$uinventory3[type]&user=$userstats3[ID]'>Unequip</a></td></tr>";
}
}

//This is where all the equipping goes to, it's quite confusing, but if you notice the first if statements simply explain that if you have a weapon equipped, you cannot equipped another, so on and so forth.
$dinventory="SELECT * from inventory where UID='$userstats3[ID]'";
$dinventory2=mysql_query($dinventory) or die("Could not get inventory.");
$dinventory3=mysql_fetch_array($dinventory2);
$dequip="SELECT * from market where ID='$dinventory3[IID]'";
$dequip2=mysql_query($dequip) or die("Could not get inventory.");
$dequip3=mysql_fetch_array($dequip2);
$equip=$_GET['equip'];
$unequip=$_GET['unequip'];
$user=$_GET['user'];
$type=$_GET['type'];
if (isset($equip)) {
if ($userstats3['iwstatus']==1&&$type=="weapon") {
print "<table class='table'><tr class='headline'><td colspan='4'><center>Inventory</center></td></tr>
<tr class='mainrow'><td><center>You can not equip more than one weapon, please return to your <a href='inventory.php'>inventory</a> to unequip your current weapon.</td></tr></table>";
exit;
}
if ($userstats3['iastatus']==1&&$type=="armor") {
print "<table class='table'><tr class='headline'><td colspan='4'><center>Inventory</center></td></tr>
<tr class='mainrow'><td><center>You can not equip more than one armor, please return to your <a href='inventory.php'>inventory</a> to unequip your current armor.</td></tr></table>";
exit;
}
if ($userstats3['isstatus']==1&&$type=="shield") {
print "<table class='table'><tr class='headline'><td colspan='4'><center>Inventory</center></td></tr>
<tr class='mainrow'><td><center>You can not equip more than one shield, please return to your <a href='inventory.php'>inventory</a> to unequip your current shield.</td></tr></table>";
exit;
} else {

//Updates the database, with the effects the items give.
mysql_query("UPDATE inventory set status=1 where UID='$userstats3[ID]' AND IID='$equip'");
if ($type=="weapon") {
mysql_query("UPDATE users set iwstatus=1, istr=istr+'$dequip3[dmg]', iarm=iarm+'$dequip3[arm]', iagil=iagil+'$dequip3[aagil]', idex=idex+'$dequip3[adex]' where ID='$userstats3[ID]'"); }
elseif ($type=="armor") {
mysql_query("UPDATE users set iastatus=1, istr=istr+'$dequip3[dmg]', iarm=iarm+'$dequip3[arm]', iagil=iagil+'$dequip3[aagil]', idex=idex+'$dequip3[adex]' where ID='$userstats3[ID]'"); }
elseif ($type=="shield") {
mysql_query("UPDATE users set isstatus=1, istr=istr+'$dequip3[dmg]', iarm=iarm+'$dequip3[arm]', iagil=iagil+'$dequip3[aagil]', idex=idex+'$dequip3[adex]' where ID='$userstats3[ID]'"); }
print "<script type='text/javascript'>
onload = function () {location.reload('inventory.php')}
</script>";
}
}

//Unequips the item you select, pretty basic.
if (isset($unequip)) {
mysql_query("UPDATE inventory set status=0 where UID='$userstats3[ID]' AND IID='$unequip'");
if ($type=="weapon") {
mysql_query("UPDATE users set iwstatus=0, istr=istr-'$dequip3[dmg]', iarm=iarm-'$dequip3[arm]', iagil=iagil-'$dequip3[aagil]', idex=idex-'$dequip3[adex]' where ID='$userstats3[ID]'"); }
elseif ($type=="armor") {
mysql_query("UPDATE users set iastatus=0, istr=istr-'$dequip3[dmg]', iarm=iarm-'$dequip3[arm]', iagil=iagil-'$dequip3[aagil]', idex=idex-'$dequip3[adex]' where ID='$userstats3[ID]'"); }
elseif ($type=="shield") {
mysql_query("UPDATE users set isstatus=0, istr=istr-'$dequip3[dmg]', iarm=iarm-'$dequip3[arm]', iagil=iagil-'$dequip3[aagil]', idex=idex-'$dequip3[adex]' where ID='$userstats3[ID]'"); }
print "<script type='text/javascript'>
onload = function () {location.reload('inventory.php')}
</script>";
}
  print "</table>";
  }
?>[/code]

That's the inventory script, I put in some pointers to point out what is what, instead of leaving you in the dark. The next is a picture of what the inventory looks like.

[img]http://img.photobucket.com/albums/v679/christopherobin/inventory.jpg[/img]

The next is of the database for the actual inventory.. IID stand for Item ID and UID stands for User ID, now the status is to know when the item is actually equipped.

[IMG]http://img.photobucket.com/albums/v679/christopherobin/databaseinv.jpg[/img]

The next image shows the market, it basically shows the effects of each item, because when it's equipped it takes the market identifiers and puts them into your user stats.

[IMG]http://img.photobucket.com/albums/v679/christopherobin/databasemarket.jpg[/img]

One final image here, this shows the portion of the user database of which the item stats are updated and the and suggests how the script knows you already have a item equipped.

[IMG]http://img.photobucket.com/albums/v679/christopherobin/databaseuser.jpg[/img]

Now istr stands for Item Strength, it's just what the item effects your stats at, so on and so forth for the others. iwstatus stands for Item Weapon Status, just suggests if you have a weapon equipped or not, and so on for the other status. If someone could please help me, I would appreciate it greatly, if you have any questions, please ask.
Link to comment
Share on other sites

Wouldn't you want to do a while loop for the inventory?

In any case, it looks to me like you are making things WAY harder than they have to be. I think instead of making this massive file with all the actions happening, you should consider breaking them up. Then, you can use the $_GET method more effectively for purchasing and equipping the player's items.

So you're having no problems buying the items? Just equipping them? Can you have more than one item and leave some of them unequipped?

Would you mind if I just wrote an equip.php page for you instead of editing what you have now? It's somewhat ridiculously complicated for what *should* be a simple process.
Link to comment
Share on other sites

Sure you can write your own.. and yes you can buy them just fine and you can leave some unequipped, only equips the ones you click Equip on. The only thing that doesn't work is when it updates the effects into the database.
Link to comment
Share on other sites

Here's something really quick...
(i'm assuming $_SESSION['player'] is the player's user ID
[code]
<?php
// Connect to database
session_start();
if (!isset($_SESSION['player'])) {
  echo 'You must log in.';
  include("footer.php");
  die();
}
else {
  $player = $_SESSION['player'];
}

if (isset($_GET['equip'])) {
  $errors = array();
  if (!is_numeric($_GET['equip'])) {
    $errors[] = 'You have accessed this page incorrectly.';
  }
  else {
   $equip = trim($_GET['equip']);
  }
  if (empty($errors)) {
    // Make sure they have the item first, and that it's not eqipped
    $query = "SELECT status FROM inventory WHERE (iid='$eqip' AND uid='$player')";
    $result = mysql_query($query);
    if (mysql_num_rows($result) == 1) { // Found item
      $row = mysql_fetch_array($result, MYSQL_NUM);
      $status = $row[0];
      if ($status == 0) { // Not equipped
        $query = "UPDATE inventory SET status=1 WHERE (iid='$eqip' AND uid='$player) LIMIT 1";
        $result = mysql_query($query);
        if ($result) {
            echo 'That item has been sucessfully equipped.';
        }
        else {
            echo 'The item could not be equipped due to a mySQL error:<br>'.mysql_error();
        }
     }
     else {
       echo 'That item is already equipped.';
     }
   }
   else {
    echo 'That item does not belong to you.';
   }
}
else {
   echo 'You have accessed this page in error.';
}
?>
[/code]

I'm not sure if all my { } line up since this little box makes it hard to tell, but thats the jist of it.

Then, when you are showing the actual items the person is carying, put a link below to equip.php?equip=1234 or whatever the ID of the item is.
Link to comment
Share on other sites

All of that already works.. usually in any RPG when you put a weapon on it updates your stats somewhat and effects your damage. Strength is damage in my game, therefore a sword will update your damage, well if say I equip my Wooden Sword, it will update the item strength to 9 instead of 5, which 9 is the Rusty Swords strength effect, what I'm saying, is it's not updating my effects correctly.
Link to comment
Share on other sites

I thought you meant that it was updating the wrong thing. I guess I misunderstood your post.

So all of your items have their own iid, and each has their own damage?
Which part of your script does that part? It's all a bit jumbled-looking to me. XD
Link to comment
Share on other sites

[code]else {

//Updates the database, with the effects the items give.
mysql_query("UPDATE inventory set status=1 where UID='$userstats3[ID]' AND IID='$equip'");
if ($type=="weapon") {
mysql_query("UPDATE users set iwstatus=1, istr=istr+'$dequip3[dmg]', iarm=iarm+'$dequip3[arm]', iagil=iagil+'$dequip3[aagil]', idex=idex+'$dequip3[adex]' where ID='$userstats3[ID]'"); }
elseif ($type=="armor") {
mysql_query("UPDATE users set iastatus=1, istr=istr+'$dequip3[dmg]', iarm=iarm+'$dequip3[arm]', iagil=iagil+'$dequip3[aagil]', idex=idex+'$dequip3[adex]' where ID='$userstats3[ID]'"); }
elseif ($type=="shield") {
mysql_query("UPDATE users set isstatus=1, istr=istr+'$dequip3[dmg]', iarm=iarm+'$dequip3[arm]', iagil=iagil+'$dequip3[aagil]', idex=idex+'$dequip3[adex]' where ID='$userstats3[ID]'"); }
print "<script type='text/javascript'>
onload = function () {location.reload('inventory.php')}
</script>";
}
}[/code]

This is where it updates into the database, everything updates just fine but when it's putting the effects in, it doesn't do it correctly, puts in the first item in line's effects, instead of the actual item you equip.
Link to comment
Share on other sites

What I would do is query the database for the stats they currently have and show them first. Make those into nicer variables, and do the math outside of mySQL first. That way you can see what it's trying to put in before you update and fix the problem there.

I'm getting off the computer, but I could write something for updating the stats tomarrow.
Link to comment
Share on other sites

You can just plug this in where it goes.
In this case, I would suggest making simpler forms of your sessions and storing the player's ID in a session also...

So while I write this, I'm assuming that $player is their username, and $player_id is their player id. Much easier than $_SESSION plus it doesn't have ' ' marks, so it won't make the script go wonky.

[code]
<?php
// First find out current stats.
$query = "SELECT istr, iagil, iarm, idex FROM users WHERE username='$player'";
$result = mysql_query($query);
if ($result) {
  $row = mysql_fetch_array($result, MYSQL_NUM);
  // Show current values -- you can take these out eventually, it's just to catch where the problem is
  echo '<li> Strength: '.$row[0].'</li>
  <li> Agility: '.$row[1].'</li>
  <li> Armory: '.$row[2].'</li>
  <li> Dex: '.$row[3].'</li>';
  $query2 = "SELECT dmg, arm, adex, aagil FROM market WHERE id='$player_id'";
  $result2 = mysql_query($query2);
  if (mysql_num_rows($result2) > 0) {
    $row2 = mysql_fetch_assoc($result);
    // Show the effects of the item being equipped
    echo 'Damage: '.$row2['dmg'].'<br>
    Armor: '.$row2['arm'].'<br>
    Adex: '.$row2['adex'].'<br>
    Agil: '.$row2['aagil'].'<br>';
    // Now do the math to make sure values are right before putting them in the DB
    $new_dmg = $row[0] + $row2['dmg'];
    $new_arm = $row[2] + $row2['arm'];
    $new_agil = $row[1] + $row2['aagil'];
    $new_dex = $row[3] + $row2['adex'];
    echo "The new damage is $new_dmg, the new armor is $new_arm, the new agil is $new_agil, the new dex is $new_dex.";
    // You should put the type of item in the item's description so we know which status to update to one.
    $new_query = "UPDATE users SET istr='$new_dmg', iagil='$new_agil', iarm='$new_arm', idex='$new_dex' WHERE uid='$player_id'";
    $new_result = mysql_query($new_query);
    if ($new_result) {
      echo 'Item stats were sucessfully updated.';
    }
    else {
      echo mysql_error();
    }
  }
  else {
    echo 'You have no items.';
  }
}
else {
  echo mysql_error();
}
?>
[/code]

As I said in the script, it dont know how you are determining the type of each item (weapon, versus armor, etc.) but we could easily adjust the query to update the status of that as equipped.
Link to comment
Share on other sites

  • 2 weeks later...
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.