Module:Sandbox/Aurelain

From Heroes of Might and Magic: Olden Era Official Wiki
Revision as of 19:40, 15 May 2026 by Aurelain (talk | contribs)

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

local p = {}
local UnitModule = require('Module:Unit')
local LocGetString = require('Module:LocGetString')
local factionUnits = mw.loadData('Module:Sandbox/AurelainFactionUnits')

-- Wrapper for Module:Unit.get()
local function get(unit, property, lang)
    return UnitModule.get({ args = { unit, property, lang or "en" } })
end

-- Wrapper for Module:LocGetString.getText()
local function describe(sid, lang)
    local text = LocGetString.getText({ args = { sid = sid, lang = lang } })
    return text or "—"
end

-- Detects the current page's language
local function getCurrentLang()
    local title = mw.title.getCurrentTitle()
    if title.isSubpage then
    	if mw.language.isSupportedLanguage(title.subpageText) then
        	return title.subpageText
        end
    end
    return 'en'
end


-- Suggests what "/lang" suffix to use
local function getLangSuffix(lang)
    if lang == 'en' then
    	return ''
    end
    return '/' .. lang
end

local statColumns = {
    { 
    	property = "initiative",
    	label = "unit_init",
    	icon = "Icon_Stats_Initiative",
    },
    { 
    	property = "speed",
    	label = "unit_speed",
    	icon = "Icon_Stats_Speed",
    },
    { 
    	property = "hp",
    	label = "unit_health",
    	icon = "Health Icon",
    }
}

-- Factions
local factionIcons = {
    Temple = "Human icon.png",
    Necropolis = "Undead icon.png", 
    Dungeon = "Dungeon icon.png",
    Schism = "Unfrozen icon.png",
    Sylvan = "Spring icon.png",
    Hive = "Hive icon.png",
    Neutral = "Primordial_Chaos.png"
}

local factionsOrder = {
    "Temple",
    "Necropolis",
    "Dungeon",
    "Schism",
    "Sylvan",
    "Hive",
    "Neutral"
}

-- Obtains a liniar list of units, sorted by faction
local function flattenUnits()
	local factionsOrder = {'Temple', 'Necropolis', 'Grove', 'Hive', 'Schism', 'Dungeon', 'Neutral'}
    local result = {}
    for _, key in ipairs(factionsOrder) do
        local subArray = factionUnits[key]
        if type(subArray) == "table" then
            for _, value in ipairs(subArray) do
                table.insert(result, value)
            end
        end
    end
    return result
end

function p.renderStatsOverview()
    local lang = getCurrentLang()
    local langSuffix = getLangSuffix(lang)
    local unitList = flattenUnits()
    
    -- We use an array to collect wikitext pieces for better performance
    local output = {}
    
    -- 1. Start the sortable wikitext table
    table.insert(output, '{| class="wikitable sortable"')
    
    -- 2. Construct the Header Row
    local headerRow = "! [[File:Diplomacy_sub_larger_than_life.png|24px]]"
    for _, col in ipairs(statColumns) do
        headerRow = headerRow .. " !! [[File:" .. col.icon .. ".png|24px|"
        headerRow = headerRow .. describe(col.label, lang) .. "]]"
    end
    table.insert(output, headerRow)
    
    -- 3. Loop through each unit to construct the data rows
    for _, unitName in ipairs(unitList) do
        table.insert(output, '|-')

        local locName = get(unitName, 'name', lang)
        local page = unitName .. langSuffix
        local imgFile = 'File:' .. unitName .. ' icon.png'
        local imgLink = 'link=' .. page
        local imgTt = locName
        local img = '[[' .. imgFile .. '|32px|' .. imgLink .. '|' .. imgTt .. ']]'
        local link = '[[' .. page .. '|' .. img .. '|' .. locName .. ']]'
        table.insert(output, '| ' .. link)
        
        -- Loop through each configured stat column and fetch the value
        for _, col in ipairs(statColumns) do
            local statValue = get(unitName, col.property, lang)
            
            -- Fallback to a dash or '0' if the stat isn't found to prevent table breakage
            if not statValue or statValue == "" then
                statValue = "-"
            end
            
            table.insert(output, '| ' .. statValue)
        end
    end
    
    -- 4. Close the table
    table.insert(output, '|}')
    
    local u1 = get('Angel', 'upgradeName');
    local u1name = get(u1, 'name', lang);
    
    -- Combine all pieces with newline characters and return the final wikitext
    return table.concat(output, '\n') .. "test:" .. "[[File:Spelaean Scorpion icon.png|40px|link=Spelaean Scorpion]]"
end

return p