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 19:46, 15 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 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, dir)
    -- deep copy since data is non writable
    local entries = {}
	for _,v in pairs(data) do table.insert(entries, v) end
	
    -- filter entries that do not have att
    local filtered_data = {}
    for _,v in pairs(entries) do
        if value[att] then  -- keep entries with valid att
            table.insert(filtered_data, value)
        end
    end
    
    -- sort entries based on att
    table.sort(filtered_data, function(a, b)
    	if dir == 'a' then
    		return a[att] < b[att]
        elseif dir == 'd' then
        	return a[att] > b[att]
    	end
    end)
    
    return filtered_data
end

function p.main(f)
	local args = getArgs(f)
	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

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

function p.order(f)
	local args = getArgs(f)
	local data = getData(args[1], args[2])
	local dir = args[4] or 'a' -- default ascending
	return sortEntries(data, args(f)[3], dir)
end

return p