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, Kurzbeschreibung und weiteren Angaben.

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

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

local p = {}

-- 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|?Zauberart|?Zaubertypus|?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 Vorschaubild, Titel, Beschreibung und Zusatzangaben 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.ustring.lower(mw.title.getCurrentTitle().text)
    table.insert(output, string.format('<h2>%d Zauber der Magieschule</h2>', totalCount))
    table.insert(output, '<table class="wikitable sortable">')
    table.insert(output, '<tr><th>Bild</th><th>Titel</th><th>Kurzbeschreibung</th><th>Typus</th><th>Art</th><th>Schwierigkeit</th><th>Option</th><th>Weitere Magieschule</th></tr>')

    for grad = 0, 5 do
        local spells = spellsByGrad[grad]
        if spells and #spells > 0 then
            table.insert(output, string.format('<tr><th colspan="8">Zaubergrad %d</th></tr>', grad))
            for _, spell in ipairs(spells) do
                local title = spell["Seitentitel"] or "Unbenannt"
                local description = spell["Kurzbeschreibung"] or ""
                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 = spell["Zaubertypus"] or "–"
                local art = spell["Zauberart"] or "–"
                local schwierigkeit = spell["Zauberschwierigkeit"] or "–"
                local option = spell["Zauberoption"] or "–"

                -- Schule prüfen und ggf. nur andere anzeigen
                local magieschule = spell["Magieschule"]
                local schuleAnzeigen = "–"
                if magieschule then
                    if type(magieschule) == "table" then
                        local andere = {}
                        for _, s in ipairs(magieschule) do
                            if mw.ustring.lower(s) ~= pageTitle then
                                table.insert(andere, s)
                            end
                        end
                        if #andere > 0 then
                            schuleAnzeigen = table.concat(andere, ", ")
                        end
                    else
                        if mw.ustring.lower(magieschule) ~= pageTitle then
                            schuleAnzeigen = magieschule
                        end
                    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