Beispiel #1
0
 /**
  * Command for current user to purchase a quantity of a specific item
  *
  * @param quantity int The quantity of the item to purchase
  * @param item string The identity of the item to purchase
  * @return Array
  */
 public function buy()
 {
     $in_quantity = in('quantity');
     $in_item = in('item');
     $gold = get_gold($this->sessionData['char_id']);
     $current_item_cost = 0;
     $no_funny_business = false;
     // Pull the item info from the database
     $item_costs = $this->itemForSaleCosts();
     $item = getItemByID(item_id_from_display_name($in_item));
     $quantity = whichever(positive_int($in_quantity), 1);
     $item_text = null;
     if ($item instanceof Item) {
         $item_text = $quantity > 1 ? $item->getPluralName() : $item->getName();
         $purchaseOrder = new PurchaseOrder();
         // Determine the quantity from input or as a fallback, default of 1.
         $purchaseOrder->quantity = $quantity;
         $purchaseOrder->item = $item;
         $potential_cost = isset($item_costs[$purchaseOrder->item->identity()]['item_cost']) ? $item_costs[$purchaseOrder->item->identity()]['item_cost'] : null;
         $current_item_cost = first_value($potential_cost, 0);
         $current_item_cost = $current_item_cost * $purchaseOrder->quantity;
         if (!$this->sessionData['char_id'] || !$purchaseOrder->item || $purchaseOrder->quantity < 1) {
             $no_funny_business = true;
         } else {
             if ($gold >= $current_item_cost) {
                 // Has enough gold.
                 try {
                     add_item($this->sessionData['char_id'], $purchaseOrder->item->identity(), $purchaseOrder->quantity);
                     subtract_gold($this->sessionData['char_id'], $current_item_cost);
                 } catch (\Exception $e) {
                     $invalid_item = $e->getMessage();
                     error_log('Invalid Item attempted :' . $invalid_item);
                     $no_funny_business = true;
                 }
             }
         }
     } else {
         $no_funny_business = true;
     }
     $parts = array('current_item_cost' => $current_item_cost, 'quantity' => $quantity, 'item_text' => $item_text, 'no_funny_business' => $no_funny_business, 'view_part' => 'buy');
     return $this->render($parts);
 }
 /**
  * Get an input display name and turn it into the internal name for use in the actual script.
  */
 private function itemIdentityFromDisplayName($item_display_name)
 {
     return item_info(item_id_from_display_name($item_display_name), 'item_internal_name');
 }
Beispiel #3
0
function item_identity_from_display_name($item_display_name)
{
    return item_info(item_id_from_display_name($item_display_name), 'item_internal_name');
}