Module:Util

-- This module blackboxes several Wiki operations (eg. Fetching skill info) -- and is intended for use by other Lua modules. -- This module should not be called from #invoke directly.

local p = {}

local cargo = mw.ext.cargo local MAX_SKILL_EFFECTS = 8

-- -- -- Skill-related functions -- --

--- Function that gets how many variable skill effects a skill has (eg. Damage %) -- @param skill Name of the skill -- @return The number of effects the provided skill has function p.getNumberOfEffects(skill) -- Query the Cargo Tables for all the effects local tables = 'SkillLevelEffects' local queryFields = "Effect1,Effect2,Effect3,Effect4,Effect5,Effect6,Effect7,Effect8" local queryArgs = { where = 'Name="' .. skill .. '"', groupBy = 'Name' } local queryResult = cargo.query(tables, queryFields, queryArgs)

-- If there was no result returned, default return 0 if queryResult == nil then return 0 end

for key,skill in ipairs(queryResult) do   -- Go through each effect until we hit an effect that is null for i=1,MAX_SKILL_EFFECTS do     -- If the effect is nil/empty, then the previous number -- is the amount of skill effects the skill has if p.isNilOrEmpty(skill['Effect' .. i]) then return i-1 end end end

-- If the query result wasn't nil and if there were no skills -- to loop through, default return 0 return 0 end

--- Function that returns how many levels a skill has -- @param skill Name of the skill -- @return The number of levels skill has function p.getSkillMaxLevel(skill) -- Query the Cargo Tables for the max level local tables = 'SkillLevelEffectValues' local queryFields = "MAX(Level)=maxLevel" local queryArgs = { where = 'Name="' .. skill .. '"', groupBy = 'Name' } local queryResult = cargo.query(tables, queryFields, queryArgs)

-- If there was no result returned, default return 1 if queryResult == nil then return 1 end

for key,skill in ipairs(queryResult) do   return skill.maxLevel end

-- If the query result wasn't nil and if there were no skills -- to loop through, default return 1 return 1 end

--- Function that returns the skill effect description (eg. Damage %) -- @param skill Name of the skill -- @param effectNum The effect number (Skills can have multiple varying effects) -- @return The skill effect description function p.getSkillEffectDescription(skill, effectNum) -- Query the Cargo Tables for the max level local tables = 'SkillLevelEffects' local queryFields = "Effect" .. effectNum local queryArgs = { where = 'Name="' .. skill .. '"', groupBy = 'Name' } local queryResult = cargo.query(tables, queryFields, queryArgs)

-- If there was no result returned, default return empty string if queryResult == nil then return '' end

for key,skill in ipairs(queryResult) do   return skill['Effect' .. effectNum] end

-- If the query result wasn't nil and if there were no skills -- to loop through, default return empty string return '' end

--- Function that returns the skill effect description (eg. Damage %) -- @param skill Name of the skill -- @param level The skill level -- @return The skill level requirement function p.getSkillLevelRequirement(skill, level) -- Query the Cargo Tables for the max level local tables = 'SkillLevelEffectValues' local queryFields = "ClassLevelRequirement" local queryArgs = { where = 'Name="' .. skill .. '" AND Level = ' .. level, groupBy = 'Name' } local queryResult = cargo.query(tables, queryFields, queryArgs)

-- If there was no result returned, default return a dash if queryResult == nil then return '-' end

for key,skill in ipairs(queryResult) do   return skill['ClassLevelRequirement'] end

-- If the query result wasn't nil and if there were no skills -- to loop through, default return a dash return '-' end

--- Function that returns the skill effect description (eg. Damage %) -- @param skill Name of the skill -- @param level The skill level -- @param effectNum The effect number (Skills can have multiple varying effects) -- @return The skill effect value function p.getSkillEffectValue(skill, level, effectNum) -- Query the Cargo Tables for the max level local tables = 'SkillLevelEffectValues' local queryFields = "Effect" .. effectNum local queryArgs = { where = 'Name="' .. skill .. '" AND Level = ' .. level, groupBy = 'Name' } local queryResult = cargo.query(tables, queryFields, queryArgs)

-- If there was no result returned, default return a dash if queryResult == nil then return '-' end

for key,skill in ipairs(queryResult) do   return skill['Effect' .. effectNum] end

-- If the query result wasn't nil and if there were no skills -- to loop through, default return a dash return '-' end

-- -- -- Misc. Wiki-related functions -- --

