Module:Sandbox/Aurelain: Difference between revisions

From Heroes of Might and Magic: Olden Era Official Wiki
No edit summary
No edit summary
 
(29 intermediate revisions by the same user not shown)
Line 2: Line 2:
local UnitModule = require('Module:Unit')
local UnitModule = require('Module:Unit')
local LocGetString = require('Module:LocGetString')
local LocGetString = require('Module:LocGetString')
local factionUnits = mw.loadData('Module:Sandbox/AurelainFactionUnits')
local statColumns = {
    {
    label = "Faction", -- forced EN
    icon = "Icon_LawsPoint",
    },
    {
    label = "Tier", -- forced EN
    icon = "Icon Resource Graal",
    },
    {
    label = "Cost", -- forced EN
    icon = "Icon_Resource_Gold",
    },
    {
    label = "unit_health",
    icon = "Health Icon",
    },
    {
    label = "unit_attack",
    icon = "Icon_Stats_Attack",
    },
    {
    label = "unit_defence",
    icon = "Icon_Stats_Defence",
    },
    {
    label = "unit_damage",
    icon = "Icon_Stats_Damage",
    },
    {
    label = "unit_init",
    icon = "Icon_Stats_Initiative",
    },
    {
    label = "unit_speed",
    icon = "Icon_Stats_Speed",
    },
}
local factionsOrder = {
'Temple',
'Necropolis',
'Grove',
'Hive',
'Schism',
'Dungeon',
'Neutral',
}


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


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


-- Detects the "/lang" suffix of the page url
--------------------------------------------------------------------------------
-- Detects the current page's language
--------------------------------------------------------------------------------
local function getCurrentLang()
local function getCurrentLang()
     local title = mw.title.getCurrentTitle()
     local title = mw.title.getCurrentTitle()
     if title.isSubpage and mw.language.isSupportedLanguage() then
     if title.isSubpage then
        return title.subpageText
    if mw.language.isSupportedLanguage(title.subpageText) then
        return title.subpageText
        end
     end
     end
     return 'en'
     return 'en'
end
end


--------------------------------------------------------------------------------
-- Suggests what "/lang" suffix to use
--------------------------------------------------------------------------------
local function getLangSuffix(lang)
    return lang == 'en' and '' or '/' .. 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"
}


-- Define the list of units to include in the table.
--------------------------------------------------------------------------------
-- (This could also be fetched dynamically from SMW/Cargo or frame arguments if needed).
-- Obtains a liniar list of units, sorted by faction
local unitList = {
--------------------------------------------------------------------------------
     "Angel",
local function flattenUnits()
     "Archangel",
    local result = {}
     "Apotheosis"
     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


--------------------------------------------------------------------------------
-- Table header
--------------------------------------------------------------------------------
local function generateTableHeader(lang)
local row = '!'
row = row .. '[[File:Icon QuestLog Main.png|24px|Unit]]'
    for _, col in ipairs(statColumns) do
        row = row .. ' !! [[File:' .. col.icon .. '.png|24px|' .. describe(col.label, lang) .. ']]'
    end
    return row
end


--------------------------------------------------------------------------------
-- Main public function
--------------------------------------------------------------------------------
function p.renderStatsOverview()
function p.renderStatsOverview()
     local lang = getCurrentLang()
     local lang = getCurrentLang()
     local langSuffix = lang == "en" and "" or "/" .. lang
     local langSuffix = getLangSuffix(lang)
      
     local unitList = flattenUnits()
    -- We use an array to collect wikitext pieces for better performance
     local output = {}
     local output = {}
      
      
     -- 1. Start the sortable wikitext table
     -- Start table
     table.insert(output, '{| class="wikitable sortable"')
     table.insert(output, '{| class="wikitable sortable"')
    table.insert(output, generateTableHeader(lang))
      
      
     -- 2. Construct the Header Row
     -- Body
    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
     for _, unitName in ipairs(unitList) do
         table.insert(output, '|-')
         table.insert(output, '|-')
        -- unit
        local nameX = get(unitName, 'name', lang)
        local page = unitName .. langSuffix
        local imgFile = 'File:' .. unitName .. ' icon.png'
        local imgLink = 'link=' .. page
        local imgTt = nameX
        local img = '[[' .. imgFile .. '|40px|' .. imgLink .. '|' .. imgTt .. ']]'
        local link = '[[' .. page .. '|' .. nameX .. ']]'
        table.insert(output, '| ' .. img .. ' ' .. link)
          
          
         -- Add the unit name column (formatted as a wiki link)
         -- faction
         table.insert(output, '| [[' .. unitName .. '/' .. lang .. ']]')
        local factionEn = get(unitName, 'faction', 'en')
        local factionX = get(unitName, 'faction', lang)
         table.insert(output, '| [[' .. factionEn .. '_Units' .. langSuffix .. '|' .. factionX .. ']]')
          
          
         -- Loop through each configured stat column and fetch the value
         -- the rest
         for _, col in ipairs(statColumns) do
         table.insert(output, '| ' .. get(unitName, 'tier'))
            local statValue = get(unitName, col.property, lang)
        table.insert(output, '| ' .. get(unitName, 'cost'))
           
        table.insert(output, '| ' .. get(unitName, 'hp'))
            -- Fallback to a dash or '0' if the stat isn't found to prevent table breakage
        table.insert(output, '| ' .. get(unitName, 'offence'))
            if not statValue or statValue == "" then
        table.insert(output, '| ' .. get(unitName, 'defence'))
                statValue = "-"
        table.insert(output, '| ' .. get(unitName, 'damageMin') .. '-' .. get(unitName, 'damageMax'))
            end
        table.insert(output, '| ' .. get(unitName, 'initiative'))
           
         table.insert(output, '| ' .. get(unitName, 'speed'))
            table.insert(output, '| ' .. statValue)
         end
     end
     end
      
      
    -- 4. Close the table
     table.insert(output, '|}')
     table.insert(output, '|}')
   
    -- Combine all pieces with newline characters and return the final wikitext
     return table.concat(output, '\n')
     return table.concat(output, '\n')
end
end


return p
return p

Latest revision as of 21:23, 15 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')
local factionUnits = mw.loadData('Module:Sandbox/AurelainFactionUnits')
local statColumns = {
    { 
    	label = "Faction", -- forced EN
    	icon = "Icon_LawsPoint",
    },
    { 
    	label = "Tier", -- forced EN
    	icon = "Icon Resource Graal",
    },
    { 
    	label = "Cost", -- forced EN
    	icon = "Icon_Resource_Gold",
    },
    { 
    	label = "unit_health",
    	icon = "Health Icon",
    },
    { 
    	label = "unit_attack",
    	icon = "Icon_Stats_Attack",
    },
    { 
    	label = "unit_defence",
    	icon = "Icon_Stats_Defence",
    },
    { 
    	label = "unit_damage",
    	icon = "Icon_Stats_Damage",
    },
    { 
    	label = "unit_init",
    	icon = "Icon_Stats_Initiative",
    },
    { 
    	label = "unit_speed",
    	icon = "Icon_Stats_Speed",
    },
}
local factionsOrder = {
	'Temple', 
	'Necropolis', 
	'Grove', 
	'Hive', 
	'Schism', 
	'Dungeon', 
	'Neutral',
}

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

--------------------------------------------------------------------------------
-- Wrapper for Module:LocGetString.getText()
--------------------------------------------------------------------------------
local function describe(sid, lang)
    local result = LocGetString.getText({ args = { sid = sid, lang = lang } })
    return (result and result ~= '') and result or sid
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)
    return lang == 'en' and '' or '/' .. lang
