Module:GameData: Difference between revisions

Jump to navigation Jump to search
Refactor get_prop: use ResourceLookup type for direct routing, unified find_entity, clearer error messages (with help from vergir-bot LLM)
Remove redundant caching wrappers, mw.loadJsonData already caches (with help from vergir-bot LLM)
Line 78: Line 78:
-- Returns raw values for use in templates and expressions.
-- Returns raw values for use in templates and expressions.
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- 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
local function load_resource_lookup()
    if not _resource_lookup then _resource_lookup = mw.loadJsonData("Data:ResourceLookup.json") end
    return _resource_lookup
end


-- Find an entity by display name or internal key.
-- Find an entity by display name or internal key.
Line 103: Line 83:
-- Returns (entity_record, type_string) or (nil, nil).
-- Returns (entity_record, type_string) or (nil, nil).
local function find_entity(identifier)
local function find_entity(identifier)
     local resource = load_resource_lookup()[identifier:lower()]
     local resource = mw.loadJsonData("Data:ResourceLookup.json")[identifier:lower()]
     if resource then
     if resource then
         local data
         local data
         if resource.type == "ability" then
         if resource.type == "ability" then
             data = load_ability_data()
             data = mw.loadJsonData(p.Dataset.ABILITIES)
         elseif resource.type == "hero" then
         elseif resource.type == "hero" then
             data = load_hero_data()
             data = mw.loadJsonData(p.Dataset.HEROES)
         elseif resource.type == "item" then
         elseif resource.type == "item" then
             data = load_item_data()
             data = mw.loadJsonData(p.Dataset.ITEMS)
         end
         end
         if data and data[resource.key] then
         if data and data[resource.key] then
Line 120: Line 100:
     -- Fallback: try as direct internal key
     -- Fallback: try as direct internal key
     local datasets = {
     local datasets = {
         { load_ability_data, "ability" },
         { p.Dataset.ABILITIES, "ability" },
         { load_hero_data,    "hero" },
         { p.Dataset.HEROES,    "hero" },
         { load_item_data,   "item" },
         { p.Dataset.ITEMS,     "item" },
     }
     }
     for _, ds in ipairs(datasets) do
     for _, ds in ipairs(datasets) do
         local data = ds[1]()
         local data = mw.loadJsonData(ds[1])
         if data[identifier] then return data[identifier], ds[2] end
         if data[identifier] then return data[identifier], ds[2] end
     end
     end