Jump to content

Module:CricketLeagueGroupStageSummary/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by SocietyBox (talk | contribs) at 00:47, 19 April 2014 (check). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p = {}

---------- Background colours for table cells ----------
local colours = {
	H = "#CCCCFF", -- Home team wins
	A = "#FFCCCC", -- Away team wins
	N = "#FFDEAD", -- Match abandoned
	D = "#F0E68C", -- Match drawn
	T = "#DDFFDD"  -- Match tied
}

local noMatchColour = "#C0C0C0"     -- No match defined
local notPlayedColour = "inherit"   -- Not played yet
local errorColour = "#FF7777"       -- Error

function trim(s)
	if not s then
		return nil
	else
		return (mw.ustring.gsub(s, "^%s*(.-)%s*$", "%1"))
	end
end

function getArgs(frame)
    local parent = frame:getParent();
    local args = {}
    for k,v in pairs(parent.args) do
        args[k] = trim(v)
    end
    for k,v in pairs(frame.args) do 
        args[k] = trim(v)
    end
    return args;
end

--
--   Match class
--
local cricmatch = {}

cricmatch.__index = function(t, key)
    local ret = rawget(t, key)
    if ret then
        return ret
    end
    ret = cricmatch[key]
    if type(ret) == 'function' then
        return function(...) 
            return ret(t, ...) 
        end 
    else
        return ret
    end
end

cricmatch.getMarginResult = function(m, row)
	local team = m.result == 'H'  and m.home or m.away
	local marginText
	if m.margin == 'F' then
		marginText = "Forfeited"
	elseif m.margin == 'SO' then
		marginText = "Super Over"
	else
        local n = tonumber(string.sub(m.margin, 1, -2))
        local t = string.upper(string.sub(m.margin, -1, -1))
        if t == 'R' then
        	marginText = "%d run"
        elseif t == 'W' then
        	marginText = "%d wicket"
        elseif t == 'I' then
        	marginText = "Inns & %d run"
    	end
    	if marginText and n then
    		marginText = string.format(marginText, n)
	        if n > 1 then marginText = marginText .. "s" end
        else
        	marginText = string.format("Match %d", m.id)
        end
        if m.dl then
        	marginText = marginText
        		.. ' <span style="font-size: 85%">(D/L)</span>'
		end
    end
	return addTableCell(row, colours[m.result],
        	string.format('%s<br />[[#match%s|%s]]',
        		team.shortName, m.id, marginText))
    	.css('padding', '3px 5px')
end

cricmatch.getResult = function(m, row)
    if m.result == 'D' then
        -- Drawn match
        return addTableCell(row, colours.D,
        		string.format('[[#match%s|Match drawn]]', m.id))
            .css('padding', '3px 5px')
            .css('line-height', '300%')
    elseif m.result == 'N' then
        -- Abandoned match
        return addTableCell(row, colours.N,
        		string.format('[[#match%s|Match<br />abandoned]]', m.id))
            .css('padding', '3px 5px')
    elseif m.result == 'T' then
        -- Tied match
        return addTableCell(row, colours.T,
        		string.format('[[#match%s|Match tied]]', m.id))
            .css('padding', '3px 5px')
    elseif m.result == 'H' or m.result == 'A' then
    	return m.getMarginResult(row)
    else
        return addTableCell(row, notPlayedColour,
        		string.format('[[#match%s|Match %s]]', m.id, m.id))
            .css('padding', '3px 5px')
            .css('line-height', '300%')
    end
end

function createMatch(id, home, away, result, margin, dl)
    if not home or not away then
    	return nil
	end
    local match = {}
    setmetatable(match, cricmatch)
    match.id = id
    match.home = home
    match.away = away
    match.result = result
    match.margin = margin
    match.dl = dl
    return match
end

--
--   Html Builder helpers
--
local htmlBuilder = require('Module:HtmlBuilder')
function addTableRow(tbl)
    return tbl.tag('tr')
end
function addTableCell(row, bg, text)
    return row.tag('td').css('background-color', bg).wikitext(text)
end
function addNoMatch(row)
    addTableCell(row, noMatchColour)
    return row
end

