public function action_buy() { $shop_id = $this->request->param('id'); $shop = ORM::factory('Shop', $shop_id); if (!$shop->loaded()) { Hint::error('You can\'t buy an item from a shop that does not exist.'); } elseif ($shop->status == 'closed') { Hint::error('You\'re trying to buy an item from a closed shop.'); } else { $item_id = $this->request->post('id'); $item = ORM::factory('Shop_Inventory')->where('shop_id', '=', $shop->id)->where('item_id', '=', $item_id)->find(); if (!$item->loaded()) { Hint::error('The item you tried to buy has already been sold.'); } elseif ($item->price > $this->user->get_property('points')) { Hint::error('You don\'t have enough points to buy ' . $item->item->name); } else { // retract the points $this->user->set_property('points', $this->user->get_property('points') - $item->price); $this->user->save(); // send over the item Item::factory($item->item)->to_user($this->user, 'shops.' . $shop_id); // remove from shop if needed if ($shop->stock_type != 'steady') { if ($item->stock - 1 == 0) { $item->delete(); } else { $item->stock -= 1; $item->save(); } } Hint::success('You\'ve successfully bought ' . $item->item->name); } } $this->redirect(Route::get('item.shops.view')->uri(array('id' => $shop_id))); }
/** * Load a user-owned item stack. * * This method will look for a stack of $item_id owned by $user_id. * If the stack does not exist, an empty (uncreated) object of * the correct type will be returned. * * @param int $user_id The user ID. * @param int $item_id The item ID. * @param obkect $db DB connector. * @return object **/ public static function stackFactory($user_id, $item_id, $db) { $basic = new Item($db); $basic = $basic->findOneBy(array('user_id' => $user_id, 'item_type_id' => $item_id)); // This stack exists. Let that crazy factory handle the details. if ($basic != null) { return Item::factory($basic->getUserItemId(), $db); } // The stack does not exist. Prep a new object and give that back. $type = new ItemType($db); $type = $type->findOneByItemTypeId($item_id); if ($type == null) { throw new ArgumentError('Could not find item type that should exist.'); } eval('$item = new ' . $type->getPhpClass() . '($db);'); $item->setUserId($user_id); $item->setItemTypeId($item_id); $item->setQuantity(0); return $item; }
public function perform($item, $param, $data = NULL) { $item = ORM::factory('Item')->where('item.name', '=', $param)->find(); Item::factory($item)->to_user(Auth::instance()->get_user()->id); return 'You\'ve recieved a' . $item->name; }
/** * Overrides the standard grabInventory and performs the factory call. * * This is (by far) the least-effective method in this application. * It loads up all of the Item objects in the efficient manner that * ActiveTable does such things, then completely throws that benefit * to the wind by re-creating the Items one-by-one from scratch. * * I may be able to do something with ActiveTable#setUp() to make * it more efficient, but I really do not think it is worth my time. * This is a template app, so since I'll never seriously run it, I'll * let this little issue of scalability be your problem! Neener-neener! * * Hopefully, paginating any pages showing the whole inventory will * mitigate the problem. But, this is still SLOW (~150 items = 5s, * p3 833mhz w/ 128mb RAM & no APC). * * == Notes on the parms... * These are optional. Specify neither or both. They will put LIMITs * on the recordset, so this can be whittled down to just a slice of * the user's whole inventory. Very good for pagination. * * @param integer $start The beginning of a slice in the recordset. * @param integer $end The end of a slice in the recordset. * @return array A list of *_Item instances. **/ public function grabInventory($start = null, $end = null) { if (($start === null && $end === null) == false && ($start !== null && $end !== null) == false) { throw new ArgumentError('Must specify either no arguments or both arguments.'); } // end problem w/ args. $inventory = new Item($this->db); $inventory = $inventory->findBy(array('user_id' => $this->getUserId(), array('table' => 'item_class', 'column' => 'normal_inventory_display', 'value' => 'Y')), 'ORDER BY item_type.item_name', false, $start, $end); $PROPER_INVENTORY = array(); foreach ($inventory as $item) { $PROPER_INVENTORY[] = Item::factory($item->getUserItemId(), $this->db); } // end inventory loop return $PROPER_INVENTORY; }
draw_errors('System error - item is of an unknown type.'); break; // end default } // end item type switch } // end no errors break; // end use_item // end use_item case 'give_process': $ERRORS = array(); $id = stripinput($_REQUEST['give']['item_id']); $other_user_name = stripinput($_REQUEST['give']['username']); $quantity = stripinput($_REQUEST['give']['quantity']); $item = Item::factory($id, $db); if ($item == null) { $ERRORS[] = 'Invalid item ID specified.'; } else { if ($item->getUserId() != $User->getUserId()) { $ERRORS[] = 'This is not your item.'; } if ($item->getTransferableItem() == 'N') { $ERRORS[] = 'You cannot give this item away.'; } if ($quantity > $item->getQuantity()) { $ERRORS[] = "You do not have {$quantity} of this item."; } if ($item->getQuantity() <= 0) { $item->destroy(); $ERRORS[] = 'You do not have any of this item.';
* write to the Free Software Foundation, Inc., 51 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * * @author Nicholas 'Owl' Evans <*****@*****.**> * @copyright Nicolas Evans, 2009 * @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 * @package Kitto_Kitto_Kitto * @subpackage Items * @version 1.0.0 **/ $ERRORS = array(); $user_item_type_id = stripinput($_REQUEST['id']); if ($user_item_type_id == null) { $ERRORS[] = 'No item ID specified.'; } else { $item = Item::factory($user_item_type_id, $db); if ($item == null) { $ERRORS[] = 'Invalid item ID specified.'; } else { if ($item->getItemClassId() != 4) { $ERRORS[] = 'That is not a recipe!'; } if ($item->getUserId() != $User->getUserId()) { $ERRORS[] = 'This is not your item.'; } if ($item->getQuantity() <= 0) { $item->destroy(); $ERRORS[] = 'You do not have any of this item.'; } } // end valid ID
public function action_complete() { $item = ORM::factory('User_Item', $this->request->param('id')); $action = $this->request->post('action'); $errors = array(); if (!$item->loaded()) { $errors[] = 'You can\'t use a recipe that does not exist'; } elseif ($item->user_id != $this->user->id) { $errors[] = 'You can\'t access another player\'s recipe'; } elseif ($item->location != 'cookbook') { $errors[] = 'The recipe you want to view is not located in your cookbook'; } elseif ($item->item->type->default_command != 'General_Cook') { $errors[] = 'You can\'t use this item as a recipe.'; } elseif ($action == NULL) { $errors[] = 'No action to perform has been specified'; } else { $recipe = ORM::factory('Item_Recipe')->where('item_recipe.name', '=', $item->item->commands[0]['param'])->find(); $coll = $recipe->materials->find_all(); $materials = 0; $db = Database::instance(); $db->begin(); foreach ($coll as $material) { $user_item = Item::factory($material->item)->user_has('inventory'); if ($user_item != FALSE and $user_item->amount >= $material->amount) { $user_item->amount('-', $material->amount); $materials++; } } if ($materials == count($coll)) { Item::factory($recipe->item)->to_user($this->user); $item->amount('-', 1); $db->commit(); $result = 'You\'ve successfully made ' . $recipe->item->name; } else { $db->rollback(); $errors[] = 'You don\'t have all the required ingredients for this recipe.'; } } if ($this->request->is_ajax()) { if (count($errors) > 0) { $return = array('status' => 'error', 'errors' => $errors); } else { $return = array('status' => 'success', 'result' => $result, 'item' => $item->amount); } $this->response->headers('Content-Type', 'application/json'); return $this->response->body(json_encode($return)); } elseif (count($errors) > 0) { Hint::error($errors[0]); $this->redirect(Route::get('item.cookbook')->uri()); } else { Hint::success($result); $this->redirect(Route::get('item.cookbook')->uri()); } }
/** * Overrides the standard grabInventory and performs the factory call. * * This is (by far) the least-effective method in this application. * It loads up all of the Item objects in the efficient manner that * ActiveTable does such things, then completely throws that benefit * to the wind by re-creating the Items one-by-one from scratch. * * I may be able to do something with ActiveTable#setUp() to make * it more efficient, but I really do not think it is worth my time. * This is a template app, so since I'll never seriously run it, I'll * let this little issue of scalability be your problem! Neener-neener! * * Hopefully, paginating any pages showing the whole inventory will * mitigate the problem. But, this is still SLOW (~150 items = 5s, * p3 833mhz w/ 128mb RAM & no APC). * * == Notes on the parms... * These are optional. Specify neither or both. They will put LIMITs * on the recordset, so this can be whittled down to just a slice of * the user's whole inventory. Very good for pagination. * * @param integer $start The beginning of a slice in the recordset. * @param integer $end The end of a slice in the recordset. * @return array A list of *_Item instances. **/ public function grabInventory($start = null, $end = null) { if (($start === null && $end === null) == false && ($start !== null && $end !== null) == false) { throw ArgumentError('Must specify either no arguments or both arguments.'); } // end problem w/ args. $PROPER_INVENTORY = array(); $inventory = $this->grab('inventory', 'ORDER BY user_item_id', false, $start, $end); foreach ($inventory as $item) { $PROPER_INVENTORY[] = Item::factory($item->getUserItemId(), $this->db); } // end inventory loop return $PROPER_INVENTORY; }
public function action_gift() { if (!$this->user->can('Admin_Item_Gift')) { throw HTTP_Exception::factory('403', 'Permission denied to view admin item gift'); } $this->view = NULL; // gift the item $item = Item::factory($this->request->post('id')); $user = ORM::factory('User')->where('username', '=', $this->request->post('username'))->find(); try { $log = $item->to_user($user, 'admin.' . $this->user->username, $this->request->post('amount')); // notify the user $log->notify($user, 'You received :item_name!', array(':item_name' => $item->item()->name)); $list = array('action' => 'success'); } catch (Item_Exception $e) { $list = array('action' => 'error', 'errors' => (array) $e->getMessage()); } // return response $this->response->headers('Content-Type', 'application/json'); $this->response->body(json_encode($list)); }