Module:Sandbox/Aurelain: Difference between revisions

From Heroes of Might and Magic: Olden Era Official Wiki
No edit summary
No edit summary
Line 6: Line 6:
-- 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 or '—'
end
end


Line 83: Line 84:
}
}


--------------------------------------------------------------------------------
-- Obtains a liniar list of units, sorted by faction
-- Obtains a liniar list of units, sorted by faction
--------------------------------------------------------------------------------
local function flattenUnits()
local function flattenUnits()
local factionsOrder = {'Temple', 'Necropolis', 'Grove', 'Hive', 'Schism', 'Dungeon', 'Neutral'}
local factionsOrder = {'Temple', 'Necropolis', 'Grove', 'Hive', 'Schism', 'Dungeon', 'Neutral'}
Line 98: Line 101:
end
end


--------------------------------------------------------------------------------
-- Main public function
--------------------------------------------------------------------------------
function p.renderStatsOverview()
function p.renderStatsOverview()
     local lang = getCurrentLang()
     local lang = getCurrentLang()
Line 104: Line 110:
     local output = {}
     local output = {}
      
      
     -- Table start
     -- Start
     table.insert(output, '{| class="wikitable sortable"')
     table.insert(output, '{| class="wikitable sortable"')
      
      
     -- Header
     -- Header
     local headerRow = "! [[File:Icon QuestLog Main.png|24px]]"
     local headerRow = "! [[File:Icon QuestLog Main.png|24px|unit]]"
     for _, col in ipairs(statColumns) do
     for _, col in ipairs(statColumns) do
         headerRow = headerRow .. " !! [[File:" .. col.icon .. ".png|24px|"
         headerRow = headerRow .. " !! [[File:" .. col.icon .. ".png|24px|"
Line 119: Line 125:
     table.insert(output, headerRow)
     table.insert(output, headerRow)
      
      
     -- 3. Loop through each unit to construct the data rows
     -- Body
     for _, unitName in ipairs(unitList) do
     for _, unitName in ipairs(unitList) do
         table.insert(output, '|-')
         table.insert(output, '|-')


         local locName = get(unitName, 'name', lang)
        -- unit
         local nameX = get(unitName, 'name', lang)
         local page = unitName .. langSuffix
         local page = unitName .. langSuffix
         local imgFile = 'File:' .. unitName .. ' icon.png'
         local imgFile = 'File:' .. unitName .. ' icon.png'
         local imgLink = 'link=' .. page
         local imgLink = 'link=' .. page
         local imgTt = locName
         local imgTt = nameX
         local img = '[[' .. imgFile .. '|40px|' .. imgLink .. '|' .. imgTt .. ']]'
         local img = '[[' .. imgFile .. '|40px|' .. imgLink .. '|' .. imgTt .. ']]'
         local link = '[[' .. page .. '|' .. locName .. ']]'
         local link = '[[' .. page .. '|' .. nameX .. ']]'
         table.insert(output, '| ' .. img .. ' ' .. link)
         table.insert(output, '| ' .. img .. ' ' .. link)
          
          
         for _, col in ipairs(statColumns) do
         -- faction
            local statValue = get(unitName, col.property, lang)
        local factionEn = get(unitName, 'faction', 'en')
           
        local factionX = get(unitName, 'faction', lang)
            -- Fallback to a dash or '0' if the stat isn't found to prevent table breakage
        table.insert(output, '| [[' .. factionEn .. '_Units' .. langSuffix .. '|' .. factionX .. ']]')
            if not statValue or statValue == "" then
       
                statValue = "-"
        -- tier
            end
        table.insert(output, '| ' .. get(unitName, 'tier'))
           
       
            table.insert(output, '| ' .. statValue)
        -- cost
         end
        table.insert(output, '| ' .. get(unitName, 'cost'))
       
        -- hp
        table.insert(output, '| ' .. get(unitName, 'hp'))
       
        -- offence
        table.insert(output, '| ' .. get(unitName, 'offence'))
       
        -- defence
        table.insert(output, '| ' .. get(unitName, 'defence'))
       
        -- damageMax
        table.insert(output, '| ' .. get(unitName, 'damageMax'))
       
        -- initiative
        table.insert(output, '| ' .. get(unitName, 'initiative'))
          
        -- speed
        table.insert(output, '| ' .. get(unitName, 'speed'))
     end
     end
      
      
    -- 4. Close the table
     table.insert(output, '|}')
     table.insert(output, '|}')
   
     return table.concat(output, '\n')
    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
end


return p
return p

Revision as of 20:51, 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')

-- Wrapper for Module:Unit.get()
local function get(unit, property, lang)
	local result = UnitModule.get({ args = { unit, property, lang or "en" } })
	return result or '—'
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 = "faction",
    	label = "",
    	icon = "Icon_LawsPoint",
    },
    { 
    	property = "tier",
    	label = "",
    	icon = "Icon Resource Graal",
    },
    { 
    	property = "cost",
    	label = "",
    	icon = "Icon_Resource_Gold",
    },
    { 
    	property = "hp",
    	label = "unit_health",
    	icon = "Health Icon",
    },
    { 
    	property = "offence",
    	label = "unit_attack",
    	icon = "Icon_Stats_Attack",
    },
    { 
    	property = "defence",
    	label = "unit_defence",
    	icon = "Icon_Stats_Defence",
    },
    { 
    	property = "damageMax",
    	label = "unit_damage",
    	icon = "Icon_Stats_Damage",
    },
    { 
    	property = "initiative",
    	label = "unit_init",
    	icon = "Icon_Stats_Initiative",
    },
    { 
    	property = "speed",
    	label = "unit_speed",
    	icon = "Icon_Stats_Speed",
    },
}

--------------------------------------------------------------------------------
-- 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

--------------------------------------------------------------------------------
-- Main public function
--------------------------------------------------------------------------------
function p.renderStatsOverview()
    local lang = getCurrentLang()
    local langSuffix = getLangSuffix(lang)
    local unitList = flattenUnits()
    local output = {}
    
    -- Start
    table.insert(output, '{| class="wikitable sortable"')
    
    -- Header
    local headerRow = "! [[File:Icon QuestLog Main.png|24px|unit]]"
    for _, col in ipairs(statColumns) do
        headerRow = headerRow .. " !! [[File:" .. col.icon .. ".png|24px|"
        local tooltip = describe(col.label, lang)
        if not tooltip or tooltip == '' then
        	tooltip = col.property
        end
        headerRow = headerRow .. tooltip .. "]]"
    end
    table.insert(output, headerRow)
    
    -- 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 .. ']]')
        
        -- tier
        table.insert(output, '| ' .. get(unitName, 'tier'))
        
        -- cost
        table.insert(output, '| ' .. get(unitName, 'cost'))
        
        -- hp
        table.insert(output, '| ' .. get(unitName, 'hp'))
        
        -- offence
        table.insert(output, '| ' .. get(unitName, 'offence'))
        
        -- defence
        table.insert(output, '| ' .. get(unitName, 'defence'))
        
        -- damageMax
        table.insert(output, '| ' .. get(unitName, 'damageMax'))
        
        -- initiative
        table.insert(output, '| ' .. get(unitName, 'initiative'))
        
        -- speed
        table.insert(output, '| ' .. get(unitName, 'speed'))
    end
    
    table.insert(output, '|}')
    return table.concat(output, '\n')
end

return p