Module:GameData: Difference between revisions

Vergir (talk | contribs)
m skip AutoRegisterModifierValueFromAbilityPropertyName on property matchin
Vergir (talk | contribs)
Add get_prop: unified property lookup across abilities, heroes, and items (with help from vergir-bot LLM)
Line 71: Line 71:


     return results
     return results
end
--------------------------------------------------------------------------------
-- Unified property lookup: get_prop
-- Searches Abilities → Heroes → Items, returns raw values for use in templates.
--------------------------------------------------------------------------------
-- Lazy-loaded data caches
local _ability_data, _item_data, _hero_data, _resource_lookup
local function load_ability_data()
    if not _ability_data then _ability_data = mw.loadJsonData(p.Dataset.ABILITIES) end
    return _ability_data
end
local function load_item_data()
    if not _item_data then _item_data = mw.loadJsonData(p.Dataset.ITEMS) end
    return _item_data
end
local function load_hero_data()
    if not _hero_data then _hero_data = mw.loadJsonData(p.Dataset.HEROES) end
    return _hero_data
end
-- Find an ability by key or display name (mirrors Module:Abilities logic)
local function find_ability(identifier)
    local data = load_ability_data()
    if data[identifier] then return data[identifier] end
    if not _resource_lookup then
        _resource_lookup = mw.loadJsonData("Data:ResourceLookup.json")
    end
    local resource = _resource_lookup[identifier:lower()]
    if resource then return data[resource.key] end
    return nil
end
-- Find an item by display name (mirrors Module:ItemData logic, prefers active)
local function find_item(name)
    local data = load_item_data()
    for _, v in pairs(data) do
        if v["Name"] == name and is_active(v) then return v end
    end
    for _, v in pairs(data) do
        if v["Name"] == name then return v end
    end
    return nil
end
-- Find a hero by key or display name (mirrors Module:HeroData logic)
local function find_hero(identifier)
    local data = load_hero_data()
    if data[identifier] then return data[identifier] end
    for _, v in pairs(data) do
        if v["Name"] == identifier then return v end
    end
    return nil
end
-- Traverse a table using dot notation, unwrap tables with a .Value field
local function resolve_prop(tbl, prop)
    if not prop or prop == "" then return nil end
    local element = tbl
    for segment in string.gmatch(prop, "[^%.]+") do
        if type(element) ~= "table" then return nil end
        element = element[segment]
        if element == nil then return nil end
    end
    if type(element) == "table" then
        return element.Value or ""
    end
    return element
end
-- {{#invoke:GameData|get_prop|ENTITY_NAME|PROPERTY}}
-- Searches Abilities → Heroes → Items. Returns the raw value (no formatting).
-- Supports dot notation for nested properties (e.g. "Scale.Value").
-- Tables with a .Value field are automatically unwrapped.
function p.get_prop(frame)
    local name = frame.args[1]
    local prop = frame.args[2]
    if not name or not prop then return "" end
    -- 1. Abilities
    local entity = find_ability(name)
    if entity then
        local result = resolve_prop(entity, prop)
        if result ~= nil then return result end
    end
    -- 2. Heroes (also checks Weapon / Weapon.AltFire)
    entity = find_hero(name)
    if entity then
        local result = resolve_prop(entity, prop)
        if result ~= nil then return result end
        if entity.Weapon then
            result = resolve_prop(entity.Weapon, prop)
            if result ~= nil then return result end
            if entity.Weapon.AltFire then
                result = resolve_prop(entity.Weapon.AltFire, prop)
                if result ~= nil then return result end
            end
        end
    end
    -- 3. Items
    entity = find_item(name)
    if entity then
        local result = resolve_prop(entity, prop)
        if result ~= nil then return result end
    end
    return ""
end
end


return p
return p