Module:Spell: Difference between revisions

From Kronopolis and the Flat Plane
No edit summary
No edit summary
 
(142 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}


function p.listDomain(frame)
local util = require('Module:Utility')
    local json = frame.args[1]
    json = firstToUpper(json)
    json = 'Module:Spell/Domain/' .. json .. '.json'
    local all_spells = mw.loadJsonData(json)


function p.list(frame)
     local out = {}
     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')


    table.insert(out, '{| class="wikitable"\n! Level !! 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


    local ordered_spells = {}
                 if spell_index > 1 then  
    for spell_lv in pairs(all_spells.spells) do
                    table.insert(out, "<br>")  
        table.insert(ordered_spells, spell_lv)
                end
    end
                 table.insert(out, "[[" .. spell .. "]]")
 
               
    table.sort(ordered_spells)
    for i = 1,#ordered_spells do
        local spell_lv = ordered_spells[i]
        local spells = all_spells.spells[spell_lv]
        table.insert(out, "| '''" .. formatLevel(spell_lv) .. "''' \n|")
        if #spells > 0 then
            for spell_index, spell in ipairs(spells) do
                 if spell_index > 1 then table.insert(out, "<br>") end
                 table.insert(out, "[[" .. spell .. "]] ")
             end
             end
            table.insert(out, '\n|-\n')
         end
         end
        table.insert(out, '\n|-')
    end
    table.insert(out, '|}')


    return table.concat(out)
        table.insert(out, '|}')
end


function firstToUpper(str)
    elseif frame.args.category ~= nil then
    return (str:gsub("^%l", string.upper))
        local payload = {
end
            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'
function formatLevel(lv)
        }
    local str
        table.insert(out, frame:expandTemplate({title='Spell/CSS'}))
    if lv == 0 then str = 'Cantrip'  
        local json = mw.ext.externalData.getExternalData(payload)
    elseif lv == 1 then str = '1st'  
        for _,spell in ipairs(json) do
    elseif lv == 2 then str = '2nd'  
            if (spell['title'] ~= nil) then
    elseif lv == 3 then str = '3rd'  
                if (spell['title'] ~= frame.args['category']) and (string.find(spell['title'],'Category:') == nil) and (string.find(spell['title'],'Spell/List') == nil) then
    else str = lv .. 'th'
                    table.insert(out, frame:preprocess('\n==' .. spell['title'] .. '==\n'))
                    table.insert(out, frame:expandTemplate({title='SmallLink', args = { spell['title'] } }))
                    table.insert(out, frame:expandTemplate({title=':' .. spell['title']}))
                    table.insert(out, frame:preprocess('\n----'))
                end
            end
        end
     end
     end
     return str
     return table.concat(out)
end
end


return p
return p

Latest revision as of 09:57, 17 January 2024

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'
        }
        table.insert(out, frame:expandTemplate({title='Spell/CSS'}))
        local json = mw.ext.externalData.getExternalData(payload)
        for _,spell in ipairs(json) do
            if (spell['title'] ~= nil) then
                if (spell['title'] ~= frame.args['category']) and (string.find(spell['title'],'Category:') == nil) and (string.find(spell['title'],'Spell/List') == nil) then
                    table.insert(out, frame:preprocess('\n==' .. spell['title'] .. '==\n'))
                    table.insert(out, frame:expandTemplate({title='SmallLink', args = { spell['title'] } }))
                    table.insert(out, frame:expandTemplate({title=':' .. spell['title']}))
                    table.insert(out, frame:preprocess('\n----'))
                end
            end
        end 
    end
    return table.concat(out)
end

return p