Unable to Sell Items

Forum
Last Post
Threads / Messages

Constellraetion

Member
Member
Joined
May 6, 2022
Messages
20
Points
3
Mysidian Dollar
208
back again with another question/problem! I can't sell items, every time i try to i get an error message that tells me i'm trying to sell more items than i own, even if i own more than one. I'm not sure how to fix this.
edit: here's the code for owneditem.php
namespace Model\DomainModel;
use Resource\Core\Model;
use Resource\Core\Registry;
use Resource\Native\MysString;
class OwnedItem extends Item{
// The OwnedItem class, which defines functionalities for items that belong to specific users
const IDKEY = "iid";
protected $iid;
protected $item;
protected $owner;
protected $quantity;
protected $status;

public function __construct($iteminfo, $itemowner = "", $dto = NULL){
// the item is an owned item in user inventory, so retrieve database info to assign properties
$mysidia = Registry::get("mysidia");
if($iteminfo instanceof MysString) $iteminfo = $iteminfo->getValue();
if(!$dto){
$fetchmode = $itemowner ? "item" : "iid";
$whereclause = ($fetchmode == "iid") ? "{$fetchmode} = :iteminfo" : "{$fetchmode} = :iteminfo AND owner = '{$itemowner}'";
$dto = $mysidia->db->join("items", "items.id = inventory.item")
->select("inventory", [], $whereclause, ["iteminfo" => $iteminfo])->fetchObject();
if(!is_object($dto)){
if($fetchmode == "iid") return;
$this->iid = 0;
$this->quantity = 0;
parent::__construct($iteminfo);
return;
}
}
$this->createFromDTO($dto);
}

public function getInventoryID(){
return $this->iid;
}

public function getItemID(){
return $this->id;
}

public function getItem($fetchMode = ""){
if($fetchMode == Model::MODEL) return new Item($this->id);
else return $this->id;
}

public function inInventory(){
return ($this->iid > 0);
}

public function getOwner($fetchMode = ""){
if($fetchMode == Model::MODEL) return new Member($this->owner);
else return $this->owner;
}

public function getOwnerName(){
return $this->getOwner(Model::MODEL)->getUsername();
}

public function getQuantity(){
return $this->quantity;
}

public function getStatus(){
return $this->status;
}

public function hasItem(){
// This method checks if the item exists in inventory or not, not to be confused with parent class' getitem() class.
$mysidia = Registry::get("mysidia");
$stmt = $mysidia->db->select("inventory", [], "item ='{$this->item}' and owner ='{$this->owner}'");
return $stmt->fetchObject();
}

public function isOverCap($quantity){
return ($this->quantity + $quantity > $this->cap);
}

public function isSellable($quantity){
return ($quantity > 0 && $this->price > 0 && $quantity <= $this->quantity);
}

public function getCost($quantity = 0, $discount = 0){
// This method returns the cost of items.
return $this->price * $quantity * (1 - $discount);
}

public function apply($adopt = "", $user = ""){
if(is_numeric($adopt)) $target = new OwnedAdoptable($adopt);
if(!empty($user)) $target = new Member($user);
if(!$target) throw new ItemException("Cannot apply item to invalid target.");

// Now we decide which function to call...
$itemFunction = new ItemFunction($this->function);
$message = $itemFunction->apply($this, $target);
if($this->consumable) $this->remove();
return $message;
}
public function append(){
return $this->add(1);
}

public function add($quantity = 1, $owner = NULL){
$mysidia = Registry::get("mysidia");
$this->owner = empty($owner) ? $this->owner : $owner;
$this->quantity += $quantity;
if($this->iid) $mysidia->db->update("inventory", ["quantity" => $this->quantity], "iid = '{$this->iid}'");
else{
$mysidia->db->insert("inventory", ["iid" => NULL, "item" => $this->id, "owner" => $this->owner, "quantity" => $this->quantity, "status" => "Available"]);
$this->iid = $mysidia->db->lastInsertId();
}
return $this->iid;
}
public function sell($quantity = 1, $owner = NULL){
// This method sells items from user inventory
$this->owner = empty($owner) ? $this->owner : $owner;
if($this->remove($quantity)){
$owner = $this->getOwner(Model::MODEL);
$profit = $this->getCost($quantity, 0.5);
$owner->changeMoney($profit);
return TRUE;
}
else return FALSE;
}

public function toss(){
$this->remove($this->quantity);
return TRUE;
}

public function remove($quantity = 1, $owner = NULL){
// This method removes items from user inventory

$mysidia = Registry::get("mysidia");
$this->owner = empty($owner) ? $this->owner : $owner;
$this->quantity -= $quantity;
if(!$quantity || $this->quantity < 0) return FALSE;
else{
switch($this->quantity){
case 0:
$mysidia->db->delete("inventory", "iid = '{$this->iid}'");
break;
default:
$mysidia->db->update("inventory", ["quantity" => $this->quantity], "iid = '{$this->iid}'");
}
return TRUE;
}
}
}
and this is the error i get when I try to sell an item
Screen Shot 2022-05-10 at 12.52.53 AM.png
 
