Module:Sandbox/Aurelain: Difference between revisions

From Heroes of Might and Magic: Olden Era Official Wiki
No edit summary
No edit summary
Line 12: Line 12:
     local text = LocGetString.getText({ args = { sid = sid, lang = lang } })
     local text = LocGetString.getText({ args = { sid = sid, lang = lang } })
     return text or "—"
     return text or "—"
end
-- Detects the "/lang" suffix of the page url
local function getCurrentLang()
    local title = mw.title.getCurrentTitle()
    if title.isSubpage then
        return title.subpageText
    end
    return 'en'
end
end


Line 53: Line 62:




function p.renderStatsOverview(frame)
function p.renderStatsOverview()
    -- Allow passing language code from the template, defaulting to 'en'
     local lang = getCurrentLang()
    local args = frame.args
     local lang = mw.text.trim(args[1] or args.lang or "en")
      
      
     -- We use an array to collect wikitext pieces for better performance
     -- We use an array to collect wikitext pieces for better performance
Line 77: Line 84:
          
          
         -- Add the unit name column (formatted as a wiki link)
         -- Add the unit name column (formatted as a wiki link)
         table.insert(output, '| [[' .. unitName .. ']]')
         table.insert(output, '| [[' .. unitName .. '/' .. lang .. ']]')
          
          
         -- Loop through each configured stat column and fetch the value
         -- Loop through each configured stat column and fetch the value

Revision as of 16:52, 14 May 2026

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

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

-- 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 "/lang" suffix of the page url
local function getCurrentLang()
    local title = mw.title.getCurrentTitle()
    if title.isSubpage then
        return title.subpageText
    end
    return 'en'
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"
}

-- Define the list of units to include in the table.
-- (This could also be fetched dynamically from SMW/Cargo or frame arguments if needed).
local unitList = {
    "Angel",
    "Archangel",
    "Apotheosis"
}


function p.renderStatsOverview()
    local lang = getCurrentLang()
    
    -- 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]]"
    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, '|-')
        
        -- Add the unit name column (formatted as a wiki link)
        table.insert(output, '| [[' .. unitName .. '/' .. lang .. ']]')
        
        -- 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, '|}')
    
    -- Combine all pieces with newline characters and return the final wikitext
    return table.concat(output, '\n')
end

return p