示例#1
0
    private static void HandlePurchase(GameSession session, PacketReader packet)
    {
        long listingId = packet.ReadLong();
        int  amount    = packet.ReadInt();

        BlackMarketListing listing = GameServer.BlackMarketManager.GetListingById(listingId);

        if (listing == null)
        {
            return;
        }

        if (listing.OwnerAccountId == session.Player.AccountId)
        {
            session.Send(BlackMarketPacket.Error((int)BlackMarketError.CannotPurchaseOwnItems));
            return;
        }

        if (listing.Item.Amount < amount)
        {
            session.Send(BlackMarketPacket.Error((int)BlackMarketError.QuantityNotAvailable));
            return;
        }

        if (!session.Player.Wallet.Meso.Modify(-listing.Price * amount))
        {
            return;
        }

        Item purchasedItem;
        bool removeListing = false;

        if (listing.Item.Amount == amount)
        {
            purchasedItem = listing.Item;
            GameServer.BlackMarketManager.RemoveListing(listing);
            DatabaseManager.BlackMarketListings.Delete(listing.Id);
            removeListing = true;
        }
        else
        {
            listing.Item.Amount -= amount;
            Item newItem = new(listing.Item)
            {
                Amount = amount
            };
            newItem.Uid   = DatabaseManager.Items.Insert(newItem);
            purchasedItem = newItem;
        }

        purchasedItem.DecreaseTradeCount();

        MailHelper.BlackMarketTransaction(purchasedItem, listing, session.Player.CharacterId, listing.Price, removeListing);
        session.Send(BlackMarketPacket.Purchase(listingId, amount));
    }