Module:Spell: Difference between revisions

From Kronopolis and the Flat Plane
No edit summary
No edit summary
Line 5: Line 5:
     json = firstToUpper(json)
     json = firstToUpper(json)
     json = 'Module:Spell/Domain/' .. json .. '.json'
     json = 'Module:Spell/Domain/' .. json .. '.json'
     local spells = mw.loadJsonData(json)
     local all_spells = mw.loadJsonData(json)


     local out = {}
     local out = {}
Line 12: Line 12:
      
      
     local ordered_keys = {}
     local ordered_keys = {}
     for k in pairs(spells) do
     for k in pairs(all_spells) do
         table.insert(ordered_keys, k)
         table.insert(ordered_keys, k)
     end
     end
     table.sort(ordered_keys)
     table.sort(ordered_keys)
     for i = 0,#ordered_keys do
     for i = 0,#ordered_keys do
         local k = ordered_keys[i]
         local spell_lv = ordered_keys[i]
         local v = spells[k]
         local spells = all_spells[spell_lv]
         table.insert(out, "\n| '''" .. getLevelString(k) .. "'''\n|-")
         table.insert(out, "\n| '''" .. formatLevel(spell_lv) .. "'''\n|-")
         for _, vs in ipairs(v) do
         for _, vs in ipairs(v) do
             table.insert(out, "|| [[" .. vs .. "]] ")
             table.insert(out, "|| [[" .. vs .. "]] ")
Line 33: Line 33:
end
end


function getLevelString(lv)
function formatLevel(lv)
     local str
     local str
     if lv == 0 then str = 'Cantrip'  
     if lv == 0 then str = 'Cantrip'  

Revision as of 14:31, 1 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 all_spells = mw.loadJsonData(json)

    local out = {}

    table.insert(out, '{| class="wikitable"\n! Level !! Spells\n|-')
    
    local ordered_keys = {}
    for k in pairs(all_spells) do
        table.insert(ordered_keys, k)
    end
    table.sort(ordered_keys)
    for i = 0,#ordered_keys do
        local spell_lv = ordered_keys[i]
        local spells = all_spells[spell_lv]
        table.insert(out, "\n| '''" .. formatLevel(spell_lv) .. "'''\n|-")
        for _, vs in ipairs(v) do
            table.insert(out, "|| [[" .. vs .. "]] ")
        end
    end
    table.insert(out, '\n|}')

	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