end


--------------------------------------------------------------------------------
-- Obtains a liniar list of units, sorted by faction
--------------------------------------------------------------------------------
local function flattenUnits()
    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

--------------------------------------------------------------------------------
-- Table header
--------------------------------------------------------------------------------
local function generateTableHeader(lang)
	local row = '!'
	row = row .. '[[File:Icon QuestLog Main.png|24px|Unit]]'
    for _, col in ipairs(statColumns) do
        row = row .. ' !! [[File:' .. col.icon .. '.png|24px|' .. describe(col.label, lang) .. ']]'
    end
    return row
end

--------------------------------------------------------------------------------
-- Main public function
--------------------------------------------------------------------------------
function p.renderStatsOverview()
    local lang = getCurrentLang()
    local langSuffix = getLangSuffix(lang)
    local unitList = flattenUnits()
    local output = {}
    
    -- Start table
    table.insert(output, '{| class="wikitable sortable"')
    table.insert(output, generateTableHeader(lang))
    
    -- Body
    for _, unitName in ipairs(unitList) do
        table.insert(output, '|-')

        -- unit
        local nameX = get(unitName, 'name', lang)
        local page = unitName .. langSuffix
        local imgFile = 'File:' .. unitName .. ' icon.png'
        local imgLink = 'link=' .. page
        local imgTt = nameX
        local img = '[[' .. imgFile .. '|40px|' .. imgLink .. '|' .. imgTt .. ']]'
        local link = '[[' .. page .. '|' .. nameX .. ']]'
        table.insert(output, '| ' .. img .. ' ' .. link)
        
        -- faction
        local factionEn = get(unitName, 'faction', 'en')
        local factionX = get(unitName, 'faction', lang)
        table.insert(output, '| [[' .. factionEn .. '_Units' .. langSuffix .. '|' .. factionX .. ']]')
        
        -- the rest
        table.insert(output, '| ' .. get(unitName, 'tier'))
        table.insert(output, '| ' .. get(unitName, 'cost'))
        table.insert(output, '| ' .. get(unitName, 'hp'))
        table.insert(output, '| ' .. get(unitName, 'offence'))
        table.insert(output, '| ' .. get(unitName, 'defence'))
        table.insert(output, '| ' .. get(unitName, 'damageMin') .. '-' .. get(unitName, 'damageMax'))
        table.insert(output, '| ' .. get(unitName, 'initiative'))
        table.insert(output, '| ' .. get(unitName, 'speed'))
    end
    
    table.insert(output, '|}')
    return table.concat(output, '\n')
end

return p