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 und Kurzbeschreibung.

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

Verwendung:
{{#invoke:Modulname|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|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 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 %s</h2>', totalCount, pageTitle))
    table.insert(output, '<table class="wikitable">')
    for grad = 0, 5 do
        local spells = spellsByGrad[grad]
        if spells and #spells > 0 then
            table.insert(output, string.format('<tr><th colspan="3">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 = image ~= "" and string.format('[[Datei:%s|50px]]', image) or ""
                table.insert(output, string.format('<tr><td>%s</td><td>[[%s]]</td><td>%s</td></tr>', imageTag, title, description))
            end
        end
    end
    table.insert(output, '</table>')
    return table.concat(output, '\n')
end

return p