Module:Sandbox/Aurelain: Difference between revisions

From Heroes of Might and Magic: Olden Era Official Wiki
Created page with "local p = {} -- A simple function that returns a custom greeting function p.hello(frame) -- 'frame' contains interface data from the wiki page calling this module local name = frame.args[1] or "World" return "Hello, " .. name .. "! Welcome to my Lua sandbox." end return p"
 
No edit summary
Line 1: Line 1:
local p = {}
local p = {}


-- A simple function that returns a custom greeting
-- 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)
function p.hello(frame)
     -- 'frame' contains interface data from the wiki page calling this module
     return dataCache[frame.args[1]].name
    local name = frame.args[1] or "World"
    return "Hello, " .. name .. "! Welcome to my Lua sandbox."
end
end


return p
return p

Revision as of 16:02, 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)
    return dataCache[frame.args[1]].name
end

return p