Files

74 lines
3.3 KiB
Lua

-- OctoTextSize: addon-local factory; Blizzard CreateFrame is unchanged.
local CreateFrame = OctoTextSize and OctoTextSize.WrapFactory(CreateFrame) or CreateFrame
-- Octo TW_UI service: source player GUID (hex), then a decimal challenge mask.
-- The gateway maps RESPONSE_PLAYER_CHALLENGES to GW_CHALLENGES (16-byte limit).
local prefix = "GW_CHALLENGES"
local cache, pending = {}, {}
local names = { "Slow & Steady", "Exhaustion", "War Mode", "Hardcore", "Vagrant",
"Boaring Adventure", "Lunatic", "Craftmaster", "Brewmaster", "Heroic",
"Samurai", "Together Forever" }
local function readable(v) return not issecretvalue or not issecretvalue(v) end
local function sourceGuid(unit)
local guid = UnitGUID(unit)
if not readable(guid) or type(guid) ~= "string" then return end
local low = guid:match("^Player%-%d+%-(%x+)$")
if low and #low <= 8 then return "0x" .. string.rep("0", 16 - #low) .. low:upper() end
end
local function parse(text)
if not readable(text) or type(text) ~= "string" then return end
local hex, mask = text:match("^0[xX](%x+):(%d+)$")
if not hex or #hex ~= 16 or hex:sub(1, 8) ~= "00000000" then return end
mask = tonumber(mask)
if not mask or mask > 4294967295 then return end
return "0x" .. hex:upper(), mask
end
local shown
local function addLines(tooltip, guid, mask)
if shown == guid or mask == 0 then return end
local header = false
for i, name in ipairs(names) do
if math.floor(mask / 2^(i-1)) % 2 == 1 then
if not header then tooltip:AddLine("Active Challenges:", 1, 0.82, 0); header = true end
tooltip:AddLine(name, 1, 1, 1)
end
end
shown = guid
if header then tooltip:Show() end
end
local function tooltipUnit(tooltip)
local _, unit = tooltip:GetUnit()
if readable(unit) and type(unit) == "string" then return sourceGuid(unit) end
end
local function update(tooltip)
if tooltip ~= GameTooltip then return end
local guid = tooltipUnit(tooltip)
if not guid then return end
local now = GetTime()
local entry = cache[guid]
if entry and now - entry.time < 30 then addLines(tooltip, guid, entry.mask); return end
if pending[guid] and now - pending[guid] < 5 then return end
local owner = UnitName("player")
if not readable(owner) or type(owner) ~= "string" then return end
pending[guid] = now
C_ChatInfo.SendAddonMessage("TW_UI", "REQUEST_PLAYER_CHALLENGES;" .. guid, "WHISPER", owner)
end
local frame = CreateFrame("Frame")
frame:RegisterEvent("CHAT_MSG_ADDON")
frame:RegisterEvent("PLAYER_ENTERING_WORLD")
frame:SetScript("OnEvent", function(_, event, incoming, text)
if event == "PLAYER_ENTERING_WORLD" then cache = {}; pending = {}; shown = nil; return end
if incoming ~= prefix then return end
local guid, mask = parse(text)
if not guid or not pending[guid] then return end
pending[guid] = nil
cache[guid] = { mask = mask, time = GetTime() }
if GameTooltip:IsShown() and tooltipUnit(GameTooltip) == guid then addLines(GameTooltip, guid, mask) end
end)
C_ChatInfo.RegisterAddonMessagePrefix(prefix)
GameTooltip:HookScript("OnTooltipCleared", function() shown = nil end)
if TooltipDataProcessor and Enum and Enum.TooltipDataType then
TooltipDataProcessor.AddTooltipPostCall(Enum.TooltipDataType.Unit, update)
else
GameTooltip:HookScript("OnTooltipSetUnit", update)
end