Module:Spells

-- Purposes of this module: -- Functions for spell lists. -- Generate a spell list for all spells in a certain class -- Make a spell list for a single character --     (Option for just spell names by level, and for reference data version) local p = {} local Shared = require( "Module:Shared" ) local SpellData = mw.loadData( 'Module:Data/spells' ) --================================================================== --A parent function; meant to be used in CSS. Returns all spells. function p.SpellcasterStats(frame) return p.displaySpellcasterStats(SpellData) end --A parent function; meant to be used in CSS. User asks for a spell list when calling this function, and the function provides, sorts, and displays it. function p.spellCaller(frame) local classStr = frame.args[1] or frame.args["name"] local List = p.getClassSpells(classStr) return p.displaySpellList(List) end --A parent function; meant to be used in CSS. User asks for many spells, separated by commas function p.callTheseSpells(frame) local Str = frame.args[1] or frame.args["name"] local List = p.getTheseSpells(Str) return p.displaySpellList(List) end --A parent function; meant to be used in CSS. Returns all spells. function p.callAllSpells(frame) return p.displaySpellList(SpellData) end --========================================================== -- SPELL DISPLAYER --========================================================== --Displays a spell list as a sorted table, categorized by spell level. Displaying School is optional. function p.displaySpellList(List, School) --if(School == "n") then tablestart = "\n{| class=\"article-table\" style=\"font-size:70%; line-height:80%;\" \n|-\n!\n!Name\n!Save\n!Time\n!Range\n!Comp.\n!Duration\n!Description\n" --else tablestart = "\n{| class=\"article-table\" style=\"font-size:70%; line-height:80%;\" \n|-\n!\n!Name\n!Save\n!Time\n!Range\n!Comp.\n!School\n!Duration\n!Description\n" end local tablestart = "\n{| class=\"article-table\" style=\"font-size:70%; line-height:90%;\" \n|-\n!\n!Name\n!Save\n!Time\n!Range\n!Comp.\n!Duration\n!Description\n" body0 = "" body1 = "" body2 = "" body3 = "" body4 = "" body5 = "" body6 = "" body7 = "" body8 = "" body9 = "" tableend = "\n|\}" --table.sort(List) --table.sort(temp, function(a, b) return a[2] > b[2] end) local spellK = {} for i=1,500 do       for j=1, 500 do            if (List[j] ~= nil and List[j].Index == i) then spellK = List[j] --for i, spellK in ipairs(List) do       --for spellK, v in Shared.spairs(List) do            --local newRow = "" --if(School == "n") then newRow = "|-\n|\n|["..spellK.Link.." "..spellK.Name.."]\n|"..spellK.Save.."\n|"..spellK.Time.."\n|"..spellK.Range.."\n|"..spellK.Components.."\n|"..spellK.Duration.."\n|"..spellK.DescriptionBrief.."\n" --else newRow = "|-\n|\n|["..spellK.Link.." "..spellK.Name.."]\n|"..spellK.Save.."\n|"..spellK.Time.."\n|"..spellK.Range.."\n|"..spellK.Components.."\n|"..spellK.School.."\n|"..spellK.Duration.."\n|"..spellK.DescriptionBrief.."\n" end local newRow = "|-\n|\n|["..spellK.Link.." "..spellK.Name.."]\n|"..spellK.Save.."\n|"..spellK.Time.."\n|"..spellK.Range.."\n|"..spellK.Components.."\n|"..spellK.Duration.."\n|"..spellK.DescriptionBrief.."\n" if (spellK.Level == 0) then body0 = body0..newRow elseif (spellK.Level == 1) then body1 = body1..newRow elseif (spellK.Level == 2) then body2 = body2..newRow elseif (spellK.Level == 3) then body3 = body3..newRow elseif (spellK.Level == 4) then body4 = body4..newRow elseif (spellK.Level == 5) then body5 = body5..newRow elseif (spellK.Level == 6) then body6 = body6..newRow elseif (spellK.Level == 7) then body7 = body7..newRow elseif (spellK.Level == 8) then body8 = body8..newRow elseif (spellK.Level == 9) then body9 = body9..newRow else body0 = body0 end end end end if(body0 ~= "") then body0 = "|-\n! colspan=\"8\" |Cantrips\n"..body0 end if(body1 ~= "") then body1 = "|-\n! colspan=\"8\" |1st Level\n"..body1 end if(body2 ~= "") then body2 = "|-\n! colspan=\"8\" |2nd Level\n"..body2 end if(body3 ~= "") then body3 = "|-\n! colspan=\"8\" |3rd Level\n"..body3 end if(body4 ~= "") then body4 = "|-\n! colspan=\"8\" |4th Level\n"..body4 end if(body5 ~= "") then body5 = "|-\n! colspan=\"8\" |5th Level\n"..body5 end if(body6 ~= "") then body6 = "|-\n! colspan=\"8\" |6th Level\n"..body6 end if(body7 ~= "") then body7 = "|-\n! colspan=\"8\" |7th Level\n"..body7 end if(body8 ~= "") then body8 = "|-\n! colspan=\"8\" |8th Level\n"..body8 end if(body9 ~= "") then body9 = "|-\n! colspan=\"8\" |9th Level\n"..body9 end return tablestart..body0..body1..body2..body3..body4..body5..body6..body7..body8..body9..tableend end --Returns all spells function p.getAllSpells local sortedList = {} for i, listI in pairs(SpellData) do       table.insert(sortedList, listI) end return sortedList end --Returns all spells from a single class's spell list function p.getClassSpells(Class) local spellList = {} if(Class == nil) then return nil end for i, spellK in pairs(SpellData) do       --if (Shared.contains(spellK.Classes, Class) ~= false) then if (string.find(spellK.Class, Class) ~= nil) then table.insert(spellList, spellK) end end return spellList end --Returns all spells of a certain level from a list function p.getSpellsOfLevel(SpellList, findLevel) local sortedList = {} if(type(SpellList) == "table") then for i, listI in pairs(SpellList) do           if(listI.Level == findLevel) then table.insert(sortedList, listI) end end else return nil end return sortedList end --Returns a table of the spells given in a string function p.getTheseSpells(SpellString) local stringList = {} local sortStringList = {} local sortedList = {} if(SpellString ~= nil) then stringList = Shared.splitString(SpellString, ",") else return nil end for i=1, 500 do       if(SpellData[i] ~= nil) then for j,v in ipairs(stringList) do               local comparison = ""           --If the first character is a space, remove it                if(string.sub(stringList[j],1,1) == " ") then comparison = string.sub(stringList[j],2) else comparison = stringList[j] end if(string.lower(SpellData[i].Name) == string.lower(comparison)) then table.insert (sortedList, SpellData[i]) end end end end return sortedList end --========================================================== --UNFINISHED  Displays spellcaster's Stats. function p.displaySpellcasterStats(Level, Progression, DC, Attack) local highestSlot = 0 local tablestart = "\n\{| class=\"article-table\" \n! rowspan=\"2\" |Caster Level \n! rowspan=\"2\" style=\"text-align:center\" |Ability \n! rowspan=\"2\" |Save DC \n! rowspan=\"2\" style=\"text-align:center\" |Attack Bonus\n " if(highestSlot > 0) then tablestart = tablestart.."! style=\"text-align:center\" colspan="..highestSlot.."|Spell Slots\n |-\n" if(Progression == "full") then tablestart = tablestart.."! 1st \n!2nd \n!3rd \n!4th \n!5th \n!6th \n!7th \n!8th \n!9th \n" elseif(Progression == "half") then tablestart = tablestart.."! 1st \n!2nd \n!3rd \n!4th \n!5th \n" elseif(Progression == "quarter") then tablestart = tablestart.."! 1st \n!2nd \n!3rd \n!4th \n" else end end body0 = "|-\n| style=\"text-align:center\" |"..Level.."\n| style=\"text-align:center\" |"..Ability.."\n| style=\"text-align:center\" |"..DC.."\n| style=\"text-align:center\" |"..Attack.."" tableend = "\n|\}" return tablestart..body0..body1..body2..body3..body4..body5..body6..body7..body8..body9..tableend end --========================================================== return p