Module:HeroDataParser/ru: Difference between revisions

From Heroes of Might and Magic: Olden Era Official Wiki
Created page with "local p = {} local heroDataCache = nil local function getHeroData() if not heroDataCache then local content = mw.title.new('Data:HeroData/ru.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..."
 
mNo edit summary
Line 60: Line 60:
         for _, hero in ipairs(json.heroes) do
         for _, hero in ipairs(json.heroes) do
             if hero.faction == faction then
             if hero.faction == faction then
                 -- Build a HeroTableLine call for each hero
                 -- Build a HeroTableLine/ru call for each hero
                 table.insert(result, string.format('{{HeroTableLine|%s}}', hero.name))
                 table.insert(result, string.format('{{HeroTableLine/ru|%s}}', hero.name))
             end
             end
         end
         end
Line 78: Line 78:
         for _, hero in ipairs(json.heroes) do
         for _, hero in ipairs(json.heroes) do
             if hero.class == className then
             if hero.class == className then
                 -- Build a HeroTableLine call for each hero
                 -- Build a HeroTableLine/ru call for each hero
                 table.insert(result, string.format('{{HeroTableLine|%s}}', hero.name))
                 table.insert(result, string.format('{{HeroTableLine/ru|%s}}', hero.name))
             end
             end
         end
         end

Revision as of 22:32, 2 November 2025

Documentation for this module may be created at Module:HeroDataParser/ru/doc

local p = {}

local heroDataCache = nil
local function getHeroData()
    if not heroDataCache then
        local content = mw.title.new('Data:HeroData/ru.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/ru call for each hero
                table.insert(result, string.format('{{HeroTableLine/ru|%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/ru call for each hero
                table.insert(result, string.format('{{HeroTableLine/ru|%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|50px|link=%s]]<br/><span style="font-size:1.1em;">%s</span><br/><span style="font-size:1.1em;">%s</span></div>', icon, unit, unit, count))
        end
    end
    table.insert(html, '</div>')
    return table.concat(html, '\n')
end

return p