/** hasItem * run a check to see if this user has an item. never call from a view * @params $itemId, $itemParam * @return (bool) $hasItem or throw an exception **/ public function hasItem($itemId, $itemParam, $quantity=null){ $itemId = (int) $itemId; if (!ctype_digit($itemParam)) throw new CircuitException("Invalid serial number"); $ir = new InventoryReader($this->userId); $ir->hasItem($itemId); $rs = $ir->execute(); if(!$rs->isSuccess()) { throw new CircuitExecutionException('Could not retrieve inventory, maybe trying again will help?'); } //do they have the item? if( !$rs->hasItem($itemId)) { $this->messenger->addMessage("The item you specified is no longer available. Please make sure the item is in your inventory and not equipped on your avatar. "); return FALSE; } $user_quantity = $rs->getQuantityByItemId($itemId); $this->set('user_quantity',$user_quantity); //do they have the quantity required? $user_quantity = $rs->getQuantityByItemId($itemId); $this->set('user_quantity',$user_quantity); if($quantity > $user_quantity) { $this->messenger->addMessage("The item you specified is no longer avaialable or is not available in the <i>quantity</i> you specified. "); return FALSE; } return TRUE; }
public function swapItem($from_serial, $to_item_id, $user_id = NULL) { // default the user_id to the current logged in user if nothing was passed in $user_id = empty($user_id) ? SC::get('userdata.user_id') : intval($user_id); // validate that the serial we want to delete belongs to the user $ir = new InventoryReader($user_id); $ir->bySerial($from_serial); $rs = $ir->execute(); if (!$rs->isSuccess()) { return $this->throwError('Unable to load your inventory at this time. Please try again later. Error: ' . $rs->getError(), $rs); } $data = $rs->getItems(); // data is empty so that means the user doesn't own this serial if (empty($data)) { return $this->throwError('The item was not found in your inventory.'); } // validate that the item we want to swap to exists // to_item_id can be either an INT (item_id that we want to grant) or // an array of item_ids that we want to grant and how many of each we want to grant // get the ids of the items we want to grant $item_ids = array(); if (is_array($to_item_id)) { foreach ($to_item_id as $item) { $item_ids[] = $item['item_id']; } } else { $item_ids[] = $to_item_id; } // check to see if the items we want to grant exist $idr = new ItemDefinitionReader(); $idr->addIds($item_ids); if (!$idr->execute('__meta__')) { return $this->throwError('Unable to load item detail, please try again. ' . $idr->getError(), $idr); } $found_ids = $idr->getItemIds(); // if item_ids contains ids that arent in $found_ids then an item doesn't exist $difference = array_diff($item_ids, $found_ids); if (!empty($difference)) { return $this->throwError('Item(s) not found', $difference); } // at this point we have a valid serial that belongs to the user_id and valid items that we want to grant $txn = DaoFactory::create('transactionmanager'); // clear frozen status (if any) as we should allow users to open frozen letters $prop = PropertyReader::instance()->getProperties($from_serial, $user_id); $from_item_frozen_property = NULL; if (isset($prop['frozen']) && FrozenChecker::isFrozenByProperty($prop)) { $from_item_frozen_property = $prop['frozen']; // would be nice if we can attach $txn to $iw here, but it would create a dirty read // problem. we could potentially fail later after clearing the frozen status. we will live // with the unlikely case for now. $iw = new InventoryWriter(IW_APPCODE_FUNC_SWAP_ITEM); $iw->deleteProperty($from_serial, $user_id, INVENTORY_LOCATION_MAIN, 'frozen'); $iwr = $iw->execute(); if (!$iwr->isSuccess()) { $txn->rollback(); return $this->throwError($iwr->getError()); } } //delete item we are swapping out $iw = new InventoryWriter(IW_APPCODE_FUNC_SWAP_ITEM, $txn); $iw->deleteSerializedItem($user_id, $from_serial); //grant item(s) we are swapping in if (is_array($to_item_id)) { foreach ($to_item_id as $item) { $quantity = isset($item['quantity']) ? $item['quantity'] : 1; $iw->grantNewItems($user_id, $item['item_id'], $quantity); } } else { $iw->grantNewItems($user_id, $to_item_id, 1); } $iwr = $iw->execute(FALSE); if (!$iwr->isSuccess()) { $txn->rollback(); return $this->throwError($iwr->getError()); } // get the serials of the granted items $this->granted_items = $iwr->getGeneratedSerials(); if ($from_item_frozen_property) { // transfer the frozen property value to the swapped item // with packages it's not possible that a frozen item will grant multiple items. // all these items should be frozen $iw = new InventoryWriter(IW_APPCODE_FUNC_SWAP_ITEM, $txn); foreach ($this->granted_items as $new_serial) { $iw->setProperty($new_serial, $user_id, INVENTORY_LOCATION_MAIN, 'frozen', $from_item_frozen_property); } $iw->execute(FALSE); if (!$iwr->isSuccess()) { $txn->rollback(); return $this->throwError($iwr->getError()); } } $txn->commit(); // set the new serials associated with the item_ids return TRUE; }
private function getInventory() { $this->page = $this->page ? $this->page : 1; $ir = new InventoryReader($this->userId); $ir->setPage( $this->page ); $ir->setPerpage( $this->resultsPerPage ); // set filter for inventory reader switch( $this->type ) { case 'zomg': $ir->addFilter( new MainInventoryFilter() ); $ir->addFilter( new ZomgFilter() ); break; case 'game': $ir->addFilter( new GameItemsFilter() ); $ir->addFilter( new NotLootFilter() ); break; case 'equip': $ir->addFilter( new UnequippedFilter() ); $ir->addFilter( new EquippablesFilter() ); break; case 'housing': $ir->addFilter( new HousingItemsFilter() ); $ir->addFilter( new UnequippedFilter() ); break; case 'special': $ir->addFilter( new SpecialsFilter() ); $ir->addFilter( new UnequippedFilter() ); break; default: $ir->addFilter( new MainInventoryFilter() ); $ir->addFilter( new UnequippedFilter() ); break; } $ir->retrieveProperties(); $rs = $ir->execute(); //error handling if( !$rs->isSuccess()) { throw new CircuitExecutionException($rs->whyYouBreak(), $rs->getDebug()); } //format data $items = $rs->getItems(); $item_ids = $rs->getItemIds(); $items_total = $rs->getTotalItems(); $serials = $rs->getSerials(); if ( $this->type == 'zomg'&& count($items) < $this->resultsPerPage ) { $ir = new InventoryReader($this->userId); $ir->addFilter( new LootFilter() ); $ir->retrieveProperties(); $rs = $ir->execute(); if( !$rs->isSuccess()) { throw new CircuitExecutionException($rs->whyYouBreak(), $rs->getDebug()); } $items = array_merge($items,$rs->getItems()); $item_ids = array_merge($item_ids,$rs->getItemIds()); $items_total += $rs->getTotalItems(); $serials = array_merge($serials,$rs->getSerials()); } // save stuff for later. $this->set('inventory', $items); $this->set('serials', $serials); $this->set('page', $this->page); $this->set('totalPages', ceil($items_total / $this->resultsPerPage)); $this->set('resultsPerPage', $this->resultsPerPage); //return return $item_ids; }