Jump to content

Module:Ru Paul's Drag Race tables

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Wugapodes (talk | contribs) at 07:20, 11 July 2020 (Wugapodes moved page Module:Drag Race contestant table to Module:Ru Paul's Drag Race tables without leaving a redirect: generalize name). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {} --p stands for package

function p._getContestant( k )
	return string.match( k, "contestant%-(%d+)" )
end

function p._getField( k )
	return string.match( k, "contestant%-%d+%-(.*)")
end

function p._inTable( t, k )
	return (t[k] ~= nil)
end

function p._getContestantData( frame )
    local contestantData = {}
    for k, v in pairs( frame.args ) do
    	-- Read inputs and organize them by contestant
   		if not p._inTable(contestantData, p._getContestant(k)) then
   			contestantData[p._getContestant(k)] = {}
   		end
   		if p._getField(k) ~= nil then
    		contestantData[p._getContestant(k)][p._getField(k)] = v
    	else
    		contestantData[p._getContestant(k)]["name"] = v
    	end
    end
    for k, v in pairs( contestantData ) do
    	-- Final cleanup of the input before rendering table
	    if not p._inTable(contestantData[k],"nrows") then
	    	contestantData[k]["nrows"] = 1
	    end
	    if not p._inTable(contestantData[k],"place-sort") then
		    if #tostring(contestantData[k]['place']) > 2 then 
		    	place = string.match(contestantData[k]['place'],'(%d+)%D%D')
		    else
		    	place = contestantData[k]['place']
		    end
	    	contestantData[k]["place-sort"] = p._makePlaceSort(place)
	    end
	    if #tostring(contestantData[k]['place']) < 3 then 
	    	contestantData[k]['place'] = p._makePlace(contestantData[k]['place'])
		end
    end
    return contestantData
end

function p.makeRow( contestant )
	local rowTemplate = [=[
	|-
	! scope="row" rowspan="${NROWS}"|[[${NAME}]]
	|rowspan="${NROWS}"|${AGE}
	|rowspan="${NROWS}"|${HOMETOWN}
	]=]
	if string.find(contestant['season'], 'All Stars') ~= nil then
		rowTemplate = rowTemplate .. "|[[RuPaul's Drag Race All Stars (season ${SEASON-NUM})|''All Stars'' ${SEASON-NUM}]]\n"
		contestant['season-num'] = string.match(contestant['season'], 'All Stars (%d+)')
		contestant['season'] = nil
	else
		rowTemplate = rowTemplate .. "|[[RuPaul's Drag Race (season ${SEASON})|Season ${SEASON}]]\n"
	end
	rowTemplate = rowTemplate .. [=[
	|<span data-sort-value="${PLACE-SORT}">${PLACE}</span>
	]=]
	if contestant['outcome'] ~= nil then
		rowTemplate = rowTemplate .. '|rowspan="${NROWS}"|${OUTCOME}\n'
		if #tostring(contestant['outcome']) < 3 then
			contestant['outcome'] = p._makePlace(contestant['outcome'])
		end
	else
		rowTemplate = rowTemplate .. '|rowspan="${NROWS}" style="background: #DDF; color: #2C2C2C; vertical-align: middle; text-align: center;" class="no table-no2"|TBA\n'
	end

	if tonumber(contestant['nrows']) > 1 then
		rowTemplate = rowTemplate .. "|-\n"
		if string.find(contestant['season2'], 'All Stars') ~= nil then
			rowTemplate = rowTemplate .. "|[[RuPaul's Drag Race All Stars (season ${SEASON2-NUM})|''All Stars'' ${SEASON2-NUM}]]\n"
			contestant['season2-num'] = tostring(string.match(contestant['season2'], '%d+'))
			contestant['season2'] = nil
		else
			rowTemplate = rowTemplate .. "|[[RuPaul's Drag Race (season ${SEASON2})|Season ${SEASON2}]]\n"
		end
		rowTemplate = rowTemplate .. '|<span data-sort-value="${PLACE2-SORT}">${PLACE2}</span>\n'
		if contestant['place2-sort'] == nil then
			contestant['place2-sort'] = p._makePlaceSort(contestant['place2'])
		end
		local place
		if #tostring(contestant['place2']) > 2 then 
		  	place = string.match(contestant['place2'],'(%d+)%D%D')
		else
		   	place = contestant['place2']
		end
		contestant['place2'] = p._makePlace(place)
	end
	for k, v in pairs( contestant ) do
		mw.log(k:upper())
		rowTemplate = string.gsub(rowTemplate,"${"..k:upper():gsub('%-','%%-').."}",contestant[k])
	end
	return rowTemplate	
end

function p._makePlaceSort( place )
	if #tostring(place) < 2 then
		return '0'..place
	else
		return place
	end
end

function p._makePlace( place )
	place = tonumber(place)
	if place == 1 then
		return '1st Place'
	elseif place == 2 then
		return '2nd Place'
	elseif place == 3 then
		return'3rd Place'
	else
		return place .. 'th Place'
	end
end

function p.main( frame )
    local templateFrame = frame:getParent()
    local contestantData = p._getContestantData( templateFrame )
    ret = [=[
    {| class="wikitable sortable" border="2" style="text-align:center;"
	|+ Contestants of ''All Stars 5'' and their backgrounds
	! scope="col"| Contestant
	! scope="col"| Age
	! scope="col"| Hometown
	! scope="col"| Original season(s)
	! scope="col"| Original placement(s)
	! scope="col"| Outcome
    ]=]
    for k, v in pairs( contestantData ) do
    	ret = ret .. p.makeRow(contestantData[k])
    end
    return ret .. "|}"
end

-- Contestant progress table
function p.progressTable( frame )
	local templateFrame = frame:getParent()
	local contestantData = {}
	ret = ""
	for i=1,20 do
		arg = templateFrame.args[i]
		if arg == nil then
			break
		elseif i % 2 == 1 then
			contestantData[arg] = {}
		else
			contestantData[templateFrame.args[i-1]] = arg
		end
	end
	contestantData, width = p._parseRanks( contestantData )
	ret = ret .. [=[{| class="wikitable" style="text-align:center;"
	|+Progress of contests including rank/position in each episode
	! scope="col"| Contestant
	]=]
	for i=1,width do
		ret = ret .. "! scope='col'| " .. i .."\n"	
	end
	rowList = {}
	for k,v in pairs(contestantData) do
		table.insert(rowList,p._makeRow(k,v,width))
	end
	table.sort(rowList,p._sortRows)
	for i=1,20 do
		if rowList[i] == nil then
			break
		end
		ret = ret .. rowList[i][2]
	end
	return ret
end

function p._parseRanks( contestantData )
	local data = {}
	local high = 0
	for k,v in pairs(contestantData) do
		data[k] = mw.text.split(contestantData[k],',%s')
		if #data[k] > high then
			high = #data[k]
		end
	end
	return data, high
end
	
function p._makeRow( contestant, placements, width )
	row = "|-\n! scope='row'| " .. contestant
	final = 0
	for i=1,20 do
		if placements[i] == nil then
			final = i
			if i > width then
				break
			end
			row = row .. "| colspan='" .. width + 1 - i .. "' bgcolor='darkgray' |\n"
			break
		elseif string.find(placements[i],"BTM") ~= nil then
			colorKey = "BTM"
		else
			colorKey = string.gsub(placements[i],"%s+","")
		end
		text = p._getProgressText(placements[i])
		row = row .. "| style='background:" .. p._getColor(colorKey) .. ";' |" .. text .. "\n"
	end
	retRow = {
		final,
		row
	}
	return retRow
end

function p._getColor( key )
	if key == "WIN" then
		return "royalblue" -- The contestant won the challenge and won the Lip Sync against the Lip Sync Assassin.
	elseif key == "WINTIE" then
		return "darkblue" -- The contestant won the challenge and tied in the Lip Sync against the Lip Sync Assassin.
	elseif key == "WINLOSE" then
		return "yellow" -- The contestant won the challenge, but lost the Lip Sync against the Lip Sync Assassin.
	elseif key == "HIGH" then
		return "lightblue" -- The contestant received positive critiques and was ultimately declared safe.
	elseif key == "SAFE" then
		return "#F8F9FA" -- Safe
	elseif key == "SAFECRIT" then
		return "cornsilk" -- The contestant received critiques but was ultimately declared safe.
	elseif key == "LOW" then
		return "lightpink" -- The contestant received negative critiques but was ultimately declared safe.
	elseif key == "BTM" then
		return "tomato" -- The contestant was up for elimination.
	elseif key == "ELIM" then
		return "red" -- The contestant was eliminated.
	else
		return "black"
	end
end

function p._getProgressText( key )
	key = string.gsub(key,"%s+","")
	if key == "ELIM" then
		return "'''ELIM'''"
	elseif string.find(key, "WIN") ~= nil then
		if key == "WIN" or key == "WINTIE" then
			return "<span style='color:white; font-weight: bold;'>WIN</span>"
		end
		return "WIN"
	elseif #key < 5 then
		return key
	elseif string.find(key, "SAFE") ~= nil then
		return "SAFE"
	end
end

function p._sortRows(a,b)
	return a[1] > b[1]	
end

return p