Exemple #1
0
        public void Sort(GameSession session)
        {
            List <Item> tempItems = Items.Where(x => x != null).ToList();

            tempItems.Sort((x, y) => x.Id.CompareTo(y.Id));
            Items = new Item[DEFAULT_SIZE + ExtraSize];
            for (int i = 0; i < tempItems.Count; i++)
            {
                Items[i] = tempItems[i];
            }
            session.Send(StorageInventoryPacket.Sort(Items));
        }
    public void Sort(GameSession session)
    {
        IEnumerable <Item> items = Items.Where(x => x is not null);

        // group items by item id and sum the amount, return a new list of items with updated amount (ty gh copilot)
        List <Item> groupedItems = items.Where(x => x.StackLimit > 1).GroupBy(x => x.Id).Select(x => new Item(x.First())
        {
            Amount          = x.Sum(y => y.Amount),
            BankInventoryId = Id
        }).ToList();

        // Add items that can't be grouped
        groupedItems.AddRange(items.Where(x => x.StackLimit == 1));

        // sort items by id
        groupedItems.Sort((x, y) => x.Id.CompareTo(y.Id));

        // Delete items that got grouped
        foreach (Item oldItem in items)
        {
            Item newItem = groupedItems.FirstOrDefault(x => x.Uid == oldItem.Uid);
            if (newItem is null)
            {
                DatabaseManager.Items.Delete(oldItem.Uid);
            }
        }

        Items = new Item[DEFAULT_SIZE + ExtraSize];
        for (short i = 0; i < groupedItems.Count; i++)
        {
            Item item = groupedItems[i];

            item.Slot = i;
            Items[i]  = item;

            DatabaseManager.Items.Update(item);
        }

        session.Send(StorageInventoryPacket.Update());
        session.Send(StorageInventoryPacket.Sort(Items));
        session.Send(StorageInventoryPacket.ExpandAnim());
    }