-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCore.lua
More file actions
2091 lines (1937 loc) · 77.1 KB
/
Copy pathCore.lua
File metadata and controls
2091 lines (1937 loc) · 77.1 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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---------------------------------------------------------------------------
-- PugzRaidTools - Core
-- Addon namespace, utilities, saved variables, slash commands
---------------------------------------------------------------------------
local addonName, PRT = ...
_G.PugzRaidTools = PRT
PRT.VERSION = "1.4.1"
-- Media
PRT.FONT = "Interface\\AddOns\\PugzRaidTools\\Media\\Fonts\\PTSansNarrow.ttf"
PRT.SND_MARIO = "Interface\\AddOns\\PugzRaidTools\\Media\\Sounds\\MarioCoin.ogg"
PRT.SND_LINK = "Interface\\AddOns\\PugzRaidTools\\Media\\Sounds\\Link.ogg"
PRT.FONT_SIZE = 12
PRT.FONT_SIZE_HEADER = 16
PRT.FONT_SIZE_TITLE = 20
-- Roster slot indices (shared with reorder engine)
PRT.RR_NAME = 1
PRT.RR_SUBGROUP = 2
PRT.RR_INDEX = 3
PRT.RR_LOCKED = 4
PRT.RR_START = 5
-- Classic Era Raid Instances (instanceMapID from GetInstanceInfo)
PRT.RAID_INSTANCES = {
{ id = 0, name = "Any Raid" },
{ id = 249, name = "Onyxia's Lair" },
{ id = 309, name = "Zul'Gurub" },
{ id = 409, name = "Molten Core" },
{ id = 469, name = "Blackwing Lair" },
{ id = 509, name = "Ruins of Ahn'Qiraj" },
{ id = 531, name = "Temple of Ahn'Qiraj" },
{ id = 533, name = "Naxxramas" },
}
PRT.RAID_INSTANCE_MAP = {}
for _, info in ipairs(PRT.RAID_INSTANCES) do
PRT.RAID_INSTANCE_MAP[info.id] = info.name
end
---------------------------------------------------------------------------
-- Raid Mark Icons
---------------------------------------------------------------------------
PRT.MARK_ICONS = {
{ id = 0, name = "Clear", color = { 0.5, 0.5, 0.5 } },
{ id = 1, name = "Star", color = { 1.0, 1.0, 0.0 } },
{ id = 2, name = "Circle", color = { 1.0, 0.5, 0.0 } },
{ id = 3, name = "Diamond", color = { 0.8, 0.2, 1.0 } },
{ id = 4, name = "Triangle", color = { 0.0, 1.0, 0.0 } },
{ id = 5, name = "Moon", color = { 0.7, 0.7, 1.0 } },
{ id = 6, name = "Square", color = { 0.0, 0.5, 1.0 } },
{ id = 7, name = "Cross", color = { 1.0, 0.2, 0.2 } },
{ id = 8, name = "Skull", color = { 1.0, 1.0, 1.0 } },
}
---------------------------------------------------------------------------
-- Colors
---------------------------------------------------------------------------
PRT.C = {
TITLE = { 0.2, 1.0, 0.6 },
SETTINGS_FONT = { 61 / 255, 1.0, 139 / 255 }, -- #3DFF8B
GOLD = { 1.0, 0.82, 0.0 },
WHITE = { 1.0, 1.0, 1.0 },
GRAY = { 0.5, 0.5, 0.5 },
RED = { 1.0, 0.3, 0.3 },
GREEN = { 0.3, 1.0, 0.3 },
YELLOW = { 1.0, 1.0, 0.3 },
SIDEBAR_BG = { 0.08, 0.08, 0.08, 0.95 },
SIDEBAR_SEL = { 0.13, 0.38, 0.13, 1.0 },
CONTENT_BG = { 0.04, 0.04, 0.04, 0.92 },
FRAME_BG = { 0.0, 0.0, 0.0, 0.92 },
BORDER = { 0.25, 0.25, 0.25, 1.0 },
INPUT_BG = { 0.08, 0.08, 0.08, 0.9 },
BTN_BG = { 0.14, 0.14, 0.14, 0.95 },
BTN_HOVER = { 0.22, 0.22, 0.22, 1.0 },
}
---------------------------------------------------------------------------
-- Utility functions
---------------------------------------------------------------------------
function PRT.CanonName(n)
if not n then return "" end
n = tostring(n)
n = n:gsub("^%s+", ""):gsub("%s+$", "")
n = n:gsub('^"+', ''):gsub('"+$', '')
n = n:gsub("^'+", ""):gsub("'+$", "")
n = n:gsub("%-.*$", "")
return string.lower(n)
end
function PRT.Trim(s)
local trimmed = (s or ""):gsub("^%s+", ""):gsub("%s+$", "")
return trimmed
end
function PRT:GetHomeRealmName()
local realm = GetRealmName and GetRealmName() or ""
realm = self.Trim(realm)
if realm == "" and GetNormalizedRealmName then
realm = self.Trim(GetNormalizedRealmName() or "")
end
return realm
end
function PRT.NormalizeRealmName(realm)
realm = PRT.Trim(realm or "")
if realm == "" then return "" end
realm = string.lower(realm)
realm = realm:gsub("[%s%p_]+", "")
return realm
end
function PRT:SplitNameRealm(fullName, fillHomeRealm)
local raw = self.Trim(fullName or "")
raw = raw:gsub('^"+', ""):gsub('"+$', "")
raw = raw:gsub("^'+", ""):gsub("'+$", "")
if raw == "" then
return "", ""
end
local name, realm = raw:match("^(.-)%-(.+)$")
if not name then
name = raw
realm = ""
end
name = self.Trim(name)
realm = self.Trim(realm or "")
if fillHomeRealm and name ~= "" and realm == "" then
realm = self:GetHomeRealmName()
end
return name, realm
end
function PRT:MakeCharacterFullName(name, realm, forceRealm)
name = self.Trim(name or "")
realm = self.Trim(realm or "")
if name == "" then return "" end
if realm == "" then return name end
if not forceRealm
and self.NormalizeRealmName(realm) == self.NormalizeRealmName(self:GetHomeRealmName()) then
return name
end
return name .. "-" .. realm
end
function PRT:MakePlayerIdentityKey(name, realm)
name = self.Trim(name or "")
realm = self.Trim(realm or "")
if name == "" then return "" end
local baseName, embeddedRealm = self:SplitNameRealm(name, false)
if embeddedRealm ~= "" then
name = baseName
realm = embeddedRealm
end
return string.lower(name) .. "@" .. self.NormalizeRealmName(realm)
end
-- Unsuffixed saved names represent characters on the user's current realm.
function PRT:GetPlayerIdentityKey(fullName)
local name, realm = self:SplitNameRealm(fullName, true)
return self:MakePlayerIdentityKey(name, realm)
end
-- GetRaidRosterInfo normally includes remote realms, but UnitFullName is used
-- as a fallback so cross-realm identity remains intact on clients that omit it.
function PRT:GetRaidMemberIdentity(raidIndex, rosterName)
local name, realm = self:SplitNameRealm(rosterName, false)
if realm == "" and raidIndex and UnitFullName then
local unitName, unitRealm = UnitFullName("raid" .. raidIndex)
if unitName and unitName ~= "" then
local unitBaseName, embeddedRealm = self:SplitNameRealm(unitName, false)
name = unitBaseName
if embeddedRealm ~= "" then
realm = embeddedRealm
end
end
if unitRealm and unitRealm ~= "" then
realm = unitRealm
end
end
if realm == "" and name ~= "" then
realm = self:GetHomeRealmName()
end
return name, realm
end
function PRT:GetRaidMemberIdentityKey(raidIndex, rosterName)
local name, realm = self:GetRaidMemberIdentity(raidIndex, rosterName)
return self:MakePlayerIdentityKey(name, realm)
end
function PRT:GetUnitIdentity(unit)
local name, realm
if UnitFullName then
name, realm = UnitFullName(unit)
end
if (not name or name == "") and UnitName then
name, realm = UnitName(unit)
end
name = self.Trim(name or "")
realm = self.Trim(realm or "")
local baseName, embeddedRealm = self:SplitNameRealm(name, false)
if embeddedRealm ~= "" then
name = baseName
realm = embeddedRealm
end
if name ~= "" and realm == "" then
realm = self:GetHomeRealmName()
end
return name, realm
end
function PRT:GetUnitIdentityKey(unit)
local name, realm = self:GetUnitIdentity(unit)
return self:MakePlayerIdentityKey(name, realm)
end
function PRT:FindRaidUnitByIdentityKey(identityKey)
if not identityKey or identityKey == "" then return nil end
local count = GetNumGroupMembers()
local sawUnitIdentity = false
for raidIndex = 1, count do
local unit = "raid" .. raidIndex
local unitIdentityKey = self:GetUnitIdentityKey(unit)
if unitIdentityKey ~= "" then
sawUnitIdentity = true
end
if unitIdentityKey == identityKey then
return unit, raidIndex
end
end
-- Avoid pairing a partially rebuilt roster index with a different raidN
-- token.
if sawUnitIdentity then return nil end
-- Compatibility fallback when this client exposes no unit identities.
for raidIndex = 1, count do
local rosterName = GetRaidRosterInfo(raidIndex)
if rosterName and self:GetRaidMemberIdentityKey(raidIndex, rosterName) == identityKey then
return "raid" .. raidIndex, raidIndex
end
end
end
function PRT.Print(...)
local n = select("#", ...)
local t = {}
for i = 1, n do t[i] = tostring(select(i, ...)) end
print("|cFF33FF99PugzRaidTools|r " .. table.concat(t, " "))
end
function PRT.GetNpcId(guid)
if not guid then return nil end
local npcId = select(6, strsplit("-", guid))
return tonumber(npcId)
end
function PRT.StripRealm(name)
if not name then return "" end
return tostring(name):gsub("%-.*$", "")
end
function PRT.GetClassColor(classFile)
if classFile and RAID_CLASS_COLORS and RAID_CLASS_COLORS[classFile] then
local c = RAID_CLASS_COLORS[classFile]
return c.r, c.g, c.b
end
return 1, 1, 1
end
-- Returns table of realm-aware identity key -> raid member information.
function PRT.GetRaidRoster()
local roster = {}
local n = GetNumGroupMembers()
for i = 1, n do
local name, rank, subgroup, _, _, classFile, _, _, _, role =
GetRaidRosterInfo(i)
if name and subgroup then
local baseName, realm = PRT:GetRaidMemberIdentity(i, name)
local key = PRT:MakePlayerIdentityKey(baseName, realm)
if key ~= "" then
local unit = "raid" .. i
local isMainTank = role == "MAINTANK"
if not isMainTank and GetPartyAssignment then
isMainTank = GetPartyAssignment("MAINTANK", unit, true) and true or false
end
roster[key] = {
name = name,
baseName = baseName,
realm = realm,
displayName = PRT:MakeCharacterFullName(baseName, realm, false),
classFile = classFile,
subgroup = subgroup,
index = i,
unit = unit,
rank = rank or 0,
role = role,
isMainTank = isMainTank,
}
end
end
end
return roster
end
---------------------------------------------------------------------------
-- Shared CLEU Dispatcher
-- Both AutoSwap and AutoMark register callbacks here so we only listen
-- to COMBAT_LOG_EVENT_UNFILTERED when at least one feature needs it.
---------------------------------------------------------------------------
PRT._cleuListeners = {}
PRT._cleuRegistered = false
function PRT:RegisterCLEUListener(key, fn)
self._cleuListeners[key] = fn
self:_UpdateCLEURegistration()
end
function PRT:UnregisterCLEUListener(key)
self._cleuListeners[key] = nil
self:_UpdateCLEURegistration()
end
function PRT:_UpdateCLEURegistration()
local need = false
for _ in pairs(self._cleuListeners) do need = true; break end
if need and not self._cleuRegistered then
if not self._cleuFrame then
self._cleuFrame = CreateFrame("Frame")
self._cleuFrame:SetScript("OnEvent", function()
for _, fn in pairs(PRT._cleuListeners) do fn() end
end)
end
self._cleuFrame:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
self._cleuRegistered = true
elseif not need and self._cleuRegistered then
self._cleuFrame:UnregisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
self._cleuRegistered = false
end
end
---------------------------------------------------------------------------
-- Saved Variables defaults
---------------------------------------------------------------------------
PRT.DEFAULTS = {
version = 1,
compositions = {}, -- array of { name=string, roster={40 strings} }
autoSwap = {
enabled = false,
triggers = {}, -- legacy flat array (migrated to swapPresets on load)
swapPresets = {}, -- array of { name, triggers[], instanceId, allowAnywhere }; excluded from DeepMerge
activeSwapPreset = "", -- name of active preset
},
floatingList = {
locked = false,
hideOutsideRaid = false,
mouseoverOnly = false,
point = "CENTER",
relPoint = "CENTER",
x = 0,
y = 0,
scale = 1.0,
fontSize = 14,
fontOutline = "OUTLINE",
textMode = "expand",
width = 180,
rowHeight = 20,
textWidth = 180,
fontColor = { 61 / 255, 1.0, 139 / 255 },
bgAlpha = 0.7,
shown = true,
},
settings = {
mainBgAlpha = 0.92,
keepChanges = false,
requireServerNames = false,
hideServerNames = false,
forcePositions = false,
frameW = 1000,
frameH = 600,
frameX = 0,
frameY = 0,
fontColorDefaultsVersion = 1,
minimapAngle = 195, -- degrees; 195° = bottom-left of minimap
showMinimapIcon = true,
lastImportShape = "", -- last used import shape key ("8col","2col","1col","cooked")
lastExportShape = "8col", -- last used single-composition export shape
},
notification = {
enabled = true,
fontSize = 32,
fontColor = { 61 / 255, 1.0, 139 / 255 },
x = 0,
y = 80,
duration = 3.0,
sound = false,
},
autoMark = {
enabled = false,
activePreset = "",
presets = {}, -- array of { name, markGroups[], instanceId, allowAnywhere }; excluded from DeepMerge
},
targetMarks = {
enabled = false,
allowSolo = false,
activePreset = "",
modifiers = {
main = "CTRL",
alt1 = "ALT",
alt2 = "SHIFT",
},
presets = {}, -- array of { name, groups[] }; excluded from DeepMerge
},
prtProfiles = {
activeProfile = "",
profiles = {}, -- array of overall feature-preset and enabled-default mappings
},
profileFloat = {
embedInGroupList = false,
embeddedPosition = "top",
embeddedHighlight = true,
embeddedAlignment = "left",
shown = false,
mouseoverOnly = false,
locked = false,
point = "CENTER",
relPoint = "CENTER",
x = 0,
y = 160,
scale = 1.0,
width = 220,
height = 30,
fontSize = 12,
textMode = "truncate",
buttonTextMode = "full",
bgAlpha = 0.92,
notificationEnabled = true,
notificationSound = true,
notificationX = 0,
notificationY = 80,
},
rosterMatcher = {
threshold = 50,
aliases = {}, -- array of { id, label, characters={ { name, realm, classFile } } }
nextAliasId = 1,
},
autoLog = {
enabled = false,
},
raidCheck = {
showOnReadyCheck = true,
onlyLeaderAssist = true,
checkWorldBuffs = true,
checkFood = false,
checkFlask = true,
checkZanza = true,
checkConsumes = true,
checkPotions = true,
checkDisallowed = true,
checkBuffs = true,
checkDurability = true,
allianceBlessingsOnly = true,
dismissOnRightClick = true,
columnSettings = {},
worldBuffValidity = {},
sortMode = "classGroup",
autoClose = true,
closeDelay = 5,
collapsed = false,
columnOrder = {
"worldBuffs",
"attackPower",
"disallowed",
"flask",
"zanza",
"potions",
"consumes",
"food",
"stamina",
"druid",
"intellect",
"spirit",
"shadow",
"armor",
"kings",
"might",
"wisdom",
"salvation",
"light",
"durability",
},
scale = 1.0,
frameStrata = "FULLSCREEN_DIALOG",
point = "CENTER",
relPoint = "CENTER",
x = 0,
y = 0,
},
inviteTools = {
enabled = true,
activePreset = "",
presets = {},
bannedPlayers = {}, -- global realm-aware block list; not part of presets
autoInvite = {
enabled = false,
keywords = { "inv" },
guildOnly = false,
autoAcceptTrusted = false,
raidInvites = {
enabled = false,
},
},
autoPromote = {
enabled = false,
names = "",
guildRankThreshold = 0, -- 0 = explicit names only; otherwise top N guild ranks
},
loot = {
enabled = false,
method = "group",
assignMasterLooter = false,
masterLooter = "",
threshold = 1,
onlyInRaid = true,
zones = {
naxxramas = false,
aq40 = false,
bwl = false,
moltenCore = false,
zulgurub = false,
aq20 = false,
blastedLands = false,
azshara = false,
ashenvale = false,
hinterlands = false,
duskwood = false,
feralas = false,
},
customZones = {},
},
lootToChat = {
enabled = false,
includeItemLevel = false,
},
reinviteSnapshot = {
createdAt = 0,
members = {},
},
},
}
---------------------------------------------------------------------------
-- Deep merge defaults into saved table (preserves existing values)
---------------------------------------------------------------------------
local function DeepMerge(defaults, saved)
if type(defaults) ~= "table" then return saved end
if type(saved) ~= "table" then return defaults end
for k, v in pairs(defaults) do
if saved[k] == nil then
if type(v) == "table" then
saved[k] = {}
DeepMerge(v, saved[k])
else
saved[k] = v
end
elseif type(v) == "table" and type(saved[k]) == "table"
and k ~= "compositions" and k ~= "triggers"
and k ~= "presets" and k ~= "swapPresets"
and k ~= "keywords" and k ~= "columnOrder" then
DeepMerge(v, saved[k])
end
end
return saved
end
---------------------------------------------------------------------------
-- Composition management
---------------------------------------------------------------------------
function PRT:GetDB()
return PugzRaidToolsDB or self.DEFAULTS
end
function PRT:GetComp(name)
local db = self:GetDB()
for _, comp in ipairs(db.compositions) do
if comp.name == name then return comp end
end
end
function PRT:GetCompOrder()
local db = self:GetDB()
local order = {}
for _, comp in ipairs(db.compositions) do
order[#order + 1] = comp.name
end
return order
end
function PRT:AddComp(name, roster)
local db = self:GetDB()
roster = roster or {}
while #roster < 40 do roster[#roster + 1] = "" end
db.compositions[#db.compositions + 1] = { name = name, roster = roster }
return true
end
function PRT:RemoveComp(name)
local db = self:GetDB()
for i, comp in ipairs(db.compositions) do
if comp.name == name then
table.remove(db.compositions, i)
return true
end
end
return false
end
function PRT:RenameComp(oldName, newName)
local comp = self:GetComp(oldName)
if not comp then return false end
comp.name = newName
local db = self:GetDB()
-- Legacy flat triggers
for _, trigger in ipairs(db.autoSwap.triggers) do
if trigger.compName == oldName then trigger.compName = newName end
end
-- Swap presets
for _, preset in ipairs(db.autoSwap.swapPresets or {}) do
for _, trigger in ipairs(preset.triggers or {}) do
if trigger.compName == oldName then trigger.compName = newName end
end
end
-- Auto mark: swap triggers and smart comp references
for _, amPreset in ipairs(db.autoMark.presets or {}) do
for _, mg in ipairs(amPreset.markGroups or {}) do
for _, st in ipairs(mg.swapTriggers or {}) do
if st.compName == oldName then st.compName = newName end
end
if mg.smartComp == oldName then mg.smartComp = newName end
end
end
return true
end
function PRT:UpdateCompRoster(name, roster)
local comp = self:GetComp(name)
if not comp then return false end
while #roster < 40 do roster[#roster + 1] = "" end
comp.roster = roster
return true
end
function PRT:MoveComp(fromIndex, toIndex)
local db = self:GetDB()
if fromIndex == toIndex then return false end
if fromIndex < 1 or fromIndex > #db.compositions then return false end
if toIndex < 1 or toIndex > #db.compositions then return false end
local comp = table.remove(db.compositions, fromIndex)
table.insert(db.compositions, toIndex, comp)
return true
end
-- A tagged roster is a new import/editor model. Bare entries in it are NOT
-- home-realm identities. Legacy untagged saves retain their original meaning.
function PRT:GetRosterSlotIdentityKey(roster, index)
local raw = self.Trim(roster and roster[index] or "")
if raw == "" then return "" end
if roster._prtRealmVersion ~= nil then
local _, realm = self:SplitNameRealm(raw, false)
if roster._prtRealmVersion ~= 1 or realm == "" then return nil end
end
return self:GetPlayerIdentityKey(raw)
end
function PRT:GetRosterExportName(roster, index)
local raw = self.Trim(roster and roster[index] or "")
if raw == "" or roster._prtRealmVersion ~= nil then return raw end
local name, realm = self:SplitNameRealm(raw, true)
return self:MakeCharacterFullName(name, realm, true)
end
-- Build target table (8 groups x 5 slots) from a flat roster array.
-- Fail before any engine action rather than guessing an unresolved realm.
function PRT:BuildTarget(roster)
local target = {}
local seen = {}
for g = 1, 8 do
target[g] = {}
for s = 1, 5 do
target[g][s] = { [self.RR_NAME] = "" }
end
end
for i, name in ipairs(roster) do
local g = math.floor((i - 1) / 5) + 1
local s = ((i - 1) % 5) + 1
if g >= 1 and g <= 8 then
local key = self:GetRosterSlotIdentityKey(roster, i)
if key == nil then
return nil, ("Resolve server names in Raid Groups and save before applying groups (Group %d, Slot %d: %s).")
:format(g, s, tostring(name))
end
if roster._prtRealmVersion ~= nil and key ~= "" then
if seen[key] then
return nil, "The same exact player appears in multiple Raid Groups cells: " .. tostring(name)
end
seen[key] = true
end
target[g][s][self.RR_NAME] = key
end
end
return target
end
-- Build a run-only target from the exact identities that are present now.
-- Unresolved and absent entries remain unchanged in the saved composition;
-- they simply do not constrain this sort attempt.
function PRT:CompileSortTarget(roster, raid)
roster = roster or {}
raid = raid or self.GetRaidRoster()
local report = {
unresolved = {},
missing = {},
presentResolvedCount = 0,
protectedLeader = nil,
}
if roster._prtRealmVersion ~= nil
and roster._prtRealmVersion ~= 1 then
return nil, "This Raid Groups composition uses unsupported server-name data. Reimport or resave it before sorting.", report
end
local target = {}
for group = 1, 8 do
target[group] = {}
for slot = 1, 5 do
target[group][slot] = { [self.RR_NAME] = "" }
end
end
local seen = {}
local targetGroupByKey = {}
for index = 1, 40 do
local raw = self.Trim(roster[index] or "")
if raw ~= "" then
local group = math.floor((index - 1) / 5) + 1
local slot = ((index - 1) % 5) + 1
local key = self:GetRosterSlotIdentityKey(roster, index)
if key == nil then
report.unresolved[#report.unresolved + 1] = {
raw = raw,
group = group,
slot = slot,
}
else
if roster._prtRealmVersion ~= nil and seen[key] then
return nil,
"The same exact player appears in multiple Raid Groups cells: " .. raw,
report
end
seen[key] = true
local member = raid[key]
if member then
target[group][slot][self.RR_NAME] = key
targetGroupByKey[key] = group
report.presentResolvedCount =
report.presentResolvedCount + 1
else
report.missing[#report.missing + 1] = {
raw = raw,
key = key,
group = group,
slot = slot,
}
end
end
end
end
if report.presentResolvedCount == 0 then
return nil,
"No resolved players from this composition are currently in the raid, so there is nothing to sort.",
report
end
-- A raid leader can be assigned to another subgroup, but the game forces
-- them to the first occupied position in that subgroup. If the composition
-- explicitly targets the leader, preserve that requested subgroup and let
-- the engines move them. If the leader is omitted or unresolved, keep them
-- in their current subgroup so partial sorting cannot move them as
-- unintended collateral.
local leaderKey, leader
for key, member in pairs(raid) do
if member.rank == 2 then
leaderKey, leader = key, member
break
end
if member.index == 1 and not leader then
leaderKey, leader = key, member
end
end
if leaderKey and leader and leader.subgroup then
local desiredGroup = targetGroupByKey[leaderKey]
local leaderName = self:MakeCharacterFullName(
leader.baseName or leader.name,
leader.realm or "",
true)
if not desiredGroup then
local preferredSlot = 1
for _, member in pairs(raid) do
if member.subgroup == leader.subgroup
and member.index < leader.index then
preferredSlot = preferredSlot + 1
end
end
local protectedSlot
if preferredSlot <= 5
and target[leader.subgroup][preferredSlot][self.RR_NAME] == "" then
protectedSlot = preferredSlot
else
for slot = 1, 5 do
if target[leader.subgroup][slot][self.RR_NAME] == "" then
protectedSlot = slot
break
end
end
end
if not protectedSlot then
return nil,
("Cannot sort this composition because Group %d assigns five other present players while raid leader %s must remain in that group.")
:format(leader.subgroup, leaderName),
report
end
target[leader.subgroup][protectedSlot][self.RR_NAME] = leaderKey
report.protectedLeader = {
key = leaderKey,
group = leader.subgroup,
slot = protectedSlot,
}
end
end
return target, nil, report
end
function PRT:ReportSortTargetSkips(report)
if not report then return end
local function PrintEntries(label, entries)
local parts = {}
for _, entry in ipairs(entries) do
parts[#parts + 1] = ("%s (G%d S%d)"):format(
tostring(entry.raw), entry.group, entry.slot)
end
for first = 1, #parts, 5 do
local last = math.min(first + 4, #parts)
local chunk = {}
for index = first, last do
chunk[#chunk + 1] = parts[index]
end
PRT.Print(label .. " " .. table.concat(chunk, ", "))
end
end
if #report.unresolved > 0 then
PRT.Print(("Partial sort: skipping %d unresolved roster %s. Unresolved raid members may move as needed.")
:format(#report.unresolved,
#report.unresolved == 1 and "name" or "names"))
PrintEntries("Unresolved:", report.unresolved)
end
if #report.missing > 0 then
PRT.Print(("Partial sort: %d resolved roster %s missing from the raid. Remaining present players will still be sorted.")
:format(#report.missing,
#report.missing == 1 and "player is" or "players are"))
PrintEntries("Missing:", report.missing)
end
end
function PRT:FindDuplicateRosterSlots(roster)
local identities = {}
roster = roster or {}
for slotIndex = 1, 40 do
local raw = self.Trim(roster[slotIndex] or "")
local key = self:GetRosterSlotIdentityKey(roster, slotIndex)
if key and key ~= "" then
local entry = identities[key]
if not entry then
local name, realm = self:SplitNameRealm(raw, true)
entry = {
key = key,
displayName = self:MakeCharacterFullName(name, realm, true),
slots = {},
}
identities[key] = entry
end
entry.slots[#entry.slots + 1] = slotIndex
end
end
local duplicatesBySlot = {}
for _, entry in pairs(identities) do
if #entry.slots > 1 then
for _, slotIndex in ipairs(entry.slots) do
duplicatesBySlot[slotIndex] = entry
end
end
end
return duplicatesBySlot
end
---------------------------------------------------------------------------
-- Import / Export
---------------------------------------------------------------------------
function PRT:ExportRosterByShape(name, roster, shape)
roster = roster or {}
shape = shape or "8col"
local lines = {}
local function Slot(index)
local value = self:GetRosterExportName(roster, index)
return value ~= "" and value or "-"
end
if shape == "2col" then
for pair = 0, 3 do
local leftGroup = pair * 2 + 1
local rightGroup = leftGroup + 1
for position = 1, 5 do
lines[#lines + 1] = table.concat({
Slot((leftGroup - 1) * 5 + position),
Slot((rightGroup - 1) * 5 + position),
}, " ")
end
end
elseif shape == "1col" then
for index = 1, 40 do
lines[#lines + 1] = Slot(index)
end
elseif shape == "cooked" then
lines[#lines + 1] = "[" .. tostring(name or "Imported") .. "]"
for group = 1, 8 do
local row = {}
for position = 1, 5 do
row[#row + 1] = Slot((group - 1) * 5 + position)
end
lines[#lines + 1] = table.concat(row, " ")
end
else
-- 8 columns (one per group), with five raid-position rows.
for position = 1, 5 do
local row = {}
for group = 1, 8 do
row[#row + 1] = Slot((group - 1) * 5 + position)
end
lines[#lines + 1] = table.concat(row, " ")
end
end
return table.concat(lines, "\n")
end
function PRT:ExportCompRoster(name, shape)
local comp = self:GetComp(name)
if not comp then return "" end
return self:ExportRosterByShape(comp.name, comp.roster, shape)
end
function PRT:ExportComps(names)
local db = self:GetDB()
local lines = {}
for _, comp in ipairs(db.compositions) do
local include = not names
if names then
for _, n in ipairs(names) do
if n == comp.name then include = true; break end
end
end
if include then
-- Preserve empty slots and exact legacy identities on reimport.
lines[#lines + 1] = self:ExportRosterByShape(comp.name, comp.roster, "cooked")
lines[#lines + 1] = ""
end
end
return table.concat(lines, "\n")
end
---------------------------------------------------------------------------