Loading comodities into the inventory

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
Cdaman
Posts: 15
Joined: Tue Dec 06, 2011 2:08 am

Loading comodities into the inventory

Post by Cdaman »

Code: Select all

<?php
//links connect.php
include 'connect.php';
//starts a session
session_start();

?>
<!-- sets up a style sheet, that sets the text colour, and the background image -->
<style type="text/css">

<!--

body {

	color: #FFFFFF;
}

body {

	background-color: #000000;

	background-image: url(empty_space.jpg);

}

-->

</style><center>

<!-- puts the logo on the page -->
  <img src="logo.png" width="600" height="220" />

</center>

<?php

//if the session 'player' is started then it runs this code
if (isset ($_SESSION['player']))
{
  $player=$_SESSION['player'];
}
else
{
  //if not then it redirects to the login page
  echo "Not Logged in <br><br> <A href='login.php'>Login</a>";
  exit;
}

//need to get player and creature stats
$playerinfo="select * from gamers where playername='$player'";
$playerinfo2=mysql_query($playerinfo) or die("could not get player stats");
$playerinfo3=mysql_fetch_array($playerinfo2);

//gets the creature info if creature is manually assigned
if (isset($_GET['creature']))
{
  $creature=$_GET['creature'];
  $creatureinfo="select * from banditoes where bname='$creature'";
  $creatureinfo2=mysql_query($creatureinfo) or die("could not get the creature you where fighting");
  $creatureinfo3=mysql_fetch_array($creatureinfo2);
}
else
{
  //if it can't get the selection then do this
  echo "<a href='testingpve.php>No enemy Selected. Go Back!</a>";
  exit;
}
 //assigns the player's hpoints, attack, and defense to variables
$playerhp = $playerinfo3['shields'];
$playerattack = $playerinfo3['attack'];
$playerdefense = $playerinfo3['defense'];

