Example #1
0
    private static void HandleAddItemToTrade(GameSession session, PacketReader packet)
    {
        long itemUid = packet.ReadLong();
        int  amount  = packet.ReadInt();

        if (!session.Player.Inventory.HasItem(itemUid) || session.Player.TradeInventory.IsLocked)
        {
            return;
        }

        Item item = session.Player.Inventory.GetByUid(itemUid);

        if (item.IsBound())
        {
            return;
        }

        // Split item
        if (item.Amount > amount)
        {
            item.TrySplit(amount, out Item splitItem);
            session.Send(ItemInventoryPacket.UpdateAmount(itemUid, item.Amount));
            item = splitItem;
        }
        else
        {
            session.Player.Inventory.RemoveItem(session, itemUid, out Item removedItem);
            item = removedItem;
        }

        session.Player.TradeInventory.AddItem(session, item);
    }
Example #2
0
    public void Add(GameSession session, long uid, int amount, short slot)
    {
        Item item = session.Player.Inventory.GetByUid(uid);

        if (amount < item.Amount)
        {
            item.TrySplit(amount, out Item splitItem);
            session.Send(ItemInventoryPacket.UpdateAmount(uid, item.Amount));
            item = splitItem;
        }
        else
        {
            session.Player.Inventory.RemoveItem(session, uid, out Item removedItem);
            item = removedItem;
        }

        // If slot is free, add item to it
        if (Items[slot] is null)
        {
            item.Slot   = slot;
            Items[slot] = item;
            session.Send(StorageInventoryPacket.Add(item));
            return;
        }

        // Find first item with the same id, if true update the amount
        Item existingItem = Items.FirstOrDefault(x => x is not null && x.Id == item.Id && x.Rarity == item.Rarity);

        if (existingItem is not null)
        {
            if (existingItem.Amount + item.Amount <= existingItem.StackLimit)
            {
                existingItem.Amount += item.Amount;
                session.Send(StorageInventoryPacket.UpdateItem(existingItem));
                return;
            }

            existingItem.Amount = existingItem.StackLimit;
            item.Amount         = existingItem.Amount + item.Amount - existingItem.StackLimit;
            session.Send(StorageInventoryPacket.UpdateItem(existingItem));
        }

        // Find first free slot
        for (short i = 0; i < Items.Length; i++)
        {
            if (Items[i] is not null)
            {
                continue;
            }

            item.Slot = i;
            Items[i]  = item;
            session.Send(StorageInventoryPacket.Add(item));
            return;
        }
    }
Example #3
0
    private static void HandleCreateListing(GameSession session, PacketReader packet)
    {
        long itemUid  = packet.ReadLong();
        long price    = packet.ReadLong();
        int  quantity = packet.ReadInt();

        if (!session.Player.Inventory.HasItem(itemUid))
        {
            session.Send(BlackMarketPacket.Error((int)BlackMarketError.ItemNotInInventory));
            return;
        }

        Item item = session.Player.Inventory.GetByUid(itemUid);

        if (item.Amount < quantity || item.IsBound())
        {
            return;
        }

        double depositRate = 0.01; // 1% deposit rate
        int    maxDeposit  = 100000;

        int calculatedDeposit = (int)(depositRate * (price * quantity));
        int deposit           = Math.Min(calculatedDeposit, maxDeposit);

        if (!session.Player.Wallet.Meso.Modify(-deposit))
        {
            return;
        }

        if (item.Amount > quantity)
        {
            item.TrySplit(quantity, out Item newStack);
            session.Send(ItemInventoryPacket.UpdateAmount(item.Uid, item.Amount));
            item = newStack;
        }
        else
        {
            session.Player.Inventory.ConsumeItem(session, item.Uid, quantity);
        }

        BlackMarketListing listing = new(session.Player, item, quantity, price, deposit);

        session.Send(BlackMarketPacket.CreateListing(listing));
    }