Jump to content

Module:MLB standings

Permanently protected module
From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Isaacl (talk | contribs) at 23:08, 4 April 2013 (copied content from Template:MLB standings; see that article's history for attribution). 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)

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

local me = { }

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) then
        return nil
    end
    return {
        name     = args[currentIdx],
        wins     = args[currentIdx+1],
        losses   = args[currentIdx+2],
        homeRecord = args[currentIdx+3],
        roadRecord = args[currentIdx+4],
    }
end  -- function readTeamInfo()


function me.generateStandingsTable(frame)
    local year = frame.args.year
    local division = frame.args.division

    local listOfTeams = {};
    local currentArgIdx = 1;

    local fTeamInfoPresent = false

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

    if (not fTeamInfoPresent) then
        return ''
    end

    local outputSeq = { }

    table.insert(outputSeq,
        '{{navbar|' .. year .. ' ' .. division .. ' standings|mini=1}}\
{| 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]]\n'
    )

    local leadingHalfGames = 0;

    for idx, teamInfo in ipairs(listOfTeams) do
        local gamesBehind
        if (leadingHalfGames == 0) 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.homeRecord .. ' || ' .. teamInfo.roadRecord .. '\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