Module:GameData: Difference between revisions

Jump to navigation Jump to search
Vergir (talk | contribs)
record_matches_prop: handle table-valued properties without a Value key (e.g. modifier objects like DragModifier) by checking for non-empty table (with help from vergir-bot LLM)
Vergir (talk | contribs)
Add Convar lookup mode to get_prop (Data:Convars.json), unwrapping the {value, description} form (with help from vergir-bot LLM)
Line 9: Line 9:
     ABILITIES = "Data:AbilityData.json",
     ABILITIES = "Data:AbilityData.json",
     HEROES    = "Data:HeroData.json",
     HEROES    = "Data:HeroData.json",
    CONVARS  = "Data:Convars.json",
}
}


Line 138: Line 139:
end
end


-- {{#invoke:GameData|get_prop|ENTITY_NAME|PROPERTY}}
-- Looks up a console variable in Data:Convars.json. A convar value is stored
-- Finds entity via ResourceLookup, returns the raw value (no formatting).
-- either as a plain scalar (e.g. "adsp_alley_min": 122) or as a table holding
-- Supports dot notation for nested properties (e.g. "Scale.Value").
-- the value plus a description (e.g. { value = 75, description = "..." }), in
-- Tables with a .Value field are automatically unwrapped.
-- which case only the value is returned. The exact key is tried first, then a
-- lower-case form. Returns (value, found).
local function lookup_convar(key)
    local data = mw.loadJsonData(p.Dataset.CONVARS)
    local entry = data[key]
    if entry == nil then
        entry = data[mw.ustring.lower(key)]
    end
    if entry == nil then
        return nil, false
    end
    if type(entry) == "table" then
        return entry.value, true
    end
    return entry, true
end
 
-- {{#invoke:GameData|get_prop|...}}
-- Two modes:
--  Entity:  {{#invoke:GameData|get_prop|ENTITY_NAME|PROPERTY}}
--           Finds entity via ResourceLookup, returns the raw value (no
--            formatting). Supports dot notation for nested properties
--            (e.g. "Scale.Value"); tables with a .Value field are unwrapped.
--  Convar:  {{#invoke:GameData|get_prop|Convar|CONVAR_NAME}}
--            Looks up the convar in Data:Convars.json and returns its value,
--            unwrapping the { value, description } form when present.
function p.get_prop(frame)
function p.get_prop(frame)
     local name = frame.args[1]
     local arg1 = frame.args[1]
     local prop = frame.args[2]
     local arg2 = frame.args[2]
     if not name or not prop then return "" end
     if not arg1 then return "" end
 
    -- Convar mode: first argument is the literal keyword "Convar".
    if mw.ustring.lower(arg1) == "convar" then
        if not arg2 or arg2 == "" then return "" end
        local value, found = lookup_convar(arg2)
        if not found then
            return '<span style="color:red;">Convar not found: ' .. arg2 .. '</span>'
        end
        if value == nil then return "" end
        return value
    end
 
    -- Entity mode: search abilities, heroes, and items.
    local name = arg1
    local prop = arg2
    if not prop then return "" end


     local entity, etype = find_entity(name)
     local entity, etype = find_entity(name)