Jump to content

Module:IndianPremierLeagueProgression

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Jfd34 (talk | contribs) at 09:51, 20 February 2014 (Creating module). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

--[[
    Module for generating league progresion tables intended for use in Indian Premier League season articles.
]]

local _module = {}

_module.create = function(frame)
    
    local strSplit = mw.text.split
    local strFormat = string.format
    
    local args = frame.args
    
    local matchesPerTeam = tonumber(args.matchesPerTeam)
    local ktype = tonumber(args.knockoutType)
    local teams = strSplit(args.teams, '%s*,%s*')

    local colours = { W = "#99FF99", L = "#FFDDDD", N = "#DFDFFF", E = "#DCDCDC" }
    local classes = { W = "yes table-yes2", L = "no table-no2", N = "noresult", E = "" }
    local points = { W = 2, L = 0, N = 1, U = 0 }
    
    local groupStageHeaders = {}
    for i = 1, matchesPerTeam do
        -- Generate the headers for each group match
        groupStageHeaders[i] = ((i == 1) and '! style="width: 30px; border-left: 4px solid #454545"' or '!! style="width: 30px"') .. ' scope="col" | ' .. i .. ' '
    end
    
    local knockoutTypeMatches = { 2, 3 }
    local kMatches = knockoutTypeMatches[ktype]
    
    if not kMatches then
        error("Invalid knockout type: " .. ktype)
    end
    
    --[[
        Headers specific to each knockout type
    ]]
    
    local knockoutHeaders = {
    -- Knockout type 1 (used from 2008 to 2010)
[[
! scope="col" style="width: 32px; border-left: 4px solid #454545" | <abbr title="Semi-final">SF</abbr>
! scope="col" style="width: 32px" | <abbr title="Final">F</abbr>]],
    -- Knockout type 2 (used from 2011 onwards)
[[
! scope="col" style="width: 32px; border-left: 4px solid #454545" | <abbr title="Qualifier 1 or Eliminator">Q1/E</abbr>
! scope="col" style="width: 32px" | <abbr title="Qualifier 2">Q2</abbr>
! scope="col" style="width: 32px" | <abbr title="Final">F</abbr>]]
    
    }
    
    --[[
        Common header
    ]]
    local header = strFormat(
[[
{| class="wikitable" style="text-align: center"
! scope="col" rowspan="2" | Team
! colspan="%d" style="border-left: 4px solid #454545" | Group matches
! colspan="%d" style="border-left: 4px solid #454545" | Playoffs
|-
%s
%s
]],
    matchesPerTeam, kMatches, table.concat(groupStageHeaders), knockoutHeaders[ktype])
    
    --[[
        Common footer
    ]]
    local footer = strFormat(
[[

|}

{| class="wikitable" style="float: right; width: 20%%; text-align: center; margin-right: 5%%"
| class="yes table-yes2" style="background-color: %s" | Win
| class="no table-no2" style="background-color: %s" | Loss
| class="noresult" style="background-color: %s" | No result
|}

* '''Note''': The total points at the end of each group match are listed.
* '''Note''': Click on the points (group matches) or W/L (playoffs) to see the match summary.
]],
    colours.W, colours.L, colours.N)
    
    local body = {}
    local argCounter = 1
    
    --[[
        Formats a result
        label: The label to display in the table
        result: The result of the match (W = win, L = loss, N = no result, U = unplayed)
        match: The match number to link to
    ]]
    local formatResult = function(label, result, match)
        if result == 'U' then
            return '" | '
        end
        local style = 'background-color: ' .. colours[result] .. (first and '; border-left: 4px solid #454545' or '')
        return style .. '" class="' .. classes[result] .. '" | [[#match' .. match .. '|' .. label .. ']] '
    end

    -- Generate the table
    for i = 1, #teams do
    
        local team = teams[i]
        local listing = '|-\n! scope="row" style="text-align: left; padding-right: 10px" | [[' .. team .. ']]\n'   -- Add the team name
        local group, knockout = strSplit(args[argCounter], '%s*,%s*'), strSplit(args[argCounter + 1], '%s*,%s*')  -- Split the results at commas
        local runningScore = 0
        argCounter = argCounter + 2

        -- Check that the number of results is less than or equal to the maximum number of matches
        if #group > matchesPerTeam then
            error("Too many results for group stage (expecting no more than " .. matchesPerTeam .. ", got " .. #group .. ") (team: " .. team ..")")
        end
        if #knockout > kMatches then
            error("Too many results for knockout stage (expecting no more than " .. kMatches .. ", got " .. #knockout .. ") (team: " .. team ..")")
        end
        
        for j = 1, matchesPerTeam do
            local r = group[j]
            listing = listing .. ((j == 1) and '| style="border-left: 4px solid #444444;' or '|| style="')
            if r then
                -- If there is a result, get its identifier (first character) and match number (remaining characters)
                local result, match = r:sub(1, 1):upper(), r:sub(2)
                local p = points[result]
                if p then
                    runningScore = runningScore + p  -- Add the points to the running score
                else
                    -- If the result identifier is not in the points dictionary, it is invalid
                    error("Unsupported result: " .. r .. ", expecting 'W', 'L', 'N' or 'U' as first character (team: " .. team .. ")")
                end
                listing = listing .. formatResult(runningScore, result, match)
            else
                -- Result not given
                listing = listing .. '" | '
            end
        end
        listing = listing .. '\n'

        for j = 1, kMatches do
            local r = knockout[j]
            listing = listing .. ((j == 1) and '| style="border-left: 4px solid #444444;' or '|| style="')
            if r then
                local result, match = r:sub(1, 1):upper(), r:sub(2)
                if result == 'E' then  -- Eliminated
                    listing = listing .. 'background-color: ' .. colours.E .. '" colspan="' .. kMatches - j + 1 .. '" | '
                    if knockout[j + 1] then
                        -- Check that there are no more results after an E
                        error("'E' should be the last result in the playoff result list (team: " .. team .. ")")
                    end
                    break
                elseif not points[result] then  -- Check the points dictionary to see if the result identifier is valid
                    error("Unsupported result: " .. r .. ", expecting 'W', 'L', 'N' or 'U' as first character (team: " .. team .. ")")
                end
                listing = listing .. formatResult(result, result, match)
            else
                -- Result not given
                listing = listing .. '" | '
            end
        end

        body[i] = listing
    end
    
    return header .. table.concat(body, '\n') .. footer
    
end

return _module