Module:Spell: Difference between revisions

From Kronopolis and the Flat Plane
No edit summary
No edit summary
Line 2: Line 2:


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


     local out = {}
     local out = {}
Line 12: Line 12:
     table.insert(out, '{| class="wikitable"\n! Level !! Spells\n|-\n')
     table.insert(out, '{| class="wikitable"\n! Level !! Spells\n|-\n')


    local ordered_spells = {}
     for spell_level = 0,9 do
     for spell_level, spells in ipairs(all_spells) do
        table.insert(ordered_spells, spell_level)
    end
    table.sort(ordered_spells)
 
    for _, spell_level in ipairs(ordered_spells) do
         table.insert(out, "| '''" .. formatLevel(spell_level) .. "''' \n|")
         table.insert(out, "| '''" .. formatLevel(spell_level) .. "''' \n|")
        if spells[spell_level] ~= nil then
            if #spells[spell_level] > 0 then
                for index, spell in ipairs(spells[spell_level]) do


        if #all_spells[spell_level] > 0 then
                    if index > 1 then  
            for index, spell in ipairs(all_spells[spell_level]) do
                        table.insert(out, "<br>")  
 
                    end
                if index > 1 then
                     table.insert(out, "-" .. spell .. " ")
                     table.insert(out, "<br>")  
                   
                 end
                 end
                table.insert(out, "-" .. spell .. " ")
               
             end
             end
         end
         end
         table.insert(out, '\n|-')
         table.insert(out, '\n|-')
     end
     end

Revision as of 13:05, 2 March 2023

Usage

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

local p = {}

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

    local out = {}

    table.insert(out, '{| class="wikitable"\n! Level !! Spells\n|-\n')

    for spell_level = 0,9 do
        table.insert(out, "| '''" .. formatLevel(spell_level) .. "''' \n|")
        if spells[spell_level] ~= nil then
            if #spells[spell_level] > 0 then
                for index, spell in ipairs(spells[spell_level]) do

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

    return table.concat(out)
end

function firstToUpper(str)
    return (str:gsub("^%l", string.upper))
end

function formatLevel(lv)
    local str
    if lv == 0 then str = 'Cantrip' 
    elseif lv == 1 then str = '1st' 
    elseif lv == 2 then str = '2nd' 
    elseif lv == 3 then str = '3rd' 
    else str = lv .. 'th'
    end
    return str
end

return p