--
--   Helper functions
-- 
function buildLegend(container, types)
    local key = container.tag('table')
        .addClass('wikitable')
        .css('float', 'right')
        .css('text-align', 'center')
        .css('font-size', '90%')
        .css('margin', '0 0 0 10px')

    local keys = { 'H', 'A', 'D', 'N', 'T' }
    local text = {
		H = 'Home team won',
		A = 'Visitor team won',
		D = 'Match drawn',
		N = 'Match abandoned',
		T = 'Match tied'
	}
	local count = 0
	for _, _ in pairs(types) do count = count + 1 end
    local row = addTableRow(key)
	for _, k in ipairs(keys) do
		if types[k] then addTableCell(row, colours[k], text[k]) end
    end

    local list = container.tag('ul')
        .css('font-size', '90%')
        .tag('li')
            .wikitext("'''Note''': Results listed are according to the " ..
            	"home (horizontal) and visitor (vertical) teams.")
        .done()
        .tag('li')
            .wikitext("'''Note''': Click on a result to see " ..
            	"a summary of the match.")
        .done()
    return container
end

function getMatchData(args, teams)
    local i, m = 0, 1
    local match
    local matches = {}
    local home, away, result, margin, dl
    while args[i * 5 + 5] do
        home = teams[trim(args[i * 5 + 1])]
        away = teams[args[i * 5 + 2]]
        result = args[i * 5 + 3]
        margin = args[i * 5 + 4]
        dl = args[i * 5 + 5] == "Y"
        match = createMatch(m, home, away, result, margin, dl)
        if match then
        	table.insert(matches, match)
        	m = m + 1
    	end
    	i = i + 1
    end
    return matches
end

p.create = function(args, teams, tableStyle)
    local matches = getMatchData(args, teams)

	-- organise by team
    local codes, results, types = {}, {}, {}
    for i, match in ipairs(matches) do
    	local home = match.home.code
    	local away = match.away.code
    	if not results[home] then
	        table.insert(codes, home)
	        results[home] = {}
		end
    	if not results[away] then
	        table.insert(codes, away)
	        results[away] = {}
		end
        results[home][away] = match
        types[match.result] = true
    end
	local teamsort = function(t1, t2)
		return teams[t1].fullName < teams[t2].fullName
	end
    table.sort(codes, teamsort)

	local wrapper = htmlBuilder.create('div')

    -- Construct the header
    local container = wrapper.tag('div')
    local tbl = container.tag('table').attr('class', 'wikitable')
    if tableStyle then
    	tbl.cssText(tableStyle)
	else
		tbl.css('text-align', 'center')
    		.css('white-space', 'nowrap')
    		.css('width', '100%')
    	if #codes > 8 then
    		tbl.css('font-size', (100 - (#codes - 8) * 10) .. '%')
    	end
	end
    local header = addTableRow(tbl)
        .tag('th')
            .attr('scope', 'row')
            .wikitext('Visitor team →')
        .done()
    for i, code in ipairs(codes) do
        local team = teams[code]
        header.tag('th')
            .attr('rowspan', '2')
            .attr('scope', 'col')
            .css('padding', 'inherit 10px')
            .wikitext(string.format('[[%s|%s]]', team.pageName, team.code))
            .newline()
    end
    tbl.tag('tr').tag('th').attr('scope', 'col').wikitext('Home team ↓')

    -- Output the main body of the table
    for i, homecode in ipairs(codes) do
        local home = teams[homecode]
        local row = addTableRow(tbl)
        local teamcell = row.tag('th')
            .attr('scope', 'row')
            .css('text-align', 'left')
            .css('padding', '3px 5px')
            .css('white-space', 'normal')
            .wikitext(string.format('[[%s|%s]]', home.pageName, home.fullName))
        for j, awaycode in ipairs(codes) do
            local match = results[homecode][awaycode]
            if match then match.getResult(row) else addNoMatch(row) end
        end
    end

    -- Legend and notes
    buildLegend(container, types)
    wrapper.tag('div').css('clear', 'both')
    return tostring(wrapper)
end

p.IPL = function(frame)
    local args = getArgs(frame)
    local teams = mw.loadData("Module:IndianPremierLeague/Teams")
    local teamsAssoc = {}
    local i = 1
    while teams[i] do
        teamsAssoc[teams[i].code] = teams[i]
        i = i + 1
    end
    return p.create(args, teamsAssoc)
end

return p