Module:Crossbox

From NBA 2K Esports Wiki
Jump to: navigation, search

To edit the documentation or categories for this module, click here.


local m_team = require('Module:Team')

local p = {}

function p.crosstable( frame )
	-- Standard crosstable, no toggle, color is determined by score
	if frame == mw.getCurrentFrame() then
		args = require( 'Module:ProcessArgs').norm()
	else
		frame = mw.getCurrentFrame()
	end
		
	local style = args['style'] or "margin-top:0px;"
	local text = {}
	text[1] = "{| class=\"wikitable\" style=\"text-align:center;" .. style .. "\"\n!style=\"background-color:#AAAAAA;\"|"
	
	local i = 1
	local t = 2
	while args['team' .. i] do
		text[#text+1] = "\n! " .. m_team.onlyimagelinked(args['team' .. i])
		i = i + 1
	end
	
	text[#text+1] = "\n!Overall"

	nTeams = i - 1
	
	local win = 0
	local loss = 0
	local score = "0 - 0"
	local color = "#EFA"
	local wins = 0
	local losses = 0
	local draws = 0
	
	for j = 1, nTeams do
		text[#text+1] = "\n|-\n|" .. m_team.onlyimagelinked(args['team' .. j])
		for k = 1, nTeams do
			if j == k then
				text[#text+1] = "\n|style=\"background-color:#AAAAAA;\"| "
			else
				score = args['t' .. j .. 't' .. k .. 'score'] or '0 - 0'
				-- If there's a draw, it will be W - T - L, so check first if there's two dashes
				draw = string.match(score, '^%d*%s*-%s*(%d*)%s*-%s*%d*$')
				if draw then
					-- Get all 3 values but some might be empty if the table isn't filled in yet
					win, draw, loss = string.match(score, '^(%d+)%s*-%s*(%d+)%s*-%s*(%d+)$')
					draw = draw or 0
					draws = draws + draw
				else
					-- Get just win and loss, but these still might be empty. Also deliberately keep draw nil in this case so we know for the Overall column.
					win, loss = string.match(score, '^(%d+)%s*-%s*(%d+)$')
				end
				win = win or 0
				loss = loss or 0
				wins = wins + win
				losses = losses + loss
				-- Pick colors for cells based only on win-loss record
				if win - loss < 0 then
					color = "#FCC"
				elseif win - loss > 0 then
					color = "#CFC"
				elseif win + loss + (draw or 0) == 0 then
					color = ""
				else
					color = "#EFA"
				end
				text[#text+1] = "\n|style=\"background-color:" .. color .. "\"| " .. score
			end
		end
		if draw then
			drawtext = " - " .. draws
		else
			drawtext = ""
		end
		if wins - losses < 0 then
			color = "#FF8080"
		elseif wins - losses > 0 then
			color = "#97ecab"
		elseif wins + losses == 0 then
			color = ""
		else
			color = "#f6ea89"
		end
		text[#text+1] = "\n! style= \"background-color: " .. color .. ";\" class=\"_tog_object _toggle crossbox\"| " .. wins .. drawtext .. " - " .. losses
		wins = 0
		losses = 0
		draws = 0
	end
	
	text[#text+1] = "\n|}"
	return table.concat(text)
	
end

function p.crosstableSeries( frame )
	-- Toggle, both colors are determined by input
	if frame == mw.getCurrentFrame() then
		args = require( 'Module:ProcessArgs').norm()
	else
		frame = mw.getCurrentFrame()
	end
		
	local style = args['style'] or "margin-top:0px;"
	local text = {}
	text[1] = "{| class=\"wikitable\" style=\"text-align:center;" .. style .. "\"\n!style=\"background-color:#AAAAAA;\"|"
	local Team = require('Module:Team').team

	local i = 1
	local t = 2
	while args['team' .. i] do
		text[#text+1] = "\n! " .. m_team.onlyimagelinked(args['team' .. i])
		i = i + 1
	end

	text[#text+1] = "\n!\'\'\'Overall\'\'\'"
	
	nTeams = i - 1
	

	local win = 0
	local loss = 0
	local draw = 0
	local score = "0 - 0"
	local color = "#EFA"
	local wins = 0
	local losses = 0
	local draws = 0
	local wins2 = 0
	local losses2 = 0
	
	for j = 1, nTeams do
		text[#text+1] = "\n|-\n|" .. m_team.onlyimagelinked(args['team' .. j])
		for k = 1, nTeams do
			if j == k then
				text[#text+1] = "\n|style=\"background-color:#AAAAAA;\"| "
			else
				-- First do matches
				score = args['t' .. j .. 't' .. k .. 'score'] or '0 - 0'
				-- If there's a draw, it will be W - T - L, so check first if there's two dashes
				draw = string.match(score, '^%d*%s*-%s*(%d*)%s*-%s*%d*$')
				if draw then
					-- Get all 3 values but some might be empty if the table isn't filled in yet
					win, draw, loss = string.match(score, '^(%d+)%s*-%s*(%d+)%s*-%s*(%d+)$')
					draw = draw or 0
					draws = draws + draw
				else
					-- Get just win and loss, but these still might be empty. Also deliberately keep draw nil in this case so we know for the Overall column.
					win, loss = string.match(score, '^(%d+)%s*-%s*(%d+)$')
				end
				win = win or 0
				loss = loss or 0
				wins = wins + win
				losses = losses + loss
				-- Pick colors for cells based only on win-loss record
				if win - loss < 0 then
					color = "#FCC"
				elseif win - loss > 0 then
					color = "#CFC"
				elseif win + loss + (draw or 0) == 0 then
					color = ""
				else
					color = "#EFA"
				end
				-- Now do games, this one is guaranteed to not have any draws
				text[#text+1] = "\n|style=\"background-color:" .. color .. "; display:none;\" class=\"_tog_object _toggle crossbox\"| " .. score
				score = args['t' .. j .. 't' .. k .. 'games'] or '0 - 0'
				win, loss = string.match(score, '^(%d+)%s*-%s*(%d+)$')
				win = win or 0
				loss = loss or 0
				if win - loss < 0 then
					color = "#FCC"
				elseif win - loss > 0 then
					color = "#CFC"
				elseif win + loss == 0 then
					color = ""
				else
					color = "#EFA"
				end
				text[#text+1] = "\n|style=\"background-color:" .. color .. ";\" class=\"_tog_object _toggle crossbox\"| " .. score
				wins2 = wins2 + win
				losses2 = losses2 + loss
			end
		end
		-- At the end of the row make an Overall column entry, include 2 dashes / 3 number spaces only if draws was defined.
		if draw then
			drawtext = " - " .. draws
		else
			drawtext = ""
		end
		if wins - losses < 0 then
			color = "#FF8080"
		elseif wins - losses > 0 then
			color = "#97ecab"
		elseif wins + losses == 0 then
			color = ""
		else
			color = "#f6ea89"
		end
		text[#text+1] = "\n! style= \"background-color: " .. color .. ";display:none;\" class=\"_tog_object _toggle crossbox\"| " .. wins .. drawtext .. " - " .. losses
		if wins2 - losses2 < 0 then
			color = "#FF8080"
		elseif wins2 - losses2 > 0 then
			color = "#97ecab"
		elseif wins2 + losses2 == 0 then
			color = ""
		else
			color = "#f6ea89"
		end
		text[#text+1] = "\n! style= \"background-color: " .. color .. ";\" class=\"_tog_object _toggle crossbox\"| " .. wins2 .. " - " .. losses2
		wins = 0
		losses = 0
		draws = 0
		wins2 = 0
		losses2 = 0
	end
	
	text[#text+1] = "\n|}"
	return table.concat(text)
	
end

function p.crosstableUnmatched( frame )
	-- Standard crosstable, no toggle, color is determined by score
	if frame == mw.getCurrentFrame() then
		args = require( 'Module:ProcessArgs').norm()
	else
		frame = mw.getCurrentFrame()
	end
		
	local style = args['style'] or "margin-top:0px;"
	local text = {}
	text[1] = "{| class=\"wikitable\" style=\"text-align:center;" .. style .. "\"\n!style=\"background-color:#AAAAAA;\" class=\"_tog_object _toggler-crossboxSwitch\" | Toggle"
	local Team = require('Module:Team').team
	
	local i = 1
	local t = 2
	while args['teamx' .. i] do
		text[#text+1] = "\n! class=\"_tog_object _toggle crossboxSwitch\" | " .. m_team.onlyimagelinked(args['teamx' .. i])
		text[#text+1] = "\n! class=\"_tog_object _toggle crossboxSwitch\ _toggle_inithide\" | " .. m_team.onlyimagelinked(args['team' .. i])
		i = i + 1
	end
	
	text[#text+1] = "\n!Overall"

	nTeams = i - 1
	
	local win = 0
	local loss = 0
	local score = "0 - 0"
	local color = "#EFA"
	local wins = 0
	local losses = 0
	local wins2 = 0
	local losses2 = 0
	
	for j = 1, nTeams do
		text[#text+1] = "\n|-\n| class=\"_tog_object _toggle crossboxSwitch\" | " .. m_team.onlyimagelinked(args['team' .. j])
		text[#text+1] = "\n| class=\"_tog_object _toggle crossboxSwitch _toggle_inithide\" | " .. m_team.onlyimagelinked(args['teamx' .. j])
		for k = 1, nTeams do
			score = args['t' .. j .. 't' .. k .. 'score'] or '0 - 0'
			win, loss = string.match(score, '^(%d+)%s*-%s*(%d+)%s*.*$')
			win = win or 0
			loss = loss or 0
			if win - loss < 0 then
				color = "#FCC"
			elseif win - loss > 0 then
				color = "#CFC"
			elseif win + loss == 0 then
				color = ""
			else
				color = "#EFA"
			end
			text[#text+1] = "\n| class=\"_tog_object _toggle crossboxSwitch\" style=\"background-color:" .. color .. "\"| " .. score
			wins = wins + win
			losses = losses + loss
			score = args['t' .. k .. 't' .. j .. 'score'] or '0 - 0'
			-- Backwards for the other part of the table
			loss, win = string.match(score, '^(%d+)%s*-%s*(%d+)%s*.*$')
			win = win or 0
			loss = loss or 0
			if win - loss < 0 then
				color = "#FCC"
			elseif win - loss > 0 then
				color = "#CFC"
			elseif win + loss == 0 then
				color = ""
			else
				color = "#EFA"
			end
			text[#text+1] = "\n| class=\"_tog_object _toggle crossboxSwitch _toggle_inithide\" style=\"background-color:" .. color .. "\"| " .. win .. " - " .. loss
			wins2 = wins2 + win
			losses2 = losses2 + loss
		end
		if wins - losses < 0 then
			color = "#FF8080"
		elseif wins - losses > 0 then
			color = "#97ecab"
		elseif wins + losses == 0 then
			color = ""
		else
			color = "#f6ea89"
		end
		text[#text+1] = "\n! class=\"_tog_object _toggle crossboxSwitch\" style= \"background-color: " .. color .. ";\" |" .. wins .. " - " .. losses
		if wins2 - losses2 < 0 then
			color = "#FF8080"
		elseif wins2 - losses2 > 0 then
			color = "#97ecab"
		elseif wins2 + losses2 == 0 then
			color = ""
		else
			color = "#f6ea89"
		end
		text[#text+1] = "\n! class=\"_tog_object _toggle crossboxSwitch _toggle_inithide\" style= \"background-color: " .. color .. ";\" |" .. wins2 .. " - " .. losses2
		wins = 0
		losses = 0
		wins2 = 0
		losses2 = 0
	end
	
	text[#text+1] = "\n|}"
	return table.concat(text)
	
end

return p