Module:Sandbox/Monster Domosed/AbilityTable/ComplexRenderers: Difference between revisions

No edit summary
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 404: Line 404:
end
end


-- Resolves the human-readable display Name for a prop key. Prefers the prop's
-- Memoized Key -> label map built from Data:AbilityCards.json. The flat
-- own "Name" field on the top-level table; otherwise recurses through the whole
-- AbilityData.json records only carry { Value, Scale }; the human-readable
-- ability record looking for any entry of the form { Key = key, Name = str }
-- label lives inside the Info1/Info2 prop entries of AbilityCards.json, where
-- (e.g. prop entries inside Info1/Info2 ... .Main.Props). Falls back to the key.
-- Title takes precedence over Name ("On Perfect Hold" before "Perfect Damage").
local function find_prop_name(ability, key)
-- A trailing colon on a Title is stripped so it reads cleanly in "(...)".
     local direct = ability[key]
local _card_labels = nil
     if type(direct) == "table" and type(direct["Name"]) == "string" then
local function build_card_label_map()
         return direct["Name"]
     if _card_labels then return _card_labels end
    _card_labels = {}
     if type(mw) ~= "table" or type(mw.loadJsonData) ~= "function" then
         return _card_labels
     end
     end
    local ok, cards = pcall(mw.loadJsonData, "Data:AbilityCards.json")
    if not ok or type(cards) ~= "table" then return _card_labels end


     local found
    -- Label for a single prop entry: Title wins, else Name; strip any
    -- trailing colon/whitespace (a Title like "On Perfect Hold:" -> "On Perfect Hold").
     local function entry_label(entry)
        local label = entry["Title"] or entry["Name"]
        if type(label) ~= "string" then return nil end
        if entry["Title"] ~= nil then label = label:gsub(":%s*$", "") end
        return label
    end
 
    local seen = {}
     local function walk(node)
     local function walk(node)
         if found or type(node) ~= "table" then return end
         if type(node) ~= "table" or seen[node] then return end
        seen[node] = true
        local k = node["Key"]
        if type(k) == "string" and (node["Title"] ~= nil or node["Name"] ~= nil) then
            local label = entry_label(node)
            if label and label ~= "" then _card_labels[k] = label end
        end
         for _, v in pairs(node) do
         for _, v in pairs(node) do
             if type(v) == "table" then
             if type(v) == "table" then walk(v) end
                if v["Key"] == key and type(v["Name"]) == "string" then
                    found = v["Name"]
                    return
                end
                walk(v)
            end
         end
         end
     end
     end
     walk(ability)
     walk(cards)
     return found or key
     return _card_labels
end
end


Line 440: Line 454:
function p.RenderSpiritScaling(ability)
function p.RenderSpiritScaling(ability)
     local props = {}
     local props = {}
    -- Build a Key -> display Name lookup for the prop names. The flattened
    -- top-level prop only keeps { Value, Scale }, while the human-readable
    -- Name lives alongside the Key in the nested Info1/Info2 entries, so scan
    -- the whole record for any entry pairing a string Key with a string Name.
    local names, seen = {}, {}
    local function walk(node)
        if type(node) ~= "table" or seen[node] then return end
        seen[node] = true
        local k, n = node["Key"], node["Name"]
        if type(k) == "string" and type(n) == "string" then names[k] = n end
        for _, v in pairs(node) do
            if type(v) == "table" then walk(v) end
        end
    end
    walk(ability)


     -- Gather every base Spirit-scaling instance (skip the Upgrades table).
     -- Gather every base Spirit-scaling instance (skip the Upgrades table).
Line 449: Line 479:
               and type(scale["Value"]) == "number"
               and type(scale["Value"]) == "number"
               and scale["Value"] ~= 0 then
               and scale["Value"] ~= 0 then
                 local label = find_prop_name(ability, k)
                -- Prefer a Name carried directly on the prop, then any label
                -- recovered from nested Key/Title-or-Name entries inside this
                -- record, then the AbilityCards label map (Title over Name),
                -- else the key.
                 local label = type(v["Name"]) == "string" and v["Name"]
                              or names[k]
                              or build_card_label_map()[k]
                              or k
                 table.insert(props, { key = k, value = scale["Value"], name = label })
                 table.insert(props, { key = k, value = scale["Value"], name = label })
             end
             end