Module:Sandbox/Aurelain/Cargo: Difference between revisions
From Heroes of Might and Magic: Olden Era Official Wiki
No edit summary |
No edit summary |
||
| Line 27: | Line 27: | ||
local title = mw.title.getCurrentTitle() | local title = mw.title.getCurrentTitle() | ||
if title.isSubpage then | if title.isSubpage then | ||
if mw.language.isSupportedLanguage(title.subpageText) then | |||
return title.subpageText | |||
end | end | ||
end | end | ||
| Line 35: | Line 35: | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- | -- Retrieves the data from Cargo | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
function | local function query(lang) | ||
local tables = 'Unit, Translation' | local tables = 'Unit, Translation' | ||
local | local fields = '' .. | ||
'Unit.id=id, ' .. | |||
'Unit.faction=faction, ' .. | 'Unit.faction=faction, ' .. | ||
'Unit.tier=tier, ' .. | 'Unit.tier=tier, ' .. | ||
'Unit.hp=hp, ' .. | |||
'Unit.offence=attack, ' .. | |||
'Translation.language=language, ' .. | 'Translation.language=language, ' .. | ||
'Translation.name=transName | 'Translation.name=transName' | ||
-- | -- Get BOTH the English row AND the target language row | ||
local cargoArgs = { | local cargoArgs = { | ||
join = | join = 'Unit.id = Translation.target_id', | ||
where = 'Translation.language = "en" OR Translation.language = "' .. lang .. '"', | where = 'Translation.language = "en" OR Translation.language = "' .. lang .. '"', | ||
orderBy = 'Unit.id ASC', | orderBy = 'Unit.id ASC', | ||
limit = 1000 -- Increased limit since each unit might return 2 rows now | limit = 1000 -- Increased limit since each unit might return 2 rows now | ||
} | } | ||
return mw.ext.cargo.query(tables, fields, cargoArgs) | |||
end | |||
local results = | -------------------------------------------------------------------------------- | ||
-- Main public function | |||
-------------------------------------------------------------------------------- | |||
function p.displayOverview() | |||
local lang = getCurrentLang() | |||
local results = query(lang) | |||
if not results or #results == 0 then | if not results or #results == 0 then | ||
return "No unit data found." | return "No unit data found." | ||
Revision as of 13:58, 16 May 2026
Documentation for this module may be created at Module:Sandbox/Aurelain/Cargo/doc
local p = {}
local FACTION_ORDER = {
human = 1,
undead = 2,
nature = 3,
demon = 4,
unfrozen = 5,
dungeon = 6,
neutral = 7,
}
local TRANSLATIONS_PATCH = {
unit = {
en = 'Unit',
},
faction = {
en = 'Faction',
},
tier = {
en = 'Tier',
}
}
--------------------------------------------------------------------------------
-- Detects the current page's language
--------------------------------------------------------------------------------
local function getCurrentLang()
local title = mw.title.getCurrentTitle()
if title.isSubpage then
if mw.language.isSupportedLanguage(title.subpageText) then
return title.subpageText
end
end
return 'en'
end
--------------------------------------------------------------------------------
-- Retrieves the data from Cargo
--------------------------------------------------------------------------------
local function query(lang)
local tables = 'Unit, Translation'
local fields = '' ..
'Unit.id=id, ' ..
'Unit.faction=faction, ' ..
'Unit.tier=tier, ' ..
'Unit.hp=hp, ' ..
'Unit.offence=attack, ' ..
'Translation.language=language, ' ..
'Translation.name=transName'
-- Get BOTH the English row AND the target language row
local cargoArgs = {
join = 'Unit.id = Translation.target_id',
where = 'Translation.language = "en" OR Translation.language = "' .. lang .. '"',
orderBy = 'Unit.id ASC',
limit = 1000 -- Increased limit since each unit might return 2 rows now
}
return mw.ext.cargo.query(tables, fields, cargoArgs)
end
--------------------------------------------------------------------------------
-- Main public function
--------------------------------------------------------------------------------
function p.displayOverview()
local lang = getCurrentLang()
local results = query(lang)
if not results or #results == 0 then
return "No unit data found."
end
-- 2. Group the results by Unit ID
local orderedIds = {}
local units = {}
-- Because Cargo returns a row for English and a row for the translation,
-- we fold them together into a single Lua table based on their ID.
for _, row in ipairs(results) do
local id = row['id']
-- If this is the first time seeing this ID, set up its basic stats
if not units[id] then
table.insert(orderedIds, id)
units[id] = {
hp = tonumber(row['hp']) or 0,
attack = tonumber(row['attack']) or 0,
enName = nil,
targetName = nil
}
end
-- Save the names based on which language row we are currently looping over
if row['language'] == 'en' then
units[id].enName = row['transName']
end
if row['language'] == lang then
units[id].targetName = row['transName']
end
end
-- 3. Create the HTML table
local htmlTable = mw.html.create('table')
:addClass('wikitable sortable')
htmlTable:tag('tr')
:tag('th'):wikitext('Unit Name'):done()
:tag('th'):wikitext('Health'):done()
:tag('th'):wikitext('Attack'):done()
-- 4. Loop through our newly grouped Lua data to build the display rows
for _, id in ipairs(orderedIds) do
local u = units[id]
-- Fallback logic: If English is somehow missing, show the raw ID.
-- If the translation is missing, fall back to English.
local originalName = u.enName or ('UnknownUnit_' .. id)
local displayName = u.targetName or originalName
-- Construct the localized page link
local linkTarget = originalName
if lang ~= 'en' then
linkTarget = originalName .. '/' .. lang
end
local tr = htmlTable:tag('tr')
-- The link now outputs correctly using the database IDs
tr:tag('td'):wikitext('[[' .. linkTarget .. '|' .. displayName .. ']]'):done()
tr:tag('td'):wikitext(tostring(u.hp)):done()
tr:tag('td'):wikitext(tostring(u.attack)):done()
end
return tostring(htmlTable)
end
return p


