Module:Spell: Difference between revisions

From Kronopolis and the Flat Plane
No edit summary
No edit summary
Line 4: Line 4:
     local json = 'Module:Spell/Domain/' .. firstToUpper(frame.args[1]) .. '.json'
     local json = 'Module:Spell/Domain/' .. firstToUpper(frame.args[1]) .. '.json'
     local spells = mw.loadJsonData(json)
     local spells = mw.loadJsonData(json)
 
   
    local out = {}
   
     for _,spell in ipairs(spells) do
     for _,spell in ipairs(spells) do
         for __,spell2 in ipairs(spell) do
         for __,spell2 in ipairs(spell) do
Line 11: Line 13:
     end
     end


    local out = {}


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

Revision as of 13:34, 2 March 2023

Usage

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

local p = {}

function p.listDomain(frame)
    local json = 'Module:Spell/Domain/' .. firstToUpper(frame.args[1]) .. '.json'
    local spells = mw.loadJsonData(json)
    
    local out = {}
    
    for _,spell in ipairs(spells) do
        for __,spell2 in ipairs(spell) do
            table.insert(out, __ .. " - " .. spell2)
        end
    end


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

    for spell_level = 0,9 do
        table.insert(out, "| '''" .. formatLevel(spell_level) .. "''' #:" .. #spells .. "\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|-\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