Jump to content

Module:SongContestData

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by TheThomanski (talk | contribs) at 22:26, 17 March 2025. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local getArgs = require('Module:Arguments').getArgs
local p = {}

local function makeInvokeFunc(funcName)
	return function (frame)
		local args = getArgs(frame)
		return p[funcName](args)
	end
end

local function processOutput(o)
	if type(o) == "table" then
	    local result = {}
	    for _, v in pairs(o) do
	        table.insert(result, tostring(v))
	    end
	    return table.concat(result, ";")
	end
	return o
end

local function getData(contest, year)
	return mw.loadData('Module:SongContestData/'..contest..'/'..year)
end

local function sortEntries(data, att, desc, excludeAtt, excludeVal, excludeInvert)
	-- filter entries that do not have att, filter entries that have exclude
	local filtered_data = {}
	for k, v in pairs(data) do
	    if v[att] and not (
	        excludeAtt and v[excludeAtt] and excludeVal and
	        ((not excludeInvert and v[excludeAtt] == excludeVal) or  -- normal exclusion
	        (excludeInvert and v[excludeAtt] ~= excludeVal))         -- inverted exclusion
	    ) then
	        filtered_data[k] = v
	    end
	end

    -- sort the keys based on the corresponding attribute values
    local keys = {}
    for k in pairs(filtered_data) do
        table.insert(keys, k)  -- store original keys for sorting
    end

    table.sort(keys, function(a, b)
        local aVal, bVal = filtered_data[a][att], filtered_data[b][att]
        if desc then
        	return aVal > bVal
        else
        	return aVal < bVal
        end
    end)
    
    return keys
end

p.main = makeInvokeFunc('_main')
function p._main(args)
	local data = getData(args[1], args[2])
	local entryData = data[args[3]]
	
	if entryData and entryData[args[4]] then
        return processOutput(entryData[args[4]])
    end

    return ""
end

p.entryAmount = makeInvokeFunc('_entryAmount')
function p._entryAmount(args)
	local data = getData(args[1], args[2])
	local amount = 0
	for _ in pairs(data) do amount = amount + 1 end
	return amount
end

p.order = makeInvokeFunc('_order')
function p._order(args)
	local data = getData(args[1], args[2])
	local desc = args['desc'] or false
	local excludeAtt = args['excludeAtt'] or nil
	local excludeAtt = args['excludeVal'] or nil
	local excludeAtt = args['excludeInvert'] or false
	return sortEntries(data, args[3], desc, excludeAtt, excludeVal, excludeInvert)
end

return p