Module:SkillArtifacts: Difference between revisions
From Heroes of Might and Magic: Olden Era Official Wiki
No edit summary |
No edit summary |
||
| (3 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
-- Usage: {{#invoke:SkillArtifacts|display|lang=en|skill=skill_assault}} | -- Usage: {{#invoke:SkillArtifacts|display|lang=en|skill=skill_assault}} | ||
local p = {} | local p = {} | ||
local f = mw.ustring.format | |||
local SkillsOverview = require("Module:SkillsOverview") | local SkillsOverview = require("Module:SkillsOverview") | ||
local PATCH_NAME = { | local PATCH_NAME = { | ||
['Summon Avatar'] = 'Summon Avatar (Skill)', | ['Summon Avatar'] = 'Summon Avatar (Skill)', | ||
} | |||
-- The following are exceptions caused by collision between the Skill name and Attribute name in English: | |||
local SPECIAL_PATTERNS = { | |||
Defense = 'Skills.-Defense', | |||
Luck = 'Skills.-Luck' | |||
} | } | ||
| Line 72: | Line 78: | ||
-- | -- | ||
------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------ | ||
local function linkifyDescription(description, | local function linkifyDescription(description, skillsHub, lang) | ||
for _, group in | for _, group in pairs(skillsHub) do | ||
local url = getUrl(group.nameEn, lang) | local url = getUrl(group.nameEn, lang) | ||
description = mw.ustring.gsub(description, '(' .. group.name .. ')', '[[' .. url .. '|' .. '%1]]') | description = mw.ustring.gsub(description, '(' .. group.name .. ')', '[[' .. url .. '|' .. '%1]]') | ||
| Line 92: | Line 98: | ||
-- | -- | ||
------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------ | ||
local function findRelatedArtifacts(artifacts, | local function findRelatedArtifacts(artifacts, pattern, skillName, skillsHub, lang) | ||
local related = {} | local related = {} | ||
for | for id, artifact in pairs(artifacts) do | ||
if artifact.descriptionEn:match( | if artifact.descriptionEn:match(pattern) then | ||
-- We found an artifact that mentions our target skill by name | -- We found an artifact that mentions our target skill by name | ||
local description = linkifyDescription(artifact.description, | local description = linkifyDescription(artifact.description, skillsHub, lang) | ||
description = highlightCurrentSkill(description, | -- Note: We cannot highlight the current skill because we're passing the description to ArtifactSummary | ||
--description = highlightCurrentSkill(description, skillName) | |||
table.insert(related, { | table.insert(related, { | ||
id = id, | |||
name = artifact.name, | name = artifact.name, | ||
description = description, | description = description, | ||
| Line 112: | Line 120: | ||
-- | -- | ||
------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------ | ||
local function renderArtifacts(artifacts) | local function renderArtifacts(artifacts, lang, frame) | ||
local | local list = {} | ||
for _, artifact in ipairs(artifacts) do | for _, artifact in ipairs(artifacts) do | ||
local | local formatted = f( | ||
'{{ArtifactSummary | lang=%s | id=%s | description=%s}}', | |||
lang, | |||
artifact.id, | |||
artifact.description | |||
); | |||
table.insert(list, frame:preprocess(formatted)) | |||
end | end | ||
return table.concat(list) | |||
end | end | ||
| Line 151: | Line 146: | ||
if skillId == '' then return 'You must provide a skill id!' end | if skillId == '' then return 'You must provide a skill id!' end | ||
--lang = 'en' | --lang = 'en' | ||
skillId = 'skill_diplomacy' | --skillId = 'skill_diplomacy' | ||
-- Cargo | -- Cargo | ||
local | local skillsHub = SkillsOverview.getSkillsFromCargo(lang) | ||
local skill = | local skill = skillsHub[skillId] | ||
if not skill then return 'Cannot resolve skill!' end | if not skill then return 'Cannot resolve skill!' end | ||
local artifactsResult = queryArtifacts(lang) | local artifactsResult = queryArtifacts(lang) | ||
| Line 161: | Line 156: | ||
-- Main computation | -- Main computation | ||
local artifacts = findRelatedArtifacts(artifactsHub, skill, | local pattern = SPECIAL_PATTERNS[skill.nameEn] or skill.nameEn | ||
local artifacts = findRelatedArtifacts(artifactsHub, pattern, skill.name, skillsHub, lang) | |||
--if 1 then return dump(artifacts) end | --if 1 then return dump(artifacts) end | ||
if #artifacts == 0 then return "''No results''" end | if #artifacts == 0 then return "''No results''" end | ||
-- Output | -- Output | ||
local markup = renderArtifacts(artifacts) | local markup = renderArtifacts(artifacts, lang, frame) | ||
return markup | return markup | ||
end | end | ||
return p | return p | ||
Latest revision as of 14:48, 28 June 2026
This module lists the artifacts that mention a specific skill. Additionally, it applies hyperlinks to words that exactly match skill names.
Usage
1. English:
Input: {{#invoke:SkillArtifacts|display | lang=en | skill=skill_diplomacy}}
Output:
Holy Sigil of Insara —
Epic Relic
While worn, grants all unlearned Subskills of the following Skills: Leadership, Insight, Diplomacy, Tactics.Diplomatic Gifts —
Legendary Item
+10% to Persuasion Power in Diplomacy.Ambassador's Sash —
Legendary Back
+10% to Persuasion Power in Diplomacy.2. Japanese:
Input: {{#invoke:SkillArtifacts|display | lang=ja | skill=skill_diplomacy}}
Output:
-- Usage: {{#invoke:SkillArtifacts|display|lang=en|skill=skill_assault}}
local p = {}
local f = mw.ustring.format
local SkillsOverview = require("Module:SkillsOverview")
local PATCH_NAME = {
['Summon Avatar'] = 'Summon Avatar (Skill)',
}
-- The following are exceptions caused by collision between the Skill name and Attribute name in English:
local SPECIAL_PATTERNS = {
Defense = 'Skills.-Defense',
Luck = 'Skills.-Luck'
}
------------------------------------------------------------------------------------------------------------------------
-- 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
------------------------------------------------------------------------------------------------------------------------
--
------------------------------------------------------------------------------------------------------------------------
local function queryArtifacts(lang)
-- fields:
local fields = {}
table.insert(fields, 'Artifact.id = id');
table.insert(fields, 'Artifact.icon = icon');
table.insert(fields, 'Translation.name = name');
table.insert(fields, 'Translation.description = description');
table.insert(fields, 'Translation.language = language');
-- where:
local where = {}
table.insert(where, '(Translation.language = "en" OR Translation.language = "' .. lang .. '")')
-- query:
return mw.ext.cargo.query('Artifact, Translation', table.concat(fields, ','), {
join = 'Artifact.id = Translation.target_id',
where = table.concat(where, ' AND '),
limit = 1000
})
end
------------------------------------------------------------------------------------------------------------------------
-- Folds all artifact results from Cargo into a compacted table
------------------------------------------------------------------------------------------------------------------------
local function consolidateArtifacts(artifactsResult, lang)
local hub = {}
for _, row in ipairs(artifactsResult) do
local id = row.id
hub[id] = hub[id] or { id = id }
local entry = hub[id]
if row.language == lang then
entry.name = row.name or ''
entry.description = row.description or ''
entry.icon = row.icon or ''
end
if row.language == 'en' then
entry.descriptionEn = row.description or ''
end
end
return hub
end
------------------------------------------------------------------------------------------------------------------------
--
------------------------------------------------------------------------------------------------------------------------
local function linkifyDescription(description, skillsHub, lang)
for _, group in pairs(skillsHub) do
local url = getUrl(group.nameEn, lang)
description = mw.ustring.gsub(description, '(' .. group.name .. ')', '[[' .. url .. '|' .. '%1]]')
end
return description
end
------------------------------------------------------------------------------------------------------------------------
--
------------------------------------------------------------------------------------------------------------------------
local function highlightCurrentSkill(description, name)
local spanMarkup = '<span style="color:peru">' .. name .. '</span>'
description = mw.ustring.gsub(description, '%[%[[^%]]*' .. name .. '%]%]', spanMarkup)
return description
end
------------------------------------------------------------------------------------------------------------------------
--
------------------------------------------------------------------------------------------------------------------------
local function findRelatedArtifacts(artifacts, pattern, skillName, skillsHub, lang)
local related = {}
for id, artifact in pairs(artifacts) do
if artifact.descriptionEn:match(pattern) then
-- We found an artifact that mentions our target skill by name
local description = linkifyDescription(artifact.description, skillsHub, lang)
-- Note: We cannot highlight the current skill because we're passing the description to ArtifactSummary
--description = highlightCurrentSkill(description, skillName)
table.insert(related, {
id = id,
name = artifact.name,
description = description,
icon = artifact.icon,
})
end
end
return related
end
------------------------------------------------------------------------------------------------------------------------
--
------------------------------------------------------------------------------------------------------------------------
local function renderArtifacts(artifacts, lang, frame)
local list = {}
for _, artifact in ipairs(artifacts) do
local formatted = f(
'{{ArtifactSummary | lang=%s | id=%s | description=%s}}',
lang,
artifact.id,
artifact.description
);
table.insert(list, frame:preprocess(formatted))
end
return table.concat(list)
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 ''
if skillId == '' then return 'You must provide a skill id!' end
--lang = 'en'
--skillId = 'skill_diplomacy'
-- Cargo
local skillsHub = SkillsOverview.getSkillsFromCargo(lang)
local skill = skillsHub[skillId]
if not skill then return 'Cannot resolve skill!' end
local artifactsResult = queryArtifacts(lang)
local artifactsHub = consolidateArtifacts(artifactsResult, lang)
-- Main computation
local pattern = SPECIAL_PATTERNS[skill.nameEn] or skill.nameEn
local artifacts = findRelatedArtifacts(artifactsHub, pattern, skill.name, skillsHub, lang)
--if 1 then return dump(artifacts) end
if #artifacts == 0 then return "''No results''" end
-- Output
local markup = renderArtifacts(artifacts, lang, frame)
return markup
end
return p


