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

No edit summary
No edit summary
 
Line 404: Line 404:
end
end


-- Memoized display-name lookup that calls Module:Lang's get_string with the prop
-- Memoized Key -> label map built from Data:AbilityCards.json. The flat
-- key suffixed by "_label" (e.g. "MaxDamageBeforePerfect_label"). The flat
-- AbilityData.json records only carry { Value, Scale }; the human-readable
-- AbilityData.json records only carry { Value, Scale }; the localized,
-- label lives inside the Info1/Info2 prop entries of AbilityCards.json, where
-- human-readable prop name is a Lang string keyed by the prop key + "_label".
-- Title takes precedence over Name ("On Perfect Hold" before "Perfect Damage").
-- The module is required lazily so this file can still load when Lang is absent
-- A trailing colon on a Title is stripped so it reads cleanly in "(...)".
-- (e.g. module console), in which case the fallback to the raw key applies.
local _card_labels = nil
local Lang = nil
local function build_card_label_map()
local _label_lookup = nil
     if _card_labels then return _card_labels end
local function prop_label(key)
    _card_labels = {}
     if _label_lookup == nil then _label_lookup = {} end
    if type(mw) ~= "table" or type(mw.loadJsonData) ~= "function" then
     if _label_lookup[key] ~= nil then return _label_lookup[key] end
        return _card_labels
    end
    local ok, cards = pcall(mw.loadJsonData, "Data:AbilityCards.json")
     if not ok or type(cards) ~= "table" then return _card_labels end


     if Lang == nil then
     -- Label for a single prop entry: Title wins, else Name; strip any
         local ok, mod = pcall(require, "Module:Lang")
    -- trailing colon/whitespace (a Title like "On Perfect Hold:" -> "On Perfect Hold").
         if ok and type(mod) == "table" then Lang = mod end
    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
     end
     if Lang and type(Lang.get_string) == "function" then
 
         local ok, label = pcall(Lang.get_string, key .. "_label")
    local seen = {}
         if ok and type(label) == "string" and label ~= "" then
     local function walk(node)
             _label_lookup[key] = label
        if type(node) ~= "table" or seen[node] then return end
             return label
        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
             if type(v) == "table" then walk(v) end
         end
         end
     end
     end
 
     walk(cards)
     -- Cache the miss (as nil) so we don't hit Lang again for this key.
     return _card_labels
    _label_lookup[key] = nil
     return nil
end
end


Line 467: Line 479:
               and type(scale["Value"]) == "number"
               and type(scale["Value"]) == "number"
               and scale["Value"] ~= 0 then
               and scale["Value"] ~= 0 then
                 -- Prefer a Name carried directly on the prop, then any name
                 -- Prefer a Name carried directly on the prop, then any label
                 -- recovered from nested Key/Name entries inside this record,
                 -- recovered from nested Key/Title-or-Name entries inside this
                 -- then the localized Lang label for "<key>_label", else the key.
                 -- record, then the AbilityCards label map (Title over Name),
                -- else the key.
                 local label = type(v["Name"]) == "string" and v["Name"]
                 local label = type(v["Name"]) == "string" and v["Name"]
                               or names[k]
                               or names[k]
                               or prop_label(k)
                               or build_card_label_map()[k]
                               or k
                               or k
                 table.insert(props, { key = k, value = scale["Value"], name = label })
                 table.insert(props, { key = k, value = scale["Value"], name = label })