/** * Send x copies of the registered item to a user. * * @param integer|Model_User $user * @param integer $amount * @param string $location * * @throws Item_Exception */ public function to_user($user, $origin = "app", $amount = 1, $location = 'inventory') { if (!Valid::digit($amount)) { throw new Item_Exception('The supplied amount should be a number.'); } if (Valid::digit($user)) { $user = ORM::factory('User', $user); } elseif (!is_a($user, 'Model_User')) { throw new Item_Exception('The supplied user does not come from a model.'); } if (!$user->loaded()) { throw new Item_Exception('The supplied user does not exist.'); } else { $user_item = ORM::factory('User_Item')->where('user_id', '=', $user->id)->where('item_id', '=', $this->_item->id)->where('location', '=', $location)->find(); $action = $amount > 0 ? '+' : '-'; if ($user_item->loaded()) { // update item amount $user_item->amount($action, $amount); } elseif ($action == '+') { $id = $this->_item->id; // create new copy $user_item = ORM::factory('User_Item')->values(array('user_id' => $user->id, 'item_id' => $id, 'location' => $location, 'amount' => $amount))->save(); } return Journal::log('item.in.' . $origin, 'item', 'Player received :amount :item_name @ :origin', array(':amount' => $amount, ':item_name' => $user_item->item->name($amount, FALSE), ':origin' => str_replace('.', ' ', $origin))); } }
/** * Transfer the initialised item to a different user. * * Returns false if you're trying to transfer a higher amount of this item than the owner already has, * if successfull it will return a user item instance of where the item copies transfered to. * * @param Model_User $user A user model instance of the new owner * @param Integer $amount The amount of copies you want to transfer * * @throws Item_Exception When trying to transfer an untransferable item * @return boolean|Model_User_Item */ public function transfer(Model_User $user, $amount = 1) { if ($this->item->transferable == FALSE) { throw new Item_Exception('":item" is bound to your account only.', array(':item' => $this->item->name)); } else { $this->_relocate($user->id, 'inventory', $amount); return Journal::log('transfer' . $this->item->id, 'items.gift', ':item_name transferred to :other_user', array(':item_name' => $this->item->name, ':other_user' => $user->username)); } }
public function action_buy() { $shop = ORM::factory('User_Shop', $this->request->param('id')); // if no shop's found redirect to previous page if (!$shop->loaded()) { $this->redirect($this->request->referrer()); } if ($this->request->method() == HTTP_Request::POST) { $item_id = $this->request->post('item_id'); $item = ORM::factory('User_Item', $item_id); if (!$item->loaded() or $item->location != 'shop') { Hint::error('This item is not in stock'); } elseif ($this->user->id == $item->user->id) { Hint::error('You cannot buy items from your own shop.'); } elseif ($this->user->get_property('points') < $item->parameter) { Hint::error(__('You don\'t have enough :currency to buy a ":item_name"', array(':item_name' => $item->item->name))); } else { $this->user->set_property('points', $this->user->get_property('points') - $item->parameter); $this->user->save(); // log this action $log = Journal::log('user_shop.' . $shop->id, 'item', ':username bought 1 :item_name for :price', array('item_name' => $item->item->name, 'username' => $this->user->username, 'price' => $item->parameter)); $shop->till += $item->parameter; $shop->save(); $item->transfer($this->user); $log->notify($shop->user, 'user_shop.buy'); Hint::success(__('You\'ve successfully bought :item_name from :shop_owner for :price.', array(':shop_owner' => $shop->user->username, ':item_name' => $item->item->name('1'), ':price' => $item->parameter))); } } $this->redirect(Route::get('item.user_shop.view')->uri(array('id' => $shop->id))); }
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()); }