Module:Sandbox/Aurelain: Difference between revisions

From Heroes of Might and Magic: Olden Era Official Wiki
No edit summary
No edit summary
Line 34: Line 34:
     return dataCache
     return dataCache
end
end
local Unit = require('Module:Unit')


function p.hello(frame)
function p.hello(frame)
unitName = frame.args[1]
unitName = frame.args[1]
     local units = loadUnits()
     return Unit.get(name, "initiative")
    local unit = units[unitName]
    return unit.id
end
end


return p
return p

Revision as of 16:11, 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

-- Function to load JSON data from wiki page
local function loadJson(title)
    local page = mw.title.new(title)
    local content = page and page:getContent() or ""
    if content == "" then return {} end
    local ok, data = pcall(mw.text.jsonDecode, content)
    if not ok then return {} end
    return data
end

-- Load unit data from unitdata_work.json
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

local Unit = require('Module:Unit')

function p.hello(frame)
	unitName = frame.args[1]
    return Unit.get(name, "initiative")
end

return p