-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCore.lua
More file actions
1660 lines (1524 loc) · 44.6 KB
/
Copy pathCore.lua
File metadata and controls
1660 lines (1524 loc) · 44.6 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
local ADDON_NAME, Addon = ...
local L = Addon.L
Addon.ADDON_NAME = ADDON_NAME
Addon.CUSTOM_PREFIX = "KSWheel1"
Addon.LIB_PREFIX = "LibKS"
Addon.entries = {}
Addon.roster = {}
Addon.rosterOrder = {}
Addon.mapCache = {}
Addon.peerStates = {}
Addon.drawnCycle = {}
local WHEEL_PROTOCOL_VERSION = "1"
local FATE_LOCK_SECONDS = 30
local REROLL_VOTE_SECONDS = 12
local PEER_STATE_MAX_AGE = 75
local MAX_WHEEL_KEYS = 5
Addon.FATE_LOCK_SECONDS = FATE_LOCK_SECONDS
local DUNGEON_TELEPORTS = {
[161] = 159898, -- Skyreach
[239] = 1254551, -- Seat of the Triumvirate
[402] = 393273, -- Algeth'ar Academy
[556] = 1254555, -- Pit of Saron
[557] = 1254400, -- Windrunner Spire
[558] = 1254572, -- Magisters' Terrace
[559] = 1254563, -- Nexus-Point Xenas
[560] = 1254559, -- Maisara Caverns
}
local SOURCE_ORDER = { "self", "addon", "lib", "chat", "manual" }
local SOURCE_LABELS = {
self = L.SOURCE_SELF,
addon = L.SOURCE_ADDON,
lib = L.SOURCE_LIB,
chat = L.SOURCE_CHAT,
manual = L.SOURCE_MANUAL,
}
local eventFrame = CreateFrame("Frame")
Addon.eventFrame = eventFrame
local function SafeNumber(value)
value = tonumber(value)
if not value then
return 0
end
return math.floor(value + 0.5)
end
local function GroupChannel()
if IsInGroup(LE_PARTY_CATEGORY_INSTANCE) then
return "INSTANCE_CHAT"
elseif IsInRaid() then
return "RAID"
elseif IsInGroup() then
return "PARTY"
end
end
local function IsPrefixRegistered(prefix)
local checker = C_ChatInfo and C_ChatInfo.IsAddonMessagePrefixRegistered
or _G.IsAddonMessagePrefixRegistered
return checker and checker(prefix) == true or false
end
local function GetAddonBuild()
if C_AddOns and C_AddOns.GetAddOnMetadata then
return C_AddOns.GetAddOnMetadata(ADDON_NAME, "X-KeystoneWheel-Build")
or C_AddOns.GetAddOnMetadata(ADDON_NAME, "Version")
or "?"
elseif GetAddOnMetadata then
return GetAddOnMetadata(ADDON_NAME, "X-KeystoneWheel-Build")
or GetAddOnMetadata(ADDON_NAME, "Version")
or "?"
end
return "?"
end
local function FullUnitName(unit)
local name = GetUnitName(unit, true)
if not name or name == "" then
name = UnitName(unit)
end
return name
end
local function ShortName(name)
if not name then
return nil
end
return Ambiguate(name, "short")
end
local function NormalizePlayerName(name)
if type(name) ~= "string" or name == "" then
return nil
end
return name:lower():gsub("%s+", "")
end
local function BasePlayerName(name)
if type(name) ~= "string" then
return nil
end
return name:match("^([^-]+)")
end
local function ComparableShortName(name)
local shortName = ShortName(name)
if not shortName or NormalizePlayerName(shortName) == NormalizePlayerName(name) then
return BasePlayerName(name) or shortName
end
return shortName
end
local function DebugValue(value)
if issecretvalue and issecretvalue(value) then
return L.PROTECTED
end
if value == nil then
return "nil"
end
return tostring(value)
end
local function YesNo(value)
return value and L.YES or L.NO
end
local function HashText(value)
local hash = 5381
for index = 1, #value do
hash = ((hash * 33) + value:byte(index)) % 2147483647
end
return ("%08x"):format(hash)
end
function Addon:Print(message)
DEFAULT_CHAT_FRAME:AddMessage(("|cffffc94dKeystoneWheel|r: %s"):format(message))
end
function Addon:DebugLog(formatString, ...)
if not self.db or not self.db.debug then
return
end
local ok, message = pcall(string.format, formatString, ...)
self:Print("|cff61d7ffDEBUG|r " .. (ok and message or formatString))
end
function Addon:SendAddonPayload(prefix, message, channel)
if not channel or not IsPrefixRegistered(prefix) then
return false
end
local ok, sent = pcall(C_ChatInfo.SendAddonMessage, prefix, message, channel)
return ok and sent ~= false
end
function Addon:GetSourceLabel(source)
return SOURCE_LABELS[source] or source or L.UNKNOWN
end
function Addon:NewMessageID()
return ("%d-%04d"):format(GetServerTime(), math.random(0, 9999))
end
function Addon:IsOwnSender(sender)
if type(sender) ~= "string" or sender == "" then
return false
end
local playerName = FullUnitName("player")
if not playerName then
return false
end
if NormalizePlayerName(sender) == NormalizePlayerName(playerName) then
return true
end
local senderShortName = ComparableShortName(sender)
local playerShortName = ComparableShortName(playerName)
return senderShortName and playerShortName
and NormalizePlayerName(senderShortName) == NormalizePlayerName(playerShortName)
end
function Addon:CanPlayerSpin()
if not self.db or not self.db.leaderOnly or not IsInGroup() then
return true
end
if UnitIsGroupLeader and UnitIsGroupLeader("player") then
return true
end
if IsInRaid() and UnitIsGroupAssistant and UnitIsGroupAssistant("player") then
return true
end
return false, IsInRaid() and L.LEADER_ONLY_RAID or L.LEADER_ONLY_PARTY
end
function Addon:GetDungeonInfo(mapID)
mapID = SafeNumber(mapID)
local cached = self.mapCache[mapID]
if cached then
return cached.name, cached.texture
end
local name, texture
if mapID > 0 and C_ChallengeMode and C_ChallengeMode.GetMapUIInfo then
name, _, _, texture = C_ChallengeMode.GetMapUIInfo(mapID)
end
if name then
self.mapCache[mapID] = { name = name, texture = texture or 134400 }
return name, texture or 134400
end
return L.DUNGEON_FALLBACK:format(mapID), 134400
end
function Addon:GetDungeonTeleportSpell(mapID)
local spellID = DUNGEON_TELEPORTS[SafeNumber(mapID)]
if not spellID then
return nil, false
end
local known = false
if C_SpellBook and C_SpellBook.IsSpellKnownOrInSpellBook then
known = C_SpellBook.IsSpellKnownOrInSpellBook(spellID)
elseif C_SpellBook and C_SpellBook.IsSpellKnown then
known = C_SpellBook.IsSpellKnown(spellID)
elseif C_SpellBook and C_SpellBook.IsSpellInSpellBook then
known = C_SpellBook.IsSpellInSpellBook(spellID, nil, true)
elseif IsSpellKnown then
known = IsSpellKnown(spellID)
end
return spellID, known == true
end
function Addon:RefreshRoster()
wipe(self.roster)
wipe(self.rosterOrder)
local units = { "player" }
if IsInRaid() then
for index = 1, GetNumGroupMembers() do
units[#units + 1] = "raid" .. index
end
else
for index = 1, 4 do
units[#units + 1] = "party" .. index
end
end
local addedNames = {}
for _, unit in ipairs(units) do
if UnitExists(unit) then
local fullName = FullUnitName(unit)
local displayName = UnitName(unit) or ShortName(fullName) or fullName
local normalizedName = NormalizePlayerName(fullName)
if fullName and not addedNames[normalizedName] then
addedNames[normalizedName] = true
local info = {
id = fullName,
displayName = displayName,
classFile = select(2, UnitClass(unit)),
unit = unit,
}
self.rosterOrder[#self.rosterOrder + 1] = fullName
self.roster[fullName:lower()] = info
self.roster[normalizedName] = info
self.roster[NormalizePlayerName(displayName)] = info
local ambiguousName = Ambiguate(fullName, "none")
if ambiguousName then
self.roster[ambiguousName:lower()] = info
self.roster[NormalizePlayerName(ambiguousName)] = info
end
end
end
end
local rosterIDs = {}
for _, id in ipairs(self.rosterOrder) do
rosterIDs[id] = true
end
for id, entry in pairs(self.entries) do
if not rosterIDs[id] then
local manual = entry.sources and entry.sources.manual
if manual then
entry.sources = { manual = manual }
else
self.entries[id] = nil
end
end
end
local groupMembers = {}
for _, id in ipairs(self.rosterOrder) do
groupMembers[#groupMembers + 1] = id:lower()
end
table.sort(groupMembers)
local groupSignature = table.concat(groupMembers, ";")
if self.groupSignature and self.groupSignature ~= groupSignature then
wipe(self.drawnCycle)
self.fateLockUntil = nil
self.currentRollID = nil
self.pendingVote = nil
end
self.groupSignature = groupSignature
for sender in pairs(self.peerStates) do
if not self:IsCurrentGroupMember(sender) then
self.peerStates[sender] = nil
end
end
self:UpdateOwnKey(false)
self:RefreshUI()
self:ScheduleWheelStateBroadcast()
end
function Addon:ResolvePlayer(name)
name = strtrim(name or "")
if name == "" then
return nil
end
local info = self.roster[name:lower()] or self.roster[NormalizePlayerName(name)]
if not info then
local short = ComparableShortName(name)
info = short and (
self.roster[short:lower()]
or self.roster[NormalizePlayerName(short)]
)
end
if info then
return info.id, info.displayName, info.classFile, true
end
return "manual:" .. name:lower(), ShortName(name) or name, nil, false
end
function Addon:UpdateEntry(playerName, level, mapID, source, rating)
local id, displayName, classFile, inRoster = self:ResolvePlayer(playerName)
if not id then
self:DebugLog(L.DEBUG_ENTRY_NO_ID, source or "?")
return false
end
if source ~= "manual" and not inRoster then
self:DebugLog(L.DEBUG_NOT_IN_ROSTER, source or "?", displayName or "?")
return false
end
level = SafeNumber(level)
mapID = SafeNumber(mapID)
rating = SafeNumber(rating)
local entry = self.entries[id]
if not entry then
entry = { id = id, sources = {} }
self.entries[id] = entry
end
entry.displayName = displayName
entry.classFile = classFile or entry.classFile
entry.sources[source] = {
level = level,
mapID = mapID,
rating = rating,
source = source,
seen = GetTime(),
}
self:DebugLog(L.DEBUG_KEY_REPORTED, source, displayName, level, mapID)
self:RefreshUI()
self:ScheduleWheelStateBroadcast()
return true
end
function Addon:GetBestData(entry)
local newestNoKey
for _, source in ipairs(SOURCE_ORDER) do
local data = entry.sources[source]
if data then
if data.level > 0 and data.mapID > 0 then
if not newestNoKey or data.seen > newestNoKey then
return data
end
return nil
end
newestNoKey = math.max(newestNoKey or 0, data.seen or 0)
end
end
end
function Addon:GetIgnoreKey(id, level, mapID)
return ("%s|%d|%d"):format(tostring(id):lower(), SafeNumber(mapID), SafeNumber(level))
end
function Addon:GetEntrySignature(entry)
if not entry then
return nil
end
return self:GetIgnoreKey(entry.id or entry.displayName or "?", entry.level, entry.mapID)
end
function Addon:IsEntryDrawn(entry)
local signature = self:GetEntrySignature(entry)
return signature and self.drawnCycle[signature] == true or false
end
function Addon:MarkEntryDrawn(entry)
local signature = self:GetEntrySignature(entry)
if signature then
self.drawnCycle[signature] = true
end
end
function Addon:GetSpinEligibleIndices(entries, resetCycle)
local eligible, allowed = {}, {}
local cycleReset = false
for index, entry in ipairs(entries or {}) do
if not entry.ignored then
allowed[#allowed + 1] = index
if not self.db.noRepeat or not self:IsEntryDrawn(entry) then
eligible[#eligible + 1] = index
end
end
end
if self.db.noRepeat and #allowed > 0 and #eligible == 0 and resetCycle then
wipe(self.drawnCycle)
cycleReset = true
for _, entry in ipairs(entries or {}) do
entry.drawn = false
end
for _, index in ipairs(allowed) do
eligible[#eligible + 1] = index
end
self:Print(L.NO_REPEAT_NEW_ROUND)
end
return eligible, allowed, cycleReset
end
function Addon:AddResultHistory(entry, rollID, selectedBy)
if not entry or not self.db or type(self.db.history) ~= "table" then
return
end
if rollID and self.db.history[1] and self.db.history[1].rollID == rollID then
return
end
table.insert(self.db.history, 1, {
rollID = rollID,
displayName = entry.displayName,
level = SafeNumber(entry.level),
mapID = SafeNumber(entry.mapID),
dungeonName = entry.dungeonName,
selectedBy = selectedBy and (ShortName(selectedBy) or selectedBy) or nil,
time = GetServerTime(),
})
while #self.db.history > 3 do
table.remove(self.db.history)
end
end
function Addon:ClearResultHistory()
wipe(self.db.history)
wipe(self.drawnCycle)
self:Print(L.HISTORY_RESET_DONE)
self:RefreshUI()
end
function Addon:IsKeyIgnored(entry)
if not entry or not self.db or type(self.db.ignoredKeys) ~= "table" then
return false
end
return self.db.ignoredKeys[self:GetIgnoreKey(entry.id, entry.level, entry.mapID)] == true
end
function Addon:ToggleKeyIgnored(entry)
if not entry or self.spinning then
return
end
local ignoreKey = self:GetIgnoreKey(entry.id, entry.level, entry.mapID)
local ignored = not self.db.ignoredKeys[ignoreKey]
self.db.ignoredKeys[ignoreKey] = ignored or nil
self:Print(L.IGNORE_TOGGLE:format(
entry.displayName,
entry.level,
entry.dungeonName,
ignored and L.IGNORE_ON or L.IGNORE_OFF
))
self:RefreshUI()
end
function Addon:GetActiveKeys()
local result, added = {}, {}
local function AddEntry(id)
local entry = Addon.entries[id]
local data = entry and Addon:GetBestData(entry)
if not data then
return
end
local dungeonName, texture = Addon:GetDungeonInfo(data.mapID)
local activeEntry = {
id = id,
displayName = entry.displayName,
classFile = entry.classFile,
level = data.level,
mapID = data.mapID,
rating = data.rating,
source = data.source,
dungeonName = dungeonName,
texture = texture,
}
activeEntry.ignored = Addon:IsKeyIgnored(activeEntry)
result[#result + 1] = activeEntry
added[id] = true
end
for _, id in ipairs(self.rosterOrder) do
AddEntry(id)
end
local extra = {}
for id, entry in pairs(self.entries) do
if not added[id] and entry.sources.manual then
extra[#extra + 1] = id
end
end
table.sort(extra, function(left, right)
return (Addon.entries[left].displayName or left) < (Addon.entries[right].displayName or right)
end)
for _, id in ipairs(extra) do
AddEntry(id)
end
return result
end
function Addon:GetWheelPoolState()
local signatures = {}
for index, entry in ipairs(self:GetActiveKeys()) do
if index > MAX_WHEEL_KEYS then
break
end
signatures[#signatures + 1] = self:GetEntrySignature(entry)
end
table.sort(signatures)
return HashText(table.concat(signatures, ";")), #signatures
end
function Addon:PrunePeerStates()
local now = GetTime()
for sender, state in pairs(self.peerStates) do
local reason
if not self:IsCurrentGroupMember(sender) then
reason = "not-in-roster"
elseif not state.seen then
reason = "missing-timestamp"
elseif now - state.seen > PEER_STATE_MAX_AGE then
reason = "expired"
end
if reason then
self:DebugLog(L.DEBUG_SYNC_PRUNED, state.name or sender, reason)
self.peerStates[sender] = nil
end
end
end
function Addon:TouchPeerState(sender, transport)
local peerKey = NormalizePlayerName(sender) or sender:lower()
local state = self.peerStates[peerKey]
local isNew = state == nil
if not state then
state = {
name = sender,
transports = {},
}
self.peerStates[peerKey] = state
end
state.name = sender
state.seen = GetTime()
state.transports = state.transports or {}
state.transports[transport or L.UNKNOWN] = true
return state, isNew
end
function Addon:StorePeerPoolState(sender, transport, hash, count, build)
local state = self:TouchPeerState(sender, transport)
state.hash = hash
state.count = math.floor(count)
state.version = build
self:DebugLog(
L.DEBUG_SYNC_ACCEPTED,
sender,
transport or L.UNKNOWN,
build,
hash,
state.count
)
self:RefreshUI()
end
function Addon:GetSyncStatus()
self:PrunePeerStates()
local ownHash, ownCount = self:GetWheelPoolState()
local matching, total = 1, 1
for _, state in pairs(self.peerStates) do
total = total + 1
if state.hash == ownHash and state.count == ownCount then
matching = matching + 1
end
end
return matching, total, ownHash, ownCount
end
function Addon:GetSyncDetails()
self:PrunePeerStates()
local ownHash, ownCount = self:GetWheelPoolState()
local details = {}
for sender, state in pairs(self.peerStates) do
local transports = {}
for prefix in pairs(state.transports or {}) do
transports[#transports + 1] = prefix
end
table.sort(transports)
details[#details + 1] = {
name = ShortName(state.name or sender) or state.name or sender,
matches = state.hash == ownHash and state.count == ownCount,
count = state.count,
version = state.version,
transport = #transports > 0 and table.concat(transports, " + ") or nil,
hasState = state.hash ~= nil and state.count ~= nil,
}
end
table.sort(details, function(left, right)
return left.name < right.name
end)
return details, ownCount
end
function Addon:SendWheelCommand(kind, ...)
local channel = GroupChannel()
if not channel then
return false
end
local fields = { "KW", WHEEL_PROTOCOL_VERSION, kind }
for index = 1, select("#", ...) do
local value = tostring(select(index, ...) or ""):gsub(";", "")
fields[#fields + 1] = value
end
local payload = table.concat(fields, ";")
local directSent = self:SendAddonPayload(self.CUSTOM_PREFIX, payload, channel)
local fallbackSent = self:SendAddonPayload(self.LIB_PREFIX, payload, channel)
return directSent or fallbackSent
end
function Addon:BroadcastWheelState(force)
if not IsInGroup() then
return
end
local hash, count = self:GetWheelPoolState()
local stateKey = hash .. ":" .. count
local now = GetTime()
if not force and self.lastBroadcastState == stateKey
and self.lastStateBroadcastAt
and now - self.lastStateBroadcastAt < 8 then
return
end
local build = GetAddonBuild()
if self:SendWheelCommand("STATE", hash, count, build) then
self.lastBroadcastState = stateKey
self.lastStateBroadcastAt = now
end
end
function Addon:ScheduleWheelStateBroadcast(force)
if not self.initialized or not IsInGroup() then
return
end
if self.stateBroadcastTimer then
if force then
self.pendingForcedStateBroadcast = true
end
return
end
self.pendingForcedStateBroadcast = force == true
self.stateBroadcastTimer = C_Timer.NewTimer(0.45, function()
Addon.stateBroadcastTimer = nil
local shouldForce = Addon.pendingForcedStateBroadcast
Addon.pendingForcedStateBroadcast = nil
Addon:BroadcastWheelState(shouldForce)
end)
end
function Addon:RequestWheelStates()
if not IsInGroup() then
return
end
local hash, count = self:GetWheelPoolState()
self:SendWheelCommand("SYNCREQ", self:NewMessageID(), hash, count, GetAddonBuild())
self:ScheduleWheelStateBroadcast(true)
end
function Addon:GetOwnKeyInfo()
local level = C_MythicPlus.GetOwnedKeystoneLevel() or 0
local mapID = C_MythicPlus.GetOwnedKeystoneChallengeMapID() or 0
local rating = 0
if C_PlayerInfo and C_PlayerInfo.GetPlayerMythicPlusRatingSummary then
local summary = C_PlayerInfo.GetPlayerMythicPlusRatingSummary("player")
if summary then
rating = summary.currentSeasonScore or 0
end
end
return SafeNumber(level), SafeNumber(mapID), SafeNumber(rating)
end
function Addon:UpdateOwnKey(shouldBroadcast)
local playerName = FullUnitName("player")
if not playerName then
return
end
local level, mapID, rating = self:GetOwnKeyInfo()
local changed = level ~= self.ownKeyLevel or mapID ~= self.ownKeyMapID
self.ownKeyLevel = level
self.ownKeyMapID = mapID
self:UpdateEntry(playerName, level, mapID, "self", rating)
if shouldBroadcast and changed then
self:BroadcastOwnKey()
end
end
function Addon:BroadcastDirectKey(channel)
channel = channel or GroupChannel()
if not IsInGroup() or not channel then
return false
end
local level, mapID, rating = self:GetOwnKeyInfo()
local message = ("KEY;%d;%d;%d"):format(level, mapID, rating)
return self:SendAddonPayload(self.CUSTOM_PREFIX, message, channel)
end
function Addon:BroadcastLibKey()
if not IsInGroup() or IsInRaid() then
return false
end
local level, mapID, rating = self:GetOwnKeyInfo()
local message = ("%d,%d,%d"):format(level, mapID, rating)
return self:SendAddonPayload(self.LIB_PREFIX, message, "PARTY")
end
function Addon:BroadcastOwnKey(channel)
channel = channel or GroupChannel()
if not IsInGroup() or not channel then
return false
end
local directSent = self:BroadcastDirectKey(channel)
local fallbackSent = channel == "PARTY" and self:BroadcastLibKey() or false
return directSent or fallbackSent
end
function Addon:TryAttachLibKeystone()
if self.libRegistered or not _G.LibStub then
return
end
local lib = _G.LibStub("LibKeystone", true)
if not lib or not lib.Register then
return
end
self.libKeystone = lib
lib.Register(self, function(level, mapID, rating, playerName, channel)
if channel == "PARTY" then
Addon:UpdateEntry(playerName, level, mapID, "lib", rating)
end
end)
self.libRegistered = true
self:DebugLog(L.DEBUG_LIB_CALLBACK)
end
function Addon:RequestLibKeystone()
if not IsInGroup() or IsInRaid() then
return
end
self:TryAttachLibKeystone()
if self.libKeystone and self.libKeystone.Request then
self.libKeystone.Request("PARTY")
else
self:SendAddonPayload(self.LIB_PREFIX, "R", "PARTY")
end
end
function Addon:RequestDirectKeys(channel)
channel = channel or GroupChannel()
if IsInGroup() and channel then
self:SendAddonPayload(self.CUSTOM_PREFIX, "REQUEST", channel)
end
end
function Addon:RequestAll(force)
local now = GetTime()
if not force and self.lastRequest and now - self.lastRequest < 3 then
return
end
self.lastRequest = now
self:UpdateOwnKey(false)
local channel = GroupChannel()
self:DebugLog(L.DEBUG_REQUEST_CHANNEL, channel or L.NONE)
if IsInGroup() then
self:BroadcastOwnKey(channel)
end
self:RequestDirectKeys(channel)
self:RequestLibKeystone()
self:RequestWheelStates()
self:RefreshUI()
end
function Addon:ParseKeystoneLink(message)
if issecretvalue and issecretvalue(message) then
return nil
end
if type(message) ~= "string" then
return nil
end
local payload = message:match("|Hkeystone:([^|]+)|h") or message:match("keystone:([%d:]+)")
if not payload then
return nil
end
local _, mapID, level = strsplit(":", payload)
mapID, level = tonumber(mapID), tonumber(level)
if not mapID or not level or mapID <= 0 or level <= 0 then
return nil
end
return level, mapID
end
function Addon:AddManualKey(playerName, link)
playerName = strtrim(playerName or "")
if playerName == "" then
return false, L.ERROR_PLAYER_REQUIRED
end
local level, mapID = self:ParseKeystoneLink(link)
if not level then
return false, L.ERROR_KEYSTONE_LINK
end
self:UpdateEntry(playerName, level, mapID, "manual", 0)
return true
end
function Addon:RemoveFallback(id, source)
local entry = self.entries[id]
if not entry or (source ~= "chat" and source ~= "manual") then
return
end
entry.sources[source] = nil
if not next(entry.sources) then
self.entries[id] = nil
end
self:RefreshUI()
end
function Addon:ClearFallbacks()
for id, entry in pairs(self.entries) do
entry.sources.chat = nil
entry.sources.manual = nil
if not next(entry.sources) then
self.entries[id] = nil
end
end
self:RefreshUI()
end
function Addon:AskForLinks()
local channel = GroupChannel()
if not channel then
self:Print(L.NO_GROUP)
return
end
SendChatMessage(L.ASK_LINKS_CHAT, channel)
end
function Addon:GetFateLockRemaining()
if not self.fateLockUntil then
return 0
end
local remaining = self.fateLockUntil - GetTime()
if remaining <= 0 then
self.fateLockUntil = nil
self.currentRollID = nil
return 0
end
return remaining
end
function Addon:StartFateLock(rollID, duration)
duration = math.max(0, math.min(SafeNumber(duration), 60))
if duration <= 0 then
self.fateLockUntil = nil
self.currentRollID = rollID
return
end
self.currentRollID = rollID
self.fateLockUntil = GetTime() + duration
end
function Addon:GetPendingVoteCount()
local vote = self.pendingVote
if not vote then
return 0, 0
end
local count = 0
for _ in pairs(vote.votes) do
count = count + 1
end
return count, vote.required
end
function Addon:RequestRerollVote()
local remaining = self:GetFateLockRemaining()
if remaining <= 0 then
self:RefreshUI()
return
end
if self.pendingVote and self.pendingVote.expires > GetTime() then
local count, required = self:GetPendingVoteCount()
self:Print(L.VOTE_ALREADY_RUNNING:format(count, required))
return
end
local _, addonUsers = self:GetSyncStatus()
if addonUsers < 2 then
self:Print(L.FATE_NO_PEER:format(
math.ceil(remaining)
))
return
end
local voteID = self:NewMessageID()
local required = math.max(2, math.floor(addonUsers / 2) + 1)
self.pendingVote = {
id = voteID,
required = required,
expires = GetTime() + REROLL_VOTE_SECONDS,
votes = { player = true },
}
self:SendWheelCommand("VOTE", voteID, required)
self:Print(L.VOTE_STARTED:format(required))
self:RefreshUI()
C_Timer.After(REROLL_VOTE_SECONDS + 0.2, function()
if Addon.pendingVote and Addon.pendingVote.id == voteID then
Addon.pendingVote = nil
Addon:Print(L.VOTE_EXPIRED)
Addon:RefreshUI()
end
end)
end
function Addon:SendRerollVoteYes(voteID)
if type(voteID) ~= "string" or not voteID:match("^[%w%-]+$") then
return
end
self:SendWheelCommand("VOTEYES", voteID)
self:Print(L.VOTE_SENT)
end
function Addon:AcceptRerollVote(sender, voteID)
local vote = self.pendingVote
if not vote or vote.id ~= voteID or vote.expires <= GetTime() then
return
end
vote.votes[NormalizePlayerName(sender) or sender:lower()] = true
local count, required = self:GetPendingVoteCount()
self:RefreshUI()
if count < required then
return
end
self.pendingVote = nil
self.fateLockUntil = nil
self:SendWheelCommand("UNLOCK", voteID)
self:Print(L.VOTE_MAJORITY:format(count, required))
self:RefreshUI()
C_Timer.After(0.15, function()
Addon:Spin(true)
end)
end
function Addon:AnnounceWinner(entry)
local message = L.WINNER_MESSAGE:format(entry.displayName, entry.level, entry.dungeonName)
local channel = GroupChannel()
if self.db.announce and channel then
SendChatMessage("KeystoneWheel: " .. message, channel)
else
self:Print(message)
end
end
function Addon:GetSyncedResultKey(sender, winner)
local winnerName = winner and (winner.id or winner.displayName)
local winnerShortName = ComparableShortName(winnerName) or winnerName or L.UNKNOWN
return table.concat({
NormalizePlayerName(sender) or tostring(sender):lower(),