Module:GameData: Difference between revisions

Jump to navigation Jump to search
Vergir (talk | contribs)
Return visible error message when entity/property not found in get_prop (with help from vergir-bot LLM)
Refactor get_prop: use ResourceLookup type for direct routing, unified find_entity, clearer error messages (with help from vergir-bot LLM)
Line 75: Line 75:
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Unified property lookup: get_prop
-- Unified property lookup: get_prop
-- Searches Abilities → Heroes → Items, returns raw values for use in templates.
-- Uses ResourceLookup to route directly to the correct dataset.
-- Returns raw values for use in templates and expressions.
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


Line 92: Line 93:
     if not _hero_data then _hero_data = mw.loadJsonData(p.Dataset.HEROES) end
     if not _hero_data then _hero_data = mw.loadJsonData(p.Dataset.HEROES) end
     return _hero_data
     return _hero_data
end
local function load_resource_lookup()
    if not _resource_lookup then _resource_lookup = mw.loadJsonData("Data:ResourceLookup.json") end
    return _resource_lookup
end
end


-- Find an ability by key or display name (mirrors Module:Abilities logic)
-- Find an entity by display name or internal key.
local function find_ability(identifier)
-- Uses ResourceLookup type field to go directly to the right dataset.
     local data = load_ability_data()
-- Returns (entity_record, type_string) or (nil, nil).
     if data[identifier] then return data[identifier] end
local function find_entity(identifier)
    if not _resource_lookup then
     local resource = load_resource_lookup()[identifier:lower()]
         _resource_lookup = mw.loadJsonData("Data:ResourceLookup.json")
     if resource then
        local data
        if resource.type == "ability" then
            data = load_ability_data()
         elseif resource.type == "hero" then
            data = load_hero_data()
        elseif resource.type == "item" then
            data = load_item_data()
        end
        if data and data[resource.key] then
            return data[resource.key], resource.type
        end
     end
     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)
    -- Fallback: try as direct internal key
local function find_item(name)
     local datasets = {
     local data = load_item_data()
        { load_ability_data, "ability" },
     for _, v in pairs(data) do
        { load_hero_data,    "hero" },
         if v["Name"] == name and is_active(v) then return v end
        { load_item_data,    "item" },
    }
     for _, ds in ipairs(datasets) do
         local data = ds[1]()
        if data[identifier] then return data[identifier], ds[2] end
     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)
     return nil, nil
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
end


Line 144: Line 148:


-- {{#invoke:GameData|get_prop|ENTITY_NAME|PROPERTY}}
-- {{#invoke:GameData|get_prop|ENTITY_NAME|PROPERTY}}
-- Searches Abilities → Heroes → Items. Returns the raw value (no formatting).
-- Finds entity via ResourceLookup, returns the raw value (no formatting).
-- Supports dot notation for nested properties (e.g. "Scale.Value").
-- Supports dot notation for nested properties (e.g. "Scale.Value").
-- Tables with a .Value field are automatically unwrapped.
-- Tables with a .Value field are automatically unwrapped.
Line 152: Line 156:
     if not name or not prop then return "" end
     if not name or not prop then return "" end


    -- 1. Abilities
     local entity, etype = find_entity(name)
     local entity = find_ability(name)
     if not entity then
     if entity then
         return '<span style="color:red;">Entity not found: ' .. name .. '</span>'
         local result = resolve_prop(entity, prop)
        if result ~= nil then return result end
     end
     end


     -- 2. Heroes (also checks Weapon / Weapon.AltFire)
     local result = resolve_prop(entity, prop)
    entity = find_hero(name)
    if result ~= nil then return result end
    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
     if entity.Weapon then
    entity = find_item(name)
         result = resolve_prop(entity.Weapon, prop)
     if entity then
         local result = resolve_prop(entity, prop)
         if result ~= nil then return result end
         if result ~= nil then return result end
     end
     end


     return '<span style="color:red;">Prop not found: ' .. name .. ' | ' .. prop .. '</span>'
     return '<span style="color:red;">Prop not found: ' .. name .. '/' .. prop .. '</span>'
end
end


return p
return p