-
Notifications
You must be signed in to change notification settings - Fork 1
/
BattlegroundTools.lua
989 lines (794 loc) · 35.2 KB
/
BattlegroundTools.lua
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
local _G, ModuleName, Private, AddonName, Namespace = _G, 'BattlegroundTools', {}, ...
local Addon = Namespace.Addon
local Module = Addon:NewModule(ModuleName, 'AceEvent-3.0', 'AceTimer-3.0')
local L = Namespace.Libs.AceLocale:GetLocale(AddonName)
local LSM = Namespace.Libs.LibSharedMedia
local LibDD = Namespace.Libs.LibDropDown
Namespace.BattlegroundTools = Module
local TimeDiff = Namespace.Utils.TimeDiff
local Channel = Namespace.Communication.Channel
local GetGroupLeaderData = Namespace.PlayerData.GetGroupLeaderData
local GetPlayerDataByUnit = Namespace.PlayerData.GetPlayerDataByUnit
local GetPlayerDataByName = Namespace.PlayerData.GetPlayerDataByName
local Role = Namespace.PlayerData.Role
local GetMessageDestination = Namespace.Communication.GetMessageDestination
local GroupType = Namespace.Utils.GroupType
local GetGroupType = Namespace.Utils.GetGroupType
local ForEachUnitData = Namespace.PlayerData.ForEachUnitData
local InActiveBattleground = Namespace.Battleground.InActiveBattleground
local QueueStatus = Namespace.Battleground.QueueStatus
local GetCurrentZoneId = Namespace.Battleground.GetCurrentZoneId
local RaidIconIndex = Namespace.Utils.RaidIconIndex
local CreateFrame = CreateFrame
local FlashClientIcon = FlashClientIcon
local GetTime = GetTime
local CreateColor = CreateColor
local ReplaceIconAndGroupExpressions = C_ChatInfo.ReplaceIconAndGroupExpressions
local PromoteToLeader = PromoteToLeader
local PromoteToAssistant = PromoteToAssistant
local DemoteAssistant = DemoteAssistant
local PlaySound = PlaySound
local UnitIsGroupLeader = UnitIsGroupLeader
local SendChatMessage = SendChatMessage
local SetRaidTarget = SetRaidTarget
local UISpecialFrames = UISpecialFrames
local ReadyCheckSound = SOUNDKIT.READY_CHECK
local ActivateWarmodeSound = SOUNDKIT.UI_WARMODE_ACTIVATE
local DeactivateWarmodeSound = SOUNDKIT.UI_WARMODE_DECTIVATE
local UNKNOWNOBJECT = UNKNOWNOBJECT
local concat = table.concat
local sort = table.sort
local format = string.format
local floor = math.floor
local min = math.min
local pairs = pairs
local tinsert = tinsert
local GiveLeadStatus = {
None = 0,
RecentlyAccepted = 1,
RecentlyRejected = 2,
}
local Memory = {
currentZoneId = nil,
iconSlots = {
-- [iconIndex] = name or boolean,
},
WantBattlegroundLead = {
DialogFrame = nil,
dropdownSelection = {},
giveLeadStatus = {},
ackTimer = nil,
ackLeader = nil,
},
InstructionFrame = nil,
RaidWarningLogs = {
last = nil,
list = {
-- {time, message},
},
size = 0,
timer = nil,
},
}
local GiveLeadBehavior = {
NoAutomation = 0,
GiveLead = 1,
RejectLead = 2,
OverrideLead = 3,
}
local MarkBehavior = {
NoMark = 0,
AnyAvailable = 1,
PreferredMark = 2,
}
Namespace.BattlegroundTools.GiveLeadBehavior = GiveLeadBehavior
Namespace.BattlegroundTools.MarkBehavior = MarkBehavior
function Private.ApplyFont(textObject, fontConfig)
textObject:SetFont(LSM:Fetch('font', fontConfig.family), fontConfig.size, fontConfig.flags)
textObject:SetTextColor(fontConfig.color.r, fontConfig.color.g, fontConfig.color.b)
textObject:SetShadowColor(fontConfig.shadowColor.r, fontConfig.shadowColor.g, fontConfig.shadowColor.b, fontConfig.shadowColor.a)
textObject:SetShadowOffset(fontConfig.shadowOffset.x, fontConfig.shadowOffset.y)
textObject:SetJustifyH('LEFT')
textObject:SetJustifyV(fontConfig.topToBottom and 'TOP' or 'BOTTOM')
end
function Private.ApplyFrameSettings()
local settings = Namespace.Database.profile.BattlegroundTools.InstructionFrame.settings
local frame = Memory.InstructionFrame
frame:SetBackdrop({
bgFile = LSM:Fetch('background', settings.backgroundTexture),
edgeFile = LSM:Fetch('border', settings.borderTexture),
edgeSize = settings.borderSize,
insets = {
left = settings.backgroundInset,
right = settings.backgroundInset,
top = settings.backgroundInset,
bottom = settings.backgroundInset,
}
})
local bgColor = settings.backgroundColor
local borderColor = settings.borderColor
frame:SetBackdropColor(bgColor.r, bgColor.g, bgColor.b, bgColor.a)
frame:SetBackdropBorderColor(borderColor.r, borderColor.g, borderColor.b, borderColor.a)
end
function Module:SetFontSetting(setting, value)
local fontConfig = Namespace.Database.profile.BattlegroundTools.InstructionFrame.font
fontConfig[setting] = value
Private.ApplyFont(Memory.InstructionFrame.Text, fontConfig)
end
function Module:GetFontSetting(setting)
return Namespace.Database.profile.BattlegroundTools.InstructionFrame.font[setting]
end
function Module:SetFrameSetting(setting, value)
Namespace.Database.profile.BattlegroundTools.InstructionFrame.settings[setting] = value
Private.ApplyFrameSettings()
end
function Module:GetFrameSetting(setting)
return Namespace.Database.profile.BattlegroundTools.InstructionFrame.settings[setting]
end
function Private.ResetLogs()
local logs = Memory.RaidWarningLogs
logs.last = nil
logs.list = {}
logs.size = 0
end
function Private.AddLog(message)
local logs = Memory.RaidWarningLogs
if message == logs.last then
logs.list[logs.size].time = floor(GetTime())
return
end
logs.size = logs.size + 1
logs.list[logs.size] = {
time = GetTime(),
message = ReplaceIconAndGroupExpressions(message),
}
logs.last = message
end
function Private.ApplyLogs(textObject)
local frameConfig = Namespace.Database.profile.BattlegroundTools.InstructionFrame
local colorHighlight = frameConfig.font.colorHighlight
local colorTime = frameConfig.font.colorTime
local maxInstructions = frameConfig.settings.maxInstructions
local count, directionModifier
if frameConfig.font.topToBottom then
count = 0
directionModifier = 1
else
count = min(maxInstructions, Memory.RaidWarningLogs.size) + 1
directionModifier = -1
end
local timePrefix = format('|cff%.2x%.2x%.2x', colorTime.r * 255, colorTime.g * 255, colorTime.b * 255)
local now = floor(GetTime())
local list = Memory.RaidWarningLogs.list
local messages = {}
for i = Memory.RaidWarningLogs.size, Memory.RaidWarningLogs.size - maxInstructions + 1, -1 do
count = count + directionModifier
local log = list[i]
if not log then break end
local diff = TimeDiff(now, log.time)
if i == Memory.RaidWarningLogs.size then
log = concat({
timePrefix,
diff.format(),
'|r ',
format('|cff%.2x%.2x%.2x', colorHighlight.r * 255, colorHighlight.g * 255, colorHighlight.b * 255),
log.message,
'|r',
})
else
log = concat({ timePrefix, diff.format(), '|r ', log.message })
end
messages[count] = log
end
textObject:SetText(concat(messages, "\n"))
end
function Private.InitializeInstructionFrame()
if Memory.InstructionFrame then return Memory.InstructionFrame end
local instructionFrame = CreateFrame('Frame', 'bgcInstructionFrame', _G.UIParent, _G.BackdropTemplateMixin and 'BackdropTemplate')
instructionFrame:SetFrameStrata('LOW')
instructionFrame:SetResizeBounds(100, 50)
instructionFrame:SetClampedToScreen(true)
instructionFrame:SetMovable(true)
instructionFrame:RegisterForDrag('LeftButton')
instructionFrame:SetResizable(true)
local moveOverlay = CreateFrame('Button', nil, instructionFrame)
moveOverlay:SetPoint('TOPLEFT')
moveOverlay:SetPoint('BOTTOMRIGHT', -16, 0)
moveOverlay:SetScript('OnMouseDown', function(self)
if not Namespace.Database.profile.BattlegroundTools.InstructionFrame.move then return end
self:GetParent():StartMoving()
end)
moveOverlay:SetScript('OnMouseUp', function(self)
local parent = self:GetParent()
parent:StopMovingOrSizing()
local position = Namespace.Database.profile.BattlegroundTools.InstructionFrame.position
local anchor, _, _, x, y = parent:GetPoint(1)
position.anchor, position.x, position.y = anchor, x, y
end)
local resizeButton = CreateFrame('Button', nil, instructionFrame)
resizeButton:SetPoint('BOTTOMRIGHT')
resizeButton:SetSize(16, 16)
resizeButton:SetNormalTexture([[Interface\ChatFrame\UI-ChatIM-SizeGrabber-Down]])
resizeButton:SetHighlightTexture([[Interface\ChatFrame\UI-ChatIM-SizeGrabber-Highlight]])
resizeButton:SetPushedTexture([[Interface\ChatFrame\UI-ChatIM-SizeGrabber-Up]])
resizeButton:SetScript('OnMouseDown', function(self)
self:GetParent():StartSizing('BOTTOMRIGHT')
end)
resizeButton:SetScript('OnMouseUp', function(self)
local parent = self:GetParent()
parent:StopMovingOrSizing()
local size = Namespace.Database.profile.BattlegroundTools.InstructionFrame.size
size.width, size.height = parent:GetSize()
end)
local text = instructionFrame:CreateFontString(nil, 'OVERLAY')
Private.ApplyFont(text, Namespace.Database.profile.BattlegroundTools.InstructionFrame.font)
text:SetPoint('TOPLEFT', 7, -7)
text:SetPoint('BOTTOMRIGHT', -17, 7)
instructionFrame.MoveOverlay = moveOverlay
instructionFrame.ResizeButton = resizeButton
instructionFrame.Text = text
Memory.InstructionFrame = instructionFrame
end
function Module:OnInitialize()
Private.InitializeInstructionFrame()
end
function Module:TriggerUpdateWantBattlegroundLeadDialogFrame(newVisibility)
local mem = Memory.WantBattlegroundLead
local dialog = mem.DialogFrame
if not dialog then return end
local dropdown = dialog.Dropdown
local lastName
local count = 0
LibDD:UIDropDownMenu_Initialize(dropdown, function ()
ForEachUnitData(function(data)
local name = data.name
if not data.wantLead or data.units.player then
mem.dropdownSelection[name] = nil
return
end
local recentlyRejected = mem.giveLeadStatus[name] == GiveLeadStatus.RecentlyRejected
if recentlyRejected or mem.dropdownSelection[name] == nil then
mem.dropdownSelection[name] = false
end
local visibleName = Namespace.QueueTools:GetPlayerNameForDisplay(data)
lastName = CreateColor(data.classColor.r, data.classColor.g, data.classColor.b, data.classColor.a):WrapTextInColorCode(visibleName)
count = count + 1
local info = LibDD:UIDropDownMenu_CreateInfo()
info.text = lastName
info.checked = mem.dropdownSelection[name]
info.isNotRadio = true
info.keepShownOnClick = true
info.arg1 = name
info.arg2 = data.units.primary
info.func = dialog.Dropdown.OnSelect
LibDD:UIDropDownMenu_AddButton(info)
end)
end)
local message
if count == 1 then
LibDD:UIDropDownMenu_SetWidth(dropdown, 230)
message = format(L['%s is requesting lead'], lastName)
else
LibDD:UIDropDownMenu_SetWidth(dropdown, 190)
message = format(L['%d people requested lead'], count)
end
LibDD:UIDropDownMenu_SetText(dropdown, message)
Private.UpdateAcceptRejectButtonState()
if newVisibility ~= nil then
dialog:SetShown(newVisibility)
end
end
function Private.TriggerUpdateInstructionFrame()
local frameConfig = Namespace.Database.profile.BattlegroundTools.InstructionFrame
local currentZoneId = GetCurrentZoneId()
if frameConfig.show and frameConfig.zones[currentZoneId] then
Module:ShowInstructionsFrame()
else
Module:HideInstructionsFrame()
end
end
function Private.RequestRaidLeadListener(_, _, newRole)
if newRole ~= Role.Leader then return end
Module:RequestRaidLead()
end
function Private.PlayLeaderSoundListener(playerData, oldRole, newRole)
if not playerData.units.player then return end
if not Namespace.Database.profile.BattlegroundTools.LeaderTools.leaderSound then return end
if not InActiveBattleground() then return end
if oldRole and newRole == Role.Leader then
PlaySound(ActivateWarmodeSound)
elseif newRole and oldRole == Role.Leader then
PlaySound(DeactivateWarmodeSound)
end
end
function Private.UpdateRaidLeaderIconListener(playerData, _, newRole)
if newRole ~= Role.Leader or not playerData.units.player then return end
if not InActiveBattleground() then return end
Memory.iconSlots = {}
Private.MarkRaidMembers()
end
--- this listener in specific deals with automatic promotion and demotion of players when PLAYER gets lead
function Private.PromoteAssistantsWhenPlayerBecomesLeaderListener(playerData, _, newRole)
if newRole ~= Role.Leader or not playerData.units.player then return end
if not InActiveBattleground() then return end
local demoteUnlisted = Namespace.Database.profile.BattlegroundTools.LeaderTools.demoteUnlisted
ForEachUnitData(function (data)
if data.role == Role.Member and Module:GetPlayerConfigValue(data.name, 'promoteToAssistant') then
PromoteToAssistant(data.units.primary)
elseif demoteUnlisted and data.role == Role.Assist and not Module:GetPlayerConfigValue(data.name, 'promoteToAssistant') then
DemoteAssistant(data.units.primary)
end
end)
end
function Private.TriggerUpdateWantBattlegroundLeadDialogFrameAfterRoleChange(playerData)
if not playerData.units.player then return end
Module:TriggerUpdateWantBattlegroundLeadDialogFrame()
end
--- this listener in specific deals with new members becoming assistant
function Private.PromoteNewMemberToAssistantListener(playerData, oldRole, newRole)
if oldRole or newRole ~= Role.Member or playerData.units.player then return end
if not InActiveBattleground() then return end
local leader = GetGroupLeaderData()
if not leader or not leader.units.player then return end
if Module:GetPlayerConfigValue(playerData.name, 'promoteToAssistant') then
PromoteToAssistant(playerData.name)
end
end
function Private.MarkRaidMembers()
local preferMarks = {}
local preferredFallback = {}
local remainingMarked = {}
local leaderIcon = Namespace.Database.profile.BattlegroundTools.LeaderTools.leaderIcon
ForEachUnitData(function(data)
local name = data.name
if data.units.player then
preferMarks[leaderIcon] = { name = name, unit = 'player' }
return
end
local behavior = Module:GetPlayerConfigValue(name, 'markBehavior')
if behavior == MarkBehavior.PreferredMark then
local preferredIcon = Module:GetPlayerConfigValue(name, 'preferredIcon')
if preferredIcon and not preferMarks[preferredIcon] then
preferMarks[preferredIcon] = { name = name, unit = data.units.primary }
else
preferredFallback[#preferredFallback + 1] = { name = name, unit = data.units.primary }
end
elseif behavior == MarkBehavior.AnyAvailable then
remainingMarked[#remainingMarked + 1] = { name = name, unit = data.units.primary }
end
end)
sort(preferredFallback)
sort(remainingMarked)
local fallbackIndex = 1
local remainingIndex = 1
local leaderTools = Namespace.Database.profile.BattlegroundTools.LeaderTools.availableIcons
for iconIndex = 1, 8 do
if not preferMarks[iconIndex] and leaderTools[iconIndex] then
if preferredFallback[fallbackIndex] then
preferMarks[iconIndex] = preferredFallback[fallbackIndex]
fallbackIndex = fallbackIndex + 1
elseif remainingMarked[remainingIndex] then
preferMarks[iconIndex] = remainingMarked[remainingIndex]
remainingIndex = remainingIndex + 1
end
end
if preferMarks[iconIndex] then
local proposedIcon = preferMarks[iconIndex]
local unit = proposedIcon.unit
local name = proposedIcon.name
if Memory.iconSlots[iconIndex] ~= name then
Memory.iconSlots[iconIndex] = name
SetRaidTarget(unit, iconIndex)
if unit ~= 'player' then
Addon:Print(format(L['Marked %s with %s'], name, format([[|TInterface\TargetingFrame\UI-RaidTargetingIcon_%d:16:16|t]], iconIndex)))
end
end
end
end
end
function Private.MarkRaidMembersIfLeadingBattleground()
if not InActiveBattleground() then return end
local leader = GetGroupLeaderData()
if not leader or not leader.units.player then return end
Private.MarkRaidMembers()
end
function Private.UpdatePlayerConfigLabels(unitPlayerData)
local updated = false
for _, playerData in pairs(unitPlayerData) do
if playerData.classColor then
local config = Module:GetPlayerConfig(playerData.name)
if config and config.playerName == config.groupLabel then
config.groupLabel = playerData.classColor:WrapTextInColorCode(playerData.name)
updated = true
end
end
end
if not updated then return end
Addon:InitializePlayerConfig()
end
function Private.DetectBattlegroundExit(previousState, newState)
if previousState.status ~= QueueStatus.Active then return end
if newState.status ~= QueueStatus.None then return end
Memory.WantBattlegroundLead.giveLeadStatus = {}
Memory.WantBattlegroundLead.ackLeader = nil
if Namespace.Database.profile.BattlegroundTools.InstructionFrame.settings.clearFrameOnExitBattleground then
Private.ResetLogs()
end
Module:HideInstructionsFrame()
Module:TriggerUpdateWantBattlegroundLeadDialogFrame(false)
end
function Private.DetectBattlegroundEntryAfterConfirm(previousState, newState, mapName)
if previousState.status ~= QueueStatus.Confirm then return end
if newState.status ~= QueueStatus.Active then return end
Private.AddLog(format(L['Entered %s'], mapName))
Memory.WantBattlegroundLead.giveLeadStatus = {}
Memory.WantBattlegroundLead.ackLeader = nil
Private.TriggerUpdateInstructionFrame()
Module:RequestRaidLead()
end
function Module:OnEnable()
self:RegisterEvent('CHAT_MSG_RAID_WARNING')
self:RegisterEvent('PLAYER_ENTERING_WORLD')
Namespace.Database.RegisterCallback(self, 'OnProfileChanged', 'RefreshConfig')
Namespace.Database.RegisterCallback(self, 'OnProfileCopied', 'RefreshConfig')
Namespace.Database.RegisterCallback(self, 'OnProfileReset', 'RefreshConfig')
Private.InitializeBattlegroundLeaderDialog()
Namespace.PlayerData.RegisterOnRoleChange('request_raid_lead', Private.RequestRaidLeadListener)
Namespace.PlayerData.RegisterOnRoleChange('play_leader_sound', Private.PlayLeaderSoundListener)
Namespace.PlayerData.RegisterOnRoleChange('update_raid_leader_icon', Private.UpdateRaidLeaderIconListener)
Namespace.PlayerData.RegisterOnRoleChange('promote_assistant_when_player_becomes_leader', Private.PromoteAssistantsWhenPlayerBecomesLeaderListener)
Namespace.PlayerData.RegisterOnRoleChange('update_want_lead_dialog', Private.TriggerUpdateWantBattlegroundLeadDialogFrameAfterRoleChange)
Namespace.PlayerData.RegisterOnRoleChange('promote_new_member_to_assistant', Private.PromoteNewMemberToAssistantListener)
Namespace.PlayerData.RegisterOnUpdate('mark_players', Private.MarkRaidMembersIfLeadingBattleground)
Namespace.PlayerData.RegisterOnUpdate('update_player_config_labels', Private.UpdatePlayerConfigLabels)
Namespace.Battleground.RegisterQueueStateListener('reset_logs', Private.DetectBattlegroundExit)
Namespace.Battleground.RegisterQueueStateListener('clean_pre_bg_raid_lead_info', Private.DetectBattlegroundEntryAfterConfirm)
self:RefreshConfig()
Private.AddLog(L['Battleground Commander loaded'])
if Namespace.Database.profile.BattlegroundTools.InstructionFrame.firstTime then
Private.AddLog(L['You can access the configuration via /bgc or through the interface options'])
Namespace.Database.profile.BattlegroundTools.InstructionFrame.firstTime = false
end
Private.ApplyLogs(Memory.InstructionFrame.Text)
Namespace.Libs.LibDropDownExtension:RegisterEvent('OnShow OnHide', function (dropdown, event, options)
local data = dropdown.unit and GetPlayerDataByUnit(dropdown.unit)
if not data then return end
if event == 'OnShow' then
options[1] = {
text = L['BGC: Configure'],
func = function ()
Namespace.Config.AddPlayerConfig(data.name)
Addon:OpenPlayerConfig(data.name, true)
end
}
return true
elseif event == 'OnHide' then
options[1] = nil
end
end, 1)
end
function Module:PLAYER_ENTERING_WORLD(_, isLogin, isReload)
Private.TriggerUpdateInstructionFrame()
if not isLogin and not isReload then return end
self:RequestRaidLead()
end
function Module:CHAT_MSG_RAID_WARNING(_, message)
Private.AddLog(message)
end
function Module:RefreshConfig()
local frameConfig = Namespace.Database.profile.BattlegroundTools.InstructionFrame
Memory.InstructionFrame:SetSize(frameConfig.size.width, frameConfig.size.height)
Memory.InstructionFrame:ClearAllPoints()
Memory.InstructionFrame:SetPoint(frameConfig.position.anchor, _G.UIParent, frameConfig.position.anchor, frameConfig.position.x, frameConfig.position.y)
Memory.InstructionFrame.ResizeButton:SetShown(frameConfig.move)
Private.TriggerUpdateInstructionFrame()
self:TriggerUpdateWantBattlegroundLeadDialogFrame()
end
function Module:ShowInstructionsFrame()
Private.ApplyFont(Memory.InstructionFrame.Text, Namespace.Database.profile.BattlegroundTools.InstructionFrame.font)
Private.ApplyFrameSettings()
Memory.InstructionFrame:Show()
if Memory.InstructionFrame.timer then return end
Memory.InstructionFrame.timer = self:ScheduleRepeatingTimer(function ()
Private.ApplyLogs(Memory.InstructionFrame.Text)
Private.TriggerUpdateInstructionFrame()
end, 0.2)
end
function Module:HideInstructionsFrame()
Memory.InstructionFrame:Hide()
if not Memory.InstructionFrame.timer then return end
self:CancelTimer(Memory.InstructionFrame.timer)
Memory.InstructionFrame.timer = nil
end
function Module:SetInstructionFrameState(enableState)
local frameConfig = Namespace.Database.profile.BattlegroundTools.InstructionFrame
frameConfig.show = enableState
Private.TriggerUpdateInstructionFrame()
end
function Module:GetInstructionFrameState()
return Namespace.Database.profile.BattlegroundTools.InstructionFrame.show
end
function Module:SetInstructionFrameMoveState(enableState)
Namespace.Database.profile.BattlegroundTools.InstructionFrame.move = enableState
Memory.InstructionFrame.ResizeButton:SetShown(enableState)
end
function Module:GetInstructionFrameMoveState()
return Namespace.Database.profile.BattlegroundTools.InstructionFrame.move
end
function Module:SetZoneId(zoneId, value)
Namespace.Database.profile.BattlegroundTools.InstructionFrame.zones[zoneId] = value
Private.TriggerUpdateInstructionFrame()
end
function Module:GetZoneId(zoneId)
return Namespace.Database.profile.BattlegroundTools.InstructionFrame.zones[zoneId]
end
function Private.CanRequestLead()
if not Namespace.Database.profile.BattlegroundTools.WantBattlegroundLead.wantLead then return false end
if UnitIsGroupLeader('player') then return false end
return InActiveBattleground()
end
function Private.PromoteToLeader(playerData)
if not playerData.units.primary then return end
PromoteToLeader(playerData.units.primary)
local mem = Memory.WantBattlegroundLead
mem.giveLeadStatus[playerData.name] = GiveLeadStatus.RecentlyAccepted
if not mem.DialogFrame then return end
mem.DialogFrame:Hide()
end
function Module:WantBattlegroundLead(senderData)
if not InActiveBattleground() then
return self:TriggerUpdateWantBattlegroundLeadDialogFrame()
end
local playerData = GetPlayerDataByUnit('player')
if playerData == senderData or playerData.role ~= Role.Leader then
return self:TriggerUpdateWantBattlegroundLeadDialogFrame()
end
local channel = GetMessageDestination()
if channel == Channel.Whisper then
return self:TriggerUpdateWantBattlegroundLeadDialogFrame()
end
local config = Namespace.Database.profile.BattlegroundTools.WantBattlegroundLead
local mem = Memory.WantBattlegroundLead
local sender = senderData.name
local giveLeadBehavior = self:GetPlayerConfigValue(sender, 'giveLeadBehavior')
if giveLeadBehavior == GiveLeadBehavior.RejectLead or mem.giveLeadStatus[sender] == GiveLeadStatus.RecentlyRejected then
return self:TriggerUpdateWantBattlegroundLeadDialogFrame()
end
if config.wantLead and giveLeadBehavior ~= GiveLeadBehavior.OverrideLead then
return self:TriggerUpdateWantBattlegroundLeadDialogFrame()
end
if giveLeadBehavior == GiveLeadBehavior.GiveLead or giveLeadBehavior == GiveLeadBehavior.OverrideLead then
Private.PromoteToLeader(senderData)
Addon:Print(format(L['Automatically giving lead to %s'], sender))
return self:TriggerUpdateWantBattlegroundLeadDialogFrame()
end
if mem.DialogFrame and not mem.DialogFrame:IsVisible() then
PlaySound(ReadyCheckSound)
FlashClientIcon()
end
self:TriggerUpdateWantBattlegroundLeadDialogFrame(true)
end
function Private.SendManualChatMessages()
local mem = Memory.WantBattlegroundLead
local config = Namespace.Database.profile.BattlegroundTools.WantBattlegroundLead
mem.ackTimer = nil
if not config.enableManualRequest or not Private.CanRequestLead() then return end
local groupLeader = GetGroupLeaderData()
if not groupLeader or groupLeader.addonVersion then return end
local name = groupLeader.name
if name == UNKNOWNOBJECT then return end
local message = config.manualRequestMessage:gsub('{leader}', name)
if config.sendWhisper then SendChatMessage(message, Channel.Whisper, nil, name) end
if config.sendSay then SendChatMessage(message, Channel.Say) end
if config.sendRaid then
local groupType = GetGroupType()
if groupType == GroupType.Raid then SendChatMessage(message, Channel.Raid) end
if groupType == GroupType.InstanceRaid then SendChatMessage(message, Channel.Instance) end
end
end
function Module:RequestRaidLead()
Namespace.QueueTools:ScheduleSendSyncData()
if not Private.CanRequestLead() then return end
if GetMessageDestination() == Channel.Whisper then return end
local mem = Memory.WantBattlegroundLead
if Namespace.Database.profile.BattlegroundTools.WantBattlegroundLead.enableManualRequest then
local groupLeader = GetGroupLeaderData()
if groupLeader and not groupLeader.addonVersion and (mem.ackLeader == nil or groupLeader.name ~= mem.ackLeader) then
-- re-buffer the timer each time the leader changes to give them enough time to reply
if mem.ackTimer then Module:CancelTimer(mem.ackTimer) end
mem.ackLeader = groupLeader.name
mem.ackTimer = Module:ScheduleTimer(Private.SendManualChatMessages, 5)
end
end
end
function Private.ProcessDropDownOptions(onPlayerSelected)
local mem = Memory.WantBattlegroundLead
local found = 0
local total = 0
local lastData
local players = {}
for name, isChecked in pairs(mem.dropdownSelection) do
local data = GetPlayerDataByName(name)
if data and data.wantLead then
found = found + 1
lastData = data
if isChecked then
total = total + 1
players[total] = data
end
end
end
if found == 1 and total == 0 then
-- automatically select the only option without having to select it
players[1] = lastData
end
for _, data in pairs(players) do
if onPlayerSelected(data) then break end
end
end
function Private.AcceptManualBattlegroundLeaderRequest()
local mem = Memory.WantBattlegroundLead
local remember = mem.DialogFrame.RememberNameCheckbox:GetChecked()
Private.ProcessDropDownOptions(function (data)
if remember then Module:SetPlayerConfigValue(data.name, 'giveLeadBehavior', GiveLeadBehavior.GiveLead) end
Private.PromoteToLeader(data)
return false -- only ever attempt once
end)
Module:TriggerUpdateWantBattlegroundLeadDialogFrame()
end
function Private.RejectManualBattlegroundLeaderRequest()
local mem = Memory.WantBattlegroundLead
local remember = mem.DialogFrame.RememberNameCheckbox:GetChecked()
Private.ProcessDropDownOptions(function (data)
mem.giveLeadStatus[data.name] = GiveLeadStatus.RecentlyRejected
if remember then Module:SetPlayerConfigValue(data.name, 'giveLeadBehavior', GiveLeadBehavior.RejectLead) end
return false
end)
Module:TriggerUpdateWantBattlegroundLeadDialogFrame()
end
function Private.UpdateAcceptRejectButtonState()
local frame = Memory.WantBattlegroundLead.DialogFrame
if not frame then return end
if GetPlayerDataByUnit('player').role ~= Role.Leader then
frame.AcceptButton:SetEnabled(false)
frame.RejectButton:SetEnabled(false)
return
end
local possibilities = 0
Private.ProcessDropDownOptions(function ()
possibilities = possibilities + 1
if possibilities > 1 then return true end
end)
frame.AcceptButton:SetEnabled(possibilities == 1)
frame.RejectButton:SetEnabled(possibilities > 0)
end
function Private.InitializeBattlegroundLeaderDialog()
local dialog = CreateFrame('Frame', 'BgcBattlegroundLeaderDialog', _G.UIParent, _G.BackdropTemplateMixin and 'BackdropTemplate')
dialog:SetSize(320, 160)
dialog:SetFrameStrata('DIALOG')
dialog:SetPoint('TOP', _G.UIParent, 'TOP', 0, -300)
dialog:SetMovable(true)
dialog:SetClampedToScreen(true)
dialog:SetBackdrop({
bgFile = LSM:Fetch('background', 'Blizzard Dialog Background Dark'),
edgeFile = LSM:Fetch('border', 'Blizzard Dialog'),
tile = true,
tileSize = 20,
edgeSize = 20,
insets = {
left = 5,
right = 5,
top = 5,
bottom = 5,
}
})
dialog:SetBackdropColor(0.5, 0.5, 0.5, 1)
dialog:SetBackdropBorderColor(0.7, 0.7, 0.7, 1)
dialog:SetScript('OnMouseDown', function(self) self:StartMoving() end)
dialog:SetScript('OnMouseUp', function(self) self:StopMovingOrSizing() end)
tinsert(UISpecialFrames, dialog:GetName());
local dialogText = dialog:CreateFontString(nil, 'ARTWORK', 'GameFontNormalLarge')
dialogText:SetText(L['Lead Requested'])
dialogText:SetPoint('TOP', 0, -12)
dialogText:SetPoint('LEFT', 0, 0)
dialogText:SetPoint('RIGHT', 0, 0)
local dropdown = LibDD:Create_UIDropDownMenu('BgcSelectLeaderDropDown', dialog)
dropdown:SetPoint('CENTER', dialog, 'CENTER', 0, 10)
function dropdown:OnSelect(name, _, checked)
Memory.WantBattlegroundLead.dropdownSelection[name] = checked
Private.UpdateAcceptRejectButtonState()
end
local acceptButton = CreateFrame('Button', nil, dialog, 'UIPanelButtonTemplate')
acceptButton:SetSize(110, 24)
acceptButton:SetText('Accept')
acceptButton:SetPoint('BOTTOMRIGHT', dialog, 'BOTTOM', -2, 8)
acceptButton:SetScript('OnClick', Private.AcceptManualBattlegroundLeaderRequest)
local rejectButton = CreateFrame('Button', nil, dialog, 'UIPanelButtonTemplate')
rejectButton:SetSize(110, 24)
rejectButton:SetText('Reject')
rejectButton:SetPoint('BOTTOMLEFT', dialog, 'BOTTOM', 2, 8)
rejectButton:SetScript('OnClick', Private.RejectManualBattlegroundLeaderRequest)
local checkbox = CreateFrame('CheckButton', nil, dialog, 'UICheckButtonTemplate')
local checkboxLabel = checkbox:CreateFontString(nil, 'ARTWORK', 'GameFontNormal')
checkbox:SetPoint('RIGHT', checkboxLabel, 'LEFT', 0, 0)
checkbox:SetSize(24, 24)
checkboxLabel:SetText('Remember this choice')
checkboxLabel:SetPoint('BOTTOM', dialog, 'BOTTOM', 0, 42)
local closeButton = CreateFrame('Button', nil, dialog, 'UIPanelCloseButton')
closeButton:SetPoint('TOPRIGHT', dialog, 'TOPRIGHT', -2, -2)
dialog.Text = dialogText
dialog.Dropdown = dropdown
dialog.AcceptButton = acceptButton
dialog.RejectButton = rejectButton
dialog.RememberNameCheckbox = checkbox
dialog.closeButton = closeButton
Memory.WantBattlegroundLead.DialogFrame = dialog
dialog:Hide()
end
function Module:SetWantLeadSetting(key, table)
Namespace.Database.profile.BattlegroundTools.WantBattlegroundLead[key] = table
end
function Module:GetWantLeadSetting(key)
return Namespace.Database.profile.BattlegroundTools.WantBattlegroundLead[key]
end
function Module:SetLeaderToolsSetting(key, value)
local config = Namespace.Database.profile.BattlegroundTools.LeaderTools
if key == 'leaderIcon' then
-- swap around values
local oldValue = config.leaderIcon
config.availableIcons[oldValue] = config.availableIcons[value]
config.availableIcons[value] = false
Memory.iconSlots = {}
Private.MarkRaidMembersIfLeadingBattleground()
end
config[key] = value
end
function Module:GetLeaderToolsSetting(key)
return Namespace.Database.profile.BattlegroundTools.LeaderTools[key]
end
function Module:SetMarkerIndexSetting(markerIndex, value)
local config = Namespace.Database.profile.BattlegroundTools.LeaderTools
if value and config.leaderIcon == markerIndex then return end
config.availableIcons[markerIndex] = value
end
function Module:GetMarkerIndexSetting(markerIndex)
return Namespace.Database.profile.BattlegroundTools.LeaderTools.availableIcons[markerIndex]
end
function Module:GetPlayerConfig(playerName)
return Namespace.Database.profile.BattlegroundTools.PlayerManagement[playerName]
end
function Module:GetPlayerConfigValue(playerName, configName)
local config = Namespace.Database.profile.BattlegroundTools.PlayerManagement[playerName]
if not config then return nil end
return config[configName]
end
function Module:SetPlayerConfigValue(playerName, configName, value)
local config = Namespace.Database.profile.BattlegroundTools.PlayerManagement[playerName] or self:CreatePlayerConfig(playerName)
config[configName] = value
if configName == 'markBehavior' or configName == 'preferredIcon' then
Memory.iconSlots = {}
Private.MarkRaidMembersIfLeadingBattleground()
end
end
function Module:CreatePlayerConfig(playerName)
local config = Namespace.Database.profile.BattlegroundTools
config.playerManagementIndex = config.playerManagementIndex + 1
local playerConfig = {
playerName = playerName,
playerNickname = '',
groupLabel = playerName,
giveLeadBehavior = GiveLeadBehavior.NoAutomation,
markBehavior = MarkBehavior.NoMark,
promoteToAssistant = false,
preferredIcon = RaidIconIndex.NoIcon,
sortOrderIndex = config.playerManagementIndex
}
config.PlayerManagement[playerName] = playerConfig
return playerConfig
end
function Module:DeletePlayerConfig(playerName)
Namespace.Database.profile.BattlegroundTools.PlayerManagement[playerName] = nil
end
function Module:GetAllPlayerConfig()
return Namespace.Database.profile.BattlegroundTools.PlayerManagement
end