Jump to content

Module:Fb overview: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
No edit summary
fix kv global variable problem that gave "Lua error in Module:Sports_overview at line 167: Tried to read nil global kv" in a few articles
Line 6: Line 6:
local getArgs = require('Module:Arguments').getArgs
local getArgs = require('Module:Arguments').getArgs
local args = getArgs(frame)
local args = getArgs(frame)

-- Get the row numbers and check for invalid input
-- Get the row numbers and check for invalid input
local rownumbers = {}
local rownumbers = {}
Line 14: Line 14:
local ignoredrows = -1
local ignoredrows = -1
local rowlimit = tonumber(args.c or 999) or 999
local rowlimit = tonumber(args.c or 999) or 999

local function addrownumber(num, flag)
local function addrownumber(num, flag)
num = tonumber(num)
num = tonumber(num)
Line 26: Line 26:
return flag
return flag
end
end

for k, v in pairs(args) do
for k, v in pairs(args) do
if k == 'u' or k == 'c' or k == 's' or k == 'pts' then
if k == 'u' or k == 'c' or k == 's' or k == 'pts' then
Line 48: Line 48:
-- Sort the row numbers
-- Sort the row numbers
table.sort(rownumbers)
table.sort(rownumbers)

-- Remove duplicates
-- Remove duplicates
for i=#rownumbers,2,-1 do
for i=#rownumbers,2,-1 do
Line 55: Line 55:
end
end
end
end

local root = {}
local root = {}
if maxrow > -1 then
if maxrow > -1 then
Line 95: Line 95:
table.insert(root,'! <abbr title="Winning percentage">Win %</abbr>')
table.insert(root,'! <abbr title="Winning percentage">Win %</abbr>')
local evenodd = 'odd'
local evenodd = 'odd'

-- Now add the rows
-- Now add the rows
local wtot, dtot, ltot, ftot, atot = 0, 0, 0, 0, 0
local wtot, dtot, ltot, ftot, atot = 0, 0, 0, 0, 0
Line 121: Line 121:
if showpos then
if showpos then
local fp = args['fp' .. r] or ''
local fp = args['fp' .. r] or ''
local bg =
local bg =
(fp:match('^Winner') and 'gold') or
(fp:match('^Winner') and 'gold') or
(fp:match('^Runners-up') and 'silver') or
(fp:match('^Runners-up') and 'silver') or
Line 137: Line 137:
atot = atot + (tonumber(args['a' .. r]) or 0)
atot = atot + (tonumber(args['a' .. r]) or 0)
table.insert(root, WDL(frame,
table.insert(root, WDL(frame,
{nil, args['w' .. r], args['d' .. r], args['l' .. r],
{nil, args['w' .. r], args['d' .. r], args['l' .. r],
['for'] = args['f' .. r], ['against'] = args['a' .. r], ['diff'] = 'yes'})
['for'] = args['f' .. r], ['against'] = args['a' .. r], ['diff'] = 'yes'})
)
)
Line 157: Line 157:
table.insert(root, 'Source: ' .. (args.s or '[[#Competitions|Competitions]]') .. frame:expandTemplate{title = 'refend'})
table.insert(root, 'Source: ' .. (args.s or '[[#Competitions|Competitions]]') .. frame:expandTemplate{title = 'refend'})
end
end

if #unknown > 0 then
if #unknown > 0 then
if frame:preprocess( "{{REVISIONID}}" ) == "" then
if frame:preprocess( "{{REVISIONID}}" ) == "" then
for i=1,#unknown do
for i=1,#unknown do
table.insert(root,'<div class="error" style="font-weight:normal">Unknown parameter: "' .. unknown[i][1] .. '"</div>')
local kv = unknown[i]
table.insert(root,'<div class="error" style="font-weight:normal">Unknown parameter: "' .. kv[1] .. '"</div>')
end
end
else
else
table.insert(root, '[[Category:Pages using sports overview with unknown parameters|' .. mw.uri.anchorEncode(kv[1]) .. ' ]]')
table.insert(root, '[[Category:Pages using sports overview with unknown parameters|' .. mw.uri.anchorEncode(unknown[1][1]) .. ' ]]')
end
end
end
end

if ignoredrows > -1 then
if ignoredrows > -1 then
table.insert(root, '[[Category:Pages using sports overview with ignored rows|' .. ignoredrows .. ']]')
table.insert(root, '[[Category:Pages using sports overview with ignored rows|' .. ignoredrows .. ']]')
end
end

return table.concat(root, '\n')
return table.concat(root, '\n')
end
end

Revision as of 01:07, 8 January 2019

-- This implements {{fb overview}}
local p = {}

