Jump to content

Module:CricketLeagueGroupStageSummary

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Jfd34 (talk | contribs) at 09:43, 6 April 2014 (Created page with 'local _module = {} _module.create = function(frame) local teams = require("Module:IndianPremierLeague/Teams") ---------- Functions ----------...'). 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)

local _module = {}

_module.create = function(frame)
    
    local teams = require("Module:IndianPremierLeague/Teams")
    
    ---------- Functions ----------
    local strSplit = mw.text.split
    local strGsplit = mw.text.gsplit
    local strTrim = mw.text.trim
    local strFind = string.find
    local strFormat = string.format
    local strSub = string.sub
    local strUpper = string.upper
    
    ---------- Arguments ----------
    local args = frame.args
    local year = tonumber(args.year or error("Missing parameter: 'year'"))
    
    -- 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 = {}
    
    -- 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
    }
    
    --[[
        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 = teams[name]
        if not t or t.startYear > year or (t.endYear and year > t.endYear) then
            error(strFormat("Team '%s' does not exist.", name))
        end
        return t
    end
    
    -- Parse the rows
    for i, row in ipairs(args) do
        local match = {}
        -- Key/value pairs for each row are separated by semicolons, and a colon separates the key from the value.
        for _i, p in strGsplit(row, "%s*;%s*") do
            local colon = strFind(p, ':')
            if not colon then   -- No colon found
                error(strFormat("Expecting ':' after '%s'. (parameter #%d)", p, i))
            end
            match[strSub(row, 0, colon - 1)] = strSub(row, colon + 1, -1)
        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(strFormat("Invalid match (home and away teams are equal). (parameter #%d)", i))
        elseif not id or id <= 0 or id % 1 ~= 0 then     -- The match number is not an integer greater than zero
            error(strFormat("Match number must be an integer greater than 0. (parameter #%d)", i))
        elseif matchIDs[id] then    -- A match with the given match number is already defined
            error(strFormat("A match with the match number %d has been previously defined. (parameter #%d)", id, i))
        end
        
        matchIDs[id] = true
        matches[home][away] = match
    end
    
    local sortedTeams = {}
    do
        -- Collect all the valid teams for the given year and sort them in alphabetical order
        local teamSorter = function(t1, t2)
            return t1.fullName < t2.fullName
        end
        local i = 1
        for _k, v in pairs(teams) do
            if v.startYear <= year and (not v.endYear or year <= v.endYear) then
                sortedTeams[i] = v
                i = i + 1
            end
        end
        table.sort(sortedTeams, teamSorter)
    end
    
    -- Construct the header
    local visitorHeaders = ''
    for i = 1, #sortedTeams do
        local team = sortedTeams[i]
        visitorHeaders = visitorHeaders .. strFormat('! rowspan="2" scope="col" | [[%s|%s]]\n', team.pageName, team.code)
    end
    
    local header = strFormat([[
%s
%s
{| class="wikitable" style="text-align: center; white-space: nowrap"
! scope="row" | Visitor team &#x2192;
%s
|-
! scope="col" | Home team &#x2193;
]],
    frame:expandTemplate({ title = "WebSlice-begin", args = { id = "1", title = year .. " IPL Group Stage Table" } }),
    frame:expandTemplate({ title = "stack begin", args = { float = "left", clear = "true" } }),
    year, visitorHeaders)
    
    --[[
        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 a string other than '0'.
        isSuperOver: If the result is decided by a super over, this should be a string other than '0'.
    ]]
    local getMarginString = function(id, margin, isDL, isSuperOver)
        if margin == 'F' then return "Forfeited"
        elseif isSuperOver ~= '0' 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.")) end
            
            if t == 'R' then
                return strFormat("%d runs%s", n, isDL ~= '0' and '<span style="font-size: 85%">D/L</span>' or "")
            elseif t == 'W' then
                return strFormat("%d wickets%s", n, isDL ~= '0' 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'."))
            end
        end
    end
    
    -- Output the main body of the table
    
    local content = ''
    
    for i = 1, #sortedTeams do
        -- Each row starts with a home team header
        local home = sortedTeams[i]
        local row = strFormat('|-\n! scope="row" style="text-align: left; white-space: normal" | [[%s|%s]]\n', home.pageName, home.fullName)
        
        for j = 1, #sortedTeams do
            local away = sortedTeams[j]
            local match = matches[home][away]
            
            if match then
                local id = match.match
                if result == 'H' then   -- Home team wins
                    row = row .. strFormat('| style="background-color: %s; padding: 0px 4px" | %s<br />[[#match%s|%s]]', colours.H, home.shortName, id, getMarginString(id, match.margin, match.dl, match.superover))
                elseif result == 'A' then     -- Away team wins
                    row = row .. strFormat('| style="background-color: %s; padding: 0px 4px" | %s<br />[[#match%s|%s]]', colours.H, away.shortName, id, getMarginString(id, match.margin, match.dl, match.superover))
                elseif result == 'X' then     -- Match has not been played yet
                    row = row .. strFormat('| style="background-color: %s; padding: 0px 4px; line-height: 225%" | [[#match%s|Match %s]]\n', colours.X, id, id)
                elseif result == 'N' then     -- Abandoned match
                    row = row .. strFormat('| style="background-color: %s; padding: 0px 4px; line-height: 225%" | [[#match%s|Match abandoned]]\n', colours.X, id)
                elseif result == 'T' then     -- Tied match
                    row = row .. strFormat('| style="background-color: %s; padding: 0px 4px; line-height: 225%" | [[#match%s|Match tied]]\n', colours.X, id)
                else  -- Invalid result
                    error(strFormat("Match %d: Invalid result: '%s'. Expected: H, A, X, N or T."))
                end
            else
                -- The match is not defined.
                row = row .. strFormat('| style="background-color: %s" |\n', noMatchColour)
            end
        end
        
        content = content .. row
    end
    
    -- Legend and notes
    local footer = strFormat([[
|}
{| class="wikitable" style="float:right; text-align: center; font-size: 90%"
| style="background-color: %s; width: 25%" | Home team won
| style="background-color: %s; width: 25%" | Visitor team won
| style="background-color: %s; width: 25%" | Match abandoned
| style="background-color: %s; width: 25%" | 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>
%s
<div style="clear: both" />
%s
]],
    colours.H, colours.A, colours.N, colours.T,
    frame:expandTemplate({ title = 'stack end', args = {} }),
    frame:expandTemplate({ title = 'WebSlice-end', args = {} }))
    
    return header .. content .. footer
    
end

return _module