Module:HeroDataParser: Difference between revisions

From Heroes of Might and Magic: Olden Era Official Wiki
No edit summary
N (talk | contribs)
Increased size of starting army icons displayed by getStartingArmyIcons from 50px to 75px
 
(5 intermediate revisions by 2 users not shown)
Line 67: Line 67:


     local wikitext = table.concat(result, '\n')
     local wikitext = table.concat(result, '\n')
    -- 🔹 Ask MediaWiki to expand the templates now
     return frame:preprocess(wikitext)
     return frame:preprocess(wikitext)
end
end


function p.getLore(frame)
function p.getClassHeroes(frame)
     local hero = findHero(frame.args[1])
     local className = frame.args[1]
     return hero and hero.lore or ''
    local json = getHeroData()
end
     local result = {}
 
    if json and json.heroes then
        for _, hero in ipairs(json.heroes) do
            if hero.class == className then
                -- Build a HeroTableLine call for each hero
                table.insert(result, string.format('{{HeroTableLine|%s}}', hero.name))
            end
        end
    end


function p.getSpecialtyDescr(frame)
     local wikitext = table.concat(result, '\n')
     local hero = findHero(frame.args[1])
     return frame:preprocess(wikitext)
     return hero and hero.specialtyDescr or ''
end
end


Line 90: Line 96:
         for unit, count in pairs(entry) do
         for unit, count in pairs(entry) do
             local icon = unit .. ' icon.png'
             local icon = unit .. ' icon.png'
             table.insert(html, string.format('<div style="display:inline-block;margin:4px 15px 4px 15px;vertical-align:top;text-align:center;">[[File:%s|50px|link=%s]]<br/><span style="font-size:1.1em;">%s</span></div><br/><span style="font-size:1.1em;">%s</span></div>', icon, unit, unit, count))
             table.insert(html, string.format('<div style="display:inline-block;margin:4px 8px 4px 8px;vertical-align:top;text-align:center;">[[File:%s|75px|link=%s]]<br/><span style="font-size:1.1em;">%s</span></div>', icon, unit, count))
         end
         end
     end
     end

Latest revision as of 18:12, 1 May 2026

HeroDataParser Module Documentation

This module provides accessors for hero data stored in Data:HeroData.json. It allows retrieving hero class, specialty, starting skills, spells, and generating wikitext lists or army icons.

Data is cached on first access for improved performance.

Functions

getClass(frame)

Retrieves the class of a hero.

