Module:Sandbox/Aurelain: Difference between revisions

From Heroes of Might and Magic: Olden Era Official Wiki
No edit summary
No edit summary
Line 25: Line 25:


function p.hello(frame)
function p.hello(frame)
loadUnits()
     return dataCache[frame.args[1]].name
     return dataCache[frame.args[1]].name
end
end


return p
return p

Revision as of 16:03, 13 May 2026

Documentation for this module may be created at Module:Sandbox/Aurelain/doc

local p = {}

-- Cache for data
local dataCache = nil
-- Cache for reverse lookup: id -> unitName
local idIndexCache = nil

local function loadUnits()
    if dataCache then return dataCache end
    dataCache = {}
    idIndexCache = {}

    local units = loadJson("Data:units.json")

    for name, unit in pairs(units) do
        dataCache[name] = unit
        -- Build reverse index: id -> display name (key in the JSON object)
        if unit.id and unit.id ~= "" then
            idIndexCache[unit.id] = name
        end
    end

    return dataCache
end

function p.hello(frame)
	loadUnits()
    return dataCache[frame.args[1]].name
end

return p