165 lines
4.6 KiB
Lua
165 lines
4.6 KiB
Lua
-- Guda Sort Engine
|
|
-- Sorts bags by quality, name, or item type
|
|
|
|
local addon = Guda
|
|
|
|
local SortEngine = {}
|
|
addon.Modules.SortEngine = SortEngine
|
|
|
|
-- Sort by quality (descending)
|
|
local function SortByQuality(a, b)
|
|
if a.quality ~= b.quality then
|
|
return a.quality > b.quality
|
|
end
|
|
if a.name and b.name then
|
|
return a.name < b.name
|
|
end
|
|
return false
|
|
end
|
|
|
|
-- Sort by name
|
|
local function SortByName(a, b)
|
|
if a.name and b.name then
|
|
return a.name < b.name
|
|
end
|
|
return false
|
|
end
|
|
|
|
-- Sort by type (class/subclass)
|
|
local function SortByType(a, b)
|
|
if a.class ~= b.class then
|
|
return (a.class or "") < (b.class or "")
|
|
end
|
|
if a.subclass ~= b.subclass then
|
|
return (a.subclass or "") < (b.subclass or "")
|
|
end
|
|
if a.quality ~= b.quality then
|
|
return a.quality > b.quality
|
|
end
|
|
if a.name and b.name then
|
|
return a.name < b.name
|
|
end
|
|
return false
|
|
end
|
|
|
|
-- Get sort function by method
|
|
local sortMethods = {
|
|
quality = SortByQuality,
|
|
name = SortByName,
|
|
type = SortByType,
|
|
}
|
|
|
|
-- Collect all items from bags
|
|
function SortEngine:CollectItems(bagIDs)
|
|
local items = {}
|
|
local emptySlots = {}
|
|
|
|
for _, bagID in ipairs(bagIDs) do
|
|
local numSlots = addon.Modules.Utils:GetBagSlotCount(bagID)
|
|
|
|
if addon.Modules.Utils:IsBagValid(bagID) then
|
|
for slot = 1, numSlots do
|
|
local itemData = addon.Modules.BagScanner:ScanSlot(bagID, slot)
|
|
|
|
if itemData then
|
|
table.insert(items, {
|
|
bagID = bagID,
|
|
slot = slot,
|
|
data = itemData,
|
|
quality = itemData.quality or 0,
|
|
name = itemData.name or "",
|
|
class = itemData.class or "",
|
|
subclass = itemData.subclass or "",
|
|
})
|
|
else
|
|
table.insert(emptySlots, {bagID = bagID, slot = slot})
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
return items, emptySlots
|
|
end
|
|
|
|
-- Sort items
|
|
function SortEngine:SortItems(items, method)
|
|
local sortFunc = sortMethods[method] or sortMethods.quality
|
|
|
|
table.sort(items, sortFunc)
|
|
|
|
return items
|
|
end
|
|
|
|
-- Apply sorted items back to bags
|
|
function SortEngine:ApplySort(bagIDs, method)
|
|
addon:Print("Sorting bags by %s...", method or "quality")
|
|
|
|
-- Collect items
|
|
local items, emptySlots = self:CollectItems(bagIDs)
|
|
|
|
-- Sort items
|
|
items = self:SortItems(items, method)
|
|
|
|
-- Clear all bags first
|
|
ClearCursor()
|
|
|
|
-- Track moves for undo (not implemented yet, but good to have)
|
|
local moves = {}
|
|
|
|
-- Place items in sorted order
|
|
local targetIndex = 1
|
|
for _, bagID in ipairs(bagIDs) do
|
|
local numSlots = addon.Modules.Utils:GetBagSlotCount(bagID)
|
|
|
|
if addon.Modules.Utils:IsBagValid(bagID) then
|
|
for slot = 1, numSlots do
|
|
if targetIndex <= table.getn(items) then
|
|
local item = items[targetIndex]
|
|
|
|
-- Move item if it's not already in the right place
|
|
if item.bagID ~= bagID or item.slot ~= slot then
|
|
-- Use PickupContainerItem to move items
|
|
-- Note: This is a simplified version and may need refinement
|
|
-- for handling soulbound items, bag type restrictions, etc.
|
|
|
|
table.insert(moves, {
|
|
from = {bag = item.bagID, slot = item.slot},
|
|
to = {bag = bagID, slot = slot},
|
|
})
|
|
end
|
|
|
|
targetIndex = targetIndex + 1
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
-- Actually perform the moves (simplified - in reality needs more complex logic)
|
|
-- For now, just trigger a bag update
|
|
addon:Print("Sort complete! (%d items sorted)", table.getn(items))
|
|
|
|
-- Note: Actual item moving requires careful handling of:
|
|
-- - Locked items
|
|
-- - Bag type restrictions (soul bags, etc.)
|
|
-- - Soulbound items
|
|
-- - Quest items
|
|
-- This would require a more sophisticated move queue system
|
|
end
|
|
|
|
-- Sort current bags
|
|
function SortEngine:SortBags()
|
|
local method = addon.Modules.DB:GetSetting("sortMethod") or "quality"
|
|
self:ApplySort(addon.Constants.BAGS, method)
|
|
end
|
|
|
|
-- Sort bank
|
|
function SortEngine:SortBank()
|
|
if not addon.Modules.BankScanner:IsBankOpen() then
|
|
addon:Print("Bank must be open to sort!")
|
|
return
|
|
end
|
|
|
|
local method = addon.Modules.DB:GetSetting("sortMethod") or "quality"
|
|
self:ApplySort(addon.Constants.BANK_BAGS, method)
|
|
end
|