View Single Post
  #3  
Old 04-14-2012, 10:38 AM
SilverDragonTears's Avatar
SilverDragonTears SilverDragonTears is offline
I am your Nemesis.
 
Join Date: Jun 2011
Posts: 1,113
Gender: Female
Credits: 110,520
SilverDragonTears is on a distinguished road
Default

I'm not sure... just now trying it out.

class_item.php

Code:
<?php

class Item{
  public $id;
  public $category;
  public $itemname;
  public $description;
  public $imageurl;
  public $function;
  public $target;
  public $shop;
  public $price;
  public $chance;
  public $cap;
  public $tradable;
  public $consumable;

  public function __construct($itemname){
	  // Fetch the database info into object property
	  $row = $GLOBALS['adopts']->select("items", array(), "itemname ='{$itemname}'")->fetchObject();
	  // loop through the anonymous object created to assign properties
      foreach($row as $key => $val){
         $this->$key = $val;		 
      }	    
  }

  public function getcategory(){
      // This method checks if the item category exists in items database or not
	  
	  $stmt = $GLOBALS['adopts']->select("items", array(), "category ='{$this->category}'");
      $cate_exist = ($row = $stmt->fetchObject())?TRUE:FALSE;     
	  return $cate_exist;
  }
 
  public function getitem(){
      // This method checks if the item exists in items database or not
	  
	  $stmt = $GLOBALS['adopts']->select("items", array(), "itemname ='{$this->itemname}'");
      $item_exist = ($row = $stmt->fetchObject())?TRUE:FALSE;     
	  return $item_exist;
  }
 
  public static function getitemimage($imageurl){
      // This method returns the item image in standard html form
   
      $imageurl = (strpos($imageurl, "http://") !== false)?$imageurl:"http://{$imageurl}";
      $image = "<img src='{$imageurl}'>";
      return $image;	  
  }

public function checktarget($aid){
      // This method checks if the item is usable
      $id = converttypetoparentid(convertidtotype($aid));
      $item_usable = FALSE;
      switch($this->target){
         case "all":
            $item_usable = TRUE;
            break;
         case "user":
            $item_usable = TRUE;
            break;
         default:
            $target = explode(",",$this->target);
            if(in_array($id, $target)) $item_usable = TRUE;            
      }
      return $item_usable;
  }
  
  public function randomchance(){
      // This method returns the item image in standard html form
	  switch($this->chance){
	     case 100:
            $item_usable = TRUE;
		    break;
         default:
		    $temp = mt_rand(0,99);
			$item_usable = ($temp < $this->chance)?TRUE:FALSE;
	  }
      return $item_usable;	  
  }
}

class Private_Item extends Item{
  public $iid;
  public $owner;
  public $quantity;
  public $status;
  
  public function __construct($itemname, $itemowner, $itemquantity = ""){
      // First of all, let's assign values to parent properties
      parent::__construct($itemname);
	 
	  // Good, now it is time to assign child properties. First let's use $itemquantity to check if it is a new item or an item in inventory
      if(empty($itemquantity)){
	    // the item is an owned item in user inventory, so retrieve database info to assign properties
	     $row = $GLOBALS['adopts']->select("inventory", array(), "itemname ='{$itemname}' and owner = '{$itemowner}'")->fetchObject();
         // loop through the anonymous object created to assign properties
         foreach($row as $key => $val){
            $this->$key = $val;
         }	 
	  }
      else{
         // the item is a new item to be added or used, and does not belong to anyone at this very moment.
         $this->owner = (!empty($itemowner))?$itemowner:"SYSTEM";
         $this->quantity = $itemquantity;
         $this->status = "Available";           
      }	  
      // the private item object is successfully created with appropriate properties assigned.	
  }
 
  public function getitem(){
      // This method checks if the item exists in inventory or not, not to be confused with parent class' getitem() class.
	  
	  $stmt = $GLOBALS['adopts']->select("inventory", array(), "itemname ='{$this->itemname}' and owner ='{$this->owner}'"); 
	  return $stmt->fetchObject();
  }
 
  public function getitemcost($salestax = 0){
      // This method returns the cost of items.
	  
      $cost = $this->price*$this->quantity*(1 + $salestax/100);
	  return $cost;
  }
  
  public static function getoldquantity($itemname, $owner = ""){
      // This method returns the quantity of items the owner already has, do not call unless both old and new quantities need to be used in script
	  $owner = (!empty($owner))?$owner:$GLOBALS['loggedinname'];
	  $stmt = $GLOBALS['adopts']->select("inventory", array("quantity"), "itemname ='{$itemname}' and owner ='{$owner}'"); 
      $oldquantity = $stmt->fetchColumn();
      return $oldquantity;	  
  }

