/** * Moving an Item from one spot to the other in a Bag */ function game_change($from_bag_id = null, $from_bag_index = null, $to_bag_id = null, $to_bag_index = null) { $this->render(false); App::import('Model', 'Inventory'); $Inventory = new Inventory(); $Inventory->unbindModelAll(); $fromInventory = $Inventory->find('first', array('conditions' => array('Inventory.character_id' => $this->characterInfo['id'], 'Inventory.bag_id' => $from_bag_id, 'Inventory.index' => $from_bag_index))); if (!empty($fromInventory)) { // KLijken of het vakje leeg is waar we heen gaan $toInventory = $Inventory->find('first', array('conditions' => array('Inventory.character_id' => $this->characterInfo['id'], 'Inventory.bag_id' => $to_bag_id, 'Inventory.index' => $to_bag_index))); if (!empty($toInventory)) { // Niks aan de hand, wisselen // to => from $oldIds = $Inventory->find('list', array('conditions' => array('Inventory.character_id' => $this->characterInfo['id'], 'Inventory.bag_id' => $from_bag_id, 'Inventory.index' => $from_bag_index))); $Inventory->updateAll(array('Inventory.bag_id' => $fromInventory['Inventory']['bag_id'], 'Inventory.index' => $fromInventory['Inventory']['index']), array('Inventory.bag_id' => $toInventory['Inventory']['bag_id'], 'Inventory.index' => $toInventory['Inventory']['index'])); // from => to $Inventory->updateAll(array('Inventory.bag_id' => $toInventory['Inventory']['bag_id'], 'Inventory.index' => $toInventory['Inventory']['index']), array('Inventory.id' => $oldIds)); } else { // kijken of er in de nieuwe bag slot wel ruimte is $this->Bag->recursive = 2; $toBag = $this->Bag->find('first', array('conditions' => array('Bag.id' => $to_bag_id, 'Bag.character_id' => $this->characterInfo['id']))); if (!empty($toBag)) { // Kijken of de index lager is dan het aantal vrije slots if ($toBag['Item']['StatNamed']['slots'] >= $to_bag_index) { // Mag naar de lege plek $Inventory->updateAll(array('Inventory.bag_id' => $to_bag_id, 'Inventory.index' => $to_bag_index), array('Inventory.bag_id' => $fromInventory['Inventory']['bag_id'], 'Inventory.index' => $fromInventory['Inventory']['index'])); } } } } $this->redirect('/game/bags/view/' . $to_bag_id); exit; }