コード例 #1
0
 /**
  * Calculates the free space of a character. If the item can stack
  * to another item it will be allowed to loot.
  * @todo set this place somewhere else. E.g. bag.
  *
  * @param int $character_id the ID of the current character
  * @param int $item_id the ID of the lootable item
  * @param boolean $returnBagIndex set true to return an array with a `bag_id` and `index`
  * @return array|boolean if there is not space left it returns a false. @see $returnBagIndex for array
  */
 function hasFreeSpace($character_id = null, $item_id = null, $returnBagIndex = false)
 {
     App::import('Model', 'Bag');
     $Bag = new Bag();
     $Bag->unbindModelAll();
     $Bag->bindModel(array('belongsTo' => array('Item')));
     $Bag->Item->unbindModelAll();
     $Bag->Item->bindModel(array('hasAndBelongsToMany' => array('Stat')));
     $Bag->recursive = 2;
     $bags = $Bag->find('all', array('conditions' => array('Bag.character_id' => $character_id)));
     $total_slots = 0;
     $total_slots_filled = 0;
     $bag_ids = array();
     $firstBagIndex = array();
     $bag_counts = array();
     $bag_indexes = array();
     foreach ($bags as $bag) {
         $bag_ids[] = $bag['Bag']['id'];
         $total_slots += $bag['Item']['StatNamed']['slots'];
         $bag_counts[$bag['Bag']['id']] = $bag['Item']['StatNamed']['slots'];
     }
     App::import('Model', 'Inventory');
     $Inventory = new Inventory();
     $inventories = $Inventory->getBag($bag_ids);
     foreach ($inventories as $inventory) {
         if ($inventory['Item']['id'] == $item_id && $inventory['Item']['stackable'] > $inventory[0]['count']) {
             if ($returnBagIndex == true) {
                 return array('bag_id' => $inventory['Inventory']['bag_id'], 'index' => $inventory['Inventory']['index']);
             } else {
                 return true;
             }
         }
         $bag_indexes[$inventory['Inventory']['bag_id']][$inventory['Inventory']['index']] = 1;
         $total_slots_filled++;
     }
     if ($total_slots_filled < $total_slots) {
         if ($returnBagIndex == true) {
             foreach ($bag_counts as $bag_id => $count) {
                 if ($count > 0) {
                     asort($bag_indexes);
                     $last_index = 0;
                     for ($i = 1; $i <= $bag_counts[$bag_id]; $i++) {
                         if (!isset($bag_indexes[$bag_id][$i])) {
                             $firstBagIndex = array('bag_id' => $bag_id, 'index' => $i);
                             break 2;
                         }
                     }
                 }
             }
             $firstBagIndex['bag_id'] = !isset($firstBagIndex['bag_id']) || $firstBagIndex['bag_id'] == 0 ? $bag_ids[0] : $firstBagIndex['bag_id'];
             $firstBagIndex['index'] = !isset($firstBagIndex['index']) || $firstBagIndex['index'] == 0 ? 1 : $firstBagIndex['index'];
             return $firstBagIndex;
         } else {
             return true;
         }
     }
     return false;
 }