Module:Sandbox/Aurelain/Heroes: Difference between revisions

From Heroes of Might and Magic: Olden Era Official Wiki
No edit summary
No edit summary
Line 2: Line 2:
local p = {}
local p = {}
local SkillsOverview = require("Module:SkillsOverview")
local SkillsOverview = require("Module:SkillsOverview")
local FACTION_ORDER = {
    human =    1,
    undead =    2,
    nature =    3,
    demon =    4,
    unfrozen =  5,
    dungeon =  6,
}


local FACTION_MAPPING = {
local FACTION_MAPPING = {
Line 10: Line 19:
     Schism =    'unfrozen',
     Schism =    'unfrozen',
     Dungeon =    'dungeon',
     Dungeon =    'dungeon',
}
local MIGHT_MAGIC_ORDER = {
    might =    1,
    magic =    2,
}
}


Line 15: Line 29:
local TRANSLATION_IDS = {
local TRANSLATION_IDS = {
     wiki_name = 'Name',
     wiki_name = 'Name',
    wiki_faction = 'Faction',
    wiki_hero_class_type = 'Class Type',
    wiki_hero_class = 'Class',
    wiki_hero_specialty = 'Specialty',
    wiki_faction_skill = 'Faction Skill',
    wiki_heroes_starting_skill = 'Starting Skill',
    wiki_spell = 'Starting Spell',
}
}


Line 75: Line 96:
     local fields = {}
     local fields = {}
     table.insert(fields, 'Hero.id = id');
     table.insert(fields, 'Hero.id = id');
    table.insert(fields, 'Hero.class_id = class_id');
     table.insert(fields, 'Hero.faction = faction');
     table.insert(fields, 'Hero.faction = faction');
     table.insert(fields, 'Hero.class_id = class_id');
     table.insert(fields, 'Hero.class_type = class_type');
     table.insert(fields, 'Hero.icon = icon');
     table.insert(fields, 'Hero.icon = icon');
     table.insert(fields, 'Hero.specialization_id = specialization_id');
     table.insert(fields, 'Hero.specialization_id = specialization_id');
