Modul:ZauberNachGrad

Aus Splitterwiki
Zur Navigation springen Zur Suche springen

Die Dokumentation für dieses Modul kann unter Modul:ZauberNachGrad/Doku erstellt werden

--[[
Erstellt eine tabellarische Übersicht aller Zauber einer Magieschule,
gruppiert nach Zaubergrad mit Vorschaubild, Titel, Beschreibung und weiteren Eigenschaften.

getSpellList – holt die Daten.
renderSpellList – rendert die HTML-Ausgabe als Tabelle.

Verwendung:
{{#invoke:ZauberNachGrad|renderSpellList|Schule=Kampfmagie}}
]]--

local p = {}

-- Hilfsfunktion: Wandelt nil oder table in String um
local function toString(value)
    if type(value) == "table" then
        return table.concat(value, ", ")
    elseif value == nil or value == "" then
        return "–"
    else
        return value
    end
end

-- Holt alle Zauber einer Magieschule, gruppiert nach Grad
function p.getSpellList(frame)
    local schule = frame.args['Schule']
    if not schule then return nil end

    local spellData = {}

    for grad = 0, 5 do
        local query = string.format(
            '[[Category::Zauber]][[%s::%d]]|?Seitentitel|?Kurzbeschreibung|?Profilbild|?Zaubertypus|?Zauberart|?Zauberschwierigkeit|?Zauberoption|?Magieschule|mainlabel=-|limit=199',
            schule, grad
        )
        local result = mw.smw.ask(query)

        if result then
            spellData[grad] = result
        end
    end

    return spellData
end

-- Gibt die Tabellenansicht mit allen Spalten aus
function p.renderSpellList(frame)
    local spellsByGrad = p.getSpellList(frame)
    if not spellsByGrad then
        return "Keine Zauber gefunden."
    end

    local output = {}
    local totalCount = 0
    for grad = 0, 5 do
        local spells = spellsByGrad[grad]
        if spells then
            totalCount = totalCount + #spells
        end
    end

    local pageTitle = mw.title.getCurrentTitle().text
    table.insert(output, string.format('<h2>%d Zauber der weiteren Magieschule</h2>', totalCount))
    table.insert(output, '<table class="wikitable sortable">')
    table.insert(output, '<tr><th>Bild</th><th>Titel</th><th>Beschreibung</th><th>Typus</th><th>Art</th><th>Schwierigkeit</th><th>Option</th><th>Schule</th></tr>')

    for grad = 0, 5 do
        local spells = spellsByGrad[grad]
        if spells and #spells > 0 then
            table.insert(output, string.format('<tr><td colspan="8" style="background-color:#eee;"><b>Zaubergrad %d</b></td></tr>', grad))
            for _, spell in ipairs(spells) do
                local title = toString(spell["Seitentitel"])
                local description = toString(spell["Kurzbeschreibung"])
                local image = spell["Profilbild"]
                if not image or image == "" then
                    image = "Icon_Vorlage_Platzhalter_dunkel_64.png"
                end
                local imageTag = string.format('[[Datei:%s|50px]]', image)

                local typus = toString(spell["Zaubertypus"])
                local art = toString(spell["Zauberart"])
                local schwierigkeit = toString(spell["Zauberschwierigkeit"])
                local option = toString(spell["Zauberoption"])

                -- Schule nur anzeigen, wenn sie NICHT der aktuellen Seitentitel entspricht
                local magieschule = spell["Magieschule"]
                local schuleAnzeigen = "–"
                if magieschule then
                    if type(magieschule) == "table" then
                        local andere = {}
                        for _, s in ipairs(magieschule) do
                            if s ~= pageTitle then table.insert(andere, s) end
                        end
                        schuleAnzeigen = (#andere > 0) and table.concat(andere, ", ") or "–"
                    else
                        schuleAnzeigen = (magieschule ~= pageTitle) and magieschule or "–"
                    end
                end

                table.insert(output, string.format(
                    '<tr><td>%s</td><td>[[%s]]</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>',
                    imageTag, title, description, typus, art, schwierigkeit, option, schuleAnzeigen
                ))
            end
        end
    end

    table.insert(output, '</table>')
    return table.concat(output, '\n')
end

return p