  public static function showinventory($owner){
      // This method returns a list of items the user owned

	  $stmt = $GLOBALS['adopts']->join("items", "items.itemname = inventory.itemname")
							    ->select("inventory", array(), constant("PREFIX")."inventory.owner = '{$owner}' ORDER BY ".constant("PREFIX")."inventory.iid");  
      $table = new Table("Inventory", array("Image", "Category", "Name", "Description", "Quantity", "Use", "Sell", "Toss"));
      $content = $table->getheader();	
				  
	  while($row = $stmt->fetchObject()){
	     // First we need to retrieve item properties.      						  
      	 $use = ($row->consumable == "yes")?"<form name='use' method='post' action='inventory.php?act=useitem'>
                    <p><input name='act' type='hidden' id='act' value='{$row->itemname}'>
	    	        <input name='itemname' type='hidden' id='itemname' value='{$row->itemname}'></p>
					<p><input type='submit' name='use' value='Use'></p></form>":"N/A";

         $sell = ($row->category == "Key Items")?"N/A":"<form name='sell' method='post' action='inventory.php?act=sellitem'>
                     <input name='itemname' type='hidden' id='itemname' value='{$row->itemname}'>
                     <input name='quantity' type='text' id='quantity' size='3' maxlength='3' />
                     <p><input type='submit' name='Submit' value='Sell'></p></form>";
			
	     $toss = ($row->category == "Key Items")?"N/A":"<form name='toss' method='post' action='inventory.php?act=tossitem'>
                     <p><input name='act' type='hidden' id='act' value='{$row->itemname}'>
		             <input name='itemname' type='hidden' id='itemname' value='{$row->itemname}'></p>
                     <p><input type='submit' name='toss' value='Toss'></p></form>";
          
         $content .= $table->buildtable(array(Item::getitemimage($row->imageurl), $row->category, $row->itemname, $row->description, $row->quantity, $use, $sell, $toss), "center")->showtable();
	  }
	  $content .= $table->endtable();
      return $content;	  
  }
  
  public function additem($quantity= 1, $owner = ""){
      // This method adds items to users inventory
  
      $this->owner = (!empty($owner))?$owner:$this->owner;
	  $oldquantity = Private_Item::getoldquantity($this->itemname, $owner);	  
      if($oldquantity > 0){
	     // the item already exists, update the row for user's inventory
	     $newquantity = $oldquantity + $quantity;
		 $GLOBALS['adopts']->update("inventory", array("quantity" => $newquantity), "itemname ='{$this->itemname}' and owner='{$this->owner}'");  
	  }
	  else{	     
		 // the item does not exist yet, insert a new row into user's inventory
		 $GLOBALS['adopts']->insert("inventory", array("iid" => NULL, "category" => $this->category, "itemname" => $this->itemname, "owner" => $this->owner, "quantity" => $quantity, "status" => 'Available'));
	  }
	  
	  $state = "success";
	  return $state;
  }
  
  public function removeitem($quantity = 1, $owner = ""){
      // This method removes items from users inventory
  
      $this->owner = (!empty($owner))?$owner:$this->owner;
      $oldquantity = Private_Item::getoldquantity($this->itemname, $owner);	  
      $newquantity = $oldquantity - $quantity;
	  if(empty($oldquantity) or $newquantity < 0) return FALSE;
	  else{
	     switch($newquantity){
		    case 0:
			   $GLOBALS['adopts']->delete("inventory", "itemname='{$this->itemname}' and owner='{$this->owner}'");
			   break;
			default:
			   $GLOBALS['adopts']->update("inventory", array("quantity" => $newquantity), "itemname ='{$this->itemname}' and owner='{$this->owner}'");
		 }
	     return TRUE;
	  }
  }

  public function sellitem($quantity = 1, $owner = ""){
      // This method sells items from users inventory
      $this->owner = (!empty($owner))?$owner:$this->owner;
      $earn = $this->price*$quantity;      
      $newamount = $GLOBALS['money'] + $earn;
      if($this->removeitem($quantity, $this->owner)){
         $GLOBALS['adopts']->update("users", array("money" => $newamount), "username = '{$this->owner}'");
	     return TRUE;
      }
      else return FALSE; 	 
  }

  public function choosetarget(){
      // This method chooses the adoptable or user to use the item on
      $content = "Now you need to choose an adoptable to use item {$this->itemname}:
      <form name='use' method='post' action='inventory.php?act=useitem&more=process'>
      <input name='act' type='hidden' id='act' value='useitem'>
      <p><select name='aid'>";

      $stmt = $GLOBALS['adopts']->select("owned_adoptables", array(), "owner ='{$this->owner}'"); 
      while($row = $stmt->fetchObject()){
         $content .= "<option value='{$row->aid}'>{$row->name} (the {$row->type})</option>";
      }

      $content .= "</select><input name='itemname' type='hidden' id='itemname' value='{$this->itemname}'>
      <p><input name='validation' type='hidden' id='validation' value='valid'>
      <input type='submit' name='submit' value='Use'></p>
      </form>";
      return $content;
  }
}
?>
__________________

Check out SilvaTales
Reply With Quote