379 lines
16 KiB
Lua
379 lines
16 KiB
Lua
-- OctoTextSize: addon-local factory; Blizzard CreateFrame is unchanged.
|
|
local CreateFrame = OctoTextSize and OctoTextSize.WrapFactory(CreateFrame) or CreateFrame
|
|
-- Octo Group Browser: the browse half of Octo's FrameXML LFT (Tortoise LFTUserGroups, TW_LFG).
|
|
-- -> C2S_GET_GROUPS_STATUS | C2S_GET_GROUPS_LIST | C2S_GET_GROUP_DETAILS;<id>
|
|
-- C2S_NEW_GROUP;<title>;<description>;<category 1-4>;<t:h:d>
|
|
-- C2S_UPDATE_GROUP;<id>;<title>;<description>;<t:h:d> | C2S_DELETE_GROUP;<id>
|
|
-- <- S2C_GROUPS_STATUS;<0|1> | S2C_GROUPS_LIST_UPDATE;start | ;<listing json> | ;end
|
|
-- S2C_UPDATE_GROUP;<id>;start;<json> | ;<roleIdx>;<signup json> | ;end | ;invalid
|
|
-- Contacting a listing whispers its creator. The listed-group role check (C2C_* over party
|
|
-- addon messages) is not ported yet. Taint rule: no Blizzard globals or tables are written.
|
|
local D = OctoDungeonFinderData
|
|
local T = D.text
|
|
local PREFIX = "TW_LFG"
|
|
local CATEGORIES = { T.GROUPS_TYPE1, T.GROUPS_TYPE2, T.GROUPS_TYPE3, T.GROUPS_TYPE4 }
|
|
|
|
local function decode(s)
|
|
local pos = 1
|
|
local value
|
|
local function ws() pos = s:find("[^ \t\r\n]", pos) or #s + 1 end
|
|
local function str()
|
|
local out, i = {}, pos + 1
|
|
while true do
|
|
local c = s:sub(i, i)
|
|
if c == "" then error("eof") end
|
|
if c == '"' then pos = i + 1; return table.concat(out) end
|
|
if c == "\\" then
|
|
local e = s:sub(i + 1, i + 1)
|
|
if e == "u" then
|
|
local code = tonumber(s:sub(i + 2, i + 5), 16) or 63
|
|
out[#out + 1] = code < 128 and string.char(code) or "?"
|
|
i = i + 6
|
|
else
|
|
out[#out + 1] = ({ b = "\b", f = "\f", n = "\n", r = "\r", t = "\t" })[e] or e
|
|
i = i + 2
|
|
end
|
|
else
|
|
out[#out + 1] = c
|
|
i = i + 1
|
|
end
|
|
end
|
|
end
|
|
value = function()
|
|
ws()
|
|
local c = s:sub(pos, pos)
|
|
if c == "{" then
|
|
local t = {}
|
|
pos = pos + 1; ws()
|
|
if s:sub(pos, pos) == "}" then pos = pos + 1; return t end
|
|
while true do
|
|
ws(); local k = str(); ws()
|
|
if s:sub(pos, pos) ~= ":" then error("colon") end
|
|
pos = pos + 1
|
|
t[k] = value(); ws()
|
|
local d = s:sub(pos, pos); pos = pos + 1
|
|
if d == "}" then return t elseif d ~= "," then error("object") end
|
|
end
|
|
elseif c == "[" then
|
|
local t = {}
|
|
pos = pos + 1; ws()
|
|
if s:sub(pos, pos) == "]" then pos = pos + 1; return t end
|
|
while true do
|
|
t[#t + 1] = value(); ws()
|
|
local d = s:sub(pos, pos); pos = pos + 1
|
|
if d == "]" then return t elseif d ~= "," then error("array") end
|
|
end
|
|
elseif c == '"' then return str()
|
|
elseif s:sub(pos, pos + 3) == "true" then pos = pos + 4; return true
|
|
elseif s:sub(pos, pos + 4) == "false" then pos = pos + 5; return false
|
|
elseif s:sub(pos, pos + 3) == "null" then pos = pos + 4; return nil
|
|
else
|
|
local n = s:match("^-?%d+%.?%d*[eE]?[-+]?%d*", pos)
|
|
if not n or n == "" then error("value") end
|
|
pos = pos + #n
|
|
return tonumber(n)
|
|
end
|
|
end
|
|
local ok, result = pcall(value)
|
|
return ok and result or nil
|
|
end
|
|
|
|
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 clean(text) return ((text or ""):gsub("[;\r\n|]", " ")) end
|
|
|
|
local enabled, groups, incoming, category, selected = false, {}, nil, 1, nil
|
|
local function mine() for _, g in ipairs(groups) do if g.creator == UnitName("player") then return g end end end
|
|
|
|
local frame = CreateFrame("Frame", "OctoGroupBrowserFrame", UIParent, "ButtonFrameTemplate")
|
|
frame:SetSize(384, 470)
|
|
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.GENERAL_BROWSE_TAB_TEXT or "Group Browser") end
|
|
|
|
local update
|
|
local filter = CreateFrame("DropdownButton", nil, frame, "WowStyle1DropdownTemplate")
|
|
filter:SetWidth(180)
|
|
filter:SetPoint("TOPLEFT", frame, "TOPLEFT", 14, -30)
|
|
filter:SetupMenu(function(_, root)
|
|
for i, name in ipairs(CATEGORIES) do
|
|
root:CreateRadio(name, function() return category == i end, function() category = i; selected = nil; update() end)
|
|
end
|
|
end)
|
|
|
|
local empty = frame:CreateFontString(nil, "OVERLAY", "GameFontDisable")
|
|
empty:SetPoint("CENTER", frame, "CENTER", 0, 30)
|
|
|
|
local scroll = CreateFrame("ScrollFrame", nil, frame, "ScrollFrameTemplate")
|
|
scroll:SetPoint("TOPLEFT", frame, "TOPLEFT", 12, -62)
|
|
scroll:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -30, 150)
|
|
local list = CreateFrame("Frame", nil, scroll)
|
|
list:SetSize(334, 10)
|
|
scroll:SetScrollChild(list)
|
|
|
|
local ROW = 34
|
|
local rows = {}
|
|
local function row(i)
|
|
if rows[i] then return rows[i] end
|
|
local r = CreateFrame("Button", nil, list)
|
|
r:SetSize(334, ROW - 2)
|
|
r:SetPoint("TOPLEFT", 0, -(i - 1) * ROW)
|
|
r:SetHighlightTexture("Interface\\QuestFrame\\UI-QuestTitleHighlight", "ADD")
|
|
r.sel = r:CreateTexture(nil, "BACKGROUND")
|
|
r.sel:SetAllPoints()
|
|
r.sel:SetColorTexture(1, 0.82, 0, 0.18)
|
|
r.title = r:CreateFontString(nil, "ARTWORK", "GameFontNormal")
|
|
r.title:SetPoint("TOPLEFT", 6, -3)
|
|
r.title:SetPoint("RIGHT", r, "RIGHT", -70, 0)
|
|
r.title:SetJustifyH("LEFT")
|
|
r.who = r:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall")
|
|
r.who:SetPoint("BOTTOMLEFT", 6, 3)
|
|
r.roles = r:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall")
|
|
r.roles:SetPoint("RIGHT", r, "RIGHT", -6, 0)
|
|
r:SetScript("OnClick", function(self)
|
|
selected = self.group and self.group.id
|
|
if selected then send("C2S_GET_GROUP_DETAILS;" .. selected) end
|
|
update()
|
|
end)
|
|
rows[i] = r
|
|
return r
|
|
end
|
|
|
|
local details = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
|
|
details:SetPoint("TOPLEFT", scroll, "BOTTOMLEFT", 4, -10)
|
|
details:SetPoint("RIGHT", frame, "RIGHT", -14, 0)
|
|
details:SetHeight(76)
|
|
details:SetJustifyH("LEFT")
|
|
details:SetJustifyV("TOP")
|
|
|
|
local contact = CreateFrame("Button", nil, frame, "UIPanelButtonTemplate")
|
|
contact:SetSize(110, 22)
|
|
contact:SetPoint("BOTTOMLEFT", frame, "BOTTOMLEFT", 8, 6)
|
|
contact:SetText(WHISPER or "Whisper")
|
|
local newButton = CreateFrame("Button", nil, frame, "UIPanelButtonTemplate")
|
|
newButton:SetSize(110, 22)
|
|
newButton:SetPoint("BOTTOM", frame, "BOTTOM", 0, 6)
|
|
local removeButton = CreateFrame("Button", nil, frame, "UIPanelButtonTemplate")
|
|
removeButton:SetSize(110, 22)
|
|
removeButton:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -8, 6)
|
|
removeButton:SetText(DELETE or "Delete")
|
|
|
|
local function classColor(class)
|
|
local c = class and RAID_CLASS_COLORS and RAID_CLASS_COLORS[class:upper()]
|
|
return c and c.colorStr and ("|c" .. c.colorStr) or "|cffffffff"
|
|
end
|
|
local function needText(g)
|
|
local lim, got = g.limit or {}, g.numConfirmed or {}
|
|
if (lim[1] or 0) + (lim[2] or 0) + (lim[3] or 0) == 0 then return "" end
|
|
local parts = {}
|
|
local names = { TANK or "Tank", HEALER or "Healer", DAMAGER or "Damage" }
|
|
for i = 1, 3 do if (lim[i] or 0) > 0 then parts[#parts + 1] = names[i] .. " " .. (got[i] or 0) .. "/" .. lim[i] end end
|
|
return table.concat(parts, " ")
|
|
end
|
|
local function find(id) for _, g in ipairs(groups) do if g.id == id then return g end end end
|
|
|
|
update = function()
|
|
filter:SetDefaultText(CATEGORIES[category])
|
|
if filter.GenerateMenu then filter:GenerateMenu() end
|
|
local shown = {}
|
|
for _, g in ipairs(groups) do if g.category == category then shown[#shown + 1] = g end end
|
|
for i, g in ipairs(shown) do
|
|
local r = row(i)
|
|
r.group = g
|
|
r.title:SetText(g.title ~= "" and g.title or (g.creator or ""))
|
|
r.who:SetText(classColor(g.class) .. (g.creator or "") .. "|r " .. (LEVEL or "Level") .. " " .. (g.level or ""))
|
|
r.roles:SetText(needText(g))
|
|
r.sel:SetShown(selected == g.id)
|
|
r:Show()
|
|
end
|
|
for i = #shown + 1, #rows do rows[i]:Hide() end
|
|
list:SetHeight(math.max(10, #shown * ROW))
|
|
empty:SetText(not enabled and (T.CHAT_ERROR_QUEUE_DISABLED or "") or (#shown == 0 and (LFG_LIST_NO_RESULTS_FOUND or "") or ""))
|
|
empty:SetShown(not enabled or #shown == 0)
|
|
|
|
local g = selected and find(selected)
|
|
if g then
|
|
local signups = {}
|
|
for i, role in ipairs(g.signups or {}) do
|
|
for _, s in ipairs(role) do signups[#signups + 1] = classColor(s.class) .. (s.name or "") .. "|r" .. (s.confirmed and "" or " ?") end
|
|
end
|
|
details:SetText("|cffffd100" .. (g.title or "") .. "|r\n" .. (g.description or "") ..
|
|
(#signups > 0 and ("\n" .. table.concat(signups, ", ")) or ""))
|
|
else
|
|
details:SetText("")
|
|
end
|
|
local own = mine()
|
|
contact:SetEnabled(g ~= nil and g.creator ~= UnitName("player"))
|
|
newButton:SetText(own and (EDIT or "Edit") or (T.NEW_GROUP or "New Group"))
|
|
newButton:SetEnabled(enabled)
|
|
removeButton:SetEnabled(own ~= nil)
|
|
end
|
|
|
|
contact:SetScript("OnClick", function()
|
|
local g = selected and find(selected)
|
|
if g and g.creator and ChatFrameUtil and ChatFrameUtil.SendTell then ChatFrameUtil.SendTell(g.creator) end
|
|
end)
|
|
removeButton:SetScript("OnClick", function()
|
|
local own = mine()
|
|
if own then send("C2S_DELETE_GROUP;" .. own.id) end
|
|
end)
|
|
|
|
-- New / edit listing ------------------------------------------------------------
|
|
local editor = CreateFrame("Frame", nil, frame, "ButtonFrameTemplate")
|
|
editor:SetSize(300, 300)
|
|
editor:SetPoint("TOPLEFT", frame, "TOPRIGHT", 4, 0)
|
|
editor:SetFrameStrata("DIALOG")
|
|
editor:EnableMouse(true)
|
|
editor:Hide()
|
|
if ButtonFrameTemplate_HidePortrait then ButtonFrameTemplate_HidePortrait(editor) end
|
|
local function label(text, y)
|
|
local l = editor:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
|
|
l:SetPoint("TOPLEFT", editor, "TOPLEFT", 16, y)
|
|
l:SetText(text)
|
|
return l
|
|
end
|
|
label(NAME or "Title", -32)
|
|
local titleBox = CreateFrame("EditBox", nil, editor, "InputBoxTemplate")
|
|
titleBox:SetSize(260, 20)
|
|
titleBox:SetPoint("TOPLEFT", editor, "TOPLEFT", 22, -46)
|
|
titleBox:SetAutoFocus(false)
|
|
titleBox:SetMaxLetters(48)
|
|
label(DESCRIPTION or "Description", -74)
|
|
local descBox = CreateFrame("EditBox", nil, editor, "InputBoxTemplate")
|
|
descBox:SetSize(260, 20)
|
|
descBox:SetPoint("TOPLEFT", editor, "TOPLEFT", 22, -88)
|
|
descBox:SetAutoFocus(false)
|
|
descBox:SetMaxLetters(120)
|
|
local editCategory = 1
|
|
local catDrop = CreateFrame("DropdownButton", nil, editor, "WowStyle1DropdownTemplate")
|
|
catDrop:SetWidth(200)
|
|
catDrop:SetPoint("TOPLEFT", editor, "TOPLEFT", 16, -116)
|
|
catDrop:SetupMenu(function(_, root)
|
|
for i, name in ipairs(CATEGORIES) do
|
|
root:CreateRadio(name, function() return editCategory == i end, function() editCategory = i end)
|
|
end
|
|
end)
|
|
label(T.ROLES or "Roles", -150)
|
|
local counts = {}
|
|
local roleNames = { TANK or "Tank", HEALER or "Healer", DAMAGER or "Damage" }
|
|
for i = 1, 3 do
|
|
local box = CreateFrame("EditBox", nil, editor, "InputBoxTemplate")
|
|
box:SetSize(28, 20)
|
|
box:SetPoint("TOPLEFT", editor, "TOPLEFT", 24 + (i - 1) * 90, -168)
|
|
box:SetAutoFocus(false)
|
|
box:SetNumeric(true)
|
|
box:SetMaxLetters(2)
|
|
local l = editor:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
|
|
l:SetPoint("LEFT", box, "RIGHT", 4, 0)
|
|
l:SetText(roleNames[i])
|
|
counts[i] = box
|
|
end
|
|
local save = CreateFrame("Button", nil, editor, "UIPanelButtonTemplate")
|
|
save:SetSize(120, 22)
|
|
save:SetPoint("BOTTOM", editor, "BOTTOM", 0, 6)
|
|
save:SetText(SAVE or "Save")
|
|
save:SetScript("OnClick", function()
|
|
local limit = {}
|
|
for i = 1, 3 do limit[i] = tostring(math.min(tonumber(counts[i]:GetText()) or 0, 40)) end
|
|
local title, desc = clean(titleBox:GetText()), clean(descBox:GetText())
|
|
local own = mine()
|
|
if own then
|
|
send("C2S_UPDATE_GROUP;" .. own.id .. ";" .. title .. ";" .. desc .. ";" .. table.concat(limit, ":"))
|
|
else
|
|
send("C2S_NEW_GROUP;" .. title .. ";" .. desc .. ";" .. editCategory .. ";" .. table.concat(limit, ":"))
|
|
end
|
|
editor:Hide()
|
|
end)
|
|
newButton:SetScript("OnClick", function()
|
|
local own = mine()
|
|
if editor.SetTitle then editor:SetTitle(own and (EDIT or "Edit") or (T.NEW_GROUP or "New Group")) end
|
|
titleBox:SetText(own and own.title or "")
|
|
descBox:SetText(own and own.description or "")
|
|
editCategory = own and own.category or category
|
|
catDrop:SetDefaultText(CATEGORIES[editCategory])
|
|
catDrop:SetEnabled(own == nil) -- the server keeps the category on update
|
|
for i = 1, 3 do counts[i]:SetText(tostring(own and own.limit and own.limit[i] or 0)) end
|
|
editor:Show()
|
|
end)
|
|
|
|
frame:SetScript("OnShow", function() send("C2S_GET_GROUPS_LIST"); update() end)
|
|
frame:SetScript("OnHide", function() editor:Hide() end)
|
|
|
|
-- Entry from the dungeon finder panel.
|
|
if OctoDungeonFinderFrame then
|
|
local open = CreateFrame("Button", nil, OctoDungeonFinderFrame, "UIPanelButtonTemplate")
|
|
open:SetSize(120, 20)
|
|
open:SetPoint("TOPRIGHT", OctoDungeonFinderFrame, "TOPRIGHT", -8, -28)
|
|
open:SetText(T.GENERAL_BROWSE_TAB_TEXT or "Group Browser")
|
|
open:SetScript("OnClick", function() OctoDungeonFinderFrame:Hide(); frame:Show() end)
|
|
end
|
|
|
|
-- Server messages -----------------------------------------------------------------
|
|
local function handle(message)
|
|
local kind, rest = message:match("^(S2C_[A-Z_]+);?(.*)$")
|
|
if kind == "S2C_GROUPS_STATUS" then
|
|
enabled = rest == "1"
|
|
if enabled then send("C2S_GET_GROUPS_LIST") end
|
|
elseif kind == "S2C_GROUPS_LIST_UPDATE" then
|
|
if rest == "start" then
|
|
incoming = {}
|
|
elseif rest == "end" then
|
|
if incoming then groups = incoming end
|
|
incoming = nil
|
|
if selected and not find(selected) then selected = nil end
|
|
elseif incoming then
|
|
local g = decode(rest)
|
|
if type(g) == "table" and g.id then
|
|
g.signups = { {}, {}, {} }
|
|
if g.creator == UnitName("player") then table.insert(incoming, 1, g) else incoming[#incoming + 1] = g end
|
|
end
|
|
end
|
|
elseif kind == "S2C_UPDATE_GROUP" then
|
|
local id, code, data = rest:match("^(%d+);([%a%d]+);?(.*)$")
|
|
id = tonumber(id)
|
|
if not id then return end
|
|
if code == "invalid" then
|
|
UIErrorsFrame:AddMessage(T.CHAT_ERROR_INVALID or "", 1, 0.1, 0.1)
|
|
selected = nil
|
|
send("C2S_GET_GROUPS_LIST")
|
|
elseif code == "start" then
|
|
local g = decode(data)
|
|
if type(g) == "table" then
|
|
g.signups = { {}, {}, {} }
|
|
local found = false
|
|
for i, old in ipairs(groups) do if old.id == id then groups[i] = g; found = true end end
|
|
if not found then groups[#groups + 1] = g end
|
|
end
|
|
elseif code ~= "end" then
|
|
local g, s, role = find(id), decode(data), tonumber(code)
|
|
if g and type(s) == "table" and role then
|
|
if role == 0 then role = 3 end
|
|
if g.signups[role] then table.insert(g.signups[role], s) end
|
|
end
|
|
end
|
|
else
|
|
return
|
|
end
|
|
if frame:IsShown() then update() end
|
|
end
|
|
|
|
local events = CreateFrame("Frame")
|
|
events:RegisterEvent("CHAT_MSG_ADDON")
|
|
events:RegisterEvent("PLAYER_ENTERING_WORLD")
|
|
events:SetScript("OnEvent", function(_, event, prefix, message, distribution, sender)
|
|
if event == "PLAYER_ENTERING_WORLD" then send("C2S_GET_GROUPS_STATUS"); 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)
|