Module:HeroDataParser: Difference between revisions

From Heroes of Might and Magic: Olden Era Official Wiki
No edit summary
No edit summary
Line 66: Line 66:
     end
     end


     -- Join all hero lines into one big string
     local wikitext = table.concat(result, '\n')
    return table.concat(result, '\n')
 
    -- 🔹 Ask MediaWiki to expand the templates now
    return frame:preprocess(wikitext)
end
end


return p
return p

Revision as of 11:32, 29 October 2025

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 ''
    end
    return ''
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 ''
    end
    return ''
end

function p.getSpell(frame)
    local hero = findHero(frame.args[1])
    return hero and hero.spell or ''
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')

    -- 🔹 Ask MediaWiki to expand the templates now
    return frame:preprocess(wikitext)
end

return p