Usage
{{#invoke:HeroDataParser|getClass|HeroName}}
Parameters
  • frame.args[1] – string: hero name
Returns
  • string: hero's class, or empty string if not found

---

getSpecialty(frame)

Retrieves the specialty of a hero.

Usage
{{#invoke:HeroDataParser|getSpecialty|HeroName}}
Parameters
  • frame.args[1] – string: hero name
Returns
  • string: hero's specialty, or empty string if not found

---

getSkill1(frame)

Retrieves the first starting skill of a hero.

Usage
{{#invoke:HeroDataParser|getSkill1|HeroName}}
Parameters
  • frame.args[1] – string: hero name
Returns
  • string: name of the first starting skill, or nil if none

---

getSkill2(frame)

Retrieves the second starting skill of a hero.

Usage
{{#invoke:HeroDataParser|getSkill2|HeroName}}
Parameters
  • frame.args[1] – string: hero name
Returns
  • string: name of the second starting skill, or nil if none

---

getSpell(frame)

Retrieves the spell of a hero.

Usage
{{#invoke:HeroDataParser|getSpell|HeroName}}
Parameters
  • frame.args[1] – string: hero name
Returns
  • string: hero's spell, or nil if none

---

getFactionHeroes(frame)

Generates a list of heroes belonging to a faction using `|- |style="padding:0.25em 0; border-right:0;" |[[File:{{{1}}}.png|50px]] |style="border-left:0;" | [[{{{1}}}]] |[[]] | |colspan=2 | [[|Advanced ]] |`.

Usage
{{#invoke:HeroDataParser|getFactionHeroes|FactionName}}
Parameters
  • frame.args[1] – string: faction name
Returns
  • wikitext: list of heroes in the faction

---

getClassHeroes(frame)

Generates a list of heroes of a specific class using `|- |style="padding:0.25em 0; border-right:0;" |[[File:{{{1}}}.png|50px]] |style="border-left:0;" | [[{{{1}}}]] |[[]] | |colspan=2 | [[|Advanced ]] |`.

Usage
{{#invoke:HeroDataParser|getClassHeroes|ClassName}}
Parameters
  • frame.args[1] – string: class name
Returns
  • wikitext: list of heroes in the class

---

getStartingArmyIcons(frame)

Generates HTML displaying a hero's starting army icons with unit count.

Usage
{{#invoke:HeroDataParser|getStartingArmyIcons|HeroName}}
Parameters
  • frame.args[1] – string: hero name
Returns
  • HTML: icons of the hero's starting army with unit names and counts
  • Empty string if hero or army data is not available

---

Internal Functions

  • getHeroData() – Loads and caches the hero JSON data from `Data:HeroData.json`.
  • findHero(heroName) – Finds and returns the hero table by name.

local p = {}

local heroDataCache = nil
local function getHeroData()
    if not heroDataCache then
        local content = mw.title.new('Data:HeroData.json'):getContent() or '{}'
        heroDataCache = mw.text.jsonDecode(content)
    end
    return heroDataCache
end

local function findHero(heroName)
    local json = getHeroData()
    if not json or not json.heroes then return nil end
    for _, hero in ipairs(json.heroes) do
        if hero.name == heroName then
            return hero
        end
    end
    return nil
end

function p.getClass(frame)
    local hero = findHero(frame.args[1])
    return hero and hero.class or ''
end

function p.getSpecialty(frame)
    local hero = findHero(frame.args[1])
    return hero and hero.specialty or ''
end

function p.getSkill1(frame)
    local hero = findHero(frame.args[1])
    if hero and hero.starting_skills and hero.starting_skills[1] then
        return hero.starting_skills[1].name or nil
    end
    return nil
end

function p.getSkill2(frame)
    local hero = findHero(frame.args[1])
    if hero and hero.starting_skills and hero.starting_skills[2] then
        return hero.starting_skills[2].name or nil
    end
    return nil
end

function p.getSpell(frame)
    local hero = findHero(frame.args[1])
    return hero and hero.spell or nil
end

function p.getFactionHeroes(frame)
    local faction = frame.args[1]
    local json = getHeroData()
    local result = {}

    if json and json.heroes then
        for _, hero in ipairs(json.heroes) do
            if hero.faction == faction then
                -- Build a HeroTableLine call for each hero
                table.insert(result, string.format('{{HeroTableLine|%s}}', hero.name))
            end
        end
    end

    local wikitext = table.concat(result, '\n')
    return frame:preprocess(wikitext)
end

function p.getClassHeroes(frame)
    local className = frame.args[1]
    local json = getHeroData()
    local result = {}

    if json and json.heroes then
        for _, hero in ipairs(json.heroes) do
            if hero.class == className then
                -- Build a HeroTableLine call for each hero
                table.insert(result, string.format('{{HeroTableLine|%s}}', hero.name))
            end
        end
    end

    local wikitext = table.concat(result, '\n')
    return frame:preprocess(wikitext)
end

function p.getStartingArmyIcons(frame)
    local hero = findHero(frame.args[1])
    if not hero or not hero.startingArmy then return '' end
    local army = hero.startingArmy
    local html = {'<div style="text-align:center;">'}
    for _, entry in ipairs(army) do
        for unit, count in pairs(entry) do
            local icon = unit .. ' icon.png'
            table.insert(html, string.format('<div style="display:inline-block;margin:4px 8px 4px 8px;vertical-align:top;text-align:center;">[[File:%s|75px|link=%s]]<br/><span style="font-size:1.1em;">%s</span></div>', icon, unit, count))
        end
    end
    table.insert(html, '</div>')
    return table.concat(html, '\n')
end

return p