Module:Sandbox/Aurelain: Difference between revisions

From Heroes of Might and Magic: Olden Era Official Wiki
No edit summary
No edit summary
Line 1: Line 1:
local p = {}
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')
local Unit = require('Module:Unit')


-- A clean, private Lua function that wraps the target module's frame logic
-- Wrapper function that allows us to easily invoke the Module:Unit.
local function get(userName, baseLocation)
local function get(unit, property, lang)
     -- 1. Construct the fake frame using the arguments passed to this function
     return Unit.get({
    local fakeFrame = {
         args = {
         args = {
             [1] = userName,
             ["unit"] = unit,
             ["base"] = baseLocation
            ["property"] = property,
             ["lang"] = lang
         }
         }
     }
     })
   
    -- 2. Call the target module and return its output directly
    return Unit.get(fakeFrame)
end
end




function p.hello(frame)
function p.hello(frame)
     return Unit.get(frame)
     return get('Archangel', 'initiative')
end
end


return p
return p

Revision as of 15:16, 14 May 2026

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

local p = {}
local Unit = require('Module:Unit')

-- Wrapper function that allows us to easily invoke the Module:Unit.
local function get(unit, property, lang)
    return Unit.get({
        args = {
            ["unit"] = unit,
            ["property"] = property,
            ["lang"] = lang
        }
    })
end


function p.hello(frame)
    return get('Archangel', 'initiative')
end

return p