Line 97: Line 119:
     return mw.ext.cargo.query('Hero, Translation', table.concat(fields, ','), {
     return mw.ext.cargo.query('Hero, Translation', table.concat(fields, ','), {
         join = 'Hero.id = Translation.target_id',
         join = 'Hero.id = Translation.target_id',
        where = table.concat(where, ' AND '),
        limit = 500
    })
end
------------------------------------------------------------------------------------------------------------------------
-- Interrogates the HeroClass Cargo table
------------------------------------------------------------------------------------------------------------------------
local function queryHeroClass(lang)
    -- fields:
    local fields = {}
    table.insert(fields, 'HeroClass.id = id');
    table.insert(fields, 'Translation.name = name');
    table.insert(fields, 'Translation.language = language');
    -- where:
    local where = {}
    table.insert(where, '(Translation.language = "en" OR Translation.language = "' .. lang .. '")')
    -- query:
    return mw.ext.cargo.query('HeroClass, Translation', table.concat(fields, ','), {
        join = 'HeroClass.id = Translation.target_id',
         where = table.concat(where, ' AND '),
         where = table.concat(where, ' AND '),
         limit = 500
         limit = 500
Line 143: Line 143:
     return mw.ext.cargo.query('HeroSpecialization, Translation', table.concat(fields, ','), {
     return mw.ext.cargo.query('HeroSpecialization, Translation', table.concat(fields, ','), {
         join = 'HeroSpecialization.id = Translation.target_id',
         join = 'HeroSpecialization.id = Translation.target_id',
        where = table.concat(where, ' AND '),
        limit = 500
    })
end
------------------------------------------------------------------------------------------------------------------------
-- Interrogates the Spell Cargo table
------------------------------------------------------------------------------------------------------------------------
local function querySpell(lang)
    -- fields:
    local fields = {}
    table.insert(fields, 'Spell.id = id');
    table.insert(fields, 'Spell.icon = icon');
    table.insert(fields, 'Translation.name = name');
    table.insert(fields, 'Translation.language = language');
    -- where:
    local where = {}
    table.insert(where, '(Translation.language = "en" OR Translation.language = "' .. lang .. '")')
    -- query:
    return mw.ext.cargo.query('Spell, Translation', table.concat(fields, ','), {
        join = 'Spell.id = Translation.target_id',
         where = table.concat(where, ' AND '),
         where = table.concat(where, ' AND '),
         limit = 500
         limit = 500
Line 182: Line 159:
             entry = {}
             entry = {}
             entry.id = id
             entry.id = id
            entry.class_id = row.class_id
             entry.faction = row.faction
             entry.faction = row.faction
             entry.class_id = row.class_id
             entry.class_type = row.class_type
             entry.icon = row.icon
             entry.icon = row.icon
             entry.specialization_id = row.specialization_id
             entry.specialization_id = row.specialization_id
Line 199: Line 177:
     end
     end
     return hub
     return hub
end
------------------------------------------------------------------------------------------------------------------------
--
------------------------------------------------------------------------------------------------------------------------
local function sortHeroes(a, b)
    local factionA = FACTION_ORDER[a.faction] or 99
    local factionB = FACTION_ORDER[b.faction] or 99
    if factionA ~= factionB then
        return factionA < factionB
    end
    local mmA = MIGHT_MAGIC_ORDER[a.class_type] or 99
    local mmB = MIGHT_MAGIC_ORDER[b.class_type] or 99
    if mmA ~= mmB then
        return mmA < mmB
    end
    return a.id < b.id
end
end


Line 228: Line 225:
--
--
------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------
local function createHeader(htmlTable)
local function createHeader(htmlTable, words)
     local tr = htmlTable:tag('tr')
     local tr = htmlTable:tag('tr')
    addTh(tr, words.wiki_name)
    addTh(tr, words.wiki_faction)
    addTh(tr, words.wiki_hero_class_type)
    addTh(tr, words.wiki_hero_class)
    addTh(tr, words.wiki_hero_specialty)
    addTh(tr, words.wiki_faction_skill)
    addTh(tr, words.wiki_heroes_starting_skill)
    addTh(tr, words.start_magics)
end
end


Line 235: Line 240:
--
--
------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------
local function createBody(htmlTable)
local function createBody(htmlTable, heroes, frame)
    local currentType = #heroes > 0 and heroes[1].class_type
    local currentFaction = #heroes > 0 and heroes[1].faction
    for _, hero in ipairs(heroes) do
 
        -- separators
        if hero.faction ~= currentFaction then
            currentFaction = hero.faction
            currentType = hero.class_type
            addSeparator(htmlTable, 'separator-large', currentFaction)
        elseif hero.class_type ~= currentType then
            currentType = hero.class_type
            addSeparator(htmlTable, 'separator-tiny', '')
        end


        local tr = htmlTable:tag('tr')
        addTd(tr, hero.name)
        addTd(tr, hero.faction)
        addTd(tr, hero.class_type)
        addTd(tr, hero.class_id)
        addTd(tr, hero.class_id)
        addTd(tr, hero.specialization_id)
        addTd(tr, hero.specialization_id)
        addTd(tr, 'faction_spec')
        addTd(tr, hero.s)
    end
end
end


Line 256: Line 285:
     -- Cargo
     -- Cargo
     local heroesResults = queryHero(lang, forcedFaction)
     local heroesResults = queryHero(lang, forcedFaction)
    local classes = queryHeroClass(lang)
     local specializations = queryHeroSpecialization(lang)
     local specializations = queryHeroSpecialization(lang)
     local skillsHub = SkillsOverview.getSkillsFromCargo(lang)
     local skillsHub = SkillsOverview.getSkillsFromCargo(lang)
    local spells = querySpell(lang)
     local heroesHub = consolidateHeroes(lang, heroesResults, specializations, skillsHub)
     local heroesHub = consolidateHeroes(lang, heroesResults, classes, specializations, skillsHub, spells)
     --if 1 then return dump(heroesHub) end
     if 1 then return dump(heroesHub) end
     local heroes = flatten(heroesHub);
     --local heroes = flatten(hub);


     -- Various manipulations
     -- Various manipulations
     --table.sort(heroes, sortSpells)
     table.sort(heroes, sortHeroes)


     -- Table
     -- Table
     local htmlTable = mw.html.create('table')
     local htmlTable = mw.html.create('table')
     htmlTable:addClass('wikitable sortable')
     htmlTable:addClass('wikitable sortable')
     createHeader(htmlTable)
     createHeader(htmlTable, words)
     createBody(htmlTable)
     createBody(htmlTable)


     -- Output
     -- Output
     local styleTag = frame:extensionTag('templatestyles', '', { src = frame:getTitle() .. '/styles.css' })
     local styleTag = frame:extensionTag('templatestyles', '', { src = frame:getTitle() .. '/styles.css' })
    --return dump(pages)
     return styleTag .. tostring(htmlTable)
     return styleTag .. tostring(htmlTable)
end
end


return p
return p

Revision as of 11:50, 15 June 2026

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

-- Usage: {{#invoke:HeroesOverview|display|lang=en|faction=Temple}}
local p = {}
local SkillsOverview = require("Module:SkillsOverview")

local FACTION_ORDER = {
    human =     1,
    undead =    2,
    nature =    3,
    demon =     4,
    unfrozen =  5,
    dungeon =   6,
}

local FACTION_MAPPING = {
    Temple =     'human',
    Necropolis = 'undead',
    Grove =      'nature',
    Hive =       'demon',
    Schism =     'unfrozen',
    Dungeon =    'dungeon',
}

local MIGHT_MAGIC_ORDER = {
    might =     1,
    magic =     2,
}

-- Note: The right side is just a fallback, it will seldom be used.
local TRANSLATION_IDS = {
    wiki_name = 'Name',
    wiki_faction = 'Faction',
    wiki_hero_class_type = 'Class Type',
    wiki_hero_class = 'Class',
    wiki_hero_specialty = 'Specialty',
    wiki_faction_skill = 'Faction Skill',
    wiki_heroes_starting_skill = 'Starting Skill',
    wiki_spell = 'Starting Spell',
}

------------------------------------------------------------------------------------------------------------------------
-- Displays a variable
------------------------------------------------------------------------------------------------------------------------
local function dump(target)
    return '<pre>' .. mw.dumpObject(target) .. '</pre>'
end

------------------------------------------------------------------------------------------------------------------------
-- Retrieves the text for some specific ids from Cargo Translations.
------------------------------------------------------------------------------------------------------------------------
local function translateIds(ids, lang, extra)
    -- Key list
    local list = {}
    for key, _ in pairs(ids) do
        list[#list + 1] = key
    end
    local idListString = '"' .. table.concat(list, '", "') .. '"'

    -- Cargo
    local where = {}
    table.insert(where, 'target_id IN (' .. idListString .. ')')
    table.insert(where, 'language = "' .. lang .. '"')
    if extra then
        table.insert(where, extra)
    end
    local results = mw.ext.cargo.query('Translation', 'target_id, name', {
        where = table.concat(where, ' AND '),
        limit = 100
    })

    -- Dictionary
    local dictionary = mw.clone(ids)
    if results then
        for _, row in ipairs(results) do
            dictionary[row['target_id']] = row['name']
        end
    end
    return dictionary
end

------------------------------------------------------------------------------------------------------------------------
-- Converts a dictionary into a linear array
------------------------------------------------------------------------------------------------------------------------
local function flatten(hub)
    local list = {}
    for _, entry in pairs(hub) do
        list[#list + 1] = entry
    end
    return list
end

------------------------------------------------------------------------------------------------------------------------
-- Interrogates the Hero Cargo table
------------------------------------------------------------------------------------------------------------------------
local function queryHero(lang, forcedFaction)
    -- fields:
    local fields = {}
    table.insert(fields, 'Hero.id = id');
    table.insert(fields, 'Hero.class_id = class_id');
    table.insert(fields, 'Hero.faction = faction');
    table.insert(fields, 'Hero.class_type = class_type');
    table.insert(fields, 'Hero.icon = icon');
    table.insert(fields, 'Hero.specialization_id = specialization_id');
    table.insert(fields, 'Hero.start_skills = start_skills');
    table.insert(fields, 'Hero.start_skill_levels = start_skill_levels');
    table.insert(fields, 'Hero.start_magics = start_magics');
    table.insert(fields, 'Translation.name = name');
    table.insert(fields, 'Translation.language = language');

    -- where:
    local where = {}
    local _ = forcedFaction and table.insert(where, 'Hero.faction="' .. forcedFaction .. '"')
    table.insert(where, 'Hero.id not like "campaign%"')
    table.insert(where, 'Hero.id not like "tutorial%"')
    table.insert(where, 'Hero.id not like "%_fun_%"')
    table.insert(where, 'Translation.type = "hero"')
    table.insert(where, '(Translation.language = "en" OR Translation.language = "' .. lang .. '")')

    -- query:
    return mw.ext.cargo.query('Hero, Translation', table.concat(fields, ','), {
        join = 'Hero.id = Translation.target_id',
        where = table.concat(where, ' AND '),
        limit = 500
    })
end

------------------------------------------------------------------------------------------------------------------------
-- Interrogates the HeroSpecialization Cargo table
------------------------------------------------------------------------------------------------------------------------
local function queryHeroSpecialization(lang)
    -- fields:
    local fields = {}
    table.insert(fields, 'HeroSpecialization.id = id');
    table.insert(fields, 'HeroSpecialization.icon = icon');
    table.insert(fields, 'Translation.name = name');
    table.insert(fields, 'Translation.description = description');
    table.insert(fields, 'Translation.language = language');

    -- where:
    local where = {}
    table.insert(where, '(Translation.language = "en" OR Translation.language = "' .. lang .. '")')

    -- query:
    return mw.ext.cargo.query('HeroSpecialization, Translation', table.concat(fields, ','), {
        join = 'HeroSpecialization.id = Translation.target_id',
        where = table.concat(where, ' AND '),
        limit = 500
    })
end

------------------------------------------------------------------------------------------------------------------------
-- Folds all Cargo results into a single dictionary:
------------------------------------------------------------------------------------------------------------------------
local function consolidateHeroes(lang, heroesResults, classes, specializations, skillsHub, spells)
    local hub = {}
    for _, row in ipairs(heroesResults) do
        local id = row.id
        local entry = hub[id]
        if not entry then
            entry = {}
            entry.id = id
            entry.class_id = row.class_id
            entry.faction = row.faction
            entry.class_type = row.class_type
            entry.icon = row.icon
            entry.specialization_id = row.specialization_id
            entry.start_skills = row.start_skills
            entry.start_skill_levels = row.start_skill_levels
            entry.start_magics = row.start_magics
            hub[id] = entry
        end
        if row.language == lang then
            entry.name = row.name or ''
        end
        if row.language == 'en' then
            entry.nameEn = row.name or ''
        end
    end
    return hub
end

------------------------------------------------------------------------------------------------------------------------
--
------------------------------------------------------------------------------------------------------------------------
local function sortHeroes(a, b)
    local factionA = FACTION_ORDER[a.faction] or 99
    local factionB = FACTION_ORDER[b.faction] or 99
    if factionA ~= factionB then
        return factionA < factionB
    end

    local mmA = MIGHT_MAGIC_ORDER[a.class_type] or 99
    local mmB = MIGHT_MAGIC_ORDER[b.class_type] or 99
    if mmA ~= mmB then
        return mmA < mmB
    end

    return a.id < b.id
end

------------------------------------------------------------------------------------------------------------------------
-- Boilerplate for the header cells
------------------------------------------------------------------------------------------------------------------------
local function addTh(tr, text)
    tr:tag('th'):wikitext(text):done()
end

------------------------------------------------------------------------------------------------------------------------
-- Boilerplate for the body cells
------------------------------------------------------------------------------------------------------------------------
local function addTd(tr, text)
    tr:tag('td'):wikitext(text):done()
end

------------------------------------------------------------------------------------------------------------------------
-- Boilerplate for the separator rows
------------------------------------------------------------------------------------------------------------------------
local function addSeparator(htmlTable, className, content)
    htmlTable:tag('tr')
        :addClass('separator')
        :addClass(className)
        :tag('td'):attr('data-sort-value', ''):attr('colspan', 7):wikitext(content):done()
end

------------------------------------------------------------------------------------------------------------------------
--
------------------------------------------------------------------------------------------------------------------------
local function createHeader(htmlTable, words)
    local tr = htmlTable:tag('tr')
    addTh(tr, words.wiki_name)
    addTh(tr, words.wiki_faction)
    addTh(tr, words.wiki_hero_class_type)
    addTh(tr, words.wiki_hero_class)
    addTh(tr, words.wiki_hero_specialty)
    addTh(tr, words.wiki_faction_skill)
    addTh(tr, words.wiki_heroes_starting_skill)
    addTh(tr, words.start_magics)
end

------------------------------------------------------------------------------------------------------------------------
--
------------------------------------------------------------------------------------------------------------------------
local function createBody(htmlTable, heroes, frame)
    local currentType = #heroes > 0 and heroes[1].class_type
    local currentFaction = #heroes > 0 and heroes[1].faction
    for _, hero in ipairs(heroes) do

        -- separators
        if hero.faction ~= currentFaction then
            currentFaction = hero.faction
            currentType = hero.class_type
            addSeparator(htmlTable, 'separator-large', currentFaction)
        elseif hero.class_type ~= currentType then
            currentType = hero.class_type
            addSeparator(htmlTable, 'separator-tiny', '')
        end

        local tr = htmlTable:tag('tr')
        addTd(tr, hero.name)
        addTd(tr, hero.faction)
        addTd(tr, hero.class_type)
        addTd(tr, hero.class_id)
        addTd(tr, hero.class_id)
        addTd(tr, hero.specialization_id)
        addTd(tr, hero.specialization_id)
        addTd(tr, 'faction_spec')
        addTd(tr, hero.s)
    end
end

------------------------------------------------------------------------------------------------------------------------
-- Main public function
------------------------------------------------------------------------------------------------------------------------
function p.display(frame)
    --if 1 then return dump(frame) end

    -- Args
    local args = frame.args
    local lang = args.lang ~= '' and args.lang or 'en'
    --lang = 'fr'
    local forcedFaction = FACTION_MAPPING[args.faction] or nil

    -- Language
    local words = translateIds(TRANSLATION_IDS, lang)

    -- Cargo
    local heroesResults = queryHero(lang, forcedFaction)
    local specializations = queryHeroSpecialization(lang)
    local skillsHub = SkillsOverview.getSkillsFromCargo(lang)
    local heroesHub = consolidateHeroes(lang, heroesResults, specializations, skillsHub)
    --if 1 then return dump(heroesHub) end
    local heroes = flatten(heroesHub);

    -- Various manipulations
    table.sort(heroes, sortHeroes)

    -- Table
    local htmlTable = mw.html.create('table')
    htmlTable:addClass('wikitable sortable')
    createHeader(htmlTable, words)
    createBody(htmlTable)

    -- Output
    local styleTag = frame:extensionTag('templatestyles', '', { src = frame:getTitle() .. '/styles.css' })
    return styleTag .. tostring(htmlTable)
end

return p