Module:Skills

local p = {}

-- When createtable is invoked you can use two methods to load data. Player name or direct arguments. -- To supply the player name, use the special argument loadName= -- Direct arguments are in the form = -- or in the form = c -- Direct arguments override data loaded from the player

local utils = require('Module:Utils')

function p.skillValid(skillName, level, crits) if(level < 0) then error('level can not be negative [skillName=' .. skillName .. ', level=' .. tostring(level) .. ']', 2) end if(crits ~= nil and crits < 0) then error('crits can not be negative [skillName=' .. skillName .. ', crits=' .. tostring(crits) .. ']', 2) end local name = string.lower(skillName) if(string.match(name,'^weapon skill') and name ~= 'weapon skill') then return true elseif((string.match(name,'^defense skill') or string.match(name,'^defence skill')) and name ~= 'defense skill' and name ~= 'defence skill') then return true elseif(string.match(name,'^vehicle skill') and name ~= 'vehicle skill') then return true end -- At this point we are certain we have not encountered any of the skills that have a subtype and thus support crits -- So we check whether or not we have been supplied with crits if (crits~=nil) then error('crits should not be supplied for that skill [skillName=' .. skillName .. ', crits=' .. tostring(crits) .. ']', 2) end if(name == 'unspent') then return true elseif(name == 'medical') then return true elseif (name == 'translate') then return true elseif (name == 'acrobatics') then return true elseif (name == 'caving') then return true elseif (name == 'persuasion') then return true elseif (name == 'machine control') then return true elseif (name == 'explosives') then return true elseif (name == 'trickery') then return true elseif (name == 'stealth') then return true elseif (name == 'survival') then return true elseif (name == 'animal handling') then return true elseif (name == 'geology') then return true elseif (name == 'archeology') then return true elseif (name == 'electronics') then return true elseif (name == 'mechanical') then return true elseif (name == 'biological') then return true elseif (name == 'chemical') then return true elseif (name == 'occult') then return true elseif (name == 'weaponsmith') then return true elseif (name == 'armorsmith') then return true elseif (name == 'steal') then return true elseif (name == 'navigation') then return true elseif (name == 'diving') then return true end error('unknown skill [skillName=' .. skillName .. ', name=' .. name .. ']', 2) end function p.addRow(htmltable, skillName, inLevels, adjustment) local criticalString = string.match(inLevels, 'c(%d)$') local pointString = inLevels local criticalColString = '-' local crits local levels local critsToLevel if(criticalString ~= nil) then pointString = string.match(inLevels,'^(%d)c') levels = tonumber(pointString) critsToLevel = levels+1 criticalColString = criticalString .. '/' .. tostring(critsToLevel) crits = tonumber(criticalString) else levels = tonumber(pointString) end local adjString = '' local adjColour = 'transparent' if(adjustment < 0) then adjString = ' - ' .. tostring(-adjustment) adjColour = 'violet' elseif(adjustment > 0) then adjString = ' + ' .. tostring(adjustment) adjColour = 'limegreen' end --throw error for invalid string p.skillValid(skillName, levels, crits) htmltable :tag('tr') :css('color', 'white') :css('background', '#313e55') :tag('th') :css('text-align', 'right') --:css('font-size', '110%') :css('border-width', '3px') :css('border-color', 'rgb(204, 153, 102)') :wikitext(skillName) :done :tag('td') :css('text-align', 'left') :css('border-width', '3px') :css('border-color', 'rgb(204, 153, 102)') :wikitext(pointString) :tag('span') :css('color', adjColour) :wikitext(adjString) :done :done :tag('td') :css('text-align', 'left') :css('border-width', '3px') :css('border-color', 'rgb(204, 153, 102)') :wikitext(criticalColString) end function p.createtableInternal(args) -- Create an empty string that will be returned at the end of the function -- New things will be added to the string as we do them local retString = "" -- Create an html node that will represent the table local htmltable = mw.html.create('table') -- Change that node's css properties so that it has the proper style htmltable :css('width','100%') :css('font-size', '14px') :css('float', 'right') :css('margin', '0 0 0em 0em') :css('border-style', 'solid') :css('border-width', '3px') :css('border-color', 'rgb(204, 153, 102)') :css('background', 'rgb(204, 153, 102)') :css('padding', '1px 1px') :css('color', 'white') :css('text-align', 'left') :attr('cellpadding', '1') :attr('cellspacing', '1') -- Add table header texts htmltable:tag('tr') :css('background', '#313e55') :tag('th') :attr('colspan', 3) :css('text-align', 'center') :css('font-size','15px') :wikitext('Skills') htmltable:tag('tr') :css('background', '#313e55') :tag('th') :css('text-align', 'center') :css('font-size','15px') :wikitext('Name') :done :tag('th') :css('text-align', 'center') :css('font-size','15px') :wikitext('Points') :done :tag('th') :css('text-align', 'center') :css('font-size','15px') :wikitext('Crits') local unspentName = nil; local unspentValue = nil; -- For each skill, add it   for name, value in utils.pairsByKeys(args) do        if(string.lower(name) ~= 'unspent') then p.addRow(htmltable, name, value, 0) else unspentName = name; unspentValue = value; end end if (unspentName ~= nil) then p.addRow(htmltable, unspentName, unspentValue, 0) end -- Add the table to the retString retString = retString .. tostring(htmltable) -- Return the retString, after everything has been processed return retString end

function p.loadArgs(name, args) if(args==nil) then args={} end if(name == nil) then return args end local player = require('Module:Players').getPlayer(name) if(player == nil) then return args end for index, skill in pairs(player.skills) do		local argName = skill.skillType if(skill.subType ~= nil) then argName = argName .. ' (' .. skill.subType .. ')' end local argValue = tostring(skill.level) if(skill.crits ~= nil) then argValue = argValue .. 'c' .. tostring(skill.crits) end args[argName]=argValue end return args end

function p.copyArgs(frame, args) if(args==nil) then args={} end for argName, argVal in pairs(frame.args) do		if(argName ~= 'loadName') then --ignore the player name we used to load the args args[argName]=argVal end end return args end

--You can use the next line to debug using the debug console -- =require('Module:Utils').tdump(p.createtableInternal(p.loadArgs('Kara', nil)))

function p.createtable(frame) local args = {} --Load args from DB	args = p.loadArgs(frame.args.loadName, args) --Override DB args from frame args args = p.copyArgs(frame, args) --Remember to preprocess everything before returning the string return frame:preprocess(p.createtableInternal(args)) end return p