Module:ScrollList

From Heroes of Might and Magic: Olden Era Official Wiki

Description

This module generates an automated table listing Magic Scrolls stored in the Cargo database. It is specifically designed to handle scroll variations (base, enchanted, mythic) and link them directly to their corresponding spell pages.

Usage

The module is invoked via the list function:

{{#invoke:ScrollList|list|level=...|language=...}}

Parameters

Parameter Description Example
level Filters scrolls by category (base, enchanted, or mythic). level=mythic
language Forces the Wiki language (if omitted, it detects the page suffix). language=fr

Key Features

  • Unified Display: Merges the Scroll name and the associated Spell link into a single column using the format: Scroll Name : Spell Name.
  • Smart Linking: Automatically generates wiki links using the English page title (e.g., SpellName/fr) while displaying the localized spell name.
  • ID Cleanup: Automatically strips prefixes (enchanted_, mythic_, magic_scroll_artifact_) to correctly identify and link the underlying spell.
  • Language Aware: Retrieves both scroll descriptions and spell names from the Translation table based on the current language.

Implementation Example

To display only "Mythic" magic scrolls:

== Mythic Scrolls ==
{{#invoke:ScrollList|list|level=mythic}}

Technical Notes

  • The module performs an id LIKE '%magic_scroll%' query on the Artifact table.
  • The base level filter excludes any IDs containing "enchanted" or "mythic".
  • If no scroll is found for the given criteria, the table displays a "No scroll found" message.

local p = {}

function p.list(frame)
    local level = frame.args["level"]
    
    -- 1. Détection de la langue (argument manuel ou suffixe de page)
    local lang = frame.args.language
    if not lang then
        local title = mw.title.getCurrentTitle().text
        local suffix = title:match("/([a-z][a-z_]+)$")
        lang = suffix or "en"
    end
    
    -- 2. Construction de la requête WHERE
    local whereClause = "id LIKE '%magic_scroll%'"
    if level == "base" then
        whereClause = whereClause .. " AND id NOT LIKE '%enchanted%' AND id NOT LIKE '%mythic%'"
    elseif level then
        whereClause = whereClause .. " AND id LIKE '%" .. level .. "%'"
    end
    
    local scrolls = mw.ext.cargo.query("Artifact", "id, rarity", {
        where = whereClause,
        orderBy = "rarity ASC"
    })
    
    local html = { '{| class="wikitable"', '! Name : Spell', '! Description' }
    
    for _, art in ipairs(scrolls) do
        -- Récupération du nom traduit du scroll
        local name_res = mw.ext.cargo.query("Translation", "name", { where = "target_id = '" .. art.id .. "' AND language = '" .. lang .. "' AND type = 'artifact'", limit = 1 })
        local name = (name_res and #name_res > 0) and name_res[1].name or art.id
        
        -- Nettoyage de l'ID du sort
        local spell_id = art.id
        spell_id = spell_id:gsub("enchanted_", "")
        spell_id = spell_id:gsub("mythic_", "")
        spell_id = spell_id:gsub("magic_scroll_artifact_", "")
        
        -- Récupération du nom du sort
        local spell_name_res = mw.ext.cargo.query("Translation", "name", { where = "target_id = '" .. spell_id .. "' AND language = '" .. lang .. "' AND type = 'spell'", limit = 1 })
        local spell_name = (spell_name_res and #spell_name_res > 0) and spell_name_res[1].name or spell_id
        
        -- -- 3. Récupération des noms pour le lien
        -- On cherche d'abord le nom traduit pour le texte du lien
        local spell_name_res = mw.ext.cargo.query("Translation", "name", { 
            where = "target_id = '" .. spell_id .. "' AND language = '" .. lang .. "' AND type = 'spell'", 
            limit = 1 
        })
        local spell_name = (spell_name_res and #spell_name_res > 0) and spell_name_res[1].name or spell_id
        
        -- On cherche ensuite le nom en anglais pour le titre de la page (l'URL)
        -- Si la langue est déjà "en", spell_name_en sera égal à spell_id
        local spell_name_en_res = mw.ext.cargo.query("Translation", "name", { 
            where = "target_id = '" .. spell_id .. "' AND language = 'en' AND type = 'spell'", 
            limit = 1 
        })
        local spell_name_en = (spell_name_en_res and #spell_name_en_res > 0) and spell_name_en_res[1].name or spell_id

        -- Construction du lien
        local spell_link = ""
        if lang == "en" then
            -- Langue anglaise : on utilise le nom anglais de la page
            spell_link = "[[" .. spell_name_en .. "|" .. spell_name .. "]]"
        else
            -- Autres langues : on utilise le nom anglais/lang
            spell_link = "[[" .. spell_name_en .. "/" .. lang .. "|" .. spell_name .. "]]"
        end
        
        -- 4. Description
        local effect = mw.ext.cargo.query("Translation", "description", { where = "target_id = '" .. art.id .. "' AND language = '" .. lang .. "' AND type = 'artifact'", limit = 1 })
        local effect_text = (effect and #effect > 0) and effect[1].description or "–"
        
        local name_and_spell = "'''" .. name .. "''' : " .. spell_link
        
        local row = '|-\n| ' .. name_and_spell .. '\n| ' .. effect_text
        table.insert(html, row)
    end
    
    if #scrolls == 0 then table.insert(html, "| colspan=2 | Aucun scroll trouvé.") end
    table.insert(html, "|}")
    return table.concat(html, "\n")
end

return p