//same as above
$creature = $creatureinfo3['bname'];
$creaturehp = $creatureinfo3['shields'];
$creatureattack = $creatureinfo3['attack'];
$creaturecredits = $creatureinfo3['credits'];
$creaturedefense = $creatureinfo3['defense'];
$creatureloot = $creatureinfo3['loot'];
$hasloot = $creatureinfo3['isloot'];

 ////////////////Player's Turn///////////////////

 $weaponinfo="SELECT * from playerweapons where equip=1 AND pid='$playerinfo3[id]'";
 $weaponinfo2=mysql_query($weaponinfo) or die(mysql_error());
 $weaponinfo3=mysql_fetch_array($weaponinfo2);
 
 $playerattack = rand(1,20) + $playerattack + $weaponinfo3['rating'];

 //says that it is the players turn to attack, then selects a random number from 1-20
 //and assigns that + the player's base attack to a varibale called $playerattack
 //does the same with the creature's defense
 echo "<u> " . $playerinfo3['playername'] . "'s Attack</u><br>";
 $creaturedefense = rand(1, 20) + $creaturedefense;

 //says what the attack and defence rolls are
 echo $playerinfo3['playername'] . "'s attack roll is " . $playerattack . "<br>";
 echo $creature . "'s defense roll is " . $creaturedefense . "<br>";

 //if the player's attack roll is higher then the creature's defence roll then this
 //code segment runs.
 if ($playerattack > $creaturedefense)
 {
   //says that the player hits
   echo $playerinfo3['playername'] . " " . $weaponinfo3['hittext'] . " and hits! <br>";
   //makes a variable called $player damage and assigns it a random value (1-6)
   $playerdamage = rand(1, 6);
   $newdamage = $playerdamage+$weaponinfo3['damage'];
   //creates a new varibale that subtracts the the $playerdamage variable from the
   //$creaturehp variable
   $newcreaturehp = $creaturehp - $playerdamage;
   echo "For " . $playerdamage . " points of damage. <br>";
   if ($newcreaturehp < 1)
   {
     //if the creature's health is depleted it says the creature has been
     //killed and deletes the creature from the database
     echo "The " . $creature . " has been killed<br>";
     //makes a varibale with the creature's max hpoints
     $crmaxhpoints = $creatureinfo3['maxhpoints'];
     
     //creates a function called creature killed
     creaturekilled($creatureinfo3['loot'],$creatureinfo3['credits'],$hasloot,$player,$playerinfo3['id'],$creature,$creatureinfo,$creatureinfo2,$creatureinfo3,$playerinfo,$playerinfo2,$playerinfo3);
     

     //then updates the creature so that it's health is back at full once it is killed
     $updateplayer = "UPDATE banditoes set shields='$crmaxhpoints' where bname='$creature' limit 1";
     mysql_query($updateplayer) or die("could not update player");

   //if the player is higher level then the creature they get a small amount of the
   //experiance
   if ($playerinfo3['level'] > $creatureinfo3['level'])
   {
       $firstmod = $playerinfo3['level'] - $creatureinfo3['level'];
       $secondmod = $firstmod * 10 ;
       if ($secondmod > 90) {$secondmod = 90;}
       $thirdmod = ($secondmod / 100) * $creatureinfo3['exper'];
       $totalexper = $creatureinfo3['exper'] - $thirdmod;
   }
   //if the creature is higher level then the player, the player gets more experiance
   else
   {
     $firstmod = $creatureinfo3['level'] - $playerinfo3['level'];
     $secondmod = $firstmod * 10 ;
     if ($secondmod > 90) {$secondmod = 90; }
     $thirdmod = ($secondmod / 100) * $creatureinfo3['exper'];
     $totalexper = $creatureinfo3['exper'] + $thirdmod;
   }

   //changes totalexper from a float to an int
   $totalexper = (int)$totalexper;

   //says you gain experiance, then updates the players table with the new
   //info, increasing the players experiance (exper), then queries this update
   //if it did not work it gives an error.
   echo "<br><b><big>You gain " . $totalexper . " experience.</b></big><br>";
   $updateplayer="update gamers set exper=exper+'$totalexper' where playername='$player'";
   mysql_query($updateplayer) or die("Could not update player");

     echo"<br>";
     echo "<a href='fight.php'>Go Back</a>";
     exit;
   }
   //if the creature's health isnt full depleted this subtracts the damage done
   //and shows it
   $updatecreature="update banditoes set shields='$newcreaturehp' where bname='$creature' limit 1";
   mysql_query($updatecreature) or die("Could not update creature");
 }
 else
 {
   //if the creature's defense roll is higher then the player's attack roll the
   //player's attack misses
     echo $playerinfo3['playername'] . " " . $weaponinfo3['hittext'] . " and Misses! <br>";
 }
 /////////////////////////creature's turn/////////////////

 //same as above
 echo "<u> " . $creature . "'s Attack</u><br>";

 //armour
 /*$randlocation = rand(0,10);
 if ($randlocation <=5)
 {
   $location= "Hull";
 }
 */

 $location= "Hull";
 $playerarmour="SELECT * from playerarmour where pid='$playerinfo3[id]' AND location='$location' AND equip=1";
 $playerarmour2=mysql_query($playerarmour) or die(mysql_error());
 $playerarmour3=mysql_fetch_array($playerarmour2);

 $creatureattack = rand(1, 20) + $creatureattack;
 $playerdefense = rand(1, 20) + $playerdefense + $playerarmour3['rating'];

 echo $creature . "'s Attack roll is " . $creatureattack . "<br>";
 echo $playerinfo3['playername'] . "'s defense roll is " . $playerdefense . "<br>";

  if ($creatureattack > $playerdefense)
 {
   echo $creatureinfo3['bname'] . " hits for a strike to the " . $location . " <br>";
   $creaturedamage = rand(1, 6);
   $newplayerhp = $playerhp - $creaturedamage;
   echo "For " . $creaturedamage . " points of damage. <br>";

   //if armour isnt equipped then noarmour = 1
   if (is_null($playerarmour3['rating']))
 {
   $playerarmour3['rating'] = 0;
   $noarmour = 1;
 }
 else
 {$noarmour =0; }

   //if no armour is equipped then it says it isnt and makes a new varible which stores the extra
   //damage you recieve
   //then subtracts that and the creature's damage off healthpoints
   if ($noarmour == 1)
   {
     echo "You do not have any hull plating equiped! <br>";
     $extradamage = rand(1,5);
     echo "You recieve an additional " . $extradamage . " points of damage! <br>";
     $newplayerhp =  $newplayerhp - $extradamage;
   }
  else
   {
     //else a variable called protection stores a random number between 1 and 5
     $protection  = rand(1,5);
     //if $protection is greater then $ creature damage then
     if ($protection > $creaturedamage)
     {
       //$new protection is equal to $creature damage
       $newprotection = $creaturedamage;
       //it tell how much is deflected (which is the full amount of the creature's damage)
       echo "Your hull armour deflects " . $newprotection . " points of damage!<br>";
       //it then subtracts the creature's damage - the amount of protection you get
       $newplayerhp = $newplayerhp + $newprotection;
     }
     //else if the amount deflected isnt equal to the amount of damage the creature does
     else
     {
      //says how much damage is reflected and subtracts your damage recieved
      echo "Your hull armour deflects " . $protection . " points of damage!<br>";
      $newplayerhp = $newplayerhp + $protection;
     }
   }
   if ($newplayerhp < 1)
   {
     //if the player dies it heals you and takes you to the main page
     $updateplayer2="update gamers set shields='$playerinfo3[maxshields]' where playername='$player'";
     mysql_query($updateplayer2) or die(mysql_error());
     echo $playerinfo3['playername'] . " has been killed<br>";
     echo "<a href='index.php'>Continue</a>";
     exit;
   }
   $updateplayer="update gamers set shields='$newplayerhp' where playername='$player'";
   mysql_query($updateplayer) or die("could not update player");
 }
 else
 {
   echo $creature . " misses!";
 }
 echo "<br><br><a href='testingpve.php?creature=$creature'>Attack!</a>";
 echo "<br><a href='index.php'>Home</a>";

