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 05:01, 7 April 2013 (add additional local variable to improve legibility). 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:MLB standings/data')
else
    mlbData = require('Module:MLB standings/data')
end

local Navbar = require('Module:Navbar')

-- Temporary workaround for missing mw.text utility functions
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

if (mw.text.gsplit == nil) then
    mw.text.gsplit = function( text, pattern, plain )
        local s, l = 1, mw.ustring.len( text )
        return function ()
            if s then
                local e, n = mw.ustring.find( text, pattern, s, plain )
                local ret
                if not e then
                    ret = mw.ustring.sub( text, s )
                    s = nil
                elseif n < e then
                    -- Empty separator!
                    ret = mw.ustring.sub( text, s, e )
                    if e < l then
                        s = e + 1
                    else
                        s = nil
                    end
                else
                    ret = e > s and mw.ustring.sub( text, s, e - 1 ) or ''
                    s = n + 1
                end
                return ret
            end
        end, nil, nil
    end
end

if (mw.text.split == nil) then
    mw.text.split = function ( text, pattern, plain )
        local ret = {}
        for m in mw.text.gsplit( text, pattern, plain ) do
            ret[#ret+1] = m
        end
        return ret
    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 ) then
        return nil
    end
    teamInfo = {
        name     = mw.text.trim(args[currentIdx]),
        homeWins   = tonumber(mw.text.trim(args[currentIdx+1])),
        homeLosses = tonumber(mw.text.trim(args[currentIdx+2])),
        roadWins   = tonumber(mw.text.trim(args[currentIdx+3])),
        roadLosses = tonumber(mw.text.trim(args[currentIdx+4])),
    }
    teamInfo.wins = teamInfo.homeWins + teamInfo.roadWins
    teamInfo.losses = teamInfo.homeLosses + teamInfo.roadLosses
    return teamInfo
end  -- function readTeamInfo()

local function parseSeeds(seedsArg, seeds)
    local seedList = mw.text.split(seedsArg, '%s*,%s*')
    if (#seedList == 0) then
        return
    end

    for idx, seed in ipairs(seedList) do
        local seedData = mw.text.split(seed, '%s*:%s*')
        if (#seedData >= 2) then
            local seedNumber = tonumber(mw.text.trim(seedData[1]))
            local team = mw.text.trim(seedData[2])
            seeds[seedNumber] = team
            seeds[team] = seedNumber
        end
    end
end  -- function parseSeeds()

local function parseHighlightArg(highlightArg, teamsToHighlight)
    local teamList = mw.text.split(highlightArg, '%s*,%s*')
    if (#teamList == 0) then
        return
    end

    for idx, team in ipairs(teamList) do
        teamsToHighlight[mw.text.trim(team)] = true
    end

end  -- function parseHighlightArg

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 standingsPage = year .. ' ' .. divisionForNavbox .. ' standings'

    local seedInfo = {}
    if (frame.args.seeds ~= nil) then
        parseSeeds(frame.args.seeds, seedInfo)
    end

    local teamsToHighlight = {}
    if (frame.args.highlight ~= nil) then
        parseHighlightArg(frame.args.highlight, teamsToHighlight)
    end

    local listOfTeams = {};
    local currentArgIdx = 1;

    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 + 5
    end

    if (#listOfTeams == 0) then
        return ''
    end

    local outputBuffer = { }


    local navbarText = 
      Navbar.navbar({
          standingsPage,
          mini = 1,
          style = 'float:left;width:0;',
      })

    table.insert(outputBuffer,
'{| class="wikitable" width="515em" style="text-align:center;"\
! width="50%" |' .. navbarText .. division .. '\
! width="7%" | [[Win (baseball)|W]]\
! width="7%" | [[Loss (baseball)|L]]\
! width="9%" | [[Winning percentage|Pct.]]\
! width="7%" | [[Games behind|GB]]\
! width="10%" | [[Home (sports)|Home]]\
! width="10%" | [[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
            local halfGamesBehind = leadingHalfGames - (teamInfo.wins - teamInfo.losses)
            gamesBehind = math.floor(halfGamesBehind / 2)
            if (halfGamesBehind % 2 == 1) then
                gamesBehind = gamesBehind .. '½'
            end

        end

        local teamSeasonPage = year .. ' ' .. teamInfo.name .. ' season'
        local winningPercentage = string.format(
            '%.3f', teamInfo.wins / ( teamInfo.wins + teamInfo.losses )
        )

        local seedText = ''
        local rowStyle = ''
        if (seedInfo[teamInfo.name] ~= nil) then
            seedText = '<sup>(' .. seedInfo[teamInfo.name] .. ')</sup> '
            rowStyle = ' style="background:#CCFFCC"'
        end

        if (teamsToHighlight[teamInfo.name]) then
            rowStyle =  ' style="background:#CCFFCC"'
        end

        table.insert(outputBuffer,
'|-' .. rowStyle .. '\
|| ' .. seedText .. '[[' .. teamSeasonPage .. '|' .. teamInfo.name .. ']]\
|| ' .. teamInfo.wins .. ' || ' .. teamInfo.losses .. '\
|| ' .. winningPercentage .. '\
|| ' .. gamesBehind .. '\
|| ' .. teamInfo.homeWins .. '–' .. teamInfo.homeLosses ..'\
|| ' .. teamInfo.roadWins .. '–' .. teamInfo.roadLosses .. '\n'
        )
    end  -- end of looping over listOfTeams

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

    return table.concat(outputBuffer)

end  -- function me.generateStandingsTable()

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

return me