Module:Sandbox/Aurelain/SkillSynergies: Difference between revisions

From Heroes of Might and Magic: Olden Era Official Wiki
No edit summary
No edit summary
 
(51 intermediate revisions by the same user not shown)
Line 2: Line 2:
local p = {}
local p = {}
local SkillsOverview = require("Module:Sandbox/Aurelain/SkillsOverview")
local SkillsOverview = require("Module:Sandbox/Aurelain/SkillsOverview")
local PATCH_NAME = {
    ['Summon Avatar'] = 'Summon Avatar (Skill)',
}


------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------
Line 8: Line 11:
local function dump(target)
local function dump(target)
     return '<pre>' .. mw.dumpObject(target) .. '</pre>'
     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
end


Line 20: Line 34:
--
--
------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------
local function findSynergies(allSkills, skill, nameEn, lang)
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(skillsHub, skillId, pattern, skipHighlight)
     local synergies = {}
     local synergies = {}
     for _, group in ipairs(allSkills) do
     for _, group in pairs(skillsHub) do
         if group.id ~= skill then
         if group.id ~= skillId then
             for _, rank in ipairs(group.ranks) do
             for _, rank in ipairs(group.ranks) do
                 for _, subSkill in pairs(rank.subSkills) do
                 for _, subSkill in pairs(rank.subSkills) do
                     local descriptionEn = repairEnglishPunctuation(subSkill.descriptionEn)
                     local descriptionEn = repairEnglishPunctuation(subSkill.descriptionEn)
                     if descriptionEn:match('“' .. nameEn .. '”') 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,
                             rankName = rank.name,
                             rankName = rank.name,
                            rankNameEn = rank.nameEn,
                             rankIcon = rank.icon,
                             rankIcon = rank.icon,
                             subName = subSkill.name,
                             subName = subSkill.name,
                             subIcon = subSkill.icon,
                             subIcon = subSkill.icon,
                             description = SkillsOverview.linkifyDescription(subSkill.description, descriptionEn, lang),
                             description = d,
                            descriptionEn = descriptionEn,
                         })
                         })
                     end
                     end
Line 49: Line 77:
--
--
------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------
local function emulateSkills(synergies)
local function renderSynergies(synergies, lang)
     local skills = {}
     local root = mw.html.create()
     for _, synergy in ipairs(synergies) do
     for _, synergy in ipairs(synergies) do
         table.insert(skills, {
         -- Header
            ranks = {
        local url = getUrl(synergy.groupNameEn, lang)
                name = synergy.rankName,
        local rankIcon = '[[File:' .. synergy.rankIcon .. '.png|64px|link=' .. url .. ']]'
                nameEn = synergy.rankNameEn,
        local rankLink = '[[' .. url .. '|' .. synergy.rankName .. ']]'
                icon = synergy.rankIcon,
        root:tag('div'):wikitext(rankIcon .. ' ' .. rankLink)
                subSkills = {
 
                    [1] = {
        -- Sub container
                        name = synergy.subName,
        local container = root:tag('div'):css('display', 'flex'):css('margin', '-4px 0 8px 96px')
                        icon = synergy.subIcon,
 
                        description = synergy.description,
        -- Left
                        descriptionEn = synergy.descriptionEn,
        local icon = '[[File:' .. synergy.subIcon .. '.png|32px|link=]]'
                    }
        container:tag('div'):css('flex-shrink', 0):wikitext(icon)
                }
 
            }
        -- Right
         })
        local content = "'''" .. synergy.subName .. "'''<br>" .. synergy.description
         container:tag('div'):css('flex-grow', 1):css('padding-left', '8px'):wikitext(content)
     end
     end
     return skills
     return tostring(root)
end
end


Line 74: Line 103:
--
--
------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------
local function findEnglishName(allSkills, skill)
local function merge(a, b)
     for _, group in ipairs(allSkills) do
    local output = {}
         if group.id == skill then
     for _, value in ipairs(a) do
            return group.nameEn
         table.insert(output, value)
         end
    end
    for _, value in ipairs(b) do
         table.insert(output, value)
     end
     end
    return output
end
end


