-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathSort.lua
More file actions
487 lines (417 loc) · 16.5 KB
/
Copy pathSort.lua
File metadata and controls
487 lines (417 loc) · 16.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
local addonName, DF = ...
-- ============================================================
-- FRAME SORTING SYSTEM
-- Sorts party/raid frames by role and name
-- ============================================================
-- Local caching of frequently used globals for performance
local pairs, ipairs, type, wipe = pairs, ipairs, type, wipe
local sort = table.sort
local tinsert = table.insert
local UnitExists = UnitExists
local UnitGUID = UnitGUID
local UnitGroupRolesAssigned = UnitGroupRolesAssigned
local UnitClass = UnitClass
local UnitIsUnit = UnitIsUnit
local UnitName = UnitName
local GetSpecializationInfoByID = GetSpecializationInfoByID
local GetSpecialization = GetSpecialization
local InCombatLockdown = InCombatLockdown
-- NOTE: Previously used reusable tables here, but that caused bugs when
-- SortFrameList was called while iterating over a previous result.
-- Now we return fresh tables each time. The garbage is minimal.
DF.Sort = {}
local Sort = DF.Sort
-- Spec to role mapping (melee vs ranged DPS)
-- This maps DPS spec IDs to whether they're melee
-- Tank/healer specs are excluded - they're filtered by role before this check
local MELEE_SPECS = {
-- Death Knight
[251] = true, [252] = true, -- Frost, Unholy
-- Demon Hunter
[577] = true, -- Havoc
-- Druid
[103] = true, -- Feral
-- Hunter
[255] = true, -- Survival
-- Monk
[269] = true, -- Windwalker
-- Paladin
[70] = true, -- Retribution
-- Rogue
[259] = true, [260] = true, [261] = true, -- Assassination, Outlaw, Subtlety
-- Shaman
[263] = true, -- Enhancement
-- Warrior
[71] = true, [72] = true, -- Arms, Fury
}
-- Cache for unit info (cleared on group changes)
Sort.UnitCache = {}
-- ============================================================
-- ROLE DETECTION
-- ============================================================
-- Get the role for a unit (TANK, HEALER, MELEE, RANGED, or DAMAGER)
function Sort:GetUnitRole(unit)
if not unit or not UnitExists(unit) then return "DAMAGER" end
-- Check cache first
local guid = UnitGUID(unit)
if guid and self.UnitCache[guid] then
return self.UnitCache[guid].role
end
-- Get assigned role
local role = UnitGroupRolesAssigned(unit)
-- For DPS, determine if melee or ranged
if role == "DAMAGER" or role == "NONE" then
local db = DF:GetDB()
if db.sortSeparateMeleeRanged then
local specID = nil
-- For player, we can get spec directly
if UnitIsUnit(unit, "player") then
specID = GetSpecializationInfo(GetSpecialization() or 1)
else
-- For other players, try to get from inspection cache or guess from class
-- Note: In a full implementation, you'd use NotifyInspect/INSPECT_READY
-- For now, we'll use class-based guessing
local _, class = UnitClass(unit)
if class then
-- Classes that are primarily melee (DPS specs are all melee)
if class == "WARRIOR" or class == "ROGUE" or class == "DEATHKNIGHT" or class == "DEMONHUNTER" or class == "PALADIN" then
role = "MELEE"
-- Classes that are primarily ranged
elseif class == "MAGE" or class == "WARLOCK" then
role = "RANGED"
-- Classes with both melee and ranged DPS specs - default to ranged
else
role = "RANGED"
end
else
role = "DAMAGER"
end
end
-- Check spec if we have it
if specID then
if MELEE_SPECS[specID] then
role = "MELEE"
else
role = "RANGED"
end
end
else
role = "DAMAGER"
end
end
-- Cache the result
if guid then
self.UnitCache[guid] = self.UnitCache[guid] or {}
self.UnitCache[guid].role = role
end
return role
end
-- Get sort priority for a role based on db settings
function Sort:GetRolePriority(role, db)
local roleOrder = db.sortRoleOrder or { "TANK", "HEALER", "MELEE", "RANGED" }
for i, r in ipairs(roleOrder) do
if r == role then
return i
end
-- Handle DAMAGER matching MELEE or RANGED when not separating
if role == "DAMAGER" and (r == "MELEE" or r == "RANGED") then
return i
end
end
return 100 -- Unknown role goes last
end
-- ============================================================
-- SORTING LOGIC
-- ============================================================
-- Get sort priority for a class based on db settings
function Sort:GetClassPriority(class, db)
if not class then return 100 end
local classOrder = db.sortClassOrder
if not classOrder then return 100 end
for i, c in ipairs(classOrder) do
if c == class then
return i
end
end
return 100 -- Unknown class goes last
end
-- Compare function for sorting frames
function Sort:CompareUnits(unitA, unitB, db)
-- Option to ignore role/class/name and simply use party/raid index
if db.sortByPartyOrder then
local function GetIndex(u)
if not u then return 9999 end
if UnitIsUnit(u, "player") then return 0 end
-- Prefer a GUID->party slot lookup so the "index order" matches the actual party roster
-- even if the unit-id on a frame isn't literally "partyN" (e.g. raidN assignment).
local guid = UnitGUID(u)
if guid then
for i = 1, 4 do
local pu = "party" .. i
if UnitExists(pu) and UnitGUID(pu) == guid then
return i
end
end
end
-- Fallback: parse digits from the unit string (party1/raid1/etc)
local num = tonumber((u:match("%d+")))
return num or 9999
end
return GetIndex(unitA) < GetIndex(unitB)
end
local roleA = self:GetUnitRole(unitA)
local roleB = self:GetUnitRole(unitB)
local prioA = self:GetRolePriority(roleA, db)
local prioB = self:GetRolePriority(roleB, db)
-- First sort by role priority
if prioA ~= prioB then
return prioA < prioB
end
-- Then sort by class if enabled
if db.sortByClass then
local _, classA = UnitClass(unitA)
local _, classB = UnitClass(unitB)
local classPrioA = self:GetClassPriority(classA, db)
local classPrioB = self:GetClassPriority(classB, db)
if classPrioA ~= classPrioB then
return classPrioA < classPrioB
end
end
-- Then sort alphabetically if enabled
-- Supports "AZ", "ZA", or legacy true (treated as "AZ")
local alpha = db.sortAlphabetical
if alpha and alpha ~= false then
local nameA = UnitName(unitA) or ""
local nameB = UnitName(unitB) or ""
if alpha == "ZA" then
return nameA > nameB
else
return nameA < nameB
end
end
return false
end
-- Compare function for test mode using test data
function Sort:CompareTestData(dataA, dataB, db)
-- If party-order sorting is requested, use the precomputed index
if db.sortByPartyOrder then
local idxA = dataA.index or 9999
local idxB = dataB.index or 9999
return idxA < idxB
end
-- Get roles from test data
local roleA = dataA.role or "DAMAGER"
local roleB = dataB.role or "DAMAGER"
-- Map test roles to sort roles, respecting melee/ranged separation
local function MapTestRole(data)
local role = data.role or "DAMAGER"
if role == "TANK" or role == "HEALER" then return role end
-- When separating melee/ranged, use spec ID for accurate classification
if db.sortSeparateMeleeRanged then
-- Check spec ID first (most accurate)
local specID = data.specID
if specID and specID > 0 then
return MELEE_SPECS[specID] and "MELEE" or "RANGED"
end
-- Fallback to class-based detection (matches live fallback)
local class = data.class
if class then
if class == "WARRIOR" or class == "ROGUE" or class == "DEATHKNIGHT" or class == "DEMONHUNTER" or class == "PALADIN" then
return "MELEE"
else
return "RANGED"
end
end
end
return "DAMAGER"
end
roleA = MapTestRole(dataA)
roleB = MapTestRole(dataB)
local prioA = self:GetRolePriority(roleA, db)
local prioB = self:GetRolePriority(roleB, db)
-- First sort by role priority
if prioA ~= prioB then
return prioA < prioB
end
-- Then sort by class if enabled
if db.sortByClass then
local classA = dataA.class
local classB = dataB.class
local classPrioA = self:GetClassPriority(classA, db)
local classPrioB = self:GetClassPriority(classB, db)
if classPrioA ~= classPrioB then
return classPrioA < classPrioB
end
end
-- Then sort alphabetically if enabled
-- Supports "AZ", "ZA", or legacy true (treated as "AZ")
local alpha = db.sortAlphabetical
if alpha and alpha ~= false then
local nameA = dataA.name or ""
local nameB = dataB.name or ""
if alpha == "ZA" then
return nameA > nameB
else
return nameA < nameB
end
end
return false
end
-- Sort a list of frame data entries
-- Each entry should have: {frame = frame, unit = unit, isPlayer = bool, testData = optional}
-- In test mode, entries should have testData with .name, .class, .role
function Sort:SortFrameList(frameList, db, isTestMode)
if not db.sortEnabled then return frameList end
local playerEntry = nil
local otherEntries = {} -- Fresh table each call
-- Separate player from others
for _, entry in ipairs(frameList) do
if entry.isPlayer then
playerEntry = entry
else
tinsert(otherEntries, entry)
end
end
-- Sort non-player entries
if isTestMode then
-- Use test data for sorting
sort(otherEntries, function(a, b)
local dataA = a.testData or {}
local dataB = b.testData or {}
return self:CompareTestData(dataA, dataB, db)
end)
else
-- Use real unit data
sort(otherEntries, function(a, b)
return self:CompareUnits(a.unit, b.unit, db)
end)
end
-- Build final list based on self position setting
local sortedList = {} -- Fresh table each call
local selfPos = db.sortSelfPosition or "SORTED"
-- Check if selfPos is a numeric position (1-5)
local numericPos = tonumber(selfPos)
if numericPos and playerEntry then
-- Insert player at specific position
local inserted = false
for i, entry in ipairs(otherEntries) do
-- Insert player before this position if we've reached the target
if i == numericPos and not inserted then
tinsert(sortedList, playerEntry)
inserted = true
end
tinsert(sortedList, entry)
end
-- If we haven't inserted yet (position is beyond list length), add at end
if not inserted then
tinsert(sortedList, playerEntry)
end
elseif selfPos == "FIRST" and playerEntry then
tinsert(sortedList, playerEntry)
for _, entry in ipairs(otherEntries) do
tinsert(sortedList, entry)
end
elseif selfPos == "LAST" and playerEntry then
for _, entry in ipairs(otherEntries) do
tinsert(sortedList, entry)
end
tinsert(sortedList, playerEntry)
else
-- SORTED (or legacy NORMAL) - sort player with everyone else
if playerEntry then
local inserted = false
for i, entry in ipairs(otherEntries) do
local playerFirst
if isTestMode then
local playerData = playerEntry.testData or {}
local entryData = entry.testData or {}
playerFirst = self:CompareTestData(playerData, entryData, db)
else
playerFirst = self:CompareUnits("player", entry.unit, db)
end
if playerFirst and not inserted then
tinsert(sortedList, playerEntry)
inserted = true
end
tinsert(sortedList, entry)
end
if not inserted then
tinsert(sortedList, playerEntry)
end
else
-- Can't reuse here, need to return the other entries directly
return otherEntries
end
end
return sortedList
end
-- ============================================================
-- CACHE MANAGEMENT
-- ============================================================
function Sort:ClearCache()
wipe(self.UnitCache)
end
function Sort:TriggerResort()
self:ClearCache()
-- SecureSort handles ALL party frame positioning
-- It queries roles FRESH each sort via roleFilter (works in combat!)
if DF.SecureSort and DF.SecureSort.initialized and DF.SecureSort.framesRegistered then
-- Push settings (only works out of combat, but that's fine for configuration)
if not InCombatLockdown() then
DF.SecureSort:PushSortSettings()
DF.SecureSort:UpdateLayoutParamsOnButtons()
end
-- Trigger the secure sort (works in AND out of combat)
-- Roles are queried fresh each time, not pre-cached
DF.SecureSort:TriggerSecureSort()
end
-- Note: We do NOT call UpdateAllFrames() here anymore.
-- SecureSort is now the only system that positions party frames.
end
-- ============================================================
-- EVENT HANDLING
-- ============================================================
-- (Event-based sorting removed - Headers.lua unified handler manages all sorting)
-- ============================================================
-- ============================================================
-- SLASH COMMAND
-- ============================================================
SLASH_DFSORT1 = "/dfsort"
SlashCmdList["DFSORT"] = function(msg)
if msg == "refresh" or msg == "resort" then
Sort:TriggerResort()
print("|cff00ff00DandersFrames:|r Re-sorted frames")
elseif msg == "clear" then
Sort:ClearCache()
print("|cff00ff00DandersFrames:|r Cleared sort cache")
elseif msg == "debug" then
print("|cff00ccffDandersFrames Sort Debug:|r")
local db = DF:GetDB()
print(" sortEnabled:", db.sortEnabled)
print(" sortSelfPosition:", db.sortSelfPosition)
print(" sortByClass:", db.sortByClass)
print(" sortAlphabetical:", tostring(db.sortAlphabetical))
print(" sortSeparateMeleeRanged:", db.sortSeparateMeleeRanged)
print(" sortByPartyOrder:", tostring(db.sortByPartyOrder))
print(" sortRoleOrder:", table.concat(db.sortRoleOrder or {}, ", "))
if db.sortByClass then
print(" sortClassOrder:", table.concat(db.sortClassOrder or {}, ", "))
end
-- Show detected roles and classes for party members
print(" Unit Info:")
local _, playerClass = UnitClass("player")
print(" player:", Sort:GetUnitRole("player"), "-", playerClass, "-", UnitName("player"))
for i = 1, 4 do
local unit = "party" .. i
if UnitExists(unit) then
local _, unitClass = UnitClass(unit)
print(" " .. unit .. ":", Sort:GetUnitRole(unit), "-", unitClass, "-", UnitName(unit))
end
end
else
print("|cff00ff00DandersFrames:|r /dfsort commands:")
print(" refresh - Re-sort frames")
print(" clear - Clear role cache")
print(" debug - Show sort debug info")
end
end