Module:Sandbox/Aurelain/Heroes: Difference between revisions
From Heroes of Might and Magic: Olden Era Official Wiki
No edit summary |
No edit summary |
||
| Line 74: | Line 74: | ||
local fields = {} | local fields = {} | ||
table.insert(fields, 'Hero.id = id'); | table.insert(fields, 'Hero.id = id'); | ||
table.insert(fields, 'Hero.name_sid = name_sid'); | |||
table.insert(fields, 'Translation.name = name'); | table.insert(fields, 'Translation.name = name'); | ||
table.insert(fields, 'Translation.language = language'); | table.insert(fields, 'Translation.language = language'); | ||
| Line 88: | Line 89: | ||
-- query: | -- query: | ||
return mw.ext.cargo.query('Hero, Translation', table.concat(fields, ','), { | return mw.ext.cargo.query('Hero, Translation', table.concat(fields, ','), { | ||
join = 'Hero. | join = 'Hero.name_sid = Translation.target_id', | ||
where = table.concat(where, ' AND '), | where = table.concat(where, ' AND '), | ||
limit = 500 | limit = 500 | ||
Revision as of 07:58, 15 June 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 FACTION_MAPPING = {
Temple = 'human',
Necropolis = 'undead',
Grove = 'nature',
Hive = 'demon',
Schism = 'unfrozen',
Dungeon = 'dungeon',
}
-- Note: The right side is just a fallback, it will seldom be used.
local TRANSLATION_IDS = {
wiki_name = 'Name',
}
------------------------------------------------------------------------------------------------------------------------
-- Displays a variable
------------------------------------------------------------------------------------------------------------------------
local function dump(target)
return '<pre>' .. mw.dumpObject(target) .. '</pre>'
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)
if results then
for _, row in ipairs(results) do
dictionary[row['target_id']] = row['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
------------------------------------------------------------------------------------------------------------------------
-- Retrieves the main hero translations from Cargo
------------------------------------------------------------------------------------------------------------------------
local function queryMain(lang, forcedFaction)
-- fields:
local fields = {}
table.insert(fields, 'Hero.id = id');
table.insert(fields, 'Hero.name_sid = name_sid');
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
table.insert(where, 'Hero.id not like "campaign%"')
table.insert(where, 'Hero.id not like "%_fun_%"')
table.insert(where, '(Translation.language = "en" OR Translation.language = "' .. lang .. '")')
-- query:
return mw.ext.cargo.query('Hero, Translation', table.concat(fields, ','), {
join = 'Hero.name_sid = Translation.target_id',
where = table.concat(where, ' AND '),
limit = 500
})
end
------------------------------------------------------------------------------------------------------------------------
-- Boilerplate for the header cells
------------------------------------------------------------------------------------------------------------------------
local function addTh(tr, text)
tr:tag('th'):wikitext(text):done()
end
------------------------------------------------------------------------------------------------------------------------
-- Boilerplate for the body cells
------------------------------------------------------------------------------------------------------------------------
local function addTd(tr, text)
tr:tag('td'):wikitext(text):done()
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', 7):wikitext(content):done()
end
------------------------------------------------------------------------------------------------------------------------
--
------------------------------------------------------------------------------------------------------------------------
local function createHeader(htmlTable)
local tr = htmlTable:tag('tr')
end
------------------------------------------------------------------------------------------------------------------------
--
------------------------------------------------------------------------------------------------------------------------
local function createBody(htmlTable)
end
------------------------------------------------------------------------------------------------------------------------
-- Main public function
------------------------------------------------------------------------------------------------------------------------
function p.display(frame)
--if 1 then return dump(frame) end
-- Args
local args = frame.args
local lang = args.lang ~= '' and args.lang or 'en'
local forcedFaction = FACTION_MAPPING[args.faction] or nil
-- Language
local words = translateIds(TRANSLATION_IDS, lang)
-- Cargo
local main = queryMain(lang, forcedFaction)
if 1 then return dump(main) end
--local hub = consolidateHeroes(main)
--local heroes = flatten(hub);
-- Various manipulations
--table.sort(heroes, sortSpells)
-- Table
local htmlTable = mw.html.create('table')
htmlTable:addClass('wikitable sortable')
createHeader(htmlTable)
createBody(htmlTable)
-- Output
local styleTag = frame:extensionTag('templatestyles', '', { src = frame:getTitle() .. '/styles.css' })
--return dump(pages)
return styleTag .. tostring(htmlTable)
end
return p


