Module:Sandbox/Aurelain/Heroes: Difference between revisions
From Heroes of Might and Magic: Olden Era Official Wiki
No edit summary |
No edit summary |
||
| (125 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
-- Usage: {{#invoke:HeroesOverview|display|lang=en|faction=Temple}} | -- Usage: {{#invoke:HeroesOverview|display|lang=en|faction=Temple}} | ||
local p = {} | local p = {} | ||
local f = mw.ustring.format | |||
local SkillsOverview = require("Module:SkillsOverview") | local SkillsOverview = require("Module:SkillsOverview") | ||
| Line 10: | Line 11: | ||
unfrozen = 5, | unfrozen = 5, | ||
dungeon = 6, | dungeon = 6, | ||
} | |||
local MIGHT_MAGIC_ORDER = { | |||
might = 1, | |||
magic = 2, | |||
} | } | ||
| Line 19: | Line 25: | ||
Schism = 'unfrozen', | Schism = 'unfrozen', | ||
Dungeon = 'dungeon', | Dungeon = 'dungeon', | ||
} | } | ||
| Line 33: | Line 34: | ||
-- Note: The right side is just a fallback, it will seldom be used. | -- Note: The right side is just a fallback, it will seldom be used. | ||
local TRANSLATION_IDS = { | local TRANSLATION_IDS = { | ||
wiki_heroes_hero = 'Hero', | |||
wiki_heroes_type = 'Type', | wiki_heroes_type = 'Type', | ||
wiki_heroes_class = 'Class', | wiki_heroes_class = 'Class', | ||
wiki_heroes_specialty = 'Specialty', | |||
wiki_might = 'Might', | wiki_might = 'Might', | ||
wiki_magic = 'Magic', | wiki_magic = 'Magic', | ||
starting_skills = 'Starting Skills', | |||
starting_spells = 'Starting Spells', | |||
wiki_heroes_toggle = 'Toggle specialty', | |||
} | } | ||
| Line 53: | Line 53: | ||
} | } | ||
local PATCH_NAME = { | |||
['Summon Avatar'] = 'Summon Avatar (Skill)', | |||
} | |||
-- Displays a variable | -- Displays a variable | ||
------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------ | ||
| Line 60: | Line 63: | ||
end | end | ||
-- Spawns a safe robotic url from an English name, fit for adding it before the visible name | |||
------------------------------------------------------------------------------------------------------------------------ | |||
local function getUrl(nameEn, lang) | |||
if not nameEn then | |||
return '' | |||
end | |||
local url = PATCH_NAME[nameEn] or nameEn | |||
if lang ~= 'en' then | |||
url = url .. '/' .. lang | |||
end | |||
return url | |||
end | |||
-- Parses the Specialty Summaries from the "Heroes Overview" page | |||
------------------------------------------------------------------------------------------------------------------------ | |||
local function getSpecialtySummaries(lang) | |||
local suffix = lang == 'en' and '' or '/' .. lang | |||
local targetTitle = mw.title.new('Heroes Overview' .. suffix) | |||
local content = targetTitle and targetTitle.exists and targetTitle:getContent() | |||
local dictionary = {} | |||
for line in string.gmatch(content, "[^\r\n]+") do | |||
local key, value = string.match(line, "%((.-)%)%s*=%s*(.*)") | |||
if key and value then | |||
dictionary[key] = value | |||
end | |||
end | |||
return dictionary | |||
end | |||
------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------ | ||
| Line 68: | Line 99: | ||
------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------ | ||
local function | local function cleanHtmlWhitespace(str) | ||
local | local cleaned = str:gsub(">%s+", ">"):gsub("%s+<", "<") | ||
return cleaned | |||
return | |||
end | end | ||
------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------ | ||
local function shortenSpecId(specId) | |||
return specId:gsub('_specialization', ''):gsub('_hero_', '') | |||
end | |||
-- Retrieves the text for some specific ids from Cargo Translations. | -- Retrieves the text for some specific ids from Cargo Translations. | ||
------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------ | ||
| Line 101: | Line 133: | ||
-- Dictionary | -- Dictionary | ||
local dictionary = mw.clone(ids) | local dictionary = mw.clone(ids) | ||
for key, value in pairs(dictionary) do | |||
for _, row in ipairs(results) do | dictionary[key] = value .. '[[Data:WikiTranslations/' .. lang .. '#' .. key .. '|💬]]' | ||
dictionary[row['target_id']] = | --dictionary[key] = value | ||
end | |||
for _, row in ipairs(results) do | |||
local name = row['name'] | |||
if name and name ~= '' and not name:find('💬') then | |||
dictionary[row['target_id']] = name | |||
end | end | ||
end | end | ||
| Line 109: | Line 146: | ||
end | end | ||
-- Converts a dictionary into a linear array | -- Converts a dictionary into a linear array | ||
------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------ | ||
| Line 120: | Line 156: | ||
end | end | ||
-- Interrogates the Hero Cargo table | -- Interrogates the Hero Cargo table | ||
------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------ | ||
local function queryHero(lang, forcedFaction) | local function queryHero(lang, forcedFaction, forcedClass, forcedHeroes) | ||
-- fields: | -- fields: | ||
local fields = {} | local fields = {} | ||
| Line 140: | Line 175: | ||
-- where: | -- where: | ||
local where = {} | local where = {} | ||
if forcedFaction then | |||
table.insert(where, 'Hero.faction="' .. forcedFaction .. '"') | |||
end | |||
if forcedClass then | |||
table.insert(where, 'Hero.class_id="' .. forcedClass .. '"') | |||
end | |||
if forcedHeroes then | |||
table.insert(where, 'Hero.id IN ("' .. forcedHeroes:gsub(',', '", "') .. '")') | |||
end | |||
table.insert(where, 'Hero.id not like "campaign%"') | table.insert(where, 'Hero.id not like "campaign%"') | ||
table.insert(where, 'Hero.id not like "tutorial%"') | table.insert(where, 'Hero.id not like "tutorial%"') | ||
| Line 155: | Line 198: | ||
end | end | ||
-- Interrogates the HeroSpecialization Cargo table | -- Interrogates the HeroSpecialization Cargo table | ||
------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------ | ||
| Line 178: | Line 220: | ||
end | end | ||
-- Folds all Cargo results into a single dictionary: | -- Folds all Cargo results into a single dictionary: | ||
------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------ | ||
local function consolidateHeroes(lang, heroesResults, specializations, | local function consolidateHeroes(lang, heroesResults, specializations, summaries) | ||
-- specializations | -- specializations | ||
local specHub = {} | local specHub = {} | ||
| Line 200: | Line 241: | ||
entry = {} | entry = {} | ||
entry.id = id or '' | entry.id = id or '' | ||
entry.name = '' | |||
entry.nameEn = '' | |||
entry.class_id = row.class_id or '' | entry.class_id = row.class_id or '' | ||
entry.faction = row.faction or '' | entry.faction = row.faction or '' | ||
| Line 208: | Line 251: | ||
entry.start_magics = row.start_magics or '' | entry.start_magics = row.start_magics or '' | ||
local spec = specHub[row.specialization_id] or {} | local spec = specHub[row.specialization_id] or {} | ||
entry.specId = row.specialization_id or '' | |||
entry.specIcon = spec.icon or '' | entry.specIcon = spec.icon or '' | ||
entry.specName = spec.name or '' | entry.specName = spec.name or '' | ||
entry.specDescription = spec.description or '' | entry.specDescription = spec.description or '' | ||
entry.specSummary = summaries[row.specialization_id] or '' | |||
hub[id] = entry | hub[id] = entry | ||
end | end | ||
| Line 223: | Line 268: | ||
end | end | ||
------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------ | ||
local function sortHeroes(a, b) | local function sortHeroes(a, b) | ||
| Line 242: | Line 285: | ||
end | end | ||
-- Boilerplate for the header cells | -- Boilerplate for the header cells | ||
------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------ | ||
local function addTh(tr, text) | local function addTh(tr, text) | ||
tr:tag('th'):wikitext(text | return tr:tag('th'):wikitext(text) | ||
end | end | ||
-- Boilerplate for the body cells | -- Boilerplate for the body cells | ||
------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------ | ||
local function addTd(tr, content) | local function addTd(tr, content) | ||
if type(content) == "string" then | if type(content) == "string" then | ||
tr:tag('td'):wikitext(content | return tr:tag('td'):wikitext(content) | ||
else | else | ||
tr:tag('td'):node(content | return tr:tag('td'):node(content) | ||
end | end | ||
end | end | ||
-- Boilerplate for the separator rows | -- Boilerplate for the separator rows | ||
------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------ | ||
| Line 271: | Line 311: | ||
------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------ | ||
local function createHeader(htmlTable, words, isCompact) | |||
local function createHeader(htmlTable, | |||
local tr = htmlTable:tag('tr') | local tr = htmlTable:tag('tr') | ||
addTh(tr, words. | addTh(tr, words.wiki_heroes_hero):attr('colspan', 2) | ||
addTh(tr, words.wiki_heroes_type) | addTh(tr, words.wiki_heroes_type) | ||
addTh(tr, words.wiki_heroes_class) | addTh(tr, words.wiki_heroes_class) | ||
addTh(tr, words. | addTh(tr, words.wiki_heroes_specialty):attr('colspan', 2) | ||
if not isCompact then | |||
addTh(tr, words. | addTh(tr, words.starting_skills) | ||
addTh(tr, words.starting_spells) | |||
end | |||
end | end | ||
| Line 294: | Line 326: | ||
local function renderFaction(faction, lang, frame) | local function renderFaction(faction, lang, frame) | ||
return frame:preprocess('{{F|' .. faction .. '|' .. lang .. '}}') | return frame:preprocess('{{F|' .. faction .. '|' .. lang .. '}}') | ||
end | |||
------------------------------------------------------------------------------------------------------------------------ | |||
local function renderIcon(nameEn, lang) | |||
local url = getUrl(nameEn, lang) | |||
return f('[[File:%s.png|70px|link=%s]]', nameEn, url) | |||
end | |||
------------------------------------------------------------------------------------------------------------------------ | |||
local function renderName(name, nameEn, lang) | |||
local url = getUrl(nameEn, lang) | |||
return f('[[%s|%s]]', url, name~='' and name or nameEn) | |||
end | end | ||
| Line 309: | Line 353: | ||
------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------ | ||
local function | local function renderSpecialtyIcon(icon) | ||
icon = convertToTitleCase(icon) | icon = convertToTitleCase(icon) | ||
for key, value in pairs(SPEC_ICON_REPLACEMENTS) do | for key, value in pairs(SPEC_ICON_REPLACEMENTS) do | ||
icon = string.gsub(icon, key, value) | icon = string.gsub(icon, key, value) | ||
end | end | ||
return f('[[File:%s.png|64px|link=]]', icon) | |||
end | end | ||
------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------ | ||
local function | local function renderSpecialtyText(name, summary, specId, words) | ||
local | local markup = [[<b>%s</b><br>%s <span class='toggle mw-customtoggle-%s' role='button' title='%s'>…</span>]] | ||
return f(markup, name, summary, shortenSpecId(specId), words.wiki_heroes_toggle) | |||
end | end | ||
------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------ | ||
local function | local function renderStartSkills(startSkills, skillsHub, lang) | ||
local items = {} | |||
local | local skillIds = mw.text.split(startSkills, ',') | ||
if 1 then | if #skillIds > 1 then | ||
skillIds = { skillIds[2], skillIds[1] } | |||
for _, skillId in ipairs(skillIds) do | |||
local skill = skillsHub[skillId] or {} | |||
local ranks = skill.ranks or {} | |||
local rank = ranks[1] or {} | |||
table.insert(items, { | |||
name = skill.name or '', | |||
nameEn = skill.nameEn or '', | |||
icon = rank.icon or '', | |||
}) | |||
end | |||
else | else | ||
local skill = skillsHub[ | local skill = skillsHub[skillIds[1]] or {} | ||
local ranks = skill.ranks or {} | local ranks = skill.ranks or {} | ||
rank = ranks[2] or { | local rank = ranks[2] or {} | ||
table.insert(items, { | |||
name = rank.name or '', | |||
nameEn = skill.nameEn or '', | |||
icon = rank.icon or '', | |||
}) | |||
end | end | ||
local file = '[[File: | local lines = {} | ||
for _, item in ipairs(items) do | |||
return | local url = getUrl(item.nameEn, lang) | ||
local file = f('[[File:%s.png|32px|link=%s]]', item.icon, url); | |||
local text = item.name~='' and item.name or item.nameEn | |||
local link = '[[' .. url .. '|' .. text .. ']]' | |||
table.insert(lines, f('<div>%s %s</div>', file, link)) | |||
end | |||
return table.concat(lines, '') | |||
end | end | ||
------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------ | ||
-- | local function renderSpell(startMagics, frame, lang) | ||
if startMagics == '' then return '' end | |||
local ids = mw.text.split(startMagics, ',') | |||
local texts = {} | |||
for _, id in ipairs(ids) do | |||
if id ~= 'change_use_necromancy' and not id:find('astral_summon') then | |||
local cleanId = string.gsub(id, '_special$', '') | |||
local text = frame:preprocess(f('{{LinkIcon|type=spell|id=%s|lang=%s}}', cleanId, lang)) | |||
--if cleanId ~= id then text = text .. '+' end | |||
table.insert(texts, text) | |||
end | |||
end | |||
return table.concat(texts, '<br>') | |||
end | |||
------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------ | ||
local function createBody(htmlTable, heroes, lang, frame, words, skillsHub) | local function getSkillSortValue(startSkills) | ||
local skillIds = mw.text.split(startSkills, ',') | |||
return skillIds[2] or skillIds[1] | |||
end | |||
------------------------------------------------------------------------------------------------------------------------ | |||
local function addSpecDescription(htmlTable, description, specId) | |||
local markup = cleanHtmlWhitespace([[ | |||
<tr class='expand-child mw-collapsible mw-collapsed' id='mw-customcollapsible-%s'> | |||
<td colspan='100' class='specialty-full-td'> | |||
%s | |||
</td> | |||
</tr> | |||
]]) | |||
htmlTable:node(f(markup, shortenSpecId(specId), description)) | |||
end | |||
------------------------------------------------------------------------------------------------------------------------ | |||
local function createBody(htmlTable, heroes, lang, frame, words, skillsHub, isCompact) | |||
local currentType = #heroes > 0 and heroes[1].class_type | local currentType = #heroes > 0 and heroes[1].class_type | ||
local currentFaction = #heroes > 0 and heroes[1].faction | local currentFaction = #heroes > 0 and heroes[1].faction | ||
| Line 379: | Line 455: | ||
end | end | ||
local tr = htmlTable:tag('tr') | local skillSortValue = getSkillSortValue(hero.start_skills) | ||
addTd(tr, hero.name) | |||
local tr = htmlTable:tag('tr'):addClass('hero') | |||
addTd(tr, renderIcon(hero.nameEn, lang)) | |||
addTd(tr, renderName(hero.name, hero.nameEn, lang)) | |||
addTd(tr, renderType(hero.class_type, words, frame)) | addTd(tr, renderType(hero.class_type, words, frame)) | ||
addTd(tr, renderClass(hero.class_id, lang, frame)) | addTd(tr, renderClass(hero.class_id, lang, frame)) | ||
addTd(tr, | addTd(tr, renderSpecialtyIcon(hero.specIcon)) | ||
addTd(tr, | addTd(tr, renderSpecialtyText(hero.specName, hero.specSummary, hero.specId, words)) | ||
if not isCompact then | |||
addTd(tr, renderStartSkills(hero.start_skills, skillsHub, lang)):attr('data-sort-value', skillSortValue) | |||
addTd(tr, renderSpell(hero.start_magics, frame, lang)) | |||
end | |||
addSpecDescription(htmlTable, hero.specDescription, hero.specId) | |||
end | end | ||
end | end | ||
------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------ | ||
-- | local function createToggleButton(heroes, words) | ||
local classes = {} | |||
for _, hero in ipairs(heroes) do | |||
table.insert(classes, 'mw-customtoggle-' .. shortenSpecId(hero.specId)) | |||
end | |||
local classesBlob = table.concat(classes, ' ') | |||
local markup = cleanHtmlWhitespace([[ | |||
<div class='toggle-container'> | |||
<span role='button' class='toggle %s'>%s</span> | |||
</div> | |||
]]) | |||
return f(markup, classesBlob, words.wiki_heroes_toggle) | |||
end | |||
-- M A I N | |||
------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------ | ||
function p.display(frame) | function p.display(frame) | ||
-- | --do return dump(frame) end | ||
-- Args | -- Args | ||
local args = frame.args | local args = frame.args | ||
local lang = args.lang ~= '' and args.lang or 'en' | local lang = args.lang ~= '' and args.lang or 'en' | ||
--lang = ' | --lang = 'zh-hans' | ||
local forcedFaction = FACTION_MAPPING[args.faction] or nil | local forcedFaction = FACTION_MAPPING[args.faction] or nil | ||
--forcedFaction = 'human' | |||
local forcedClass = args.class | |||
--forcedClass = 'might_human' | |||
local forcedHeroes = args.heroes | |||
--forcedHeroes = 'human_hero_1,human_hero_4' | |||
-- Language | -- Language | ||
local words = translateIds(TRANSLATION_IDS, lang) | local words = translateIds(TRANSLATION_IDS, lang) | ||
-- Text from the "Heroes Overview" overview | |||
local summaries = getSpecialtySummaries(lang) | |||
-- Cargo | -- Cargo | ||
local heroesResults = queryHero(lang, forcedFaction) | local heroesResults = queryHero(lang, forcedFaction, forcedClass, forcedHeroes) | ||
local specializations = queryHeroSpecialization(lang) | local specializations = queryHeroSpecialization(lang) | ||
local skillsHub = SkillsOverview.getSkillsFromCargo(lang) | local skillsHub = SkillsOverview.getSkillsFromCargo(lang) | ||
local heroesHub = consolidateHeroes(lang, heroesResults, specializations, | local heroesHub = consolidateHeroes(lang, heroesResults, specializations, summaries) | ||
local heroes = flatten(heroesHub); | local heroes = flatten(heroesHub); | ||
if | if #heroes == 0 then return 'No results' end | ||
-- Various manipulations | -- Various manipulations | ||
| Line 419: | Line 523: | ||
local htmlTable = mw.html.create('table') | local htmlTable = mw.html.create('table') | ||
htmlTable:addClass('wikitable sortable') | htmlTable:addClass('wikitable sortable') | ||
createHeader(htmlTable, | local isCompact = forcedClass or forcedHeroes | ||
createBody(htmlTable, heroes, lang, frame, words, skillsHub) | if isCompact then | ||
htmlTable:addClass('is-compact') | |||
end | |||
createHeader(htmlTable, words, isCompact) | |||
createBody(htmlTable, heroes, lang, frame, words, skillsHub, isCompact) | |||
-- Output | -- Output | ||
local toggle = isCompact and '' or createToggleButton(heroes, words) | |||
local styleTag = frame:extensionTag('templatestyles', '', { src = frame:getTitle() .. '/styles.css' }) | local styleTag = frame:extensionTag('templatestyles', '', { src = frame:getTitle() .. '/styles.css' }) | ||
return styleTag .. tostring(htmlTable) | return '<div class="heroes-overview">' .. styleTag .. toggle .. tostring(htmlTable) .. '</div>' | ||
end | end | ||
return p | return p | ||
Latest revision as of 06:52, 22 July 2026
Documentation for this module may be created at Module:Sandbox/Aurelain/Heroes/doc
-- Usage: {{#invoke:HeroesOverview|display|lang=en|faction=Temple}}
local p = {}
local f = mw.ustring.format
local SkillsOverview = require("Module:SkillsOverview")
local FACTION_ORDER = {
human = 1,
undead = 2,
nature = 3,
demon = 4,
unfrozen = 5,
dungeon = 6,
}
local MIGHT_MAGIC_ORDER = {
might = 1,
magic = 2,
}
local FACTION_MAPPING = {
Temple = 'human',
Necropolis = 'undead',
Grove = 'nature',
Hive = 'demon',
Schism = 'unfrozen',
Dungeon = 'dungeon',
}
local TYPE_ICONS = {
might = 'Wip_Might_Specialization_Icon',
magic = 'Wip_Mage_Specialization_Icon',
}
-- Note: The right side is just a fallback, it will seldom be used.
local TRANSLATION_IDS = {
wiki_heroes_hero = 'Hero',
wiki_heroes_type = 'Type',
wiki_heroes_class = 'Class',
wiki_heroes_specialty = 'Specialty',
wiki_might = 'Might',
wiki_magic = 'Magic',
starting_skills = 'Starting Skills',
starting_spells = 'Starting Spells',
wiki_heroes_toggle = 'Toggle specialty',
}
local SPEC_ICON_REPLACEMENTS = {
Human = 'Temple',
Necro = 'Necropolis',
Nature = 'Grove',
Demon = 'Hive',
Unfrozen = 'Schism',
}
local PATCH_NAME = {
['Summon Avatar'] = 'Summon Avatar (Skill)',
}
-- Displays 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)
if not nameEn then
return ''
end
local url = PATCH_NAME[nameEn] or nameEn
if lang ~= 'en' then
url = url .. '/' .. lang
end
return url
end
-- Parses the Specialty Summaries from the "Heroes Overview" page
------------------------------------------------------------------------------------------------------------------------
local function getSpecialtySummaries(lang)
local suffix = lang == 'en' and '' or '/' .. lang
local targetTitle = mw.title.new('Heroes Overview' .. suffix)
local content = targetTitle and targetTitle.exists and targetTitle:getContent()
local dictionary = {}
for line in string.gmatch(content, "[^\r\n]+") do
local key, value = string.match(line, "%((.-)%)%s*=%s*(.*)")
if key and value then
dictionary[key] = value
end
end
return dictionary
end
------------------------------------------------------------------------------------------------------------------------
local function convertToTitleCase(str)
local titleCased = str:lower():gsub("%f[%a]%a", string.upper)
return titleCased
end
------------------------------------------------------------------------------------------------------------------------
local function cleanHtmlWhitespace(str)
local cleaned = str:gsub(">%s+", ">"):gsub("%s+<", "<")
return cleaned
end
------------------------------------------------------------------------------------------------------------------------
local function shortenSpecId(specId)
return specId:gsub('_specialization', ''):gsub('_hero_', '')
end
-- Retrieves the text for some specific ids from Cargo Translations.
------------------------------------------------------------------------------------------------------------------------
local function translateIds(ids, lang, extra)
-- Key list
local list = {}
for key, _ in pairs(ids) do
list[#list + 1] = key
end
local idListString = '"' .. table.concat(list, '", "') .. '"'
-- Cargo
local where = {}
table.insert(where, 'target_id IN (' .. idListString .. ')')
table.insert(where, 'language = "' .. lang .. '"')
if extra then
table.insert(where, extra)
end
local results = mw.ext.cargo.query('Translation', 'target_id, name', {
where = table.concat(where, ' AND '),
limit = 100
})
-- Dictionary
local dictionary = mw.clone(ids)
for key, value in pairs(dictionary) do
dictionary[key] = value .. '[[Data:WikiTranslations/' .. lang .. '#' .. key .. '|💬]]'
--dictionary[key] = value
end
for _, row in ipairs(results) do
local name = row['name']
if name and name ~= '' and not name:find('💬') then
dictionary[row['target_id']] = name
end
end
return dictionary
end
-- Converts a dictionary into a linear array
------------------------------------------------------------------------------------------------------------------------
local function flatten(hub)
local list = {}
for _, entry in pairs(hub) do
list[#list + 1] = entry
end
return list
end
-- Interrogates the Hero Cargo table
------------------------------------------------------------------------------------------------------------------------
local function queryHero(lang, forcedFaction, forcedClass, forcedHeroes)
-- fields:
local fields = {}
table.insert(fields, 'Hero.id = id');
table.insert(fields, 'Hero.class_id = class_id');
table.insert(fields, 'Hero.faction = faction');
table.insert(fields, 'Hero.class_type = class_type');
table.insert(fields, 'Hero.icon = icon');
table.insert(fields, 'Hero.specialization_id = specialization_id');
table.insert(fields, 'Hero.start_skills = start_skills');
table.insert(fields, 'Hero.start_skill_levels = start_skill_levels');
table.insert(fields, 'Hero.start_magics = start_magics');
table.insert(fields, 'Translation.name = name');
table.insert(fields, 'Translation.language = language');
-- where:
local where = {}
if forcedFaction then
table.insert(where, 'Hero.faction="' .. forcedFaction .. '"')
end
if forcedClass then
table.insert(where, 'Hero.class_id="' .. forcedClass .. '"')
end
if forcedHeroes then
table.insert(where, 'Hero.id IN ("' .. forcedHeroes:gsub(',', '", "') .. '")')
end
table.insert(where, 'Hero.id not like "campaign%"')
table.insert(where, 'Hero.id not like "tutorial%"')
table.insert(where, 'Hero.id not like "%_fun_%"')
table.insert(where, 'Translation.type = "hero"')
table.insert(where, '(Translation.language = "en" OR Translation.language = "' .. lang .. '")')
-- query:
return mw.ext.cargo.query('Hero, Translation', table.concat(fields, ','), {
join = 'Hero.id = Translation.target_id',
where = table.concat(where, ' AND '),
limit = 500
})
end
-- Interrogates the HeroSpecialization Cargo table
------------------------------------------------------------------------------------------------------------------------
local function queryHeroSpecialization(lang)
-- fields:
local fields = {}
table.insert(fields, 'HeroSpecialization.id = id');
table.insert(fields, 'HeroSpecialization.icon = icon');
table.insert(fields, 'Translation.name = name');
table.insert(fields, 'Translation.description = description');
-- where:
local where = {}
table.insert(where, 'Translation.language = "' .. lang .. '"')
-- query:
return mw.ext.cargo.query('HeroSpecialization, Translation', table.concat(fields, ','), {
join = 'HeroSpecialization.id = Translation.target_id',
where = table.concat(where, ' AND '),
limit = 500
})
end
-- Folds all Cargo results into a single dictionary:
------------------------------------------------------------------------------------------------------------------------
local function consolidateHeroes(lang, heroesResults, specializations, summaries)
-- specializations
local specHub = {}
for _, row in ipairs(specializations) do
specHub[row.id] = {
icon = row.icon or '',
name = row.name or '',
description = row.description or '',
}
end
-- heroes
local hub = {}
for _, row in ipairs(heroesResults) do
local id = row.id
local entry = hub[id]
if not entry then
entry = {}
entry.id = id or ''
entry.name = ''
entry.nameEn = ''
entry.class_id = row.class_id or ''
entry.faction = row.faction or ''
entry.class_type = row.class_type or ''
entry.icon = row.icon or ''
entry.start_skills = row.start_skills or ''
entry.start_skill_levels = row.start_skill_levels or ''
entry.start_magics = row.start_magics or ''
local spec = specHub[row.specialization_id] or {}
entry.specId = row.specialization_id or ''
entry.specIcon = spec.icon or ''
entry.specName = spec.name or ''
entry.specDescription = spec.description or ''
entry.specSummary = summaries[row.specialization_id] or ''
hub[id] = entry
end
if row.language == lang then
entry.name = row.name or ''
end
if row.language == 'en' then
entry.nameEn = row.name or ''
end
end
return hub
end
------------------------------------------------------------------------------------------------------------------------
local function sortHeroes(a, b)
local factionA = FACTION_ORDER[a.faction] or 99
local factionB = FACTION_ORDER[b.faction] or 99
if factionA ~= factionB then
return factionA < factionB
end
local mmA = MIGHT_MAGIC_ORDER[a.class_type] or 99
local mmB = MIGHT_MAGIC_ORDER[b.class_type] or 99
if mmA ~= mmB then
return mmA < mmB
end
return a.id < b.id
end
-- Boilerplate for the header cells
------------------------------------------------------------------------------------------------------------------------
local function addTh(tr, text)
return tr:tag('th'):wikitext(text)
end
-- Boilerplate for the body cells
------------------------------------------------------------------------------------------------------------------------
local function addTd(tr, content)
if type(content) == "string" then
return tr:tag('td'):wikitext(content)
else
return tr:tag('td'):node(content)
end
end
-- Boilerplate for the separator rows
------------------------------------------------------------------------------------------------------------------------
local function addSeparator(htmlTable, className, content)
htmlTable:tag('tr')
:addClass('separator')
:addClass(className)
:tag('td'):attr('data-sort-value', ''):attr('colspan', 100):wikitext(content):done()
end
------------------------------------------------------------------------------------------------------------------------
local function createHeader(htmlTable, words, isCompact)
local tr = htmlTable:tag('tr')
addTh(tr, words.wiki_heroes_hero):attr('colspan', 2)
addTh(tr, words.wiki_heroes_type)
addTh(tr, words.wiki_heroes_class)
addTh(tr, words.wiki_heroes_specialty):attr('colspan', 2)
if not isCompact then
addTh(tr, words.starting_skills)
addTh(tr, words.starting_spells)
end
end
------------------------------------------------------------------------------------------------------------------------
local function renderFaction(faction, lang, frame)
return frame:preprocess('{{F|' .. faction .. '|' .. lang .. '}}')
end
------------------------------------------------------------------------------------------------------------------------
local function renderIcon(nameEn, lang)
local url = getUrl(nameEn, lang)
return f('[[File:%s.png|70px|link=%s]]', nameEn, url)
end
------------------------------------------------------------------------------------------------------------------------
local function renderName(name, nameEn, lang)
local url = getUrl(nameEn, lang)
return f('[[%s|%s]]', url, name~='' and name or nameEn)
end
------------------------------------------------------------------------------------------------------------------------
local function renderType(type, words, frame)
local file = '[[File:' .. TYPE_ICONS[type] .. '.png|32px|link=]]';
local text = type == 'magic' and words.wiki_magic or words.wiki_might
return frame:preprocess('{{Hint|' .. file .. '|' .. text .. '}}')
end
------------------------------------------------------------------------------------------------------------------------
local function renderClass(class, lang, frame)
return frame:preprocess('{{LinkIcon|type=hero_class|id=' .. class .. '|suffix=_icon.png|lang=' .. lang .. '}}')
end
------------------------------------------------------------------------------------------------------------------------
local function renderSpecialtyIcon(icon)
icon = convertToTitleCase(icon)
for key, value in pairs(SPEC_ICON_REPLACEMENTS) do
icon = string.gsub(icon, key, value)
end
return f('[[File:%s.png|64px|link=]]', icon)
end
------------------------------------------------------------------------------------------------------------------------
local function renderSpecialtyText(name, summary, specId, words)
local markup = [[<b>%s</b><br>%s <span class='toggle mw-customtoggle-%s' role='button' title='%s'>…</span>]]
return f(markup, name, summary, shortenSpecId(specId), words.wiki_heroes_toggle)
end
------------------------------------------------------------------------------------------------------------------------
local function renderStartSkills(startSkills, skillsHub, lang)
local items = {}
local skillIds = mw.text.split(startSkills, ',')
if #skillIds > 1 then
skillIds = { skillIds[2], skillIds[1] }
for _, skillId in ipairs(skillIds) do
local skill = skillsHub[skillId] or {}
local ranks = skill.ranks or {}
local rank = ranks[1] or {}
table.insert(items, {
name = skill.name or '',
nameEn = skill.nameEn or '',
icon = rank.icon or '',
})
end
else
local skill = skillsHub[skillIds[1]] or {}
local ranks = skill.ranks or {}
local rank = ranks[2] or {}
table.insert(items, {
name = rank.name or '',
nameEn = skill.nameEn or '',
icon = rank.icon or '',
})
end
local lines = {}
for _, item in ipairs(items) do
local url = getUrl(item.nameEn, lang)
local file = f('[[File:%s.png|32px|link=%s]]', item.icon, url);
local text = item.name~='' and item.name or item.nameEn
local link = '[[' .. url .. '|' .. text .. ']]'
table.insert(lines, f('<div>%s %s</div>', file, link))
end
return table.concat(lines, '')
end
------------------------------------------------------------------------------------------------------------------------
local function renderSpell(startMagics, frame, lang)
if startMagics == '' then return '' end
local ids = mw.text.split(startMagics, ',')
local texts = {}
for _, id in ipairs(ids) do
if id ~= 'change_use_necromancy' and not id:find('astral_summon') then
local cleanId = string.gsub(id, '_special$', '')
local text = frame:preprocess(f('{{LinkIcon|type=spell|id=%s|lang=%s}}', cleanId, lang))
--if cleanId ~= id then text = text .. '+' end
table.insert(texts, text)
end
end
return table.concat(texts, '<br>')
end
------------------------------------------------------------------------------------------------------------------------
local function getSkillSortValue(startSkills)
local skillIds = mw.text.split(startSkills, ',')
return skillIds[2] or skillIds[1]
end
------------------------------------------------------------------------------------------------------------------------
local function addSpecDescription(htmlTable, description, specId)
local markup = cleanHtmlWhitespace([[
<tr class='expand-child mw-collapsible mw-collapsed' id='mw-customcollapsible-%s'>
<td colspan='100' class='specialty-full-td'>
%s
</td>
</tr>
]])
htmlTable:node(f(markup, shortenSpecId(specId), description))
end
------------------------------------------------------------------------------------------------------------------------
local function createBody(htmlTable, heroes, lang, frame, words, skillsHub, isCompact)
local currentType = #heroes > 0 and heroes[1].class_type
local currentFaction = #heroes > 0 and heroes[1].faction
for _, hero in ipairs(heroes) do
-- separators
if hero.faction ~= currentFaction then
currentFaction = hero.faction
currentType = hero.class_type
addSeparator(htmlTable, 'separator-large', renderFaction(currentFaction, lang, frame))
elseif hero.class_type ~= currentType then
currentType = hero.class_type
addSeparator(htmlTable, 'separator-tiny', '')
end
local skillSortValue = getSkillSortValue(hero.start_skills)
local tr = htmlTable:tag('tr'):addClass('hero')
addTd(tr, renderIcon(hero.nameEn, lang))
addTd(tr, renderName(hero.name, hero.nameEn, lang))
addTd(tr, renderType(hero.class_type, words, frame))
addTd(tr, renderClass(hero.class_id, lang, frame))
addTd(tr, renderSpecialtyIcon(hero.specIcon))
addTd(tr, renderSpecialtyText(hero.specName, hero.specSummary, hero.specId, words))
if not isCompact then
addTd(tr, renderStartSkills(hero.start_skills, skillsHub, lang)):attr('data-sort-value', skillSortValue)
addTd(tr, renderSpell(hero.start_magics, frame, lang))
end
addSpecDescription(htmlTable, hero.specDescription, hero.specId)
end
end
------------------------------------------------------------------------------------------------------------------------
local function createToggleButton(heroes, words)
local classes = {}
for _, hero in ipairs(heroes) do
table.insert(classes, 'mw-customtoggle-' .. shortenSpecId(hero.specId))
end
local classesBlob = table.concat(classes, ' ')
local markup = cleanHtmlWhitespace([[
<div class='toggle-container'>
<span role='button' class='toggle %s'>%s</span>
</div>
]])
return f(markup, classesBlob, words.wiki_heroes_toggle)
end
-- M A I N
------------------------------------------------------------------------------------------------------------------------
function p.display(frame)
--do return dump(frame) end
-- Args
local args = frame.args
local lang = args.lang ~= '' and args.lang or 'en'
--lang = 'zh-hans'
local forcedFaction = FACTION_MAPPING[args.faction] or nil
--forcedFaction = 'human'
local forcedClass = args.class
--forcedClass = 'might_human'
local forcedHeroes = args.heroes
--forcedHeroes = 'human_hero_1,human_hero_4'
-- Language
local words = translateIds(TRANSLATION_IDS, lang)
-- Text from the "Heroes Overview" overview
local summaries = getSpecialtySummaries(lang)
-- Cargo
local heroesResults = queryHero(lang, forcedFaction, forcedClass, forcedHeroes)
local specializations = queryHeroSpecialization(lang)
local skillsHub = SkillsOverview.getSkillsFromCargo(lang)
local heroesHub = consolidateHeroes(lang, heroesResults, specializations, summaries)
local heroes = flatten(heroesHub);
if #heroes == 0 then return 'No results' end
-- Various manipulations
table.sort(heroes, sortHeroes)
-- Table
local htmlTable = mw.html.create('table')
htmlTable:addClass('wikitable sortable')
local isCompact = forcedClass or forcedHeroes
if isCompact then
htmlTable:addClass('is-compact')
end
createHeader(htmlTable, words, isCompact)
createBody(htmlTable, heroes, lang, frame, words, skillsHub, isCompact)
-- Output
local toggle = isCompact and '' or createToggleButton(heroes, words)
local styleTag = frame:extensionTag('templatestyles', '', { src = frame:getTitle() .. '/styles.css' })
return '<div class="heroes-overview">' .. styleTag .. toggle .. tostring(htmlTable) .. '</div>'
end
return p