-- Main function
function p.main(frame)
	local getArgs = require('Module:Arguments').getArgs
	local args = getArgs(frame)

	-- Get the row numbers and check for invalid input
	local rownumbers = {}
	local unknown = {}
	local showdates, showrounds, showpos = false, false, false
	local maxrow = -1
	local ignoredrows = -1
	local rowlimit = tonumber(args.c or 999) or 999

	local function addrownumber(num, flag)
		num = tonumber(num)
		if num <= rowlimit then
			table.insert(rownumbers, num)
			maxrow = (num > maxrow) and num or maxrow
			return true
		else
			ignoredrows = num
		end
		return flag
	end

	for k, v in pairs(args) do
		if k == 'u' or k == 'c' or k == 's' or k == 'pts' then
			-- These are valid
		elseif tostring(k):match('^[cwdlfa]%d%d*$') then
			local n = mw.ustring.gsub(tostring(k), '^[cwdlfa](%d%d*)$', '%1')
			local added = addrownumber(n, false)
		elseif tostring(k):match('[dfl]m%d%d*$') then
			local n = mw.ustring.gsub(tostring(k), '^[dfl]m(%d%d*)$', '%1')
			showdates = addrownumber(n, showdates)
		elseif tostring(k):match('sr%d%d*$') then
			local n = mw.ustring.gsub(tostring(k), '^sr(%d%d*)$', '%1')
			showrounds = addrownumber(n, showrounds)
		elseif tostring(k):match('fp%d%d*$') then
			local n = mw.ustring.gsub(tostring(k), '^fp(%d%d*)$', '%1')
			showpos = addrownumber(n, showpos)
		else
			table.insert(unknown, {k, v})
		end
	end
	-- Sort the row numbers
	table.sort(rownumbers)

	-- Remove duplicates
	for i=#rownumbers,2,-1 do
		if rownumbers[i-1] == rownumbers[i] then
			table.remove(rownumbers,i)
		end
	end

	local root = {}
	if maxrow > -1 then
		local WDL = require('Module:WDL').main
		-- Make the table
		table.insert(root,'{| class="wikitable" style="text-align:center"')
		-- Add the headers
		table.insert(root,'|-')
		table.insert(root,'! rowspan=2 | Competition')
		local totspan = 1
		if showdates then
			table.insert(root,'! rowspan=2 | First match')
			table.insert(root,'! rowspan=2 | Last match')
			totspan = totspan + 2
		end
		if showrounds then
			table.insert(root,'! rowspan=2 | Starting round')
			totspan = totspan + 1
		end
		if showpos then
			table.insert(root,'! rowspan=2 | Final position')
			totspan = totspan + 1
		end
		table.insert(root,'! colspan=8 | Record')
		table.insert(root,'|-')
		table.insert(root,'! <abbr title="Games played">Pld</abbr>')
		table.insert(root,'! <abbr title="Games won">W</abbr>')
		table.insert(root,'! <abbr title="Games drawn">D</abbr>')
		table.insert(root,'! <abbr title="Games lost">L</abbr>')
		if args.pts and args.pts == 'y' then
			table.insert(root,'! <abbr title="Points for">PF</abbr>')
			table.insert(root,'! <abbr title="Points against">PA</abbr>')
			table.insert(root,'! <abbr title="Point difference">PD</abbr>')
		else
			table.insert(root,'! <abbr title="Goals for">GF</abbr>')
			table.insert(root,'! <abbr title="Goals against">GA</abbr>')
			table.insert(root,'! <abbr title="Goal difference">GD</abbr>')
		end
		table.insert(root,'! <abbr title="Winning percentage">Win %</abbr>')
		local evenodd = 'odd'

		-- Now add the rows
		local wtot, dtot, ltot, ftot, atot = 0, 0, 0, 0, 0
		for i=1,#rownumbers do
			local r = rownumbers[i]
			if evenodd == 'even' then
				table.insert(root,'|- style="background-color:#EEE"')
				evenodd = 'odd'
			else
				table.insert(root,'|-')
				evenodd = 'even'
			end
			table.insert(root,'| ' .. (args['c' .. r] or ''))
			if showdates then
				if args['dm' .. r] then
					table.insert(root,'| colspan=2 | ' .. args['dm' .. r])
				else
					table.insert(root,'| ' .. (args['fm' .. r] or ''))
					table.insert(root,'| ' .. (args['lm' .. r] or ''))
				end
			end
			if showrounds then
			table.insert(root,'| ' .. (args['sr' .. r] or ''))
			end
			if showpos then
				local fp = args['fp' .. r] or ''
				local bg =
					(fp:match('^Winner') and 'gold') or
					(fp:match('^Runners-up') and 'silver') or
					(fp:match('^Runner-up') and 'silver') or nil
				if bg then
					table.insert(root,'| style="background-color:' .. bg .. '" | ' .. fp)
				else
					table.insert(root,'| ' .. fp)
				end
			end
			wtot = wtot + (tonumber(args['w' .. r]) or 0)
			dtot = dtot + (tonumber(args['d' .. r]) or 0)
			ltot = ltot + (tonumber(args['l' .. r]) or 0)
			ftot = ftot + (tonumber(args['f' .. r]) or 0)
			atot = atot + (tonumber(args['a' .. r]) or 0)
			table.insert(root, WDL(frame,
				{nil, args['w' .. r],  args['d' .. r],  args['l' .. r],
				['for'] = args['f' .. r], ['against'] = args['a' .. r], ['diff'] = 'yes'})
			)
		end
		table.insert(root,'|-')
		if totspan > 1 then
			table.insert(root,'! colspan=' .. totspan .. ' | Total')
		else
			table.insert(root,'! Total')
		end
		table.insert(root, WDL(frame,
				{wtot+dtot+ltot, wtot, dtot, ltot, ['total'] = 'y',
				['for'] = ftot, ['against'] = atot, ['diff'] = 'yes'})
			)
		table.insert(root, '|}' .. frame:expandTemplate{title = 'refbegin'})
		if args.u then
			table.insert(root, 'Last updated: ' .. args.u .. '<br>')
		end
		table.insert(root, 'Source: ' .. (args.s or '[[#Competitions|Competitions]]') .. frame:expandTemplate{title = 'refend'})
	end

	if #unknown > 0 then
		if frame:preprocess( "{{REVISIONID}}" ) == "" then
			for i=1,#unknown do
				table.insert(root,'<div class="error" style="font-weight:normal">Unknown parameter: "' .. unknown[i][1] .. '"</div>')
			end
		else
			table.insert(root, '[[Category:Pages using sports overview with unknown parameters|' .. mw.uri.anchorEncode(unknown[1][1]) .. ' ]]')
		end
	end

	if ignoredrows > -1 then
		table.insert(root, '[[Category:Pages using sports overview with ignored rows|' .. ignoredrows .. ']]')
	end

	return table.concat(root, '\n')
end

return p