Difference between revisions of "Module:NLLDivisionStanding"
Jump to navigation
Jump to search
blackwiki>Mr. Stradivarius (start work on converting Template:NLLDivisionStanding) |
blackwiki>Mr. Stradivarius (flesh out the Team class) |
||
| Line 92: | Line 92: | ||
table.insert(clinches, 'z') | table.insert(clinches, 'z') | ||
end | end | ||
| − | if | + | if clinches[1] then |
ret = string.format("%s – '''%s'''", ret, table.concat(clinches)) | ret = string.format("%s – '''%s'''", ret, table.concat(clinches)) | ||
end | end | ||
return ret | return ret | ||
| + | end | ||
| + | |||
| + | function Team:getHomeWins() | ||
| + | return self.home_win or 0 | ||
| + | end | ||
| + | |||
| + | function Team:getHomeLosses() | ||
| + | return self.home_loss or 0 | ||
| + | end | ||
| + | |||
| + | function Team:getRoadWins() | ||
| + | return self.road_win or 0 | ||
| + | end | ||
| + | |||
| + | function Team:getRoadLosses() | ||
| + | return self.road_loss or 0 | ||
end | end | ||
function Team:getGamesPlayed() | function Team:getGamesPlayed() | ||
| − | return self | + | return self:getHomeWins() + |
| + | self:getRoadWins() + | ||
| + | self:getHomeLosses() + | ||
| + | self:getRoadLosses() | ||
end | end | ||
function Team:getWins() | function Team:getWins() | ||
| − | return self | + | return self:getHomeWins() + self:getRoadWins() |
end | end | ||
function Team:getLosses() | function Team:getLosses() | ||
| − | return self | + | return self:getHomeLosses() + self:getRoadLosses() |
| + | end | ||
| + | |||
| + | function Team:_divideByGamesPlayed(val) | ||
| + | local gp = self:getGamesPlayed() | ||
| + | if gp > 0 then -- avoid divide-by-zero error | ||
| + | return val / gp | ||
| + | else | ||
| + | return 0 | ||
| + | end | ||
end | end | ||
function Team:getWinPercentage() | function Team:getWinPercentage() | ||
| − | return | + | local percent = self_divideByGamesPlayed(self:getWins()) |
| + | if percent > 1 then | ||
| + | percent = 1 | ||
| + | elseif percent < 0 then | ||
| + | percent = 0 | ||
| + | end | ||
| + | local ret = string.format('%.3f', percent) | ||
| + | if ret:sub(1, 1) == '0' then | ||
| + | -- Use strings like .123 instead of 0.123 as that is how it's done | ||
| + | -- in sports publications | ||
| + | ret = ret:sub(2, -1) | ||
| + | end | ||
| + | return ret | ||
end | end | ||
| − | function Team:getGamesBack() | + | function Team:getGamesBack(teamInFirst) |
| − | return | + | local tifDiff = teamInFirst:getWins() - teamInfirst:getLosses() |
| + | local selfDiff = self:getWins() - self:getLosses() | ||
| + | return string.format('%.1f', (tifDiff - selfDiff) / 2) | ||
end | end | ||
function Team:getHomeRecord() | function Team:getHomeRecord() | ||
| − | return self | + | return self:getHomeWins() .. '–' .. self:getHomeLosses() |
end | end | ||
function Team:getRoadRecord() | function Team:getRoadRecord() | ||
| − | return self | + | return self:getRoadWins() .. '–' .. self:getRoadLosses() |
end | end | ||
function Team:getGoalsScored() | function Team:getGoalsScored() | ||
| − | return self | + | return self.gf or 0 |
end | end | ||
function Team:getGoalsAllowed() | function Team:getGoalsAllowed() | ||
| − | return self | + | return self.ga or 0 |
end | end | ||
function Team:getDifferential() | function Team:getDifferential() | ||
| − | return | + | local diff = self:getGoalsScored() - self:getGoalsAllowed() |
| + | if diff > 0 then | ||
| + | return '+' .. tostring(diff) | ||
| + | else | ||
| + | return tostring(diff) | ||
| + | end | ||
end | end | ||
function Team:getGameScoredAverage() | function Team:getGameScoredAverage() | ||
| − | return | + | local avg = self:_divideByGamesPlayed(self:getGoalsScored()) |
| + | return string.format('%.2f', avg) | ||
end | end | ||
function Team:getGameAllowedAverage() | function Team:getGameAllowedAverage() | ||
| − | return | + | local avg = self:_divideByGamesPlayed(self:getGoalsAllowed()) |
| + | return string.format('%.2f', avg) | ||
end | end | ||
Revision as of 06:11, 31 March 2016
-- This module implements {{NLLDivisionStanding}}.
local mTableTools = require('Module:TableTools')
-------------------------------------------------------------------------------
-- Helper functions
-------------------------------------------------------------------------------
local function abbr(shortForm, longForm)
return tostring(mw.html.new('abbr')
:attr('title', longForm)
:wikitext(shortForm)
)
end
-------------------------------------------------------------------------------
-- Team class
-------------------------------------------------------------------------------
local Team = {}
Team.__index = Team
Team.stringFields = {
'name',
'link',
'short',
}
Team.numberFields = {
'pos',
'clinch_playoff',
'clinch_division',
'clinch_best_record',
'ga',
'gf',
'home_loss',
'home_win',
'road_loss',
'road_win',
}
function Team.new(options)
options = options or {}
local self = setmetatable({}, Team)
for i, field in ipairs(Team.stringFields) do
self[field] = options[field]
end
for i, field in ipairs(Team.numberFields) do
self[field] = tonumber(options[field])
end
return self
end
function Team:getPosition()
return tostring(self.pos) or '--'
end
function Team:getName()
return self.name
end
function Team:getLink()
local name = self:getName()
local link = self.link
if link and name then
return string.format('[[%s|%s]]', link, name)
elseif link then
return string.format('[[%s]]', link)
else
return name
end
end
function Team:makeDisplayName()
local ret = self:getLink()
if not ret then
return nil
end
local clinches = {}
-- The numerical syntax here is a hangover from the wikitext template
-- which used #expr hacks to calculate the number of clinches
if self.clinch_playoff == 1 then
table.insert(clinches, 'x')
end
if self.clinch_playoff == 2 then
table.insert(clinches, 'c')
end
if self.clinch_division == 1 then
table.insert(clinches, 'y')
end
if self.clinch_best_record == 1 then
table.insert(clinches, 'z')
end
if clinches[1] then
ret = string.format("%s – '''%s'''", ret, table.concat(clinches))
end
return ret
end
function Team:getHomeWins()
return self.home_win or 0
end
function Team:getHomeLosses()
return self.home_loss or 0
end
function Team:getRoadWins()
return self.road_win or 0
end
function Team:getRoadLosses()
return self.road_loss or 0
end
function Team:getGamesPlayed()
return self:getHomeWins() +
self:getRoadWins() +
self:getHomeLosses() +
self:getRoadLosses()
end
function Team:getWins()
return self:getHomeWins() + self:getRoadWins()
end
function Team:getLosses()
return self:getHomeLosses() + self:getRoadLosses()
end
function Team:_divideByGamesPlayed(val)
local gp = self:getGamesPlayed()
if gp > 0 then -- avoid divide-by-zero error
return val / gp
else
return 0
end
end
function Team:getWinPercentage()
local percent = self_divideByGamesPlayed(self:getWins())
if percent > 1 then
percent = 1
elseif percent < 0 then
percent = 0
end
local ret = string.format('%.3f', percent)
if ret:sub(1, 1) == '0' then
-- Use strings like .123 instead of 0.123 as that is how it's done
-- in sports publications
ret = ret:sub(2, -1)
end
return ret
end
function Team:getGamesBack(teamInFirst)
local tifDiff = teamInFirst:getWins() - teamInfirst:getLosses()
local selfDiff = self:getWins() - self:getLosses()
return string.format('%.1f', (tifDiff - selfDiff) / 2)
end
function Team:getHomeRecord()
return self:getHomeWins() .. '–' .. self:getHomeLosses()
end
function Team:getRoadRecord()
return self:getRoadWins() .. '–' .. self:getRoadLosses()
end
function Team:getGoalsScored()
return self.gf or 0
end
function Team:getGoalsAllowed()
return self.ga or 0
end
function Team:getDifferential()
local diff = self:getGoalsScored() - self:getGoalsAllowed()
if diff > 0 then
return '+' .. tostring(diff)
else
return tostring(diff)
end
end
function Team:getGameScoredAverage()
local avg = self:_divideByGamesPlayed(self:getGoalsScored())
return string.format('%.2f', avg)
end
function Team:getGameAllowedAverage()
local avg = self:_divideByGamesPlayed(self:getGoalsAllowed())
return string.format('%.2f', avg)
end
-------------------------------------------------------------------------------
-- DivisionStanding class
-------------------------------------------------------------------------------
local DivisionStanding = {}
DivisionStanding.__index = DivisionStanding
function DivisionStanding.new(options)
local self = setmetatable({}, DivisionStanding)
return self
end
-------------------------------------------------------------------------------
-- Exports
-------------------------------------------------------------------------------
local p = {}
function p._main(args)
end
function p.main(frame)
local args = require('Module:Arguments').getArgs(frame, {
wrappers = 'Template:NLLDivisionStanding'
})
return p._main(args)
end
return p