Module:Sandbox/Gonnym/sometest8

local libraryUtil = require('libraryUtil')

local p = {}

-- Award class. -- local function Award(o) local Award = o or {name = "", numOfWins = 0, numOfNominations = 0} local checkSelf = libraryUtil.makeCheckSelfFunction( 'Sandbox/Gonnym/sometest8', 'Award', Award, 'Sandbox/Gonnym/sometest8 object' )

function Award:setWins(value) if (value) then Award.numOfWins = value else Award.numOfWins = 0 end end function Award:setNominations(value) if (value) then Award.numOfNominations = value else Award.numOfNominations = 0 end end return Award end

local function isValidParameter(param, pattern) if (mw.ustring.sub(param, -mw.ustring.len(pattern), -1) == pattern) then return true else return false end end

-- Local function which is used to compare entries in a list. -- local function compare(a, b)	return a[1] < b[1] end

local function getOrderedArgs(args) local orderedArgs = {} local i = 1 for k, v in pairs(args) do		orderedArgs[i] = {[1] = k, [2] = v}		i = i + 1 end

table.sort(orderedArgs, compare) return orderedArgs end

local function getAwardsList(args) local orderedArgs = getOrderedArgs(args) local list = {} local data = mw.loadData('Module:Sandbox/Gonnym/sometest8/awards')

for i, v in ipairs(orderedArgs) do		local currentArg = orderedArgs[i] local paramName = currentArg[1] local value = currentArg[2] mw.log("Param name: " .. paramName)

local awardName = data.getAwardNameFromParameter[paramName:sub(1, -2)] -- Check if parameter is on the award list. if (awardName) then local award = Award -- Check if award was already added to list. if (list[awardName]) then -- Award was present in list; Use previous entry. award = list[awardName] else -- New award in list; Create new entry. award.name = awardName end -- Check if the award is for Nominations or Wins. if (isValidParameter(paramName, "N")) then award.numOfNominations = value list[awardName] = award elseif (isValidParameter(paramName, "W")) then award.numOfWins = value list[awardName] = award else -- Not one of the above; Might be a malformed parameter name. end

-- The parameter is not on the award list; Check if it is a custom "award" parameter. elseif (paramName:sub(1, -2) == "award") then local award = Award award.name = value mw.log(paramName .. "N") award:setNominations(args[paramName .. "N"]) award:setWins(args[paramName .. "W"]) mw.log(award.numOfNominations) mw.log(award.numOfWins) list[value] = award else -- Do nothing. end

end

return list end

function p._orderAwards(args) local list = getAwardsList(args)

--	for i, v in ipairs(list) do --		currentAward = list[i] --		args["label" .. i] = currentAward.name --		args["data" .. i .. "a"] = currentAward.numOfWins --		args["data" .. i .. "b"] = currentAward.numOfNominations --	end

return 0 end

local function sumValues(args) local sum = 0

for i, v in ipairs(args) do		if (args[i]) then sum = sum + args[i] end end return sum end

local function getValues(args, pattern) local list = {} for name, value in pairs(args) do		if (isValidParameter(name, pattern)) then table.insert(list, value) end end return list end

function p._nominations(args) local nominations = getValues(args, "N") return sumValues(nominations) end

function p._wins(args) local wins = getValues(args, "W") return sumValues(wins) end

local function makeInvokeFunc(funcName) return function (frame) local getArgs = require('Module:Arguments').getArgs local args = getArgs(frame) return p[funcName](args) end end

p.orderAwards = makeInvokeFunc('_orderAwards')

p.nominations = makeInvokeFunc('_nominations')

p.wins = makeInvokeFunc('_wins')

return p

-- Comment: local getArgs = require('Module:Arguments').getArgs - works for both direct module calls and inside templates. local args2 = mw.getCurrentFrame:getParent.args - works only inside templates. --