Files

400 lines
17 KiB
Lua

-- OctoTextSize: addon-local factory; Blizzard CreateFrame is unchanged.
local CreateFrame = OctoTextSize and OctoTextSize.WrapFactory(CreateFrame) or CreateFrame
-- Octo Dungeon Finder: the queue half of Octo's FrameXML LFT (Tortoise LFTMgr, TW_LFG).
-- -> C2S_QUEUE_JOIN;<code:code>;<t:h:d> | C2S_QUEUE_LEAVE | C2S_GET_QUEUE_STATUS
-- C2S_ROLECHECK_RESPONSE;<t:h:d> | C2S_OFFER_ACCEPT
-- <- S2C_QUEUE_JOINED | S2C_QUEUE_LEFT;<name> | S2C_QUEUE_ERROR;<code>
-- S2C_UPDATE_QUEUE_STATUS;queued;<codes>;<joinTime>;0;<t:h:d> | ;pending_rolecheck | ;pending_offer
-- S2C_ROLECHECK_START;<codes> | S2C_ROLECHECK_INFO;<member>;<roles>
-- S2C_OFFER_NEW;<code>;<role> | S2C_OFFER_UPDATE_COUNT;<t:h:d> | S2C_OFFER_COMPLETE
-- Taint rule (see OctoTitles): no Blizzard globals or tables are written; own frames only.
local D = OctoDungeonFinderData
local T = D.text
local PREFIX = "TW_LFG"
local MIN_LEVEL = 13
local DECISION_SECONDS = 90
local YELLOW = "|cffffff00"
local byCode = {}
for _, inst in ipairs(D.instances) do byCode[inst.code] = inst end
local ROLES = {
{ code = "t", name = TANK or "Tank", tip = T.ROLE_TANK_TOOLTIP, atlas = "groupfinder-icon-role-large-tank" },
{ code = "h", name = HEALER or "Healer", tip = T.ROLE_HEALER_TOOLTIP, atlas = "groupfinder-icon-role-large-heal" },
{ code = "d", name = DAMAGER or "Damage", tip = T.ROLE_DAMAGE_TOOLTIP, atlas = "groupfinder-icon-role-large-dps" },
}
local function roleName(code)
for _, r in ipairs(ROLES) do if r.code == code then return r.name end end
return code
end
-- Mirrors Tortoise LFTManager::AllowedRoleMask.
local CLASS_ROLES = {
DRUID = "thd", PALADIN = "thd", WARRIOR = "td", PRIEST = "hd", SHAMAN = "hd",
HUNTER = "d", MAGE = "d", ROGUE = "d", WARLOCK = "d",
}
local function allowed(code)
local _, class = UnitClass("player")
return (CLASS_ROLES[class] or ""):find(code, 1, true) ~= nil
end
local selectedRoles = { t = false, h = false, d = true }
local selected = {}
local status -- nil | "pending" | "queued" | "pending_rolecheck" | "pending_offer"
local function send(payload)
if not C_ChatInfo or type(C_ChatInfo.SendAddonMessage) ~= "function" then return false end
local result = C_ChatInfo.SendAddonMessage(PREFIX, payload, "WHISPER", UnitName("player"))
return result == 0 or result == true
end
local function say(text) DEFAULT_CHAT_FRAME:AddMessage(YELLOW .. text) end
local function split(s, sep)
local out = {}
for part in (s .. sep):gmatch("(.-)" .. sep:gsub("%p", "%%%0")) do out[#out + 1] = part end
return out
end
local function roleList(roles)
local out = {}
for _, r in ipairs(ROLES) do if roles[r.code] and allowed(r.code) then out[#out + 1] = r.code end end
return table.concat(out, ":")
end
local function instanceNames(codes)
local names = {}
for _, code in ipairs(codes) do if byCode[code] then names[#names + 1] = byCode[code].name end end
return table.concat(names, ", ")
end
-- Role checkboxes, reused by the main panel and the role check dialog.
local function roleRow(parent, anchorTo, x, y, onChange)
local boxes = {}
for i, r in ipairs(ROLES) do
local box = CreateFrame("CheckButton", nil, parent, "UICheckButtonTemplate")
box:SetPoint("TOPLEFT", anchorTo, "TOPLEFT", x + (i - 1) * 96, y)
local icon = box:CreateTexture(nil, "ARTWORK")
icon:SetSize(20, 20)
icon:SetPoint("LEFT", box, "RIGHT", 0, 0)
icon:SetAtlas(r.atlas)
local label = box:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall")
label:SetPoint("LEFT", icon, "RIGHT", 2, 0)
label:SetText(r.name)
box:SetScript("OnEnter", function(self)
GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
GameTooltip:SetText(r.name, 1, 1, 1)
if r.tip then GameTooltip:AddLine(r.tip, nil, nil, nil, true) end
GameTooltip:Show()
end)
box:SetScript("OnLeave", GameTooltip_Hide)
box:SetScript("OnClick", function(self) if onChange then onChange(r.code, self:GetChecked()) end end)
box.code = r.code
boxes[#boxes + 1] = box
end
return boxes
end
local function syncRoles(boxes, roles, enabled)
for _, box in ipairs(boxes) do
local ok = allowed(box.code)
box:SetChecked(ok and roles[box.code] or false)
box:SetEnabled(ok and enabled)
end
end
-- Main panel ------------------------------------------------------------------
local frame = CreateFrame("Frame", "OctoDungeonFinderFrame", UIParent, "ButtonFrameTemplate")
frame:SetSize(338, 460)
frame:SetPoint("TOPLEFT", 16, -116)
frame:SetToplevel(true)
frame:EnableMouse(true)
frame:SetMovable(true)
frame:RegisterForDrag("LeftButton")
frame:SetScript("OnDragStart", frame.StartMoving)
frame:SetScript("OnDragStop", frame.StopMovingOrSizing)
frame:Hide()
if ButtonFrameTemplate_HidePortrait then ButtonFrameTemplate_HidePortrait(frame) end
if frame.SetTitle then frame:SetTitle(T.QUEUE or "Dungeon Finder") end
local update
local roleBoxes = roleRow(frame, frame, 16, -30, function(code, checked)
selectedRoles[code] = checked and true or false
update()
end)
local warning = frame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
warning:SetPoint("CENTER", frame, "CENTER", 0, 20)
warning:SetWidth(280)
local scroll = CreateFrame("ScrollFrame", nil, frame, "ScrollFrameTemplate")
scroll:SetPoint("TOPLEFT", frame, "TOPLEFT", 12, -66)
scroll:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -30, 64)
local list = CreateFrame("Frame", nil, scroll)
list:SetSize(290, 10)
scroll:SetScrollChild(list)
local rows = {}
local function row(i)
if rows[i] then return rows[i] end
local r = CreateFrame("CheckButton", nil, list, "UICheckButtonTemplate")
r:SetSize(22, 22)
r:SetPoint("TOPLEFT", 2, -(i - 1) * 22)
r.name = r:CreateFontString(nil, "ARTWORK", "GameFontNormalSmall")
r.name:SetPoint("LEFT", r, "RIGHT", 2, 0)
r.levels = r:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall")
r.levels:SetPoint("RIGHT", list, "RIGHT", -4, 0)
r.levels:SetPoint("TOP", r, "TOP", 0, -5)
r:SetScript("OnClick", function(self)
selected[self.code] = self:GetChecked() or nil
update()
end)
rows[i] = r
return r
end
local queueText = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
queueText:SetPoint("BOTTOMLEFT", frame, "BOTTOMLEFT", 14, 36)
queueText:SetWidth(310)
queueText:SetJustifyH("LEFT")
local mainButton = CreateFrame("Button", nil, frame, "UIPanelButtonTemplate")
mainButton:SetSize(150, 22)
mainButton:SetPoint("BOTTOM", frame, "BOTTOM", 0, 6)
local function inQueue() return status ~= nil end
local function canQueue()
if roleList(selectedRoles) == "" or not next(selected) then return false end
if IsInRaid and IsInRaid() then return false end
if IsInGroup and IsInGroup() and not UnitIsGroupLeader("player") then return false end
return true
end
update = function()
local level = UnitLevel("player") or 0
syncRoles(roleBoxes, selectedRoles, not inQueue())
local visible = {}
for _, inst in ipairs(D.instances) do
if level >= inst.min then visible[#visible + 1] = inst else selected[inst.code] = nil end
end
table.sort(visible, function(a, b) if a.min == b.min then return a.max > b.max end return a.min > b.min end)
for i, inst in ipairs(visible) do
local r = row(i)
r.code = inst.code
r.name:SetText(inst.name)
r.levels:SetText("(" .. inst.min .. " - " .. inst.max .. ")")
local color = GetQuestDifficultyColor and GetQuestDifficultyColor(math.floor((inst.min + inst.max) / 2))
if color then r.name:SetTextColor(color.r, color.g, color.b) end
r:SetChecked(selected[inst.code] ~= nil)
r:SetEnabled(not inQueue())
r:Show()
end
for i = #visible + 1, #rows do rows[i]:Hide() end
list:SetHeight(math.max(10, #visible * 22))
local low = level < MIN_LEVEL
local raid = IsInRaid and IsInRaid()
warning:SetText(low and (T.GENERAL_LOW_LEVEL_TEXT or "") or raid and (T.GENERAL_IN_RAID_TEXT or "") or "")
scroll:SetShown(not low)
if inQueue() then
mainButton:SetText(LEAVE_QUEUE or "Leave Queue")
mainButton:SetEnabled(status ~= "pending")
else
mainButton:SetText(FIND_GROUP or "Find Group")
mainButton:SetEnabled(not low and canQueue())
end
end
mainButton:SetScript("OnClick", function()
if inQueue() then send("C2S_QUEUE_LEAVE"); return end
local codes = {}
for code in pairs(selected) do codes[#codes + 1] = code end
send("C2S_QUEUE_JOIN;" .. table.concat(codes, ":"):lower() .. ";" .. roleList(selectedRoles))
status = "pending"
update()
end)
frame:SetScript("OnShow", update)
-- Timed decision dialog (role check and ready offer) ---------------------------
local function dialog(title, height)
local f = CreateFrame("Frame", nil, UIParent, "ButtonFrameTemplate")
f:SetSize(320, height)
f:SetPoint("TOP", UIParent, "TOP", 0, -140)
f:SetFrameStrata("DIALOG")
f:SetToplevel(true)
f:EnableMouse(true)
f:Hide()
if ButtonFrameTemplate_HidePortrait then ButtonFrameTemplate_HidePortrait(f) end
if f.SetTitle then f:SetTitle(title) end
if f.CloseButton then f.CloseButton:Hide() end
f.text = f:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
f.text:SetPoint("TOP", f, "TOP", 0, -34)
f.text:SetWidth(290)
f.timer = CreateFrame("StatusBar", nil, f)
f.timer:SetSize(280, 10)
f.timer:SetPoint("BOTTOM", f, "BOTTOM", 0, 34)
f.timer:SetStatusBarTexture("Interface\\TargetingFrame\\UI-StatusBar")
f.timer:SetStatusBarColor(1, 0.8, 0)
f.timer:SetMinMaxValues(0, DECISION_SECONDS)
f.yes = CreateFrame("Button", nil, f, "UIPanelButtonTemplate")
f.yes:SetSize(120, 22)
f.yes:SetPoint("BOTTOMRIGHT", f, "BOTTOM", -4, 6)
f.no = CreateFrame("Button", nil, f, "UIPanelButtonTemplate")
f.no:SetSize(120, 22)
f.no:SetPoint("BOTTOMLEFT", f, "BOTTOM", 4, 6)
f:SetScript("OnUpdate", function(self, elapsed)
self.left = (self.left or 0) - elapsed
self.timer:SetValue(math.max(self.left, 0))
if self.left <= 0 then self:Hide(); if self.onTimeout then self.onTimeout() end end
end)
f.open = function(self)
self.left = DECISION_SECONDS
self:Show()
end
return f
end
local rolecheck = dialog(T.ROLE_CHECK_TITLE_TEXT or "Role check", 170)
local rolecheckRoles = { t = false, h = false, d = false }
local rolecheckBoxes = roleRow(rolecheck, rolecheck, 24, -70, function(code, checked)
rolecheckRoles[code] = checked and true or false
rolecheck.yes:SetEnabled(roleList(rolecheckRoles) ~= "")
end)
rolecheck.yes:SetText(T.ROLE_CHECK_CONFIRM_TEXT or ACCEPT)
rolecheck.no:SetText(T.ROLE_CHECK_DECLINE_TEXT or DECLINE)
rolecheck.yes:SetScript("OnClick", function()
for k, v in pairs(rolecheckRoles) do selectedRoles[k] = v end
send("C2S_ROLECHECK_RESPONSE;" .. roleList(rolecheckRoles))
rolecheck:Hide()
update()
end)
local function declineRolecheck() send("C2S_QUEUE_LEAVE"); rolecheck:Hide() end
rolecheck.no:SetScript("OnClick", declineRolecheck)
rolecheck.onTimeout = declineRolecheck
local offer = dialog(T.GROUP_READY_STATUS_TEXT or "Ready check", 170)
offer.role = offer:CreateFontString(nil, "OVERLAY", "GameFontNormal")
offer.role:SetPoint("TOP", offer.text, "BOTTOM", 0, -14)
offer.count = offer:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
offer.count:SetPoint("TOP", offer.role, "BOTTOM", 0, -8)
offer.yes:SetText(T.GROUP_READY_CONFIRM_TEXT or ACCEPT)
offer.no:SetText(DECLINE or "Decline")
offer.yes:SetScript("OnClick", function(self)
send("C2S_OFFER_ACCEPT")
self:Disable(); offer.no:Disable()
end)
local function declineOffer()
send("C2S_QUEUE_LEAVE")
say(T.CHAT_OFFER_DECLINED_TEXT or "")
offer:Hide()
end
offer.no:SetScript("OnClick", declineOffer)
offer.onTimeout = declineOffer
-- Minimap entry -----------------------------------------------------------------
local button = CreateFrame("Button", "OctoDungeonFinderMinimapButton", Minimap)
button:SetSize(31, 31)
button:SetFrameStrata("MEDIUM")
button:SetFrameLevel(Minimap:GetFrameLevel() + 8)
button:SetPoint("TOPLEFT", Minimap, "TOPLEFT", -2, -72)
local icon = button:CreateTexture(nil, "BACKGROUND")
icon:SetSize(20, 20)
icon:SetPoint("TOPLEFT", 7, -5)
icon:SetTexture("Interface\\Icons\\INV_Misc_GroupLooking")
local border = button:CreateTexture(nil, "OVERLAY")
border:SetSize(53, 53)
border:SetPoint("TOPLEFT")
border:SetTexture("Interface\\Minimap\\MiniMap-TrackingBorder")
button:SetHighlightTexture("Interface\\Minimap\\UI-Minimap-ZoomButton-Highlight")
button:SetScript("OnClick", function() frame:SetShown(not frame:IsShown()) end)
button:SetScript("OnEnter", function(self)
GameTooltip:SetOwner(self, "ANCHOR_LEFT")
GameTooltip:SetText(T.QUEUE or "Dungeon Finder", 1, 1, 1)
if status == "queued" then GameTooltip:AddLine(queueText:GetText(), nil, nil, nil, true) end
GameTooltip:Show()
end)
button:SetScript("OnLeave", GameTooltip_Hide)
-- Server messages -----------------------------------------------------------------
local function counts(text)
local t, h, d = (text or ""):match("^(%d+):(%d+):(%d+)$")
return tonumber(t) or 0, tonumber(h) or 0, tonumber(d) or 0
end
local function handle(message)
local f = split(message, ";")
local kind = f[1]
if kind == "S2C_QUEUE_JOINED" then
status = "queued"
elseif kind == "S2C_QUEUE_LEFT" then
status = nil
rolecheck:Hide(); offer:Hide()
queueText:SetText("")
elseif kind == "S2C_QUEUE_ERROR" then
-- Tortoise LFTMgr codes: raid, not_leader, invalid. Octo has no CHAT_ERROR_RAID text;
-- its in-raid text says the same. Unknown codes keep Octo's internal text plus the code.
local code = f[2] or ""
local text = code == "raid" and T.GENERAL_IN_RAID_TEXT or T["CHAT_ERROR_" .. code:upper()]
if not text then text = (T.CHAT_ERROR_INTERNAL or "") .. " (" .. code .. ")" end
UIErrorsFrame:AddMessage(text, 1, 0.1, 0.1)
status = nil
elseif kind == "S2C_UPDATE_QUEUE_STATUS" then
if f[2] == "queued" then
status = "queued"
wipe(selected)
local codes = split(f[3] or "", ":")
for _, code in ipairs(codes) do if byCode[code] then selected[code] = 1 end end
local tk, hl, dm = counts(f[6])
queueText:SetText((T.ROLES or "") .. " " .. ROLES[1].name .. " " .. tk .. " " .. ROLES[2].name .. " " .. hl .. " " .. ROLES[3].name .. " " .. dm)
local names = instanceNames(codes)
if names ~= "" and T.CHAT_QUEUE_JOIN_TEXT then say(T.CHAT_QUEUE_JOIN_TEXT:format(names)) end
elseif f[2] == "pending_rolecheck" or f[2] == "pending_offer" then
status = f[2]
else
status = nil
end
elseif kind == "S2C_ROLECHECK_START" then
local codes = (f[2] and f[2] ~= "") and split(f[2], ":") or {}
if #codes == 0 then return end -- Browse-tab listed group check: not part of the queue panel.
for k in pairs(rolecheckRoles) do rolecheckRoles[k] = selectedRoles[k] and allowed(k) end
syncRoles(rolecheckBoxes, rolecheckRoles, true)
rolecheck.text:SetText(#codes > 1 and (T.ROLE_CHECK_MULTIPLE_INSTANCES_TEXT or "")
or ((T.ROLE_CHECK_INSTANCES_TEXT or "") .. "|cffffffff" .. instanceNames(codes)))
rolecheck.yes:SetEnabled(roleList(rolecheckRoles) ~= "")
rolecheck:open()
PlaySound(SOUNDKIT.READY_CHECK)
elseif kind == "S2C_ROLECHECK_INFO" then
if IsInGroup and IsInGroup() and T.CHAT_SELECTED_ROLES_TEXT then
local names = {}
for _, code in ipairs(split(f[3] or "", ":")) do names[#names + 1] = roleName(code) end
say(T.CHAT_SELECTED_ROLES_TEXT:format(f[2] or "", table.concat(names, ", ")))
end
elseif kind == "S2C_OFFER_NEW" then
local inst = byCode[f[2] or ""]
offer.text:SetText((T.GROUP_READY_GROUP_FORMED_TEXT or "") .. "\n|cffffffff" .. (inst and inst.name or f[2] or ""))
offer.role:SetText((T.GROUP_READY_YOUR_ROLE_TEXT or "") .. ": " .. roleName(f[3] or ""))
offer.count:SetText("")
offer.yes:Enable(); offer.no:Enable()
offer.instance = inst
offer:open()
PlaySound(SOUNDKIT.READY_CHECK)
if FlashClientIcon then FlashClientIcon() end
elseif kind == "S2C_OFFER_UPDATE_COUNT" then
local tk, hl, dm = counts(f[2])
offer.count:SetText(ROLES[1].name .. " " .. tk .. "/1 " .. ROLES[2].name .. " " .. hl .. "/1 " .. ROLES[3].name .. " " .. dm .. "/3")
elseif kind == "S2C_OFFER_COMPLETE" then
offer:Hide()
status = nil
if offer.instance and T.GROUP_READY_COMPLETE_TEXT then say(T.GROUP_READY_COMPLETE_TEXT:format(offer.instance.name)) end
end
if frame:IsShown() then update() end
end
local events = CreateFrame("Frame")
if C_ChatInfo and C_ChatInfo.RegisterAddonMessagePrefix then C_ChatInfo.RegisterAddonMessagePrefix(PREFIX) end
for _, e in ipairs({ "CHAT_MSG_ADDON", "PLAYER_ENTERING_WORLD", "GROUP_ROSTER_UPDATE", "PLAYER_LEVEL_UP" }) do events:RegisterEvent(e) end
events:SetScript("OnEvent", function(_, event, prefix, message, distribution, sender)
if event == "PLAYER_ENTERING_WORLD" then
send("C2S_GET_QUEUE_STATUS")
return
elseif event ~= "CHAT_MSG_ADDON" then
if frame:IsShown() then update() end
return
end
if prefix ~= PREFIX or distribution ~= "GUILD" then return end
if sender and Ambiguate(sender, "none") ~= UnitName("player") then return end
handle(message)
end)