Beispiel #1
0
  @Override
  protected void runImpl() {
    Player activeChar = getClient().getActiveChar();
    if (activeChar == null || _count == 0) return;

    // Проверяем, не подменили ли id
    if (activeChar.getBuyListId() != _listId)
      // TODO audit
      return;

    if (activeChar.isActionsDisabled()) {
      activeChar.sendActionFailed();
      return;
    }

    if (activeChar.isInStoreMode()) {
      activeChar.sendPacket(
          SystemMsg
              .WHILE_OPERATING_A_PRIVATE_STORE_OR_WORKSHOP_YOU_CANNOT_DISCARD_DESTROY_OR_TRADE_AN_ITEM);
      return;
    }

    if (activeChar.isInTrade()) {
      activeChar.sendActionFailed();
      return;
    }

    if (activeChar.isFishing()) {
      activeChar.sendPacket(SystemMsg.YOU_CANNOT_DO_THAT_WHILE_FISHING);
      return;
    }

    if (!Config.ALT_GAME_KARMA_PLAYER_CAN_SHOP && activeChar.isPK() && !activeChar.isGM()) {
      activeChar.sendActionFailed();
      return;
    }

    NpcInstance merchant = activeChar.getLastNpc();
    boolean isValidMerchant = merchant != null && merchant.isMerchantNpc();
    if (!activeChar.isGM()
        && (merchant == null
            || !isValidMerchant
            || !activeChar.isInRange(merchant, Creature.INTERACTION_DISTANCE))) {
      activeChar.sendActionFailed();
      return;
    }

    NpcTradeList list = BuyListHolder.getInstance().getBuyList(_listId);
    if (list == null) {
      // TODO audit
      activeChar.sendActionFailed();
      return;
    }

    int slots = 0;
    long weight = 0;
    long totalPrice = 0;
    long tax = 0;
    double taxRate = 0;

    Castle castle = null;
    if (merchant != null) {
      castle = merchant.getCastle(activeChar);
      if (castle != null) taxRate = castle.getTaxRate();
    }

    List<TradeItem> buyList = new ArrayList<TradeItem>(_count);
    List<TradeItem> tradeList = list.getItems();
    try {
      loop:
      for (int i = 0; i < _count; i++) {
        int itemId = _items[i];
        long count = _itemQ[i];
        long price = 0;

        for (TradeItem ti : tradeList)
          if (ti.getItemId() == itemId) {
            if (ti.isCountLimited() && ti.getCurrentValue() < count) continue loop;
            price = ti.getOwnersPrice();
          }

        if (price == 0 && (!activeChar.isGM() || !activeChar.getPlayerAccess().UseGMShop)) {
          // TODO audit
          activeChar.sendActionFailed();
          return;
        }

        totalPrice = SafeMath.addAndCheck(totalPrice, SafeMath.mulAndCheck(count, price));

        TradeItem ti = new TradeItem();
        ti.setItemId(itemId);
        ti.setCount(count);
        ti.setOwnersPrice(price);

        weight =
            SafeMath.addAndCheck(weight, SafeMath.mulAndCheck(count, ti.getItem().getWeight()));
        if (!ti.getItem().isStackable()
            || activeChar.getInventory().getItemByItemId(itemId) == null) slots++;

        buyList.add(ti);
      }

      tax = (long) (totalPrice * taxRate);

      totalPrice = SafeMath.addAndCheck(totalPrice, tax);

      if (!activeChar.getInventory().validateWeight(weight)) {
        activeChar.sendPacket(SystemMsg.YOU_HAVE_EXCEEDED_THE_WEIGHT_LIMIT);
        return;
      }

      if (!activeChar.getInventory().validateCapacity(slots)) {
        activeChar.sendPacket(SystemMsg.YOUR_INVENTORY_IS_FULL);
        return;
      }

      if (!activeChar.reduceAdena(totalPrice)) {
        activeChar.sendPacket(SystemMsg.YOU_DO_NOT_HAVE_ENOUGH_ADENA);
        return;
      }

      for (TradeItem ti : buyList) activeChar.getInventory().addItem(ti.getItemId(), ti.getCount());

      // Для магазинов с ограниченным количеством товара число продаваемых предметов уменьшаем после
      // всех проверок
      list.updateItems(buyList);

      // Add tax to castle treasury if not owned by npc clan
      if (castle != null)
        if (tax > 0
            && castle.getOwnerId() > 0
            && activeChar.getReflection() == ReflectionManager.DEFAULT)
          castle.addToTreasury(tax, true, false);
    } catch (ArithmeticException ae) {
      // TODO audit
      activeChar.sendPacket(SystemMsg.YOU_HAVE_EXCEEDED_THE_QUANTITY_THAT_CAN_BE_INPUTTED);
      return;
    }

    sendPacket(new ExBuySellListPacket.SellRefundList(activeChar, true));
    activeChar.sendChanges();
  }
 public void showBuyTicket(Player player, int val) {
   if (state != ACCEPTING_BETS) return;
   int npcId = getNpcId();
   String filename, search, replace;
   NpcHtmlMessage html = new NpcHtmlMessage(player, this);
   if (val < 10) {
     filename = getHtmlPath(npcId, 2, player);
     html.setFile(filename);
     for (int i = 0; i < 8; i++) {
       int n = i + 1;
       search = "Mob" + n;
       html.replace(search, MonsterRace.getInstance().getMonsters()[i].getTemplate().name);
     }
     search = "No1";
     if (val == 0) html.replace(search, "");
     else {
       html.replace(search, "" + val);
       player.setRace(0, val);
     }
   } else if (val < 20) {
     if (player.getRace(0) == 0) return;
     filename = getHtmlPath(npcId, 3, player);
     html.setFile(filename);
     html.replace("0place", "" + player.getRace(0));
     search = "Mob1";
     replace = MonsterRace.getInstance().getMonsters()[player.getRace(0) - 1].getTemplate().name;
     html.replace(search, replace);
     search = "0adena";
     if (val == 10) html.replace(search, "");
     else {
       html.replace(search, "" + cost[val - 11]);
       player.setRace(1, val - 10);
     }
   } else if (val == 20) {
     if (player.getRace(0) == 0 || player.getRace(1) == 0) return;
     filename = getHtmlPath(npcId, 4, player);
     html.setFile(filename);
     html.replace("0place", "" + player.getRace(0));
     search = "Mob1";
     replace = MonsterRace.getInstance().getMonsters()[player.getRace(0) - 1].getTemplate().name;
     html.replace(search, replace);
     search = "0adena";
     int price = cost[player.getRace(1) - 1];
     html.replace(search, "" + price);
     search = "0tax";
     int tax = 0;
     html.replace(search, "" + tax);
     search = "0total";
     int total = price + tax;
     html.replace(search, "" + total);
   } else {
     if (player.getRace(0) == 0 || player.getRace(1) == 0) return;
     if (player.getAdena() < cost[player.getRace(1) - 1]) {
       player.sendPacket(SystemMsg.YOU_DO_NOT_HAVE_ENOUGH_ADENA);
       return;
     }
     int ticket = player.getRace(0);
     int priceId = player.getRace(1);
     player.setRace(0, 0);
     player.setRace(1, 0);
     player.reduceAdena(cost[priceId - 1], true);
     SystemMessage sm = new SystemMessage(SystemMessage.ACQUIRED__S1_S2);
     sm.addNumber(_raceNumber);
     sm.addItemName(4443);
     player.sendPacket(sm);
     ItemInstance item = ItemFunctions.createItem(4443);
     item.setEnchantLevel(_raceNumber);
     item.setCustomType1(ticket);
     item.setCustomType2(cost[priceId - 1] / 100);
     player.getInventory().addItem(item);
     return;
   }
   html.replace("1race", String.valueOf(_raceNumber));
   player.sendPacket(html);
   player.sendActionFailed();
 }