Module:CricketLeagueGroupStageSummary
Appearance
Example
{{#invoke: CricketLeagueGroupStageSummary|IPL <!-- home | away | result | margin | DL --> | MI | KKR | A | 41R | N | KKR | MI | A | 2W | N | MI | CSK | T | | Y | CSK | MI | H | SO | N | KKR | CSK | N | | N | CSK | KKR | | | }}
Script error: The function "IPL" does not exist.
local _module = {}
_module.create = function(frame)
---------- Functions ----------
local strGsplit = mw.text.gsplit
local strTrim = mw.text.trim
local strFind = string.find
local strFormat = string.format
local strSub = string.sub
local strRepeat = string.rep
local strUpper = string.upper
local yesno = require("Module:Yesno")
---------- Arguments ----------
local args = frame.args
local year = tonumber(args.year) or error("Parameter: 'year' is missing or invalid")
---------- Background colours for table cells ----------
local noMatchColour = "#C0C0C0" -- No match defined
local colours = {
H = "#CCCCFF", -- Home team wins
A = "#FFCCCC", -- Away team wins
N = "#FFDEAD", -- Match abandoned
T = "#DDFFDD", -- Match tied
X = "inherit", -- Match not played yet
}
-- The table containing all the matches, looked up by home and away teams.
local matches = {}
-- Matches which have been defined have a 'true' value for their match number key in this table.
local matchIDs = {}
-- This table contains all the team objects, sorted in alphabetical order of full name
local teams = {}
local teamCount
-- This is used to lookup team objects by name
local teamLookup = {}
do
-- Get the team objets for the given teams
local i = 1
local teamTable = require("Module:IndianPremierLeague/Teams")
local teamSorter = function(t1, t2)
return t1.fullName < t2.fullName
end
for t in strGsplit(args.teams or error("Missing parameter: 'teams'"), '%s*,%s*') do
local teamObj = teamTable[t]
if not teamObj or year < teamObj.startYear or (teamObj.endYear and teamObj.endYear < year) then
error("Team '" .. t .. "' does not exist or is not valid for the given year.")
end
teams[i] = teamObj
teamLookup[teamObj.code] = teamObj
teamLookup[teamObj.shortName] = teamObj
teamLookup[teamObj.fullName] = teamObj
i = i + 1
end
table.sort(teams, teamSorter)
teamCount = #teams
end
--[[
Returns the team object (as defined in Module:IndianPremierLeague/Teams) for a given full name, short name
or team code.
]]
local getTeam = function(name)
local t = teamLookup[name]
if not t then
error("Team '" .. name .. "' is not in the given team list.")
end
return t
end
-- Parse the rows
for i, row in ipairs(args.useParentData and frame:getParent().args or args) do
local match = {}
-- Key/value pairs for each row are separated by semicolons, and a colon separates the key from the value.
for p in strGsplit(row, "%s*;%s*") do
local colon = strFind(p, ':', 1, true)
if not colon then -- No colon found
error(strFormat("Expecting ':' after '%s'. (parameter #%d)", p, i))
end
match[strTrim(strSub(p, 0, colon - 1))] = strTrim(strSub(p, colon + 1, -1))
end
-- Check that the required parameters exist
if not match.match then error("Missing key: 'match' (parameter #" .. i .. ")") end
if not match.home then error("Missing key: 'home' (parameter #" .. i .. ")") end
if not match.away then error("Missing key: 'away' (parameter #" .. i .. ")") end
if not match.result then error("Missing key: 'result' (parameter #" .. i .. ")") end
local id, home, away = tonumber(match.match), getTeam(match.home), getTeam(match.away)
if rawequal(home, away) then -- Home and away teams cannot be the same
error("Invalid match (home and away teams are equal). (parameter #" .. i .. ")")
elseif not id or id <= 0 or id % 1 ~= 0 then -- The match number is not an integer greater than zero
error("Match number must be an integer greater than 0. (parameter #" .. i .. ")")
elseif matchIDs[id] then -- A match with the given match number is already defined
error("A match with the match number %d has been previously defined. (parameter #" .. i .. ")")
end
matchIDs[id] = true
if not matches[home] then matches[home] = {} end
matches[home][away] = match
end
-- Construct the header
local visitorHeaders = {}
for i = 1, teamCount do
local team = teams[i]
visitorHeaders[i] = strFormat('! rowspan="2" scope="col" style="padding: 0px 10px" | [[%s|%s]]\n', team.pageName, team.code)
end
local header = strFormat([[
%s
<div style="float: left">
{| class="wikitable" style="text-align: center; white-space: nowrap; width: 100%%"
! scope="row" | Visitor team →
%s
|-
! scope="col" | Home team ↓
]],
frame:expandTemplate({ title = "WebSlice-begin", args = { id = "1", title = year .. " IPL Group Stage Table" } }),
table.concat(visitorHeaders, '\n'))
--[[
Returns a margin string from the given parameters
id: The match number (used in error messages).
margin: A number suffixed with 'R' (runs) or 'W' (wickets), or 'F' for a forfeited match.
isDL: If the result is decided by the D/L method, this should be true.
isSuperOver: If the result is decided by a super over, this should be true.
]]
local getMarginString = function(id, margin, isDL, isSuperOver)
if not margin then error(strFormat("Match %d: The result 'H' or 'A' requires a margin.", id)) end
if margin == 'F' then return "Forfeited"
elseif isSuperOver then return "Super Over"
else
local n, t = tonumber(strSub(margin, 1, -2)), strSub(margin, -1, -1)
if not n then error(strFormat("Match %d: Margin must be a valid number suffixed with R or W.", id)) end
if t == 'R' then
return strFormat("%d runs%s", n, isDL and ' <span style="font-size: 85%">(D/L)</span>' or "")
elseif t == 'W' then
return strFormat("%d wickets%s", n, isDL and ' <span style="font-size: 85%">(D/L)</span>' or "")
else
error(strFormat("Match %d: Margin must be 'F', or a valid number suffixed with 'R' or 'W'.", id))
end
end
end
-- Output the main body of the table
local rows = {}
local noMatchCell = '| style="background-color: ' .. noMatchColour .. '" |'
local noMatchCellWithNewLine = noMatchCell .. '\n'
for i = 1, teamCount do
-- Each row starts with a home team header
local home = teams[i]
local noHomeMatches = not matches[home]
local cells = {}
cells[1] = strFormat('|-\n! scope="row" style="text-align: left; white-space: normal%s" | [[%s|%s]]', noHomeMatches and "; line-height: 275%" or "", home.pageName, home.fullName)
if noHomeMatches then -- No home matches played by the team
rows[i] = cells[1] .. '\n' .. strRepeat(noMatchCellWithNewLine, teamCount)
else
for j = 1, teamCount do
local away = teams[j]
local match = matches[home][away]
if match then
local id, result, dl, superover = match.match, match.result, yesno(match.dl, false), yesno(match.superover, false)
if result == 'H' then -- Home team wins
cells[j + 1] = strFormat('| style="background-color: %s; padding: 0px 5px" | %s<br />[[#match%s|%s]]', colours.H, home.shortName, id, getMarginString(id, match.margin, dl, superover))
elseif result == 'A' then -- Away team wins
cells[j + 1] = strFormat('| style="background-color: %s; padding: 0px 5px" | %s<br />[[#match%s|%s]]', colours.A, away.shortName, id, getMarginString(id, match.margin, dl, superover))
elseif result == 'X' then -- Match has not been played yet
cells[j + 1] = strFormat('| style="background-color: %s; padding: 0px 5px; line-height: 300%%" | [[#match%s|Match %s]]', colours.X, id, id)
elseif result == 'N' then -- Abandoned match
cells[j + 1] = strFormat('| style="background-color: %s; padding: 0px 5px" | [[#match%s|Match<br />abandoned]]', colours.N, id)
elseif result == 'T' then -- Tied match
cells[j + 1] = strFormat('| style="background-color: %s; padding: 0px 5px; line-height: 300%%" | [[#match%s|Match tied]]', colours.T, id)
else -- Invalid result
error(strFormat("Match %d: Invalid result: '%s'. Expected: H, A, X, N or T.", id, result))
end
else
-- The match is not defined.
cells[j + 1] = noMatchCell
end
end
rows[i] = table.concat(cells, '\n') .. '\n'
end
end
-- Legend and notes
local footer = strFormat([[
|}
{| class="wikitable" style="float:right; text-align: center; font-size: 90%%;"
| style="background-color: %s; width: 50%%" | Home team won
| style="background-color: %s; width: 50%%" | Visitor team won
|-
| style="background-color: %s; width: 50%%" | Match abandoned
| style="background-color: %s; width: 50%%" | Match tied
|}
<ul style="font-size: 90%%">
<li>'''Note''': Results listed are according to the home (horizontal) and visitor (vertical) teams.</li>
<li>'''Note''': Click on the results to see match summary.</li>
</ul>
</div>
<div style="clear: both" />
%s
]],
colours.H, colours.A, colours.N, colours.T,
frame:expandTemplate({ title = 'WebSlice-end', args = {} }))
return header .. table.concat(rows) .. footer
end
return _module