Module:Rugby league match squad/sandbox

--
local getArgs = require('Module:Arguments').getArgs
local p={}
--functions
function p.main(frame)	
	local args = getArgs(frame)
	local collapsed = args['collapsed'] or 'n' -- decide if table is collapsible or no
	local align = args['align'] or 'right' -- table alignment on page (left, right or center, defaults to right if not set)
	align = string.lower(align) --convert to lower case
	local squadsize = args['squadsize'] or 18 --number of players to list
	if tonumber(squadsize) > 26 then 
		squadsize = 26 --maximum squadsize allowed
		end
	--teams
	local hometeam = args['hometeam']
	local awayteam = args['awayteam']
    --and colours for header rows
    local homeBGcolour = args['homeBGcolour'] or 'inherit'
    local homeFGcolour = args['homeFGcolour'] or 'inherit'
    local homeBorder = args['homeBorder'] or 'none'
    local awayBGcolour = args['awayBGcolour'] or 'inherit'
    local awayFGcolour = args['awayFGcolour'] or 'inherit'
    local awayBorder = args['awayBorder'] or 'none'
    --set styles for home and away teams. Default is to inherit text and background colours from the stylesheet and border to none.
    local homestyle = '<div class="homestyle"> style= \"border:2px solid ' .. homeBorder .. ';background:' .. homeBGcolour .. '; color:' .. homeFGcolour .. ';\"</div>'
    local awaystyle = '<div class ="awaystyle"> style= \"border:2px solid ' .. awayBorder .. ';background:' .. awayBGcolour .. '; color:' .. awayFGcolour .. ';\"</div>'

	--[[postion names determined by hemisphere. Can be north (n), south (s)
	north/south (ns) or south/north (sn)]]--
	local location = args['location']
	location =string.lower(location) --convert to lower case
	local abbr = args['abbr'] or 'n' -- set to y to use abbreviated position names, default is n 
	abbr = string.lower(abbr) -- convert to lower case
	if location ~= "n" and location ~= "s" and location ~= "ns" and location ~= "sn" then
		location = "n" --catchall
		end
	--define variable positions first
	local pos_6=""
	local pos_7=""
	local pos_13=""
	if location == "n" then
		pos_6 = "[[Five-eighth|Stand off]]"
		pos_7 = "[[Scrum-half (rugby league)|Scrum half]]"
		pos_13 = "[[Loose forward (rugby league)|Loose forward]]"
		if abbr == "y" then  --abbreviated position names
			pos_6 = "[[Five-eighth|SO]]"
			pos_7 = "[[Scrum-half (rugby league)|SH]]"
			pos_13 = "[[Loose forward (rugby league)|LF]]"	
		end
	elseif location == "s" then 
		pos_6 = "[[Five-eighth|Five eighth]]"
		pos_7 = "[[Scrum-half (rugby league)|Half back]]"
		pos_13 = "[[Loose forward (rugby league)|Lock forward]]"
		if abbr == "y" then --abbreviated position names
			pos_6 = "[[Five-eighth|FE]]"
			pos_7 = "[[Scrum-half (rugby league)|HB]]"
			pos_13 = "[[Loose forward (rugby league)|LK]]"	
		end
	elseif location == "ns" then
		pos_6 = "[[Five-eighth|Stand off/Five eighth]]"
		pos_7 = "[[Rugby league positions#Scrum-half|Scrum half/Half back]]"
		pos_13 = "[[Loose forward (rugby league)|Loose forward/Lock forward]]"
		if abbr == "y" then --abbreviated position names
			pos_6 = "[[Five-eighth|SO/FE]]"
			pos_7 = "[[Scrum-half (rugby league)|SH/HB]]"
			pos_13 = "[[Loose forward (rugby league)|LF/LK]]"	
		end
	elseif location == "sn" then  --abbreviated position names
		pos_6 = "[[Five-eighth|Five eighth/Stand off]]"
		pos_7 = "[[Scrum-half (rugby league)|Half back/Scrum half]]"
		pos_13 = "[[Loose forward (rugby league)|Lock forward/Loose forward]]"
		if abbr == "y" then  --abbreviated position names
			pos_6 = "[[Five-eighth|FE/SO]]"
			pos_7 = "[[Scrum-half (rugby league)|HB/SH]]"
			pos_13 = "[[Loose forward (rugby league)|LK/LF]]"	
		end
	end
	--set rest of positions
	local pos_1 = "[[Fullback (rugby league)|Fullback]]"
	local pos_2 = "[[Wing (rugby league)|Wing]]"
	local pos_3 = "[[Centre (rugby league)|Centre]]"
	local pos_4 = "[[Centre (rugby league)|Centre]]"
	local pos_5 = "[[Wing (rugby league)|Wing]]"
	local pos_8 = "[[Prop forward (rugby league)|Prop forward]]"
	local pos_9 = "[[Hooker (rugby league)|Hooker]]"
	local pos_10 = "[[Prop forward (rugby league)|Prop forward]]"
	local pos_11 = "[[Second Row (rugby league)|Second row]]"
	local pos_12 = "[[Second Row (rugby league)|Second row]]"
	local pos_14 = args['titleIN1'] or "[[Interchange (rugby league)|Interchange]]" --allows for alternative position name
	local pos_15 = args['titleIN2'] or "[[Interchange (rugby league)|Interchange]]"
	local pos_16 = args['titleIN3'] or "[[Interchange (rugby league)|Interchange]]"
	local pos_17 = args['titleIN4'] or "[[Interchange (rugby league)|Interchange]]"
	local pos_18 = args['titleIN5'] or "[[Rugby league positions#Concussion substitute|Concussion substitute]]" -- 18th player, can always be overwritten
		if pos_18 == "Interchange" then
		pos_18 = "[[Interchange (rugby league)|Interchange]]" -- autolink alternative position
		end
	local pos_19 = "[[Interchange (rugby league)|Interchange]]"
	local pos_20 = "[[Interchange (rugby league)|Interchange]]"
	local pos_21 = "[[Interchange (rugby league)|Interchange]]"
	local pos_22 = "[[Interchange (rugby league)|Interchange]]"
	local pos_23 = "[[Interchange (rugby league)|Interchange]]"
	local pos_24 = "[[Interchange (rugby league)|Interchange]]"
	local pos_25 = "[[Interchange (rugby league)|Interchange]]"
	local pos_26 = "[[Interchange (rugby league)|Interchange]]"
	if abbr =="y" then --abbreviated position names
		pos_1 = "[[Fullback (rugby league)|FB]]"
		pos_2 = "[[Wing (rugby league)|WG]]"
		pos_3 = "[[Centre (rugby league)|CE]]"
		pos_4 = "[[Centre (rugby league)|CE]]"
		pos_5 = "[[Wing (rugby league)|WG]]"
		pos_8 = "[[Prop forward (rugby league)|PR]]"
		pos_9 = "[[Hooker (rugby league)|HK]]"
		pos_10 = "[[Prop forward (rugby league)|PF]]"
		pos_11 = "[[Second Row (rugby league)|SR]]"
		pos_12 = "[[Second Row (rugby league)|SR]]"
		if pos_14 == "[[Interchange (rugby league)|Interchange]]" then --no abbreviation if title isn't Interchange
			pos_14 = "[[Interchange (rugby league)|IN]]" 
			end
		if pos_15 == "[[Interchange (rugby league)|Interchange]]" then 
			pos_15 = "[[Interchange (rugby league)|IN]]"
			end
		if pos_16 == "[[Interchange (rugby league)|Interchange]]" then 
			pos_16 = "[[Interchange (rugby league)|IN]]"
			end
		if pos_17 == "[[Interchange (rugby league)|Interchange]]" then 
			pos_17 = "[[Interchange (rugby league)|IN]]"
			end
		if pos_18 == "[[Rugby league positions#Concussion substitute|Concussion substitute]]" then 
			pos_18 = "[[Interchange (rugby league)|CS]]"
		end
		if pos_18 == "[[Interchange (rugby league)|Interchange]]" then --alternative position linking
			pos_18 = "[[Interchange (rugby league)|IN]]"
			end
		pos_19 = "[[Interchange (rugby league)|IN]]"
		pos_20 = "[[Interchange (rugby league)|IN]]"
		pos_21 = "[[Interchange (rugby league)|IN]]"
		pos_22 = "[[Interchange (rugby league)|IN]]"
		pos_23 = "[[Interchange (rugby league)|IN]]"
		pos_24 = "[[Interchange (rugby league)|IN]]"
		pos_25 = "[[Interchange (rugby league)|IN]]"
		pos_26 = "[[Interchange (rugby league)|IN]]"
	end
	--Home players
	local hplayer_1 =args['homeFB'] or ""
	local hplayer_2 =args['homeWG1'] or ""
	local hplayer_3 =args['homeCE1'] or ""
	local hplayer_4 =args['homeCE2'] or ""
	local hplayer_5 =args['homeWG2'] or ""
	local hplayer_6 =args['homeSO'] or ""
	local hplayer_7 =args['homeSH'] or ""
	local hplayer_8 =args['homePR1'] or ""
	local hplayer_9 =args['homeHK'] or ""
	local hplayer_10 =args['homePR2'] or ""
	local hplayer_11 =args['homeSR1'] or ""
	local hplayer_12 =args['homeSR2'] or ""
	local hplayer_13 =args['homeLF'] or ""
	local hplayer_14 =args['homeIN1'] or ""
	local hplayer_15 =args['homeIN2'] or ""
	local hplayer_16 =args['homeIN3'] or ""
	local hplayer_17 =args['homeIN4'] or ""
	local hplayer_18 =args['homeIN5'] or ""
	local hplayer_19 =args['homeIN6'] or ""
	local hplayer_20 =args['homeIN7'] or ""
	local hplayer_21 =args['homeIN8'] or ""
	local hplayer_22 =args['homeIN9'] or ""
	local hplayer_23 =args['homeIN10'] or ""
	local hplayer_24 =args['homeIN11'] or ""
	local hplayer_25 =args['homeIN12'] or ""
	local hplayer_26 =args['homeIN13'] or ""
	local hcoach = args['homeCoach'] or ""
	
	-- Home team numbers
	local hnumber_1 =args['homeFBnum'] or '1'
	local hnumber_2 =args['homeWG1num'] or '2'
	local hnumber_3 =args['homeCE1num'] or '3'
	local hnumber_4 =args['homeCE2num'] or '4'
	local hnumber_5 =args['homeWG2num'] or '5'
	local hnumber_6 =args['homeSOnum'] or '6'
	local hnumber_7 =args['homeSHnum'] or '7'
	local hnumber_8 =args['homePR1num'] or '8'
	local hnumber_9 =args['homeHKnum'] or '9'
	local hnumber_10 =args['homePR2num'] or '10'
	local hnumber_11 =args['homeSR1num'] or '11'
	local hnumber_12 =args['homeSR2num'] or '12'
	local hnumber_13 =args['homeLFnum'] or '13'
	--following lines set player number to blank if no name is given
	local hnumber_14 =args['homeIN1num'] or '14'
	if hplayer_14 == "" then hnumber_14 = "" end
	local hnumber_15 =args['homeIN2num'] or '15'
	if hplayer_15 == "" then hnumber_15 = "" end
	local hnumber_16 =args['homeIN3num'] or '16'
	if hplayer_16 == "" then hnumber_16 = "" end
	local hnumber_17 =args['homeIN4num'] or '17'
	if hplayer_17 == "" then hnumber_17 = "" end
	local hnumber_18 =args['homeIN5num'] or '18'
	if hplayer_18 == "" then hnumber_18 = "" end
	local hnumber_19 =args['homeIN6num'] or '19'
	if hplayer_19 == "" then hnumber_19 = "" end
	local hnumber_20 =args['homeIN7num'] or '20'
	if hplayer_20 == "" then hnumber_20 = "" end
	local hnumber_21 =args['homeIN8num'] or '21'
	if hplayer_21 == "" then hnumber_21 = "" end
	local hnumber_22 =args['homeIN9num'] or '22'
	if hplayer_22 == "" then hnumber_22 = "" end
	local hnumber_23 =args['homeIN10num'] or '23'
	if hplayer_23 == "" then hnumber_23 = "" end
	local hnumber_24 =args['homeIN11num'] or '24'
	if hplayer_24 == "" then hnumber_24 = "" end
	local hnumber_25 =args['homeIN12num'] or '25'
	if hplayer_25 == "" then hnumber_25 = "" end
	local hnumber_26 =args['homeIN13num'] or '26'
	if hplayer_26 == "" then hnumber_26 = "" end

	--Away players
	local aplayer_1 =args['awayFB'] or ""
	local aplayer_2 =args['awayWG1'] or ""
	local aplayer_3 =args['awayCE1'] or ""
	local aplayer_4 =args['awayCE2'] or ""
	local aplayer_5 =args['awayWG2'] or ""
	local aplayer_6 =args['awaySO'] or ""
	local aplayer_7 =args['awaySH'] or ""
	local aplayer_8 =args['awayPR1'] or ""
	local aplayer_9 =args['awayHK'] or ""
	local aplayer_10 =args['awayPR2'] or ""
	local aplayer_11 =args['awaySR1'] or ""
	local aplayer_12 =args['awaySR2'] or ""
	local aplayer_13 =args['awayLF'] or ""
	local aplayer_14 =args['awayIN1'] or ""
	local aplayer_15 =args['awayIN2'] or ""
	local aplayer_16 =args['awayIN3'] or ""
	local aplayer_17 =args['awayIN4'] or ""
	local aplayer_18 =args['awayIN5'] or ""
	local aplayer_19 =args['awayIN6'] or ""
	local aplayer_20 =args['awayIN7'] or ""
	local aplayer_21 =args['awayIN8'] or ""
	local aplayer_22 =args['awayIN9'] or ""
	local aplayer_23 =args['awayIN10'] or ""
	local aplayer_24 =args['awayIN11'] or ""
	local aplayer_25 =args['awayIN12'] or ""
	local aplayer_26 =args['awayIN13'] or ""
	local acoach = args['awayCoach'] or ""

	-- Away team numbers
	local anumber_1 =args['awayFBnum'] or '1'
	local anumber_2 =args['awayWG1num'] or '2'
	local anumber_3 =args['awayCE1num'] or '3'
	local anumber_4 =args['awayCE2num'] or '4'
	local anumber_5 =args['awayWG2num'] or '5'
	local anumber_6 =args['awaySOnum'] or '6'
	local anumber_7 =args['awaySHnum'] or '7'
	local anumber_8 =args['awayPR1num'] or '8'
	local anumber_9 =args['awayHKnum'] or '9'
	local anumber_10 =args['awayPR2num'] or '10'
	local anumber_11 =args['awaySR1num'] or '11'
	local anumber_12 =args['awaySR2num'] or '12'
	local anumber_13 =args['awayLFnum'] or '13'
	--following lines set player number to blank if no name is given	
	local anumber_14 =args['awayIN1num'] or '14'
	if aplayer_14 == "" then anumber_14 = "" end
	local anumber_15 =args['awayIN2num'] or '15'
	if aplayer_15 == "" then anumber_15 = "" end
	local anumber_16 =args['awayIN3num'] or '16'
	if aplayer_16 == "" then anumber_16 = "" end
	local anumber_17 =args['awayIN4num'] or '17'
	if aplayer_17 == "" then anumber_17 = "" end
	local anumber_18 =args['awayIN5num'] or '18'
	if aplayer_18 == "" then anumber_18 = "" end
	local anumber_19 =args['awayIN6num'] or '19'
	if aplayer_19 == "" then anumber_19 = "" end
	local anumber_20 =args['awayIN7num'] or '20'
	if aplayer_20 == "" then anumber_20 = "" end
	local anumber_21 =args['awayIN8num'] or '21'
	if aplayer_21 == "" then anumber_21 = "" end
	local anumber_22 =args['awayIN9num'] or '22'
	if aplayer_22 == "" then anumber_22 = "" end
	local anumber_23 =args['awayIN10num'] or '23'
	if aplayer_23 == "" then anumber_23 = "" end
	local anumber_24 =args['awayIN11num'] or '24'
	if aplayer_24 == "" then anumber_24 = "" end
	local anumber_25 =args['awayIN12num'] or '25'
	if aplayer_25 == "" then anumber_25 = "" end
	local anumber_26 =args['awayIN13num'] or '26'
	if aplayer_26 == "" then anumber_26 = "" end
	
	--sourcing
	local source = args['source'] or ""
	--fill table
	local hplayer
	local hnumber
	local pos
	local aplayer
	local anumber

	local t = {
		{
			hplayer = hplayer_1,
			hnumber = hnumber_1,
			pos = pos_1,
			anumber = anumber_1,
			aplayer = aplayer_1,
			},
		{
			hplayer = hplayer_2,
			hnumber = hnumber_2,
			pos = pos_2,
			anumber = anumber_2,
			aplayer = aplayer_2,
			},
		{
			hplayer = hplayer_3,
			hnumber = hnumber_3,
			pos = pos_3,
			anumber = anumber_3,
			aplayer = aplayer_3,
			},
		{
			hplayer = hplayer_4,
			hnumber = hnumber_4,
			pos = pos_4,
			anumber = anumber_4,
			aplayer = aplayer_4,
			},
		{
			hplayer = hplayer_5,
			hnumber = hnumber_5,
			pos = pos_5,
			anumber = anumber_5,
			aplayer = aplayer_5,
			},
		{
			hplayer = hplayer_6,
			hnumber = hnumber_6,
			pos = pos_6,
			anumber = anumber_6,
			aplayer = aplayer_6,
			},
		{
			hplayer = hplayer_7,
			hnumber = hnumber_7,
			pos = pos_7,
			anumber = anumber_7,
			aplayer = aplayer_7,
			},
		{
			hplayer = hplayer_8,
			hnumber = hnumber_8,
			pos = pos_8,
			anumber = anumber_8,
			aplayer = aplayer_8,
			},
		{
			hplayer = hplayer_9,
			hnumber = hnumber_9,
			pos = pos_9,
			anumber = anumber_9,
			aplayer = aplayer_9,
			},
		{
			hplayer = hplayer_10,
			hnumber = hnumber_10,
			pos = pos_10,
			anumber = anumber_10,
			aplayer = aplayer_10,
			},
		{
			hplayer = hplayer_11,
			hnumber = hnumber_11,
			pos = pos_11,
			anumber = anumber_11,
			aplayer = aplayer_11,
			},
		{
			hplayer = hplayer_12,
			hnumber = hnumber_12,
			pos = pos_12,
			anumber = anumber_12,
			aplayer = aplayer_12,
			},
		{
			hplayer = hplayer_13,
			hnumber = hnumber_13,
			pos = pos_13,
			anumber = anumber_13,
			aplayer = aplayer_13,
			},
		{
			hplayer = hplayer_14,
			hnumber = hnumber_14,
			pos = pos_14,
			anumber = anumber_14,
			aplayer = aplayer_14,
			},
		{
			hplayer = hplayer_15,
			hnumber = hnumber_15,
			pos = pos_15,
			anumber = anumber_15,
			aplayer = aplayer_15,
			},
		{
			hplayer = hplayer_16,
			hnumber = hnumber_16,
			pos = pos_16,
			anumber = anumber_16,
			aplayer = aplayer_16,
			},
		{
			hplayer = hplayer_17,
			hnumber = hnumber_17,
			pos = pos_17,
			anumber = anumber_17,
			aplayer = aplayer_17,
			},
		{
			hplayer = hplayer_18,
			hnumber = hnumber_18,
			pos = pos_18,
			anumber = anumber_18,
			aplayer = aplayer_18,
			},
		{
			hplayer = hplayer_19,
			hnumber = hnumber_19,
			pos = pos_19,
			anumber = anumber_19,
			aplayer = aplayer_19,
			},
		{
			hplayer = hplayer_20,
			hnumber = hnumber_20,
			pos = pos_20,
			anumber = anumber_20,
			aplayer = aplayer_20,
			},
		{
			hplayer = hplayer_21,
			hnumber = hnumber_21,
			pos = pos_21,
			anumber = anumber_21,
			aplayer = aplayer_21,
			},
		{
			hplayer = hplayer_22,
			hnumber = hnumber_22,
			pos = pos_22,
			anumber = anumber_22,
			aplayer = aplayer_22,
			},
		{
			hplayer = hplayer_23,
			hnumber = hnumber_23,
			pos = pos_23,
			anumber = anumber_23,
			aplayer = aplayer_23,
			},
		{
			hplayer = hplayer_24,
			hnumber = hnumber_24,
			pos = pos_24,
			anumber = anumber_24,
			aplayer = aplayer_24,
			},
		{
			hplayer = hplayer_25,
			hnumber = hnumber_25,
			pos = pos_25,
			anumber = anumber_25,
			aplayer = aplayer_25,
			},
		{
			hplayer = hplayer_26,
			hnumber = hnumber_26,
			pos = pos_26,
			anumber = anumber_26,
			aplayer = aplayer_26,
			},
		-- add coaches as last row of array
		{
			hplayer = hcoach,
			hnumber="",
			pos = "Coach",
			anumber="",
			aplayer = acoach,
			},
	}
	--check for 00 values in player numbers and replace with "". This allows players to show as unnmbered in teams using squad numbers e.g. trialists
	for trow = 1, 26 do
		if t[trow].hnumber == "00" then
			t[trow].hnumber = ""
			end
		if t[trow].anumber == "00" then
			t[trow].anumber = ""
			end
		end
		--create table	
        -- <div class="position"> tags refer to formatting information used in <templatestyles> (see [[Template:Rugby league match squad/sandbox/styles.css]])
		local wt='' --main output string. Loops through the table t to extract info by positions
		for i = 1, squadsize do
			wt = wt .. t[i].hplayer .. '||' .. t[i].hnumber .. '|| <div class="position">' .. t[i].pos .. '</div> ||' .. t[i].anumber .. '||' .. t[i].aplayer .. '\n|-\n|'
		end
		--add coaches if one or both is named
		if hcoach ~="" or acoach~="" then 
			wt = wt .. t[27].hplayer .. '||' .. t[27].hnumber .. '||  <div class="position">' .. t[27].pos .. '</div>||' .. t[27].anumber .. '||' .. t[27].aplayer .. '\n|-\n|'
		end
		-- table headings. Set depending of page alignment (left, right or center) and collapsed (yes/no)
		local table_style = ""
		if align == 'right' and collapsed == 'n' then 
			table_style = '{| class = "table_style_right"'
		end
		if align == 'left' and collapsed == 'n' then
			table_style = '{| class = "table_style_left"'
		end	
		if align == 'center' and collapsed == 'n' then
			table_style = '{| class = "table_style_center"'
		end	
		if align == 'right' and collapsed == 'y' then 
			table_style = '{| class = "table_style_right mw-collapsible mw-collapsed"'
		end
		if align == 'left' and collapsed == 'y' then
			table_style = '{| class = "table_style_left mw-collapsible mw-collapsed"'
		end	
		if align == 'center' and collapsed == 'y' then
			table_style = '{| class = "table_style_center mw-collapsible mw-collapsed"'
		end	
		local caption= '+ class=\"nowrap\" | <b>Team details</b>'
		-- make teams first data row in table
        local header_row1 = 'colspan=2 ' .. homestyle .. '|' .. hometeam .. '!! rowspan=2 <div class="position"> | Position !! colspan=2 </div>' 
        	.. awaystyle .. '|' .. awayteam .. '\n|-\n'
		-- header row
		local header_row2 = 'Name !! Number !! Number !! Name \n|-\n'
		-- last row is source
		local source_row =  'colspan=5 | <div class="position">'  ..source .. '</div> \n|'
		-- put it all together
		local return_t = table_style .. '\n|' ..  caption ..'\n!' .. header_row1 .. '\n!' .. header_row2 .. '\n|'.. wt .. '}' 
		if source ~= "" then --only display source_row if it has content
			return_t = table_style .. '\n|' ..  caption ..'\n!' .. header_row1 .. '\n!' .. header_row2 .. '\n|'.. wt .. source_row .. '}' 
		end
	return return_t
end
return p