Module:CricketLeagueProgression/sandbox
Appearance
![]() | This is the module sandbox page for Module:CricketLeagueProgression (diff). |
local p = {}
---------- Background colours for table cells ----------
local colours = {
W = "#99FF99",
L = "#FFDDDD",
N = "#DFDFFF",
D = "#F0E68C",
T = "#DDFFDD"
}
local classes = {
W = "yes table-yes2",
L = "no table-no2",
N = "noresult"
}
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.render = function(m, row, team, points)
local cell = row.tag('td')
local home = m.home == team
local result = m.result
local gained = 0
if m.result == 'H' then
result = home and 'W' or 'L'
gained = home and 2 or 0
elseif m.result == 'A' then
result = home and 'L' or 'W'
gained = home and 0 or 2
elseif m.result == 'N' or m.result == 'T' then
gained = 1
end
points = points + gained
cell
.css('background-color', colours[result])
.wikitext(string.format('[[#match%d|%d]]', m.id, points))
if classes[cell] then cell.attr('class', classes[cell]) end
return points
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)
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)
local matches = getMatchData(args, teams)
local wrapper = htmlBuilder.create('div')
local codes, results = {}, {}
local count = 0
for _, 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
table.insert(results[home], match)
table.insert(results[away], match)
count = math.max(count, #results[home], #results[away])
end
local teamsort = function(t1, t2)
return teams[t1].fullName < teams[t2].fullName
end
table.sort(codes, teamsort)
local tbl = wrapper.tag('table')
.attr('class', 'wikitable')
.css('text-align', 'center')
-- headers
tbl.tag('tr')
.tag('th').attr('rowspan', '2').wikitext('Team').done()
.tag('th').attr('colspan', count).wikitext('Group matches').done()
local row = tbl.tag('tr')
for i = 1, count do
row.tag('th').css('width', '18px').wikitext(i)
end
-- matches
for _, code in ipairs(codes) do
renderTeam(tbl, count, teams[code], results[code])
end
return tostring(wrapper)
end
function renderTeam(tbl, count, team, matches)
local row = tbl.tag('tr')
.tag('th')
.css('text-align', 'left')
.wikitext(string.format('[[%s|%s]]', team.pageName, team.fullName))
.done()
local points = 0
for i = 1, count do
points = matches[i].render(row, team, points)
end
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