예제 #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()
 {
     $request = RequestWrapper::$request;
     $in_quantity = $request->get('quantity');
     $in_item = $request->get('item');
     $player = Player::findPlayable($this->getAccountId());
     $gold = $player ? $player->gold : null;
     $current_item_cost = 0;
     $no_funny_business = false;
     $no_such_item = false;
     $item = Item::findByIdentity($in_item);
     $quantity = max(Filter::toNonNegativeInt($in_quantity), self::DEFAULT_QUANTITY);
     $item_text = null;
     $valid = false;
     if (!$item instanceof Item) {
         $no_such_item = true;
     } else {
         $item_text = $quantity > 1 ? $item->getPluralName() : $item->getName();
         $purchase_order = new PurchaseOrder();
         // Determine the quantity from input or as a fallback, default of 1.
         $purchase_order->quantity = $quantity;
         $purchase_order->item = $item;
         $current_item_cost = $this->calculatePrice($purchase_order);
         if (!$player) {
             $no_funny_business = true;
         } elseif (!$purchase_order->item || $purchase_order->quantity < 1) {
             $no_such_item = true;
         } else {
             if ($gold >= $current_item_cost) {
                 // Has enough gold.
                 try {
                     $inventory = new Inventory($player);
                     $inventory->add($purchase_order->item->identity(), $purchase_order->quantity);
                     $player->setGold($player->gold - $current_item_cost);
                     $player->save();
                     $valid = true;
                 } catch (\Exception $e) {
                     $invalid_item = $e->getMessage();
                     error_log('Invalid Item attempted :' . $invalid_item);
                     $no_funny_business = true;
                 }
             }
         }
     }
     $parts = array('current_item_cost' => $current_item_cost, 'quantity' => $quantity, 'item_text' => $item_text, 'no_funny_business' => $no_funny_business, 'no_such_item' => $no_such_item, 'valid' => $valid, 'view_part' => 'buy');
     return $this->render($parts);
 }
예제 #2
0
 /**
  * Handle Standard Abstract Npcs
  *
  * @param String $victim
  * @param Player $player
  * @param Array $npcs
  * @return array [$npc_template, $combat_data]
  */
 private function attackAbstractNpc($victim, Player $player, $npcs)
 {
     $npc_stats = $npcs[$victim];
     // Pull an npcs individual stats with generic fallbacks.
     $npco = new Npc($npc_stats);
     // Construct the npc object.
     $display_name = isset($npc_stats['name']) ? $npc_stats['name'] : ucfirst($victim);
     $status_effect = isset($npc_stats['status']) ? $npc_stats['status'] : null;
     $reward_item = isset($npc_stats['item']) && $npc_stats['item'] ? $npc_stats['item'] : null;
     $is_quick = (bool) ($npco->getSpeed() > $player->getSpeed());
     // Beyond basic speed and they see you coming, so show that message.
     $is_weaker = $npco->getStrength() * 3 < $player->getStrength();
     // Npc much weaker?
     $is_stronger = $npco->getStrength() > $player->getStrength() * 3;
     // Npc More than twice as strong?
     $image = isset($npc_stats['img']) ? $npc_stats['img'] : null;
     // Assume defeat...
     $victory = false;
     $received_gold = null;
     $received_items = null;
     $added_bounty = '';
     $is_rewarded = null;
     // Gets items or gold.
     $statuses = null;
     $status_classes = null;
     $image_path = null;
     // If the image exists, set the path to it for use on the page.
     if ($image && file_exists(SERVER_ROOT . 'www/images/characters/' . $image)) {
         $image_path = IMAGE_ROOT . 'characters/' . $image;
     }
     // ******* FIGHT Logic ***********
     $npc_damage = $npco->damage();
     $survive_fight = $player->harm($npc_damage);
     $kill_npc = $npco->getHealth() < $player->damage();
     if ($survive_fight > 0) {
         // The ninja survived, they get any gold the npc has.
         $received_gold = $this->calcReceivedGold($npco, (bool) $reward_item);
         $player->setGold($player->gold + $received_gold);
         $received_items = array();
         if ($kill_npc) {
             $victory = true;
             // Victory occurred, reward the poor sap.
             if ($npco->inventory()) {
                 $inventory = new Inventory($player);
                 foreach (array_keys($npco->inventory()) as $l_item) {
                     $item = Item::findByIdentity($l_item);
                     $received_items[] = $item->getName();
                     $inventory->add($item->identity(), 1);
                 }
             }
             // Add bounty where applicable for npcs.
             if ($npco->bountyMod() > 0 && $player->level > self::MIN_LEVEL_FOR_BOUNTY && $player->level <= self::MAX_LEVEL_FOR_BOUNTY) {
                 $added_bounty = Combat::runBountyExchange($player, $npco, $npco->bountyMod());
             }
         }
         $is_rewarded = (bool) $received_gold || (bool) count($received_items);
         if (isset($npc_stats['status']) && null !== $npc_stats['status']) {
             $player->addStatus($npc_stats['status']);
             // Get the statuses and status classes for display.
             $statuses = implode(', ', Player::getStatusList());
             $status_classes = implode(' ', Player::getStatusList());
         }
     }
     $player->save();
     return ['npc.abstract.tpl', ['victim' => $victim, 'display_name' => $display_name, 'attack_damage' => $npc_damage, 'status_effect' => $status_effect, 'display_statuses' => $statuses, 'display_statuses_classes' => $status_classes, 'received_gold' => $received_gold, 'received_display_items' => $received_items, 'is_rewarded' => $is_rewarded, 'victory' => $victory, 'survive_fight' => $survive_fight, 'kill_npc' => $kill_npc, 'image_path' => $image_path, 'npc_stats' => $npc_stats, 'is_quick' => $is_quick, 'added_bounty' => $added_bounty, 'is_villager' => $npco->hasTrait('villager'), 'race' => $npco->race(), 'is_weaker' => $is_weaker, 'is_stronger' => $is_stronger]];
 }
예제 #3
0
 public function testItemPluralNameExists()
 {
     $caltrop = Item::findByIdentity('caltrops');
     $shuriken = Item::findByIdentity('shuriken');
     $this->assertInstanceOf('NinjaWars\\core\\data\\Item', $caltrop);
     $this->assertInstanceOf('NinjaWars\\core\\data\\Item', $shuriken);
     $this->assertEquals('Shuriken', $shuriken->getName());
     $this->assertEquals('Shuriken', $shuriken->getPluralName());
     $this->assertEquals('Caltrops', $caltrop->getName());
     $this->assertEquals('Caltrops', $caltrop->getPluralName());
 }
예제 #4
0
 /**
  * Helper to find an item by either id or identity
  *
  * @return Item
  */
 private function findItem($token)
 {
     if ($token == (int) $token && is_numeric($token) && $token) {
         $item = Item::find($token);
     } elseif (is_string($token) && $token) {
         $item = Item::findByIdentity($token);
     } else {
         throw new \InvalidArgumentException('Invalid item identity requested.');
     }
     return $item;
 }