Module:Sandbox/Aurelain/SkillSynergies: Difference between revisions

From Heroes of Might and Magic: Olden Era Official Wiki
No edit summary
No edit summary
Line 47: Line 47:
--
--
------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------
local function findSynergies(allSkills, skill, pattern)
local function findSynergies(allSkills, skill, pattern, skipHighlight)
     local synergies = {}
     local synergies = {}
     for _, group in ipairs(allSkills) do
     for _, group in ipairs(allSkills) do
Line 56: Line 56:
                     if descriptionEn:match(pattern) then
                     if descriptionEn:match(pattern) then
                         -- We found a subskill that mentions our target skill by name
                         -- We found a subskill that mentions our target skill by name
                        local d = subSkill.description
                        d = skipHighlight and d or highlightLastSentence(d)
                         table.insert(synergies, {
                         table.insert(synergies, {
                             groupNameEn = group.nameEn,
                             groupNameEn = group.nameEn,
Line 62: Line 64:
                             subName = subSkill.name,
                             subName = subSkill.name,
                             subIcon = subSkill.icon,
                             subIcon = subSkill.icon,
                             description = highlightLastSentence(subSkill.description),
                             description = d,
                         })
                         })
                     end
                     end
Line 131: Line 133:
     --lang = 'en'
     --lang = 'en'
     --skill = 'skill_magic_day'
     --skill = 'skill_magic_day'
    skill = 'skill_combat'
     pattern = 'Heroic Strike'
     pattern = 'Heroic Strike'


Line 148: Line 151:
     end
     end
     if pattern ~= '' then
     if pattern ~= '' then
         synergies = merge(synergies, findSynergies(allSkills, skill, pattern))
         synergies = merge(synergies, findSynergies(allSkills, skill, pattern, true))
     end
     end



Revision as of 14:49, 9 June 2026

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

-- Usage: {{#invoke:SkillSynergies|display|lang=en|skill=skill_assault}}
local p = {}
local SkillsOverview = require("Module:Sandbox/Aurelain/SkillsOverview")
local PATCH_NAME = {
    ['Summon Avatar'] = 'Summon Avatar (Skill)',
}

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

------------------------------------------------------------------------------------------------------------------------
-- Spawns a safe robotic url from an English name, fit for adding it before the visible name
------------------------------------------------------------------------------------------------------------------------
local function getUrl(nameEn, lang)
    local url = PATCH_NAME[nameEn] or nameEn
    if lang ~= 'en' then
        url = url .. '/' .. lang
    end
    return url
end

------------------------------------------------------------------------------------------------------------------------
-- Ejects punctuation from inside quotes. Example:  `foo “Bar.”` into `foo “Bar”.`
------------------------------------------------------------------------------------------------------------------------
local function repairEnglishPunctuation(text)
    return text:gsub("([%,%.%!%?])”", "”%1")
end

------------------------------------------------------------------------------------------------------------------------
--
------------------------------------------------------------------------------------------------------------------------
local function highlightLastSentence(text)
    local spanBegin = '<span style="color:peru">'
    local output = mw.ustring.gsub(text, '%(', spanBegin .. '(', 1)
    if output == text then
        output = mw.ustring.gsub(text, '([%.。])', '%1' .. spanBegin, 1)
    end
    output = output .. '</span>'
    return output
end

------------------------------------------------------------------------------------------------------------------------
--
------------------------------------------------------------------------------------------------------------------------
local function findSynergies(allSkills, skill, pattern, skipHighlight)
    local synergies = {}
    for _, group in ipairs(allSkills) do
        if group.id ~= skill then
            for _, rank in ipairs(group.ranks) do
                for _, subSkill in pairs(rank.subSkills) do
                    local descriptionEn = repairEnglishPunctuation(subSkill.descriptionEn)
                    if descriptionEn:match(pattern) then
                        -- We found a subskill that mentions our target skill by name
                        local d = subSkill.description
                        d = skipHighlight and d or highlightLastSentence(d)
                        table.insert(synergies, {
                            groupNameEn = group.nameEn,
                            rankName = rank.name,
                            rankIcon = rank.icon,
                            subName = subSkill.name,
                            subIcon = subSkill.icon,
                            description = d,
                        })
                    end
                end
            end
        end
    end
    return synergies
end

------------------------------------------------------------------------------------------------------------------------
--
------------------------------------------------------------------------------------------------------------------------
local function renderSynergies(synergies, lang)
    local root = mw.html.create()
    for _, synergy in ipairs(synergies) do
        -- Header
        local rankIcon = '[[File:' .. synergy.rankIcon .. '.png|48px|link=]]'
        local rankLink = '[[' .. getUrl(synergy.groupNameEn, lang) .. '|' .. synergy.rankName .. ']]'
        root:tag('div'):wikitext(rankIcon .. ' ' .. rankLink)

        -- Description
        local subIcon = '[[File:' .. synergy.subIcon .. '.png|24px|link=]]'
        local subName = "'''" .. synergy.subName .. "'''"
        root:tag('div'):css('margin-left', '64px'):wikitext(subIcon .. ' ' .. subName)
        root:tag('div'):css('margin', '0 0 8px 93px'):wikitext(synergy.description)
    end
    return tostring(root)
end

------------------------------------------------------------------------------------------------------------------------
--
------------------------------------------------------------------------------------------------------------------------
local function findEnglishName(allSkills, skill)
    for _, group in ipairs(allSkills) do
        if group.id == skill then
            return group.nameEn
        end
    end
end

------------------------------------------------------------------------------------------------------------------------
--
------------------------------------------------------------------------------------------------------------------------
local function merge(a, b)
    local output = {}
    for _, value in ipairs(a) do
        table.insert(output, value)
    end
    for _, value in ipairs(b) do
        table.insert(output, value)
    end
    return output
end

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

    -- Args
    local args = frame.args
    local lang = args.lang or 'en'
    local skill = args.skill or ''
    local pattern = args.pattern or ''
    if skill == '' and pattern == '' then return 'You must provide a skill or a pattern' end
    --lang = 'en'
    --skill = 'skill_magic_day'
    skill = 'skill_combat'
    pattern = 'Heroic Strike'

    -- Cargo
    local allSkills = SkillsOverview.getSkillsFromCargo(lang)
    local nameEn = findEnglishName(allSkills, skill)
    if not nameEn then return 'Cannot find name for skill "' .. skill .. '"' end

    -- Main computation
    local synergies = {}
    if skill ~= '' then
        synergies = merge(synergies, findSynergies(allSkills, skill, '“' .. nameEn .. '”'))
        -- Manual hack to synergize each Magic School with its brethren
        if skill:match('magic') then
            synergies = merge(synergies, findSynergies(allSkills, skill, 'other Magic School'))
        end
    end
    if pattern ~= '' then
        synergies = merge(synergies, findSynergies(allSkills, skill, pattern, true))
    end

    if #synergies == 0 then return '' end

    -- Output
    local markup = renderSynergies(synergies, lang)
    return markup
end

return p