--- Add a tooltip with content tooltipText to targetText -- @param targetText The visible text -- @param tooltipText The text that will appear when hovering the visible text -- @return Text with tooltip function p.createTooltipWithText(targetText, tooltipText) local div = mw.html.create('div') :addClass('tooltip') :css('border-bottom', '0')

local contentDiv = mw.html.create('div') :wikitext(targetText)

local tooltip = mw.html.create('span') :addClass('tooltiptext') :wikitext(tooltipText)

div:node(tooltip) div:node(contentDiv)

return div end

--- Add a tooltip with content tooltipText to targetElement -- @param targetElement The visible element (eg. images) -- @param tooltipText The text that will appear when hovering the visible text -- @return Element with tooltip function p.createTooltipWithElement(targetElement, tooltipText) local div = mw.html.create('div') :addClass('tooltip') :css('border-bottom', '0')

local tooltip = mw.html.create('span') :addClass('tooltiptext') :wikitext(tooltipText)

div:node(tooltip) div:node(targetElement)

return div end

-- -- -- Misc. General functions -- --

--- Checks if the specified value is nil or empty -- @param val -- @return True/false depending on whether the value provided is empty or not function p.isNilOrEmpty(val) return (val == nil or val == '') end

--- Returns '-' if the specified value is nil or empty -- @param val -- @return Return '-' or the value function p.returnDefaultIfEmpty(val) if p.isNilOrEmpty(val) then return '-' end return val end

--- Removes whitespaces from the string specified -- Copied from http://lua-users.org/wiki/StringTrim -- @param The trimmed string function trim(s) return (s:gsub("^%s*(.-)%s*$", "%1")) end

--- Splits a string based on the provided separator -- Copied from http://lua-users.org/wiki/SplitJoin -- strsplit(seperator, string) -- @param d The character we want to separate on -- @param p The string we want to split -- @return Table of the separated values function p.strsplit(d,p) if p == nil then return '' end local t, ll  t={} ll=0 if(#p == 1) then return {p} end while true do     l = string.find(p, d, ll, true) -- find the next d in the string if l ~= nil then -- if "not not" found then.. table.insert(t, string.sub(p,ll,l-1)) -- Save it in our array. ll = l + 1 -- save just after where we found it for searching next time. else table.insert(t, string.sub(p,ll)) -- Save what's left in our array. break -- Break at end, as it should be, according to the lua manual. end end return t end

--- Pretty print a timestamp in Month day, Year format. -- @param timestamp The time we want to format -- @return The provided timestamp in Month Day Year format function returnFormattedDate(timestamp) return os.date('%B %d, %Y', tonumber(timestamp)) end

--- Gets the day, month, and year of current date -- @return The current date in Day Month Year format function getCurrentDateDMY return os.date('%d %B %Y') end

--- Function that converts months from text to numbers -- @param name The name of the month -- @return The numerical value of the month provided function p.monthNum(name) local monthNum = { ["January"] = 1, ["February"] = 2, ["March"] = 3, ["April"] = 4, ["May"] = 5, ["June"] = 6, ["July"] = 7, ["August"] = 8, ["September"] = 9, ["October"] = 10, ["November"] = 11, ["December"] = 12 } if monthNum[name] ~= nil then return monthNum[name] end return name end

--- Returns the length of a specified table -- @param T Table we want to get the length for -- @return Length of the specified table function p.tablelength(T) local count = 0 for _ in pairs(T) do count = count + 1 end return count end

--- Because key,value pairs are stored arbitrarily in the tables, this --- function sorts the keys and returns the key,value pairs in sorted order. -- @param unsortedTable The unsorted table -- @return Sorted version of the provided table function p.sortedPairs(unsortedTable) -- Get all the keys from the table local keys = {} if (unsortedTable ~= nil) then for key in pairs(unsortedTable) do           keys[#keys+1] = key end end

-- Sort'em! table.sort(keys)

local i = 0 return function i = i + 1 if keys[i] then return keys[i], unsortedTable[keys[i]] end end end

--- Returns the keys of a table, sorted by the values they're associated with, --- given a tbl and a sortFunction e.g. "function(a, b) return a < b end" -- @param tbl The table we want to get the keys from -- @param sortFunction Function that determines how we want to sort the keys -- @return Table of keys in sorted order function p.getKeysSortedByValue(tbl, sortFunction) local keys = {} for key in pairs(tbl) do   table.insert(keys, key) end

table.sort(keys, function(a, b)   return sortFunction(tbl[a], tbl[b])  end)

return keys end

return p