Esempio n. 1
0
 public function action_consume()
 {
     $item = ORM::factory('User_Item', $this->request->param('id'));
     $action = $this->request->post('action');
     $errors = array();
     if (!$item->loaded()) {
         Hint::error('You can\'t use an item that does not exist');
     } elseif ($item->user_id != $this->user->id) {
         Hint::error('You can\'t access another player\'s item');
     } elseif ($item->location != 'inventory') {
         Hint::error('The item you want to view is not located in your inventory');
     } elseif ($action == NULL) {
         Hint::error('No action to perform has been specified');
     } else {
         $def_cmd = Item_Command::factory($item->item->type->default_command);
         if (Valid::digit($action)) {
             // we'll want to perform an action on a pet
             $pet = ORM::factory('User_Pet', $action);
             if (!$pet->loaded()) {
                 Hint::error('No existing pet has been specified');
             } elseif ($pet->user_id != $this->user->id) {
                 Hint::error('You can\'t let a pet comsume this item if it\'s not yours');
             } elseif ($def_cmd->pets_required() == FALSE) {
                 Hint::error('can\'t perform this item action on a pet');
             } else {
                 $commands = $item->item->commands;
                 $results = array();
                 $db = Database::instance();
                 $db->begin();
                 $error = FALSE;
                 foreach ($commands as $command) {
                     $cmd = Item_Command::factory($command['name']);
                     $res = $cmd->perform($item, $command['param'], $pet);
                     if ($res == FALSE) {
                         // the command couldn't be performed, spit out error, rollback changes and break the loop
                         Hint::error(__(':item_name could not be used on :pet_name', array(':item_name' => $item->item->name, ':pet_name' => $pet->name)));
                         $error = TRUE;
                         $db->rollback();
                         break;
                     } else {
                         $results[] = $res;
                     }
                 }
                 if ($error == FALSE) {
                     $log = Journal::log('consume', 'item', ':item_name consumed', array(':item_name' => $item->item->name));
                     $log->notify('consume' . $item->item_id, 'item', ':item_name consumed');
                     if ($def_cmd->delete_after_consume == TRUE) {
                         $item->amount('-', 1);
                     }
                     $db->commit();
                 }
             }
         } else {
             $results = array();
             switch ($action) {
                 case 'consume':
                     $commands = $item->item->commands;
                     $results = array();
                     $db = Database::instance();
                     $db->begin();
                     $error = FALSE;
                     foreach ($commands as $command) {
                         $cmd = Item_Command::factory($command['name']);
                         $res = $cmd->perform($item, $command['param']);
                         if ($res == FALSE) {
                             // the command couldn't be performed, spit out error, rollback changes and break the loop
                             Hint::error(__(':item_name could not be used', array(':item_name' => $item->name)));
                             $db->rollback();
                             $error = TRUE;
                             break;
                         } else {
                             $results[] = $res;
                         }
                     }
                     if ($error = FALSE) {
                         Journal::log('consume' . $item->item_id, 'item', ':item_name consumed', array(':item_name' => $item->item->name));
                         if ($def_cmd->delete_after_consume == TRUE) {
                             $item->amount('-', 1);
                         }
                         $db->commit();
                     }
                     break;
                 case 'remove':
                     // takes an amount
                     $amount = $this->request->post('amount');
                     if ($amount == NULL) {
                         $amount = 1;
                     }
                     if (!Valid::digit($amount)) {
                         Hint::error('The amount you submitted isn\'t a number.');
                     } elseif ($amount <= 0 or $amount > $item->amount) {
                         Hint::error('You only have ' . $item->name() . ', not ' . $amount);
                     } else {
                         if ($amount > 1) {
                             $name = Inflector::plural($item->name(), $amount);
                             $verb = 'were';
                         } else {
                             $name = $item->item->name(1);
                             $verb = 'was';
                         }
                         $item->amount('-', $amount);
                         Journal::log('remove.' . $item->item_id, 'item', ':item_name removed', array(':item_name' => $name));
                         $results = __(':item :verb deleted successfully', array(':verb' => $verb, ':item' => $name));
                     }
                     break;
                 case 'gift':
                     // takes a username
                     $username = $this->request->post('username');
                     if ($this->user->username == $username) {
                         Hint::error('You can\'t send a gift to yourself');
                     } else {
                         $user = ORM::factory('User')->where('username', '=', $username)->find();
                         if ($user->loaded()) {
                             $log = $item->transfer($user);
                             $log->notify($user, 'items.gift', array(':item_name' => $item->item->name(1)));
                             $results = __('You\'ve successfully sent :item to :username', array(':item' => $item->item->name, ':username' => $user->username));
                         } else {
                             Hint::error(__('Couldn\'t find a user named ":username"', array(':username' => $username)));
                         }
                     }
                     break;
                 default:
                     // Moving items can take an amount
                     if (substr($action, 0, 5) == 'move_') {
                         $location = substr($action, 5);
                         $cmd = Item_Command::factory('Move_' . ucfirst($location));
                         $amount = $this->request->post('amount');
                         if ($amount == NULL) {
                             $amount = 1;
                         }
                         if (!Valid::digit($amount)) {
                             Hint::error('The amount you submitted isn\'t a number.');
                         } elseif ($amount <= 0 or $amount > $item->amount) {
                             Hint::error('You only have ' . $item->name() . ', not ' . $amount);
                         } else {
                             $results = $cmd->perform($item, $amount);
                         }
                     } else {
                         Hint::error('The action you want to perform with this item does not exist');
                     }
                     break;
             }
         }
     }
     $show = Kohana::$config->load('items.inventory.consume_show_results');
     $output = array();
     if (!is_array($results)) {
         $output[] = $results;
     } elseif ($show == 'first') {
         $output[] = $results[0];
     } elseif (!empty($results)) {
         foreach ($results as $result) {
             $output[] = $result;
         }
     }
     if ($this->request->is_ajax()) {
         $return = array();
         $return = Hint::dump();
         Hint::ajax_dump();
         if ($return['status'] == 'success') {
             $amount = $item->loaded() ? $item->name() : 0;
             $return = array_merge($return, array('result' => $output, 'new_amount' => $amount));
         }
         $this->response->headers('Content-Type', 'application/json');
         return $this->response->body(json_encode($return));
     }
     if (count($output) > 0) {
         foreach ($output as $result) {
             Hint::success($result);
         }
     }
     $this->redirect(Route::get('item.inventory')->uri());
 }