Module:Sandbox/Vergir

Revision as of 23:02, 6 September 2026 by Vergir (talk | contribs) (Audit script: handle nil section type (with help from vergir-bot LLM))

Documentation for this module may be created at Module:Sandbox/Vergir/doc

-- Audit: for every item stat that goes through Infobox item's append_label
-- with attribute styling enabled (Passive/Active Main stats), compute the
-- label link target under the CURRENT priority (attr link > StatLinks)
-- and the PROPOSED priority (StatLinks > attr link) and list the results.
local p = {}
local lang = require "Module:Lang"
local data = mw.loadJsonData("Data:ItemCards.json")
local stat_links = mw.loadJsonData("Data:StatLinks.json")
local ATTR = require("Module:Icon/attr")

local LABEL_KEY_REMAP = { DotDuration = "DOTDuration" }
local IGNORED = { BuildUpPerShot = true }

local function get_pair(base, s1, s2)
    local v = lang.get_string(base .. s1)
    if v and v ~= "" then return v end
    v = lang.get_string(base .. s2)
    if v and v ~= "" then return v end
    return ""
end

local function resolve_label_key(prop_key, loc_override)
    local base = (loc_override or prop_key):gsub("^#", "")
    return LABEL_KEY_REMAP[base] or base
end

local function esc(s)
    if s == nil or s == "" then return "''(none)''" end
    return "<nowiki>" .. tostring(s) .. "</nowiki>"
end

function p.audit(frame)
    local mode = frame.args[1] or "diff" -- "diff" | "all"
    local rows = {}
    local names = {}
    for key, item in pairs(data) do
        if not item.IsDisabled then
            table.insert(names, key)
        end
    end
    table.sort(names, function(a, b)
        return (data[a].Name or a) < (data[b].Name or b)
    end)

    local n_items, n_stats, n_diff = 0, 0, 0
    for _, key in ipairs(names) do
        local item = data[key]
        n_items = n_items + 1
        local i = 1
        while item["Info" .. i] do
            local sec = item["Info" .. i]
            if sec.Type ~= "Innate" then
                local seen = {}
                for _, obj in ipairs(sec.Main or {}) do
                    local pk = obj.Key
                    if not seen[pk] and not IGNORED[pk] and not pk:match("^StatusEffect") then
                        seen[pk] = true
                        local label_key = resolve_label_key(pk, obj.LocTokenOverride)
                        local label = get_pair(label_key, "_label", "_Label")
                        if label ~= "" then
                            n_stats = n_stats + 1
                            local attr = obj.Type and ATTR[obj.Type] or nil
                            local attr_link = attr and attr.link or nil
                            if attr_link == "" then attr_link = nil end
                            local sl = stat_links[label_key]
                            if sl == "" then sl = nil end
                            local cur = attr_link or sl
                            local new = sl or attr_link
                            local differs = (cur or "") ~= (new or "")
                            if differs then n_diff = n_diff + 1 end
                            if mode == "all" or differs then
                                table.insert(rows, string.format(
                                    "|-%s\n| [[%s]] || %s || %s || <nowiki>%s</nowiki> || %s || %s || %s",
                                    differs and ' style="background:#4a3a1a"' or "",
                                    item.Name or key, tostring(sec.Type), esc(label_key), tostring(obj.Type),
                                    esc(label), esc(cur), esc(new)))
                            end
                        end
                    end
                end
            end
            i = i + 1
        end
    end

    local out = {}
    table.insert(out, string.format("Items scanned: %d. Passive/Active main stats with a label: %d. Rows whose link changes: %d.\n",
        n_items, n_stats, n_diff))
    table.insert(out, '{| class="wikitable sortable"\n! Item !! Section !! Stat key !! Type !! Label !! Current link !! Proposed link')
    for _, r in ipairs(rows) do table.insert(out, r) end
    table.insert(out, "|}")
    return table.concat(out, "\n")
end

return p