public function onSlap(Session $session)
 {
     $data = $this->game->getPlayerData($session);
     if ($data === null) {
         return;
     }
     if ($this->column === Settings::KIT_ARROWS and $data->getBowLevel() === 0) {
         $session->tell("You don't have a bow. Why are you buying arrows? Upgrade your bows first.");
         return;
     }
     $kit = $data->getEditingKit();
     $curLevel = $kit->{$this->column};
     if ($curLevel === $this->max) {
         $session->tell("You already have the maximum level of {$this->column}!");
         return;
     }
     $buy = $data->isGoingToBuy;
     $oldInfo = Settings::kitpvp_getKitUpgradeInfo($this->column, $curLevel);
     $newInfo = Settings::kitpvp_getKitUpgradeInfo($this->column, $curLevel + 1);
     if (!is_array($buy) or $buy["column"] !== $this->column or microtime(true) - $buy["timestamp"] > 5) {
         $price = $newInfo->getPrice();
         $balance = $session->getCoins();
         if ($balance < $price) {
             $session->tell("You need at least %g more coins to buy this kit!", $price - $balance);
             $data->isGoingToBuy = null;
             return;
         }
         if ($newInfo->sendCantPurchaseMessage($session)) {
             $data->isGoingToBuy = null;
             return;
         }
         $data->isGoingToBuy = ["column" => $this->column, "timestamp" => microtime(true)];
         $session->tell("Upgrade {$this->column} to level %d (%g coins required)", $curLevel + 1, $newInfo->getPrice());
         $oldName = $oldInfo->itemsToString();
         $session->tell("You will be equipped with %s%s after you purchase this upgrade.", $newInfo->itemsToString(), $oldName === false ? "" : " instead of {$oldName}");
         $session->tell("Click me again within 5 seconds to purchase this upgrade.");
     } else {
         $data->isGoingToBuy = null;
         if ($newInfo->sendCantPurchaseMessage($session)) {
             return;
         }
         $price = $newInfo->getPrice();
         $balance = $session->getCoins();
         if ($balance < $price) {
             $session->tell("You need %g more coins to buy this kit!", $price - $balance);
             return;
         }
         $session->setCoins($balance - $price);
         $kit->{$this->column} = $curLevel + 1;
         $kit->updated = true;
         $models = $this->game->getModels();
         if (isset($models[$kit->kitid])) {
             $model = $models[$kit->kitid];
             $model->despawnFrom($session->getPlayer());
             $model->spawnTo($session->getPlayer());
         }
         $session->tell("Your {$this->column} has been upgraded to level %d!", $curLevel + 1);
         $session->tell("Rejoin KitPvP or suicide to activate the kit change!");
     }
 }
Esempio n. 2
0
 protected function onSlap(Session $session)
 {
     if ($session->inSession($this->game)) {
         $data = $this->game->getPlayerData($session);
         $curEditing = $data->getEditingKitId();
         if ($curEditing !== $this->kid) {
             // choose to edit this
             $kit = $data->getKitById($this->kid);
             if ($kit === null) {
                 // create new kit
                 if (count($data->getKits()) >= Settings::kitpvp_maxKits($session)) {
                     // cannot create new kit: full
                     $session->tell("You cannot use more kits! Upgrade your account to have more kits!");
                     return;
                 }
                 // create new kit
                 $kit = PvpKit::getDefault($session->getUID(), $this->kid);
                 $data->addKit($this->kid, $kit);
             }
             /** @var PvpKitModel[] $others */
             $others = $this->game->getModels();
             if (isset($others[$curEditing])) {
                 $otherModel = $others[$curEditing];
                 $otherModel->unfireTo($session->getPlayer());
             }
             $this->fireTo($session->getPlayer());
             $data->setEditingKitId($this->kid);
             $session->tell("You are now editing \"{$kit->name}\".");
             if ($data->getKitIdInUse() !== $this->kid) {
                 $session->tell("Click me again to apply \"{$kit->name}\".");
             } else {
                 $session->tell("Click me again to swap between bow/sword kit");
             }
         } else {
             // make this the using kit
             if ($data->getKitIdInUse() === $this->kid) {
                 if ($data->getBowLevel() === 0) {
                     $session->tell("After you bought a bow, triple-click me to enable/disable it.");
                     $session->tell("But right now you haven't yet!");
                     return;
                 }
                 $data->setIsUsingBowKit($bool = !$data->isUsingBowKit());
                 $session->tell("Your %s kit has been changed into a %s kit!", $bool ? "sword" : "bow", $bool ? "bow" : "sword");
                 return;
             }
             $kit = $data->getKitById($this->kid);
             if ($kit->kitid !== $this->kid) {
                 throw new \UnexpectedValueException("Unexpected {$kit->kitid} !== {$this->kid} at " . __FILE__ . "#" . __LINE__);
                 // crash.
             }
             $this->setNameTagTo($session->getPlayer(), self::IN_USE_TAG . $this->getNameTag());
             $oldInUseId = $data->getKitIdInUse();
             $data->setKitIdInUse($this->kid);
             $others = $this->game->getModels();
             if (isset($others[$oldInUseId])) {
                 $otherModel = $others[$oldInUseId];
                 $otherModel->setNameTagTo($session->getPlayer(), substr($otherModel->getNameTag(), strlen(self::IN_USE_TAG)));
             }
             $session->tell("You are now using kit \"{$kit->name}\". Rejoin this game or suicide using /kill to activate.");
             $data->getKitInUse()->equip($session->getPlayer()->getInventory(), $data, true);
         }
     } else {
         $this->getMain()->getLogger()->warning("{$this} was spawned to an invalid entity!");
         $this->despawnFrom($session->getPlayer());
     }
 }