Module

Module:Leaderboard

From Sagan 4 Alpha Wiki

--Tools for displaying leaderboards
--WIP
local p = {}
local userlist = mw.loadJsonData('Module:Leaderboard/users.json')
function p.main(frame)
	--Generate a leaderboard of sagan 4 submissions under certain parameters
	local mainCat = frame.args[1] or "Species" --the submission type category to be used (leaving it open for landmarks)
	local taxon = frame.args["taxon"] or "Eukaryota" --Ever wanted to know who made the most saucebacks? Was it Ovi? Was it Hydro? Was it me? Who knows!
	local week = frame.args["week"] or "+" --so Species Georg can be overtaken short-term
	local gen = frame.args["generation"] or frame.args["gen"] or "+" --or even shorter-term ig
	local whateverProperty = frame.args[2] or "" --psuedo-smw goes here so I don't have to be in pain about people wanting to do More leaderboards than I accounted for
	local limit = frame.args["limit"] or "10"
	local leaderType = frame.args["property"] or "Creator"
	local label = frame.args["label"] or "Leaderboard"
	
	local PropList = ""
	local currentOffset = 0
	local endNextLoop = false
	local debugtext = ""
	
	local count = tonumber(frame:callParserFunction('#ask',{'[[Category:'..mainCat..']][[Taxonomy::'..taxon..']][[Week::'..week..']][[Generation::'..gen..']]'..whateverProperty,format="count"}))
	local limit = 500
	
	while not endNextLoop do
		if currentOffset > count then
			limit = currentOffset - count
			currentOffset = (currentOffset - 500) + (currentOffset - count)
			endNextLoop = true
		end
		local smwResult = frame:callParserFunction('#ask',{'[[Category:'..mainCat..']][[Taxonomy::'..taxon..']][[Week::'..week..']][[Generation::'..gen..']]'..whateverProperty,"?"..leaderType,limit=tostring(limit),searchlabel="",offset=tostring(currentOffset),format="plainlist",headers="hide",mainlabel="-"})
		if smwResult == "" then break end
		PropList = PropList..", "..smwResult
		if currentOffset + 500 > count then
			currentOffset = currentOffset + (currentOffset + 500 - count)
		else
			currentOffset = currentOffset + 500
		end
		debugtext = debugtext.."\n\n"..tostring(currentOffset)
	end
	
	--local PropArray = string.gmatch(PropList, '([^,]+)')
	local PropTable = {}
	
	for prop in string.gmatch(PropList, '([^,]+)') do
		prop = prop:match("^%s*(.-)%s*$")
		if not PropTable[prop] then
			PropTable[prop] = 0
		end
		PropTable[prop] = PropTable[prop] + 1
	end
	
	local proxyTable = {}
	for propval, count in pairs(PropTable) do
		table.insert(proxyTable,{property=propval,total=count})
	end
	
	table.sort(proxyTable, function(a,b)
		return a.total > b.total
	end)
	
	--generate leaderboard table
	local leaderboardTable = '{| class="wikitable"\n!colspan="3"|'..label..'!!n/entries\n|-'
	--local currentPlace = 0
	--local lastValue = 0
	
	for i, item in pairs(proxyTable) do
		leaderboardTable = leaderboardTable..'\n|\'\'\''..tostring(i)..'\'\'\'\n|colspan="2"|'..item.property.."\n|"..item.total..'\n|-'
		
		if i + 1 == limit + 1 then
			leaderboardTable = leaderboardTable..'\n|}'
			break
		end
	end
	
	return leaderboardTable.."\n"..debugtext..PropList
end




return p
Cookies help us deliver our services. By using our services, you agree to our use of cookies.