Module:Sandbox/Aurelain/Cargo: Difference between revisions

From Heroes of Might and Magic: Olden Era Official Wiki
No edit summary
No edit summary
Line 70: Line 70:
         if not units[id] then
         if not units[id] then
             units[id] = {
             units[id] = {
                 hp = tonumber(row['hp']) or 0,
            faction=row['faction'],
                 attack = tonumber(row['attack']) or 0,
            tier=row['tier'],
                 hp = row['hp'],
                 attack = row['attack'],
                 nameEn = nil,
                 nameEn = nil,
                 nameX = nil
                 nameX = nil
Line 93: Line 95:
      
      
     return unitsArray
     return unitsArray
end
--------------------------------------------------------------------------------
-- Decides the order between two units, for sorting purposes.
--------------------------------------------------------------------------------
local function sortUnits(unitA, unitB)
    local rankA = FACTION_ORDER[unitA.faction] or 99
    local rankB = FACTION_ORDER[unitB.faction] or 99
   
    if rankA ~= rankB then
        return rankA < rankB
    end
   
    if unitA.tier ~= unitB.tier then
        return unitA.tier < unitB.tier
    end
   
    local nameA = unitA.enName or ""
    local nameB = unitB.enName or ""
    return nameA < nameB
end
end


Line 102: Line 124:
     local results = query(lang)
     local results = query(lang)
     local units = consolidateResults(results, lang)
     local units = consolidateResults(results, lang)
    table.sort(units, sortUnits)


     -- 3. Create the HTML table
     -- Table
     local htmlTable = mw.html.create('table')
     local htmlTable = mw.html.create('table')
     htmlTable:addClass('wikitable sortable')
     htmlTable:addClass('wikitable sortable')
          
          
    -- Header
     local header = htmlTable:tag('tr')
     local header = htmlTable:tag('tr')
     header:tag('th'):wikitext('Unit Name'):done()
     header:tag('th'):wikitext('Unit Name'):done()
Line 112: Line 136:
     header:tag('th'):wikitext('Attack'):done()
     header:tag('th'):wikitext('Attack'):done()


    -- Body
     for _, u in ipairs(units) do
     for _, u in ipairs(units) do
         local page = u.nameEn
         local page = u.nameEn .. (lang ~= 'en' and '/' .. lang or '')
        if lang ~= 'en' then
            page = page .. '/' .. lang
        end
       
         local tr = htmlTable:tag('tr')
         local tr = htmlTable:tag('tr')
         tr:tag('td'):wikitext('[[' .. page .. '|' .. u.nameX .. ']]'):done()  
         tr:tag('td'):wikitext('[[' .. page .. '|' .. u.nameX .. ']]'):done()  

Revision as of 15:14, 16 May 2026

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

local p = {}
local FACTION_ORDER = {
    human = 1, 
    undead = 2, 
    nature = 3,
    demon = 4, 
    unfrozen = 5, 
    dungeon = 6, 
    neutral = 7,
}
local TRANSLATIONS_PATCH = {
    unit = {
        en = 'Unit',
    },
    faction = {
        en = 'Faction',
    },
    tier = {
        en = 'Tier',
    }
}

--------------------------------------------------------------------------------
-- Detects the current language from the URL
--------------------------------------------------------------------------------
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

--------------------------------------------------------------------------------
-- Retrieves the data from Cargo
--------------------------------------------------------------------------------
local function query(lang)
    local tables = 'Unit, Translation'
    local fields = '' .. 
               'Unit.id=id, ' ..
               'Unit.faction=faction, ' ..
               'Unit.tier=tier, ' ..
               'Unit.hp=hp, ' ..
               'Unit.offence=attack, ' ..
               'Translation.language=language, ' ..
               'Translation.name=transName'
    
    -- Get BOTH the English row AND the target language row
    local cargoArgs = {
        join = 'Unit.id = Translation.target_id',
        where = 'Translation.language = "en" OR Translation.language = "' .. lang .. '"',
        orderBy = 'Unit.id ASC',
        limit = 1000 -- Increased limit since each unit will return 2 rows
    }
    
    return mw.ext.cargo.query(tables, fields, cargoArgs)
end

--------------------------------------------------------------------------------
-- Scans the Cargo results and folds the EN and lang rows into a single row
--------------------------------------------------------------------------------
local function consolidateResults(results, lang)
    local units = {}
    for _, row in ipairs(results) do
        local id = row['id']
        
        -- If this is the first time seeing this id, set up its basic stats
        if not units[id] then
            units[id] = {
            	faction=row['faction'],
            	tier=row['tier'],
                hp = row['hp'],
                attack = row['attack'],
                nameEn = nil,
                nameX = nil
            }
        end
        
        -- Save the names based on which language row we are currently looping over
        if row['language'] == 'en' then
            units[id].nameEn = row['transName']
        end
        if row['language'] == lang then
            units[id].nameX = row['transName']
        end
    end
    
    -- Convert the Dictionary into an Array
    local unitsArray = {}
    for _, unitData in pairs(units) do
        table.insert(unitsArray, unitData)
    end
    
    return unitsArray
end

--------------------------------------------------------------------------------
-- Decides the order between two units, for sorting purposes.
--------------------------------------------------------------------------------
local function sortUnits(unitA, unitB)
    local rankA = FACTION_ORDER[unitA.faction] or 99
    local rankB = FACTION_ORDER[unitB.faction] or 99
    
    if rankA ~= rankB then
        return rankA < rankB
    end
    
    if unitA.tier ~= unitB.tier then
        return unitA.tier < unitB.tier
    end
    
    local nameA = unitA.enName or ""
    local nameB = unitB.enName or ""
    return nameA < nameB
end

--------------------------------------------------------------------------------
-- Main public function
--------------------------------------------------------------------------------
function p.displayOverview()
    local lang = 'it' --getCurrentLang()
    local results = query(lang)
    local units = consolidateResults(results, lang)
    table.sort(units, sortUnits)

    -- Table
    local htmlTable = mw.html.create('table')
    htmlTable:addClass('wikitable sortable')
        
    -- Header
    local header = htmlTable:tag('tr')
    header:tag('th'):wikitext('Unit Name'):done()
    header:tag('th'):wikitext('Health'):done()
    header:tag('th'):wikitext('Attack'):done()

    -- Body
    for _, u in ipairs(units) do
        local page = u.nameEn .. (lang ~= 'en' and '/' .. lang or '')
        local tr = htmlTable:tag('tr')
        tr:tag('td'):wikitext('[[' .. page .. '|' .. u.nameX .. ']]'):done() 
        tr:tag('td'):wikitext(tostring(u.hp)):done()
        tr:tag('td'):wikitext(tostring(u.attack)):done()
    end

    return tostring(htmlTable)
end

return p