Module:Sandbox/Aurelain/Spells: Difference between revisions
From Heroes of Might and Magic: Olden Era Official Wiki
No edit summary |
No edit summary |
||
| Line 36: | Line 36: | ||
local tables = 'Spell, Translation' | local tables = 'Spell, Translation' | ||
local fields = '' .. | local fields = '' .. | ||
'Spell.id, ' .. | 'Spell.id = id, ' .. | ||
'Spell.school, ' .. | 'Spell.school = school, ' .. | ||
'Spell.rank, ' .. | 'Spell.rank = rank, ' .. | ||
'Spell.used_on_map, ' .. | 'Spell.used_on_map = used_on_map, ' .. | ||
'Spell.icon, ' .. | 'Spell.icon = icon, ' .. | ||
'Spell.is_special_magic, ' .. | 'Spell.is_special_magic = is_special_magic, ' .. | ||
-- Translation | -- Translation | ||
'Translation.variant, ' .. | 'Translation.variant = variant, ' .. | ||
'Translation.name, ' .. | 'Translation.name = name, ' .. | ||
'Translation.description,' .. | 'Translation.description = description,' .. | ||
'Translation.bonus_description' | 'Translation.bonus_description = bonus_description' | ||
local where = {} | local where = {} | ||
if forcedSchool then | if forcedSchool then | ||
Revision as of 14:33, 23 May 2026
Documentation for this module may be created at Module:Sandbox/Aurelain/Spells/doc
-- Usage: {{#invoke:SpellsOverview|display}}
local p = {}
local SCHOOL_MAPPING = {
Daylight = 'day',
Nightshade = 'night',
Arcane = 'space',
Primal = 'primal',
Neutral = 'neutral',
}
------------------------------------------------------------------------------------------------------------------------
-- Displays a variable
------------------------------------------------------------------------------------------------------------------------
local function dump(target)
return '<pre>' .. mw.dumpObject(target) .. '</pre>'
end
------------------------------------------------------------------------------------------------------------------------
-- Detects the current language from the URL
------------------------------------------------------------------------------------------------------------------------
local function getCurrentLang()
local title = mw.title.getCurrentTitle()
local segments = mw.text.split(title.text, '/')
for i = #segments, 2, -1 do
local segment = mw.ustring.lower(segments[i])
if mw.language.isSupportedLanguage(segment) then
return segment
end
end
return 'en'
end
------------------------------------------------------------------------------------------------------------------------
-- Retrieves the most important data from Cargo
------------------------------------------------------------------------------------------------------------------------
local function queryMain(lang, forcedSchool)
local tables = 'Spell, Translation'
local fields = '' ..
'Spell.id = id, ' ..
'Spell.school = school, ' ..
'Spell.rank = rank, ' ..
'Spell.used_on_map = used_on_map, ' ..
'Spell.icon = icon, ' ..
'Spell.is_special_magic = is_special_magic, ' ..
-- Translation
'Translation.variant = variant, ' ..
'Translation.name = name, ' ..
'Translation.description = description,' ..
'Translation.bonus_description = bonus_description'
local where = {}
if forcedSchool then
table.insert(where, 'Spell.school="' .. forcedSchool .. '"')
end
table.insert(where, 'Translation.language = "' .. lang .. '"')
local cargoArgs = {
join = 'Spell.id = Translation.target_id',
where = table.concat(where, ' AND '),
limit = 10
}
return mw.ext.cargo.query(tables, fields, cargoArgs)
end
------------------------------------------------------------------------------------------------------------------------
-- Scans the Cargo results and folds the all rows belonging to a Spell into a single dictionary:
------------------------------------------------------------------------------------------------------------------------
local function consolidateMain(results)
local hub = {}
for _, row in ipairs(results) do
local id = row.id
local entry = hub[id]
if not entry then
entry = {}
entry.school = row.school
entry.rank = tonumber(row.rank)
entry.used_on_map = row.used_on_map
entry.icon = row.icon
entry.is_special_magic = row.is_special_magic
entry.mana_cost = 0 -- added later by `addManaCost()`
hub[id] = entry
end
if row.name then
entry.name = row.name
end
if row.description then
entry['desc' .. row.variant] = row.description
end
if row.bonus_description then
entry['bonus' .. row.variant] = row.bonus_description
end
end
return hub
end
------------------------------------------------------------------------------------------------------------------------
-- Main public function
------------------------------------------------------------------------------------------------------------------------
function p.display(frame)
-- Args
local args = frame.args
local forcedSchool = mw.text.trim(args[1] or args.school or '')
forcedSchool = SCHOOL_MAPPING[forcedSchool] or nil
-- Language
local lang = getCurrentLang()
-- Cargo
local main = queryMain(lang, forcedSchool)
--local hub = consolidateMain(main)
return dump(main)
end
return p