Line 86: Line 118:
------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------
function p.display(frame)
function p.display(frame)
    --local d = repairEnglishPunctuation('foo “Bar.”')
     --if 1 then return dump(frame) end
     --if 1 then return dump(d) end


     -- Args
     -- Args
     local args = frame.args
     local args = frame.args
     local lang = args.lang or 'en'
     local lang = args.lang or 'en'
     local skill = args.skill
     local skillId = args.skill or ''
     if not skill or skill == '' then return 'You must provide a skill id' end
     local pattern = args.pattern or ''
     lang = 'fr'
    --lang = 'fr'
     --skill = 'skill_formation'
     --skillId = 'skill_sorcery'
     --pattern = 'Heroic Strike'
 
    -- Cargo
    local skillsHub = SkillsOverview.getSkillsFromCargo(lang)


     local allSkills = SkillsOverview.getSkillsFromCargo(lang)
    -- Main computation
     local nameEn = findEnglishName(allSkills, skill)
     local synergies = {}
    if not nameEn then return 'Cannot find name for skill "' .. skill .. '"' end
     if skillId ~= '' then
    local synergies = findSynergies(allSkills, skill, nameEn, lang)
        local entry = skillsHub[skillId] or {}
    local emulatedSkills = emulateSkills(synergies)
        local nameEn = entry.nameEn
     if 1 then return dump(emulatedSkills) end
        if not nameEn then return 'Cannot find name for skill "' .. skillId .. '"' end
        synergies = merge(synergies, findSynergies(skillsHub, skillId, '“' .. nameEn .. '”'))
        -- Manual hack to synergize each Magic School with its brethren
        if skillId:match('magic') then
            synergies = merge(synergies, findSynergies(skillsHub, skillId, 'other Magic School'))
        end
    end
     if pattern ~= '' then
        synergies = merge(synergies, findSynergies(skillsHub, skillId, pattern, true))
    end


     --local markup = p.renderSkills(skills, words, lang)
     if #synergies == 0 then return "''No results''" end


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


return p
return p

Latest revision as of 09:06, 15 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(skillsHub, skillId, pattern, skipHighlight)
    local synergies = {}
    for _, group in pairs(skillsHub) do
        if group.id ~= skillId 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 url = getUrl(synergy.groupNameEn, lang)
        local rankIcon = '[[File:' .. synergy.rankIcon .. '.png|64px|link=' .. url .. ']]'
        local rankLink = '[[' .. url .. '|' .. synergy.rankName .. ']]'
        root:tag('div'):wikitext(rankIcon .. ' ' .. rankLink)

        -- Sub container
        local container = root:tag('div'):css('display', 'flex'):css('margin', '-4px 0 8px 96px')

        -- Left
        local icon = '[[File:' .. synergy.subIcon .. '.png|32px|link=]]'
        container:tag('div'):css('flex-shrink', 0):wikitext(icon)

        -- Right
        local content = "'''" .. synergy.subName .. "'''<br>" .. synergy.description
        container:tag('div'):css('flex-grow', 1):css('padding-left', '8px'):wikitext(content)
    end
    return tostring(root)
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 skillId = args.skill or ''
    local pattern = args.pattern or ''
    --lang = 'fr'
    --skillId = 'skill_sorcery'
    --pattern = 'Heroic Strike'

    -- Cargo
    local skillsHub = SkillsOverview.getSkillsFromCargo(lang)

    -- Main computation
    local synergies = {}
    if skillId ~= '' then
        local entry = skillsHub[skillId] or {}
        local nameEn = entry.nameEn
        if not nameEn then return 'Cannot find name for skill "' .. skillId .. '"' end
        synergies = merge(synergies, findSynergies(skillsHub, skillId, '“' .. nameEn .. '”'))
        -- Manual hack to synergize each Magic School with its brethren
        if skillId:match('magic') then
            synergies = merge(synergies, findSynergies(skillsHub, skillId, 'other Magic School'))
        end
    end
    if pattern ~= '' then
        synergies = merge(synergies, findSynergies(skillsHub, skillId, pattern, true))
    end

    if #synergies == 0 then return "''No results''" end

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

return p