function creaturekilled($hasloot,$player,$creature,$creatureinfo,$creatureinfo2,$creatureinfo3,$playerinfo,$playerinfo2,$playerinfo3)
{
   $playerinfo="select * from gamers where playername='$player'";
   $playerinfo2=mysql_query($playerinfo) or die("could not get player stats");
   $playerinfo3=mysql_fetch_array($playerinfo2);

   $creature=$_GET['creature'];
   $creatureinfo="select * from banditoes where bname='$creature'";
   $creatureinfo2=mysql_query($creatureinfo) or die("could not get the creature you where fighting");
   $creatureinfo3=mysql_fetch_array($creatureinfo2);

   $isloot = rand(0,100);
   $hasloot = $creatureinfo3['isloot'];

   echo "You loot " . $creatureinfo3['credits'] . " Credits.<br>";

  $updatecreature = "UPDATE gamers set credits=credits+'$creatureinfo3[credits]'";
  mysql_query($updatecreature) or die("could not update creature");

  if ($isloot < $hasloot)
  {
    echo "You find a " . $creatureinfo3['loot'] . " on the " . $creatureinfo3['bname'] . ".<br>";
    //Pull the item info
    $critem="SELECT * from store where name='$creatureinfo3[loot]'";
    $critem2=mysql_query($critem) or die(mysql_error());
    $critem3=mysql_fetch_array($critem2);

    //Give the item to the player
    $randid4 = rand(999, 999999999);
    $itembought = "INSERT into inventory (id, name, stats, statadd, randid, type) VALUES
    ('$playerinfo3[id]', '$critem3[name]' ,'$critem3[stats]', '$critem3[statadd]', '$randid4', '$critem3[type]')";
    mysql_query($itembought) or die(mysql_error());
    
  }
  else
  {
    echo "You find no items of value on the creature.<br>";
  }
}

 ?>
 </div>
Following tutorial 21, I put this in. a lot of the variables didn't work, but i got around that.
the main problem is that it Inserts into the inventory, but does not insert the player id, so it doesn't show up in my inventory.
Cdaman
Posts: 15
Joined: Tue Dec 06, 2011 2:08 am

Re: Loading comodities into the inventory

Post by Cdaman »

Never Mind, i fixed it. I had to change the function a little:

Code: Select all

function creaturekilled($hasloot,$player,$creature,$creatureinfo,$creatureinfo2,$creatureinfo3,$playerinfo,$playerinfo2,$playerinfo3)
{
   $creature=$_GET['creature'];
   $creatureinfo="select * from banditoes where bname='$creature'";
   $creatureinfo2=mysql_query($creatureinfo) or die("could not get the creature you where fighting");
   $creatureinfo3=mysql_fetch_array($creatureinfo2);

   $isloot = rand(0,100);
   $hasloot = $creatureinfo3['isloot'];

   echo "You loot " . $creatureinfo3['credits'] . " Credits.<br>";

  $updatecreature = "UPDATE gamers set credits=credits+'$creatureinfo3[credits]'";
  mysql_query($updatecreature) or die("could not update creature");

  if ($isloot < $hasloot)
  {
    echo "You find a " . $creatureinfo3['loot'] . " on the " . $creatureinfo3['bname'] . ".<br>";
    //Pull the item info
    $critem="SELECT * from store where name='$creatureinfo3[loot]'";
    $critem2=mysql_query($critem) or die(mysql_error());
    $critem3=mysql_fetch_array($critem2);

    $playerinfo="select * from gamers where playername='$player'";
    $playerinfo2=mysql_query($playerinfo) or die("could not get player stats");
    $playerinfo3=mysql_fetch_array($playerinfo2);
    
    $player=$_SESSION['player'];

    //Give the item to the player
    $randid4 = rand(999, 999999999);
    $itembought = "INSERT into inventory (name, stats, statadd, randid, type, pname) VALUES
    ('$critem3[name]' ,'$critem3[stats]', '$critem3[statadd]', '$randid4', '$critem3[type]', '$player')";
    mysql_query($itembought) or die(mysql_error());
  }
  else
  {
    echo "You find no items of value on the creature.<br>";
  }
}
Post Reply

Return to “Beginner Help and Support”