Last edited:

Hall of Famer

Administrator
Staff member
Administrator
Joined
Dec 15, 2008
Messages
4,535
Points
48
Location
United States
Mysidian Dollar
213,993
Can you try to test one more thing? Give yourself 2 such items, and see if you can sell 1? Or if not at all? Also there are certain items that may not be sellable(ie. Key items), though in this case the error message aint very helpful. Also if possible, upload the screenshot right before you are selling the item, not the error message.
 

Constellraetion

Member
Member
Joined
May 6, 2022
Messages
20
Points
3
Mysidian Dollar
208
that's what i first thought of so i gave myself two copies but it still gives me the same error. the item is set to 'Valuable'
Screen Shot 2022-05-10 at 1.40.11 PM.png
 

Hall of Famer

Administrator
Staff member
Administrator
Joined
Dec 15, 2008
Messages
4,535
Points
48
Location
United States
Mysidian Dollar
213,993
This is the code that determines if an item is sellable:

PHP:
    public function isSellable($quantity){
        return ($quantity > 0 && $this->price > 0 && $quantity <= $this->quantity);
    }

If I may ask, did you happen to specify the price of the item? If the price is 0, you wont be able to sell it.
 

Constellraetion

Member
Member
Joined
May 6, 2022
Messages
20
Points
3
Mysidian Dollar
208
okay that solved one problem! thank you, i can actually sell the items now. But the amount of the item in my inventory isn't decreasing (i tried setting it to usable but that didn't change anything), and additionally it looks like i can only sell the item and get money back once? like if I try and sell it again i don't get more money. Also the amount of money that is being returned after selling it is only half of its value when i'd like it to be 100% of said value.
edit: additionally the toss function removes all of an item from your inventory instead of just one?
 
Last edited:

Hall of Famer

Administrator
Staff member
Administrator
Joined
Dec 15, 2008
Messages
4,535
Points
48
Location
United States
Mysidian Dollar
213,993
okay that solved one problem! thank you, i can actually sell the items now. But the amount of the item in my inventory isn't decreasing (i tried setting it to usable but that didn't change anything), and additionally it looks like i can only sell the item and get money back once? like if I try and sell it again i don't get more money. Also the amount of money that is being returned after selling it is only half of its value when i'd like it to be 100% of said value.
edit: additionally the toss function removes all of an item from your inventory instead of just one?
The quantity of items is supposed to decrease, if it doesnt then it is a bug. I tested this on my demo site and it worked fine, the item is deleted if you sell as many as you have, otherwise the quantity should decrease. Can you provide more details on what happens when you try to sell the item? You just keep the item despite it is already sold?

Also the fact that half of the value is returned from sale is the expected behavior(not a bug, I designed it specifically to work this way), though you can modify the script to change it to full amount or a different factor. The toss function will remove all items of the same type from your inventory.
 

Constellraetion

Member
Member
Joined
May 6, 2022
Messages
20
Points
3
Mysidian Dollar
208
when i try to sell the item, after the very first instance of it returning money, i no longer get any additional money from selling items (I tested this with other items too but none of them give me additional money either). the item quantity doesn't decrease when I sell an item no matter what amount i try to sell.

also where can I find where it changes the value to half? so I can make sure that, once this bit with selling them is fixed, people are getting the full value they're supposed to when they sell items?
nevermind! finally got it working, thanks!
 
Last edited:

Similar threads

Users who are viewing this thread

  • Forum Contains New Posts
  • Forum Contains No New Posts

Forum statistics

Threads
4,187
Messages
32,473
Members
1,553
Latest member
Polymori
BETA

Latest Threads

Top