Module:MLB standings

This is an old revision of this page, as edited by Isaacl (talk | contribs) at 00:51, 5 April 2013 (use full division name for standings table). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

-- This module copies content from Template:MLB_standings; see the history of that page
-- for attribution.

local me = { }

local mlbData

-- if mw.loadData() not supported, use require() instead
if mw.loadData then
    mlbData = mw.loadData('Module:Sandbox/isaacl/MLB standings/data')
else
    mlbData = require('Module:Sandbox/isaacl/MLB standings/data')
end


-- Temporary workaround for missing mw.text utilities
mw.text = mw.text or {}

if (mw.text.trim == nil) then
    mw.text.trim = function(s)
        if (s == nil) then
            return ''
        end
        return mw.ustring.match(s, "^%s*(.-)%s*$")
    end
end

local function readTeamInfo(args, currentIdx)
    if (args[currentIdx]   == nil or
        args[currentIdx+1] == nil or
        args[currentIdx+2] == nil or
        args[currentIdx+3] == nil or
        args[currentIdx+4] == nil or
        args[currentIdx+5] == nil or
        args[currentIdx+6] == nil ) then
        return nil
    end
    return {
        name     = mw.text.trim(args[currentIdx]),
        wins     = tonumber(mw.text.trim(args[currentIdx+1])),
        losses   = tonumber(mw.text.trim(args[currentIdx+2])),
        homeWins   = mw.text.trim(args[currentIdx+3]),
        homeLosses = mw.text.trim(args[currentIdx+4]),
        roadWins   = mw.text.trim(args[currentIdx+5]),
        roadLosses = mw.text.trim(args[currentIdx+6]),
    }
end  -- function readTeamInfo()


function me.generateStandingsTable(frame)
    local year = mw.text.trim(frame.args.year)
    local division = mw.text.trim(frame.args.division)
    local divisionForNavbox = division

    if (mlbData.abbreviationForDivision[division] ~= nil) then
        divisionForNavbox = mlbData.abbreviationForDivision[division]
    end

    local listOfTeams = {};
    local currentArgIdx = 1;

    local fTeamInfoPresent = false

    while (frame.args[currentArgIdx] ~= nil) do
        local teamInfo = readTeamInfo(frame.args, currentArgIdx);
        if (teamInfo == nil) then
            break
        end
        fTeamInfoPresent = true
        table.insert(listOfTeams, teamInfo)
        currentArgIdx = currentArgIdx + 7
    end

    if (not fTeamInfoPresent) then
        return ''
    end

    local outputSeq = { }

    table.insert(outputSeq,
        frame:expandTemplate{ title = 'navbar', args = {
                year .. ' ' .. divisionForNavbox .. ' standings',
                mini = 1,
            }} .. '\n'
    )
    table.insert(outputSeq,
'{| class="wikitable" width="60%" style="text-align:center;"\
! width="38%" | ' .. division .. '\
! width="7%" | [[Win (baseball)|W]]\
! width="7%" | [[Loss (baseball)|L]]\
! width="9%" | [[Winning percentage|Pct.]]\
! width="7%" | [[Games behind|GB]]\
! width="9%" | [[Home (sports)|Home]]\
! width="9%" | [[Road (sports)|Road]]\
'
    )

    local leadingHalfGames = nil;

    for idx, teamInfo in ipairs(listOfTeams) do
        local gamesBehind
        if (leadingHalfGames == nil) then
            leadingHalfGames = (teamInfo.wins - teamInfo.losses)
            gamesBehind = '—'
        else
            gamesBehind = (leadingHalfGames
                - (teamInfo.wins - teamInfo.losses)) / 2
        end

        table.insert(outputSeq,
'|-\
| [[' .. year .. ' ' .. teamInfo.name .. ' season|' .. teamInfo.name .. ']]\
|| ' .. teamInfo.wins .. ' || ' .. teamInfo.losses .. '\
|| ' .. string.format('%.3f', teamInfo.wins / ( teamInfo.wins + teamInfo.losses )) .. '\
|| ' .. gamesBehind .. '\
|| ' .. teamInfo.homeWins .. '–' .. teamInfo.homeLosses ..'\
|| ' .. teamInfo.roadWins .. '–' .. teamInfo.roadLosses .. '\n'
        )
    end

    table.insert(outputSeq, '|}\n')

    return table.concat(outputSeq)

end  -- function me.generateStandingsTable()

function me.generateStandingsTable_fromTemplate(frame)
    return me.generateStandingsTable(frame:getParent())
end  -- function me.generateStandingsTable_fromTemplate()

return me