-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComm.lua
More file actions
163 lines (145 loc) · 5.57 KB
/
Copy pathComm.lua
File metadata and controls
163 lines (145 loc) · 5.57 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
local Comm = {}
CrieffList.Comm = Comm
local PREFIX = "CrieffList"
local PROTO = "v1"
local BROADCAST_THROTTLE = 5
local lastBroadcast = 0
local keystoneLib
local openRaidLib
local keystoneToken = {}
local function GuidForName(name)
if not name then return nil end
local short = Ambiguate(name, "short")
local guid = UnitGUID(short)
if guid then return guid, short end
if not IsInGroup() then return nil, short end
local prefix = IsInRaid() and "raid" or "party"
local count = GetNumGroupMembers()
for i = 1, count do
local unit = prefix .. i
local uname = GetUnitName(unit, true)
if uname == name or uname == short or Ambiguate(uname or "", "short") == short then
return UnitGUID(unit), short
end
end
return nil, short
end
local function InMyParty(sender)
if not sender then return false end
local short = Ambiguate(sender, "short")
if UnitInParty(short) or UnitInRaid(short) then return true end
if short == UnitName("player") then return true end
return false
end
function Comm.BroadcastNow()
if not IsInGroup() then return end
local own = CrieffList.Keystones and CrieffList.Keystones.own
if not own or not own.mapID or not own.level then return end
local msg = string.format("%s|KEY|%d|%d", PROTO, own.mapID, own.level)
pcall(C_ChatInfo.SendAddonMessage, PREFIX, msg, "PARTY")
lastBroadcast = GetTime()
end
function Comm.BroadcastOwn()
local now = GetTime()
if now - lastBroadcast < BROADCAST_THROTTLE then return end
Comm.BroadcastNow()
end
function Comm.RequestParty()
if not IsInGroup() then return end
local msg = PROTO .. "|REQ"
pcall(C_ChatInfo.SendAddonMessage, PREFIX, msg, "PARTY")
if keystoneLib and keystoneLib.Request then
pcall(keystoneLib.Request, "PARTY")
end
if openRaidLib and openRaidLib.RequestKeystoneDataFromParty then
pcall(openRaidLib.RequestKeystoneDataFromParty)
end
end
local function HandleAddonMessage(prefix, message, _, sender)
if prefix ~= PREFIX then return end
if not InMyParty(sender) then return end
if Ambiguate(sender, "short") == UnitName("player") then return end
local proto, kind, a, b = strsplit("|", message)
if proto ~= PROTO then return end
if kind == "KEY" then
local mapID = tonumber(a)
local level = tonumber(b)
if not mapID or not level then return end
local guid, short = GuidForName(sender)
if guid and CrieffList.Keystones then
CrieffList.Keystones.UpsertParty(guid, short, mapID, level, "comm")
end
elseif kind == "REQ" then
Comm.BroadcastNow()
end
end
local function OnLibKeystone(keyLevel, keyMap, _playerRating, playerName, channel)
if channel ~= "PARTY" then return end
if not playerName or not keyMap or not keyLevel then return end
if keyMap == 0 or keyLevel == 0 then return end
if Ambiguate(playerName, "short") == UnitName("player") then return end
local guid, short = GuidForName(playerName)
if not guid then return end
if CrieffList.Keystones then
CrieffList.Keystones.UpsertParty(guid, short or playerName, keyMap, keyLevel, "LibKeystone")
end
end
local function IngestOpenRaidInfo(unitName, info)
if not unitName or type(info) ~= "table" then return end
local mapID = info.mythicPlusMapID or info.challengeMapID or info.mapID
local level = info.level
if not mapID or not level or mapID == 0 or level == 0 then return end
local guid, short = GuidForName(unitName)
if not guid then return end
local existing = CrieffList.Keystones and CrieffList.Keystones.party[guid]
if existing and existing.source == "LibKeystone" then return end
if CrieffList.Keystones then
CrieffList.Keystones.UpsertParty(guid, short or unitName, mapID, level, "LibOpenRaid")
end
end
function CrieffList.OnLibOpenRaidKeystoneUpdate(unitName, keystoneInfo)
IngestOpenRaidInfo(unitName, keystoneInfo)
end
function CrieffList.OnLibOpenRaidKeystoneWipe()
if not CrieffList.Keystones then return end
for guid, entry in pairs(CrieffList.Keystones.party) do
if entry.source == "LibOpenRaid" then
CrieffList.Keystones.party[guid] = nil
end
end
end
local function SeedFromLibOpenRaid()
if not openRaidLib or not openRaidLib.GetAllKeystonesInfo then return end
local ok, all = pcall(openRaidLib.GetAllKeystonesInfo)
if not ok or type(all) ~= "table" then return end
local me = UnitName("player")
for unitName, info in pairs(all) do
if unitName ~= me then
IngestOpenRaidInfo(unitName, info)
end
end
end
function Comm.Init()
pcall(C_ChatInfo.RegisterAddonMessagePrefix, PREFIX)
CrieffList:RegisterEvent("CHAT_MSG_ADDON", HandleAddonMessage)
if LibStub then
local kok, klib = pcall(LibStub, "LibKeystone", true)
if kok and klib and klib.Register then
keystoneLib = klib
pcall(klib.Register, keystoneToken, OnLibKeystone)
if klib.Request then
pcall(klib.Request, "PARTY")
end
end
local ook, olib = pcall(LibStub, "LibOpenRaid-1.0", true)
if ook and olib then
openRaidLib = olib
pcall(olib.RegisterCallback, CrieffList, "KeystoneUpdate", "OnLibOpenRaidKeystoneUpdate")
pcall(olib.RegisterCallback, CrieffList, "KeystoneWipe", "OnLibOpenRaidKeystoneWipe")
SeedFromLibOpenRaid()
if olib.RequestKeystoneDataFromParty then
pcall(olib.RequestKeystoneDataFromParty)
end
end
end
end