Module:Spell: Difference between revisions

From Kronopolis and the Flat Plane
No edit summary
No edit summary
Line 34: Line 34:
         for k,v in ipairs(json) do
         for k,v in ipairs(json) do
             table.insert(out, '(k:' .. k .. ')')
             table.insert(out, '(k:' .. k .. ')')
             if type(v) == 'string' or type(v) == 'number' then
             if v['title'] ~= nil then
                table.insert(out, 'v:' .. v)
                if type(v['title']) == 'string' or type(v['title']) == 'number' then
                    table.insert(out, 'v:' .. v['title'])
                end
             end
             end
             table.insert(out, '<br>')
             table.insert(out, '<br>')

Revision as of 15:30, 3 March 2023

Usage

  • {{#invoke:spell|list|domain=}}
  • {{#invoke:spell|list|category=}}

local p = {}

local util = require('Module:Utility')

function p.list(frame)
    local out = {}
    if frame.args.domain ~= nil then
        local json = 'Module:Spell/Domain/' .. util.firstToUpper(frame.args['domain']) .. '.json'
        local spells = mw.loadJsonData(json)
        
        table.insert(out, '{| class="wikitable"\n! style="text-align:center" | Level !! style="text-align:center" | Spells\n|-\n')

        for spell_level,_ in ipairs(spells) do
            table.insert(out, "| '''" .. util.formatLevel(spell_level-1) .. "''' \n|")
            for spell_index, spell in ipairs(spells[spell_level]) do

                if spell_index > 1 then 
                    table.insert(out, "<br>") 
                end
                table.insert(out, "[[" .. spell .. "]]")
                
            end
            table.insert(out, '\n|-\n')
        end

        table.insert(out, '|}')

    elseif frame.args.category ~= nil then
        local payload = {
            source=mw.site.server .. mw.site.scriptPath .. '/api.php?action=query&format=json&generator=categorymembers&gcmlimit=max&gcmtitle=Category:' .. util.firstToUpper(frame.args.category),
            format='json'
        }
        local json = mw.ext.externalData.getExternalData(payload)
        for k,v in ipairs(json) do
            table.insert(out, '(k:' .. k .. ')')
            if v['title'] ~= nil then
                if type(v['title']) == 'string' or type(v['title']) == 'number' then
                    table.insert(out, 'v:' .. v['title'])
                end
            end
            table.insert(out, '<br>')
        end 
    end
    return table.concat(out)
end

return p