Module:Shared

--A collection of functions that are commonly used across many modules -- 1. When you're making a new module, add this near the top: --       local Shared = require( "Module:Shared" ) -- 2. When you need to make a call to one of these functions, just preface it with "Shared." --   So for example you could call tableCount like so: --       Shared.tableCount(data) local p = {} --================================================================== --A debug function, prints a table function p.callTablePrint(frame) local t = p.splitString("Acid Splash, Prestidigitation, Fire Bolt, Shield of Faith, Fly", ",") return p.tablePrint(t) end --========================================================== -- COMMON FUNCTIONS --========================================================== --A LUA version of the DiceCalculator template. Pass number of dice, dice type, and dice mod, and get output: "10 (1d4+6)" function p.DiceCalculator(aNum, Die, Mod) local average = 0 local newMod = 0 local Num = tonumber(aNum) if (type(Num) ~= "number") then return "Invalid Input" end if (Mod == nil) or (Mod == 0) or (Mod == "") then newMod = 0 else newMod = tonumber(Mod) end if (Die == "d2") then average = math.ceil(Num * 1.5) + newMod end if (Die == "d3") then average = math.ceil(Num * 2) + newMod end if (Die == "d4") then average = math.ceil(Num * 2.5) + newMod end if (Die == "d6") then average = math.ceil(Num * 3.5) + newMod end if (Die == "d8") then average = math.ceil(Num * 4.5) + newMod end if (Die == "d10") then average = math.ceil(Num * 5.5) + newMod end if (Die == "d12") then average = math.ceil(Num * 6.5) + newMod end if (Die == "d20") then average = math.ceil(Num * 10.5) + newMod end if (newMod > 0) then return average.." ("..Num..Die.."+"..newMod..")" end if (newMod == 0) then return average.." ("..Num..Die..")" end if (newMod < 0) then return average.." ("..Num..Die.."-"..newMod..")" end end --A shortcut function: takes a string, finds a valid damage type within, and returns that damage type as a string function p.findDmgType(String) local dmgType = "INVALID" local nString = string.lower(String) if (string.match(nString, "bludgeoning") ~= nil) then dmgType = "bludgeoning" end if (string.match(nString, "piercing") ~= nil) then dmgType = "piercing" end if (string.match(nString, "slashing") ~= nil) then dmgType = "slashing" end if (string.match(nString, "acid") ~= nil) then dmgType = "acid" end if (string.match(nString, "cold") ~= nil) then dmgType = "cold" end if (string.match(nString, "fire") ~= nil) then dmgType = "fire" end if (string.match(nString, "lightning") ~= nil) then dmgType = "lightning" end if (string.match(nString, "necrotic") ~= nil) then dmgType = "necrotic" end if (string.match(nString, "poison") ~= nil) then dmgType = "poison" end if (string.match(nString, "psychic") ~= nil) then dmgType = "psychic" end if (string.match(nString, "radiant") ~= nil) then dmgType = "radiant" end if (string.match(nString, "thunder") ~= nil) then dmgType = "thunder" end return dmgType end --Prints a table as an article table, for debugging. function p.tablePrint(t) local tablestart = "\n{| class=\"article-table\" style=\"font-size:70%; line-height:90%;\" \n|-\n!Column 1\n!Column 2\n" local body0 = "" local tableend = "\n|\}" for k,v in ipairs(t) do       local newRow = "|-\n|"..k.."\n|"..v.."\n" body0 = body0..newRow end return tablestart..body0..tableend end --Prints a table as a string, with optional line breaks between each row function p.tableString(t, Break) local Str = "" local Br = "" if(Break ~= nil) and (Break ~= "") then Br = " " end for k,v in ipairs(t) do       Str = Str..v..Br    end return Str end --Searches a list entry by entry for an item function p.contains(List, Item) if(List == nil or Item == nil) then return false end if(type(Item) ~= "string") then return false end if(type(List) == "table") then for i, listI in pairs(List) do           if(listI == Item) then return true end end else if(type(List) == "string") then local start = string.find(List, Item) if start ~= nil then return true end end return false end return false end --Returns true if ANY of the tags given are found in a string --Tags are to be inputted as a single string, with commas separating tags function p.findTags(Str, tagStr) local foundTF = nil local tags = p.splitString(tagStr, ",") for k,v in ipairs(tags) do       if (string.find(Str, v) ~= nil) then foundTF = true end end return foundTF end --Shifts a string to all lowercase function p.lowerCase(stringIn) return string.lower(stringIn) end --======================================================== --Splits a string based on a sent in separating character --For example calling splitString ("Lith V1 Relic", " ") would return {"Lith", "V1", "Relic"} function p.splitString(inputstr, sep) if sep == nil then sep = "%s" end local t={} for str in string.gmatch(inputstr, "([^"..sep.."]+)") do           table.insert(t, str) end local t2 = {} for k,v in ipairs(t) do           t2[k] = v        end return t2 end --Returns the number of rows in a table --Originally snagged this from Module:VoidByReward written by User:NoBrainz function p.tableCount(t) local count = 0 for _ in pairs(t) do count = count + 1 end return count end --Returns the first X elements in a table function p.getTableElements(t, num) local i=0 local rTable = {} for i=1, num do        table.insert(rTable, t[i]) end return rTable end --====================================================== --A sorting function - in LUA there is no sorting, but you can iterate through a table in whatever way you choose. This function is meant to ITERATE. It returns a single element of an array or table. basic usage, just sort by the keys --for k,v in spairs(HighScore) do --   print(k,v) --end > Jon    10 > Max    11 > Robin  8 this uses an custom sorting function ordering by score descending --for k,v in spairs(HighScore, function(t,a,b) return t[b] < t[a] end) do --   print(k,v) --end > Max    11 > Jon    10 > Robin  8 function p.spairs(t, order) -- collect the keys local keys = {} for k in pairs(t) do keys[#keys+1] = k end -- if order function given, sort by it by passing the table and keys a, b,   -- otherwise just sort the keys if order then table.sort(keys, function(a,b) return order(t, a, b) end) else table.sort(keys) end -- return the iterator function local i = 0 return function i = i + 1 if keys[i] then return keys[i], t[keys[i]] end end end --Currently testing version --local tkeys = {} --      populate the table that holds the keys --for k in pairs(t) do table.insert(tkeys, k) end --          sort the keys --table.sort(tkeys) --      use the keys to retrieve the values in the sorted order --for _, k in ipairs(tkeys) do print(k, t[k]) end --Sorts theTable based on the listed column function p.tableSort(theTable, sortCol, ascend) local new  function sorter(r1, r2) if(ascend) then return r1[sortCol] < r2[sortCol] else return r1[sortCol] > r2[sortCol] end end table.sort(theTable, sorter) end function p.pairsByKeys (t, f)   local a = {} for n in ipairs(t) do table.insert(a, n) end table.sort(a, f)   local i = 0      -- iterator variable local iter = function   -- iterator function i = i + 1 if a[i] == nil then return nil else return a[i], t[a[i]] end end return iter end --This is a function that returns such a sorted table. function p.sortTable(Input) local sortedOutput = {} for k,v in p.spairs(Input) do       table.insert(sortedOutput, v)    end return sortedOutput end return p