HSN Limmie Tonight Wiki
Advertisement

-- Last updated: 19:09, August 9, 2014 (UTC)

--

-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
-- 
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
-- 
-- You should have received a copy of the GNU General Public License
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.

if not unpack then
    unpack = table.unpack
end

function initialize()
    dofile( "data.lua" )
    for week, info in ipairs( data.schedule ) do
        if not info.games[1].ending then
            thisweek = week
            break
        end
    end
    if thisweek == nil then
        timer = os.clock()
        io.write( 'Season complete! No more games to roll.\n' )
        killProgram()
    end
    playoffMode = data.schedule[thisweek].playoffs
    local weekTitle = data.schedule[thisweek].title:gsub( "–", "-" )
    io.write( "Current week: ", weekTitle, "\n\n" )
    for _, game in ipairs( data.schedule[thisweek].games ) do
        if game.title then
            io.write( game.title, "\n" )
        end
        if game.location then
            io.write( game.location, "\n" )
        end
        io.write( "- ", game.away.team )
        if game.location then
            io.write( " vs. " )
        else
            io.write( " at " )
        end
        io.write( game.home.team, "\n" )
    end
    io.write( "\nEnter bonus rolls:\n" )
    for _, game in ipairs( data.schedule[thisweek].games ) do
        while true do
            io.write( game.away.team, ": " )
            game.away.bonus = tonumber( io.read() )
            if ( game.away.bonus == 25 ) or ( game.away.bonus == 28 ) or ( game.away.bonus == 30 ) or ( game.away.bonus == 32 ) then
                break
            else
                io.write( "Unrecognized input! Accept value? " )
                local input = io.read()
                if input == "yes" then
                    break
                else
                    io.write( "Value rejected!\n" )
                end
            end
        end
        while true do
            io.write( game.home.team, ": " )
            game.home.bonus = tonumber( io.read() )
            if ( game.home.bonus == 25 ) or ( game.home.bonus == 28 ) or ( game.home.bonus == 30 ) or ( game.home.bonus == 32 ) then
                break
            else
                io.write( "Unrecognized input! Accept value? " )
                local input = io.read()
                if input == "yes" then
                    break
                else
                    io.write( "Value rejected!\n" )
                end
            end
        end
    end
    timer = os.clock()
    if playoffMode > 0 then
        local seasonEnd = thisweek - playoffMode + 1
        playoffWeeks = {}
        for offset = 1, 4 do
            playoffWeeks[#playoffWeeks + 1] = data.schedule[seasonEnd + offset]
        end
        playoffGames = {}
        for _, week in ipairs(playoffWeeks) do
            playoffGames[#playoffGames + 1] = week.games
        end
    end
end

function rollScores()
    math.randomseed( os.time() )
    for _, game in ipairs( data.schedule[thisweek].games ) do
        rollRegulationScores( game )
        if not game.ending then
            rollOvertimeScores( game )
        end
        if not game.ending then
            rollShootoutScores( game )
        end
    end
end

function rollRegulationScores( game )
    game.away.regulation = math.random( 0, game.away.bonus )
    game.home.regulation = math.random( 0, game.home.bonus )
    if game.away.regulation ~= game.home.regulation then
        game.ending = "regulation"
        if playoffMode <= 1 then
            data.schedule[thisweek].teams[game.away.team].scored = game.away.regulation
            data.schedule[thisweek].teams[game.away.team].allowed = game.home.regulation
            data.schedule[thisweek].teams[game.home.team].scored = game.home.regulation
            data.schedule[thisweek].teams[game.home.team].allowed = game.away.regulation
        end
        if game.away.regulation > game.home.regulation then
            if playoffMode <= 1 then
                data.schedule[thisweek].teams[game.away.team].winner = true
                data.schedule[thisweek].teams[game.home.team].winner = false
            end
            game.winner = game.away.team
            if playoffMode >= 2 then
                game.winnerSeed = game.away.seed
                game.loser = game.home.team
                game.loserSeed = game.home.seed
            end
        else
            if playoffMode <= 1 then
                data.schedule[thisweek].teams[game.home.team].winner = true
                data.schedule[thisweek].teams[game.away.team].winner = false
            end
            game.winner = game.home.team
            if playoffMode >= 2 then
                game.winnerSeed = game.home.seed
                game.loser = game.away.team
                game.loserSeed = game.away.seed
            end
        end
    end
end

function rollOvertimeScores( game )
    game.away.overtime = game.away.regulation
    game.home.overtime = game.home.regulation
    local z = math.random( 1, 4 )
    if z == 1 then
        game.home.overtime = game.home.overtime + 3
        game.ending = "goal"
    elseif z == 2 then
        game.away.overtime = game.away.overtime + 3
        game.ending = "goal"
    end
    if playoffMode <= 1 then
        data.schedule[thisweek].teams[game.away.team].scored = game.away.overtime
        data.schedule[thisweek].teams[game.away.team].allowed = game.home.overtime
        data.schedule[thisweek].teams[game.home.team].scored = game.home.overtime
        data.schedule[thisweek].teams[game.home.team].allowed = game.away.overtime
    end
    if game.away.overtime > game.home.overtime then
        if playoffMode <= 1 then
            data.schedule[thisweek].teams[game.away.team].winner = true
            data.schedule[thisweek].teams[game.home.team].winner = false
        end
        game.winner = game.away.team
        if playoffMode >= 2 then
            game.winnerSeed = game.away.seed
            game.loser = game.home.team
            game.loserSeed = game.home.seed
        end
    elseif game.home.overtime > game.away.overtime then
        if playoffMode <= 1 then
            data.schedule[thisweek].teams[game.home.team].winner = true
            data.schedule[thisweek].teams[game.away.team].winner = false
        end
        game.winner = game.home.team
        if playoffMode >= 2 then
            game.winnerSeed = game.home.seed
            game.loser = game.away.team
            game.loserSeed = game.away.seed
        end
    end
end

function rollShootoutScores( game )
    game.away.shootout = 0
    game.home.shootout = 0
    for x = 6, 0, -1 do
        game.away.shootout = game.away.shootout + math.random( 0, 1 )
        if ( ( game.away.shootout - game.home.shootout ) > ( x + 1 ) ) or ( ( game.home.shootout - game.away.shootout ) > x ) then
            game.ending = "shootout"
            break
        end
        game.home.shootout = game.home.shootout + math.random( 0, 1 )
        if ( ( game.home.shootout - game.away.shootout ) > x ) or ( ( game.away.shootout - game.home.shootout ) > x ) then
            game.ending = "shootout"
            break
        end
    end
    while not game.ending do
        game.away.shootout = game.away.shootout + math.random( 0, 1 )
        game.home.shootout = game.home.shootout + math.random( 0, 1 )
        if game.away.shootout ~= game.home.shootout then
            game.ending = "shootout"
        end
    end
    if game.away.shootout > game.home.shootout then
        if playoffMode <= 1 then
            data.schedule[thisweek].teams[game.away.team].winner = true
            data.schedule[thisweek].teams[game.home.team].winner = false
        end
        game.winner = game.away.team
        if playoffMode >= 2 then
            game.winnerSeed = game.away.seed
            game.loser = game.home.team
            game.loserSeed = game.home.seed
        end
    else
        if playoffMode <= 1 then
            data.schedule[thisweek].teams[game.home.team].winner = true
            data.schedule[thisweek].teams[game.away.team].winner = false
        end
        game.winner = game.home.team
        if playoffMode >= 2 then
            game.winnerSeed = game.home.seed
            game.loser = game.away.team
            game.loserSeed = game.away.seed
        end
    end
end

function calculateStats()
    calculatePointsScored()
    calculatePointsAllowed()
    calculatePointDiff()
end

function calculatePointsScored()
    for _a, team in ipairs( data.stats.pointsScored ) do
        local points = 0
        for _b, week in ipairs( data.schedule ) do
            if week.playoffs <= 1 and week.teams[team.team] and week.teams[team.team].scored then
                points = points + week.teams[team.team].scored
            end
        end
        team.points = points
    end
    table.sort( data.stats.pointsScored, function( n1, n2 ) return n1.points > n2.points end )
    for rank, team in ipairs( data.stats.pointsScored ) do
        if rank == 1 then
            team.rank = rank
        else
            if team.points == data.stats.pointsScored[rank - 1].points then
                team.rank = data.stats.pointsScored[rank - 1].rank
            else
                team.rank = rank
            end
        end
    end
end

function calculatePointsAllowed()
    for _a, team in ipairs( data.stats.pointsAllowed ) do
        local points = 0
        for _b, week in ipairs( data.schedule ) do
            if week.playoffs <= 1 and week.teams[team.team] and week.teams[team.team].allowed then
                points = points + week.teams[team.team].allowed
            end
        end
        team.points = points
    end
    table.sort( data.stats.pointsAllowed, function( n1, n2 ) return n1.points < n2.points end )
    for rank, team in ipairs( data.stats.pointsAllowed ) do
        if rank == 1 then
            team.rank = rank
        else
            if team.points == data.stats.pointsAllowed[rank - 1].points then
                team.rank = data.stats.pointsAllowed[rank - 1].rank
            else
                team.rank = rank
            end
        end
    end
end

function calculatePointDiff()
    for _a, team in ipairs( data.stats.pointDiff ) do
        local scored, allowed
        for _b, lookup in ipairs( data.stats.pointsScored ) do
            if lookup.team == team.team then
                scored = lookup.points
                break
            end
        end
        for _b, lookup in ipairs( data.stats.pointsAllowed ) do
            if lookup.team == team.team then
                allowed = lookup.points
                break
            end
        end
        team.points = scored - allowed
    end
    table.sort( data.stats.pointDiff, function( n1, n2 ) return n1.points > n2.points end )
    for rank, team in ipairs( data.stats.pointDiff ) do
        if rank == 1 then
            team.rank = rank
        else
            if team.points == data.stats.pointDiff[rank - 1].points then
                team.rank = data.stats.pointDiff[rank - 1].rank
            else
                team.rank = rank
            end
        end
    end
end

function calculateStandings()
    if playoffMode <= 1 then
        calculateWinLossRecords()
        calculateTies()
    end
    if playoffMode >= 1 and playoffMode <= 4 then
        calculatePlayoffGames()
    end
end

function calculateWinLossRecords()
    -- for conference, list in pairs( data.standings ) do
        for _a, team in ipairs( data.standings ) do   -- for _a, team in ipairs( list ) do
            wins, losses = 0, 0
            -- cwins, closses = 0, 0
            for _b, week in ipairs( data.schedule ) do
                if ( week.playoffs <= 1 ) and week.teams[team.team] then
                    if week.teams[team.team].winner == true then
                        wins = wins + 1
                        --[[
                        if data.teams[week.teams[team.team].opponent].conference == conference then
                            cwins = cwins + 1
                        end
                        --]]
                    elseif week.teams[team.team].winner == false then
                        losses = losses + 1
                        --[[
                        if data.teams[week.teams[team.team].opponent].conference == conference then
                            closses = closses + 1
                        end
                        --]]
                    end
                end
            end
            team.wins, team.losses = wins, losses
            -- team.confWins, team.confLosses = cwins, closses
            team.h2hWins = 0
            for _b, lookup in ipairs( data.stats.pointDiff ) do
                if lookup.team == team.team then
                    team.pointDiff = lookup.points
                    break
                end
            end
            for _b, lookup in ipairs( data.stats.pointsAllowed ) do
                if lookup.team == team.team then
                    team.pointsAllowed = lookup.points
                    break
                end
            end
            team.randomNumber = math.random()
        end
        table.sort( data.standings, function( n1, n2 )   -- table.sort( list, function( n1, n2 )
            -- if n1.wins / (n1.wins + n1.losses) ~= n2.wins / (n2.wins + n2.losses) then
                return n1.wins / (n1.wins + n1.losses) > n2.wins / (n2.wins + n2.losses)
            -- else
            --  local a = n1.confWins + n1.confLosses > 0 and n1.confWins / (n1.confWins + n1.confLosses) or 0.01
            --  local b = n2.confWins + n2.confLosses > 0 and n2.confWins / (n2.confWins + n2.confLosses) or 0.01
            --  return a > b
            -- end
        end )
    -- end
end

function calculateTies()
    local tieArray = {}
    -- for conference, list in pairs( data.standings ) do
        for rank, team in ipairs( data.standings ) do   -- for rank, team in ipairs( list ) do
            team.h2hWins = 0
            if rank > 1
            -- and team.wins / (team.wins + team.losses) == list[rank - 1].wins / (list[rank - 1].wins + list[rank - 1].losses)
            and team.wins / (team.wins + team.losses) == data.standings[rank - 1].wins / (data.standings[rank - 1].wins + data.standings[rank - 1].losses)
            -- and team.confWins / (team.confWins + team.confLosses) == list[rank - 1].confWins / (list[rank - 1].confWins + list[rank - 1].confLosses)
            then
                tieArray[rank] = { team = team.team }
            else
                local numTies = 0
                for _a, _b in pairs( tieArray ) do
                    numTies = numTies + 1
                end
                if numTies > 1 then
                    calculateHeadToHead( tieArray )   -- calculateHeadToHead( tieArray, conference )
                end
                tieArray = nil
                tieArray = {}
                tieArray[rank] = { team = team.team }
            end
        end
        local numTies = 0
        for _a, _b in pairs( tieArray ) do
            numTies = numTies + 1
        end
        if numTies > 1 then
            calculateHeadToHead( tieArray )   -- calculateHeadToHead( tieArray, conference )
        end
        table.sort( data.standings --[[ list ]], function( n1, n2 )
            -- local a = n1.confWins + n1.confLosses > 0 and n1.confWins / (n1.confWins + n1.confLosses) or 0.01
            -- local b = n2.confWins + n2.confLosses > 0 and n2.confWins / (n2.confWins + n2.confLosses) or 0.01
            if n1.wins + n1.losses > 0 and n2.wins + n2.losses > 0 and n1.wins / (n1.wins + n1.losses) ~= n2.wins / (n2.wins + n2.losses) then
                return n1.wins / (n1.wins + n1.losses) > n2.wins / (n2.wins + n2.losses)
            -- elseif a ~= b then
            --  return a > b
            elseif n1.h2hWins ~= n2.h2hWins then
                return n1.h2hWins > n2.h2hWins
            elseif n1.pointDiff ~= n2.pointDiff then
                return n1.pointDiff > n2.pointDiff
            elseif n1.pointsAllowed ~= n2.pointsAllowed then
                return n1.pointsAllowed < n2.pointsAllowed
            else
                return n1.randomNumber > n2.randomNumber
            end
        end )
    -- end
end

function calculateHeadToHead( tieArray )   -- function calculateHeadToHead( tieArray, conference )
    local stop = false
    for rank, team in pairs( tieArray ) do
        team.wins = 0
        for _, week in ipairs( data.schedule ) do
            if week.playoffs <= 1 and week.teams[team.team] and isInTie( week.teams[team.team].opponent, tieArray ) then
                if week.teams[team.team].winner == nil then
                    stop = true
                    break
                elseif week.teams[team.team].winner == true then
                    team.wins = team.wins + 1
                end
            end
        end
        if stop then
            break
        end
    end
    if stop then
        for rank, team in pairs( tieArray ) do
            data.standings[rank].h2hWins = 0   -- data.standings[conference][rank].h2hWins = 0
        end
    else
        for rank, team in pairs( tieArray ) do
            data.standings[rank].h2hWins = team.wins   -- data.standings[conference][rank].h2hWins = team.wins
        end
    end
end

function isInTie( teamToCheck, tieArray )
    for _x, team in pairs( tieArray ) do
        if team.team == teamToCheck then
            return true
        end
    end
    return false
end

function calculatePlayoffGames()
    local numTeams = 10
    local switch = {}
    switch[8] = {calc8TeamWeek1, calc8Or9TeamWeek2, calc8TeamSemis, calcFuturesCupFinal}
    switch[9] = {calc9TeamWeek1, calc8Or9TeamWeek2, calc9Or10TeamSemis, calcFuturesCupFinal}
    switch[10] = {calc10TeamWeek1, calc10TeamWeek2, calc9Or10TeamSemis, calcFuturesCupFinal}
    switch[numTeams][playoffMode](unpack(playoffGames))
end

function calc8TeamWeek1(wk1games, wk2games, semigames, finalgame)
    wk1games[1].away.team = data.standings[8].team
    wk1games[1].away.seed = 8
    wk1games[1].home.team = data.standings[5].team
    wk1games[1].home.seed = 5
    wk1games[2].away.team = data.standings[7].team
    wk1games[2].away.seed = 7
    wk1games[2].home.team = data.standings[6].team
    wk1games[2].home.seed = 6
    wk2games[1].home.team = data.standings[3].team
    wk2games[1].home.seed = 3
    wk2games[2].home.team = data.standings[4].team
    wk2games[2].home.seed = 4
    semigames[1].home.team = data.standings[1].team
    semigames[1].home.seed = 1
    semigames[2].home.team = data.standings[2].team
    semigames[2].home.seed = 2
end

function calc8Or9TeamWeek2(wk1games, wk2games, semigames, finalgame)
    if wk1games[1].winnerSeed > wk1games[2].winnerSeed then
        wk2games[1].away.team = wk1games[1].winner
        wk2games[1].away.seed = wk1games[1].winnerSeed
        wk2games[2].away.team = wk1games[2].winner
        wk2games[2].away.seed = wk1games[2].winnerSeed
    else
        wk2games[1].away.team = wk1games[2].winner
        wk2games[1].away.seed = wk1games[2].winnerSeed
        wk2games[2].away.team = wk1games[1].winner
        wk2games[2].away.seed = wk1games[1].winnerSeed
    end
end

function calc8TeamSemis(wk1games, wk2games, semigames, finalgame)
    if wk2games[1].winnerSeed > wk2games[2].winnerSeed then
        semigames[1].away.team = wk2games[1].winner
        semigames[1].away.seed = wk2games[1].winnerSeed
        semigames[2].away.team = wk2games[2].winner
        semigames[2].away.seed = wk2games[2].winnerSeed
    else
        semigames[1].away.team = wk2games[2].winner
        semigames[1].away.seed = wk2games[2].winnerSeed
        semigames[2].away.team = wk2games[1].winner
        semigames[2].away.seed = wk2games[1].winnerSeed
    end
end

function calc9TeamWeek1(wk1games, wk2games, semigames, finalgame)
    wk1games[1].away.team = data.standings[9].team
    wk1games[1].away.seed = 9
    wk1games[1].home.team = data.standings[6].team
    wk1games[1].home.seed = 6
    wk1games[2].away.team = data.standings[8].team
    wk1games[2].away.seed = 8
    wk1games[2].home.team = data.standings[7].team
    wk1games[2].home.seed = 7
    wk2games[1].home.team = data.standings[2].team
    wk2games[1].home.seed = 2
    wk2games[2].home.team = data.standings[3].team
    wk2games[2].home.seed = 3
    wk2games[3].away.team = data.standings[5].team
    wk2games[3].away.seed = 5
    wk2games[3].home.team = data.standings[4].team
    wk2games[3].home.seed = 4
    semigames[1].home.team = data.standings[1].team
    semigames[1].home.seed = 1
end

function calc9Or10TeamSemis(wk1games, wk2games, semigames, finalgame)
    local gamecopy = {}  -- deep-copy the list so as not to alter the order in data file
    for index, game in ipairs(wk2games) do
        gamecopy[index] = game
    end
    table.sort(gamecopy, function (n1, n2) return n1.winnerSeed < n2.winnerSeed end)
    semigames[1].away.team = gamecopy[3].winner
    semigames[1].away.seed = gamecopy[3].winnerSeed
    semigames[2].away.team = gamecopy[2].winner
    semigames[2].away.seed = gamecopy[2].winnerSeed
    semigames[2].home.team = gamecopy[1].winner
    semigames[2].home.seed = gamecopy[1].winnerSeed
end

function calc10TeamWeek1(wk1games, wk2games, semigames, finalgame)
    wk1games[1].away.team = data.standings[10].team
    wk1games[1].away.seed = 10
    wk1games[1].home.team = data.standings[5].team
    wk1games[1].home.seed = 5
    wk1games[2].away.team = data.standings[9].team
    wk1games[2].away.seed = 9
    wk1games[2].home.team = data.standings[6].team
    wk1games[2].home.seed = 6
    wk1games[3].away.team = data.standings[8].team
    wk1games[3].away.seed = 8
    wk1games[3].home.team = data.standings[7].team
    wk1games[3].home.seed = 7
    wk2games[1].home.team = data.standings[2].team
    wk2games[1].home.seed = 2
    wk2games[2].home.team = data.standings[3].team
    wk2games[2].home.seed = 3
    wk2games[3].home.team = data.standings[4].team
    wk2games[3].home.seed = 4
    semigames[1].home.team = data.standings[1].team
    semigames[1].home.seed = 1
end

function calc10TeamWeek2(wk1games, wk2games, semigames, finalgame)
    local gamecopy = {}  -- deep-copy the list so as not to alter the order in data file
    for index, game in ipairs(wk1games) do
        gamecopy[index] = game
    end
    table.sort(gamecopy, function (n1, n2) return n1.winnerSeed < n2.winnerSeed end)
    wk2games[1].away.team = gamecopy[3].winner
    wk2games[1].away.seed = gamecopy[3].winnerSeed
    wk2games[2].away.team = gamecopy[2].winner
    wk2games[2].away.seed = gamecopy[2].winnerSeed
    wk2games[3].away.team = gamecopy[1].winner
    wk2games[3].away.seed = gamecopy[1].winnerSeed
end

function calcFuturesCupFinal(wk1games, wk2games, semigames, finalgame)
    if semigames[1].winnerSeed > semigames[2].winnerSeed then
        finalgame[1].away.team = semigames[1].winner
        finalgame[1].away.seed = semigames[1].winnerSeed
        finalgame[1].home.team = semigames[2].winner
        finalgame[1].home.seed = semigames[2].winnerSeed
    else
        finalgame[1].away.team = semigames[2].winner
        finalgame[1].away.seed = semigames[2].winnerSeed
        finalgame[1].home.team = semigames[1].winner
        finalgame[1].home.seed = semigames[1].winnerSeed
    end
end

function generateOutput()
    local weekTitle = data.schedule[thisweek].title:gsub( '–', '-' )
    os.execute( 'mkdir "' .. weekTitle .. '"' )
    local txtfile = io.open( weekTitle .. "\\Forum Game Thread Post.txt", "w+" )
    generateForumGamePost( txtfile )
    txtfile:close()
    txtfile = io.open( weekTitle .. "\\Forum Library Thread Post.txt", "w+" )
    generateForumLibraryPost( txtfile )
    txtfile:close()
    txtfile = io.open( weekTitle .. "\\Wiki Main Page Template.txt", "w+" )
    generateWikiTemplate( txtfile )
    txtfile:close()
    --[[
    txtfile = io.open( weekTitle .. "\\Wiki Season Article.txt", "w+" )
    generateWikiSeasonArticle( txtfile )
    txtfile:close()
    ]]
    local save = io.open( "data.lua", "w+" )
    saveData(save)
    save:close()
    os.execute('copy data.lua "' .. weekTitle .. '\\data.lua"')
end

function generateForumGamePost( txtfile )
    txtfile:write( "[b]Sub-GM Post[/b]\n\nBonus rolls this week to (points in parentheses):")
    local prevbonus = false
    for _, game in ipairs( data.schedule[thisweek].games ) do
        if game.away.bonus > 25 then
            if prevbonus then
                txtfile:write( ", ", data.teams[game.away.team].planet, " (", game.away.bonus, ")" )
            else
                txtfile:write( " ", data.teams[game.away.team].planet, " (", game.away.bonus, ")" )
                prevbonus = true
            end
        end
        if game.home.bonus > 25 then
            if prevbonus then
                txtfile:write( ", ", data.teams[game.home.team].planet, " (", game.home.bonus, ")" )
            else
                txtfile:write( " ", data.teams[game.home.team].planet, " (", game.home.bonus, ")" )
                prevbonus = true
            end
        end
    end
    if not prevbonus then
        txtfile:write( " no one" )
    end
    txtfile:write(".\n\n[b]Limmie Futures League[/b]\n" )
    printCompletedWeek(txtfile, data.schedule[thisweek])
    if playoffMode == 1 then
        txtfile:write( "[b][u]Final Standings[/u][/b]\n" )
        -- for conference, list in pairs( data.standings ) do
            -- txtfile:write( "[i]", conference, " Conference[/i]\n" )
            for rank, team in ipairs( data.standings ) do   -- for rank, team in ipairs( list ) do
                txtfile:write( rank, ". ", data.teams[team.team].colors, " (", team.wins, "–", team.losses, --[[ ", conf. ", team.confWins, "–", team.confLosses, ]] ")\n" )
            end
            txtfile:write( "\n" )
        -- end
        txtfile:write( "#####INSERT TIEBREAKERS HERE MANUALLY#####\n\n[b][u]Playoffs[/u][/b]\n\n" )
    end
    if playoffMode >= 1 then
        if playoffMode <= #playoffGames then
            for week = playoffMode, #playoffWeeks do
                printUpcomingWeek(txtfile, playoffWeeks[week])
            end
        else
            local champion = playoffGames[#playoffGames][1].winner
            txtfile:write( "Congratulations to the ", data.teams[champion].colors, ", ", data.season, " Limmie Futures Cup Champions!\n\n" )
        end
    end
    txtfile:write( "[b]TAG: " )
    for _, team in pairs( data.teams ) do
        if team.tag then
            txtfile:write( team.tag, " " )
        end
    end
    txtfile:write( "[/b]\n" )
end

function printCompletedWeek(txtfile, week)
    txtfile:write( "[b][u]", week.title, "[/u][/b]\n" )
    for _, game in ipairs(week.games) do
        if game.title then
            txtfile:write( "[i][u]", game.title, "[/u][/i]\n" )
        end
        -- if game.conference then
        --  txtfile:write( "[i]", game.conference, " Conference[/i]\n" )
        -- end
        if game.location then
            txtfile:write( "[i]", game.location, "[/i]\n" )
        end
        if game.away.team == game.winner then
            txtfile:write( "[b]" )
            if game.away.seed then
                txtfile:write( "(", game.away.seed, ") " )
            end
            txtfile:write( game.away.team, "[/b]" )
            if game.location then
                txtfile:write( " vs. " )
            else
                txtfile:write( " at " )
            end
            if game.home.seed then
                txtfile:write( "(", game.home.seed, ") " )
            end
            txtfile:write( game.home.team )
        else
            if game.away.seed then
                txtfile:write( "(", game.away.seed, ") " )
            end
            txtfile:write( game.away.team )
            if game.location then
                txtfile:write( " vs. " )
            else
                txtfile:write( " at " )
            end
            txtfile:write( "[b]" )
            if game.home.seed then
                txtfile:write( "(", game.home.seed, ") " )
            end
            txtfile:write( game.home.team, "[/b]" )
        end
        txtfile:write( " (", game.away.regulation, "–", game.home.regulation )
        if game.away.overtime then
            txtfile:write( ", OT ", game.away.overtime, "–", game.home.overtime )
        end
        if game.away.shootout then
            txtfile:write( ", shootout ", game.away.shootout, "–", game.home.shootout )
        end
        txtfile:write( ")\n" )
    end
    txtfile:write("\n")
end

function printUpcomingWeek(txtfile, week)
    txtfile:write("[b][u]", week.title, "[/u][/b]\n")
    for _, game in ipairs(week.games) do
        if game.title then
            txtfile:write( "[i][u]", game.title, "[/u][/i]\n" )
        end
        --[[
        if game.conference then
            txtfile:write( "[i]", game.conference, " Conference[/i]\n" )
        end
        --]]
        if game.location then
            txtfile:write( "[i]", game.location, "[/i]\n" )
        end
        if game.away.team then
            txtfile:write( "(", game.away.seed, ") ", game.away.team )
        else
            txtfile:write("TBD")
        end
        if game.location then
            txtfile:write( " vs. " )
        else
            txtfile:write( " at " )
        end
        if game.home.team then
            txtfile:write( "(", game.home.seed, ") ", game.home.team, "\n" )
        else
            txtfile:write("TBD\n")
        end
    end
    txtfile:write("\n")
end

function generateForumLibraryPost( txtfile )
    txtfile:write( "[b][u]", data.season, " Limmie Futures League[/u][/b]\n\n\n" )
    if playoffMode > 0 then
        txtfile:write( "[b]Final Standings:[/b]\n\n" )
    else
        txtfile:write( "[b]Current Standings:[/b]\n\n" )
    end
    -- for conference, list in pairs( data.standings ) do
        -- txtfile:write( "[i]", conference, " Conference[/i]\n" )
        for rank, team in ipairs( data.standings ) do   -- for rank, team in ipairs( list ) do
            txtfile:write( rank, ". ", data.teams[team.team].colors, " (", team.wins, "–", team.losses )
            --[[
            if team.confWins + team.confLosses > 0 then
                txtfile:write( ", conf. ", team.confWins, "–", team.confLosses )
            end
            --]]
            txtfile:write( ")\n" )
        end
        txtfile:write( "\n" )
    -- end
    txtfile:write( "\n[b]Statistics[/b]\n\n[i]Points Scored[/i]\n" )
    for _, team in ipairs( data.stats.pointsScored ) do
        txtfile:write( team.rank, ". ", team.team, " (", team.points, ")\n" )
    end
    txtfile:write( "\n[i]Points Allowed[/i]\n" )
    for _, team in ipairs( data.stats.pointsAllowed ) do
        txtfile:write( team.rank, ". ", team.team, " (", team.points, ")\n" )
    end
    txtfile:write( "\n[i]Point Differential[/i]\n" )
    for _, team in ipairs( data.stats.pointDiff ) do
        txtfile:write( team.rank, ". ", team.team, " (")
        if team.points > 0 then
            txtfile:write( "+", team.points )
        elseif team.points < 0 then
            txtfile:write( "−", math.abs( team.points ) )
        else
            txtfile:write( "0" )
        end
        txtfile:write( ")\n" )
    end
    if playoffMode >= 1 then
        txtfile:write( "\n\n[b]Playoffs[/b]\n\n" )
        for week = 1, #playoffWeeks do
            if week < playoffMode then
                printCompletedWeek(txtfile, playoffWeeks[week])
            else
                printUpcomingWeek(txtfile, playoffWeeks[week])
            end
        end
        if playoffMode > #playoffWeeks then
            local champion = playoffGames[#playoffGames][1].winner
            txtfile:write( "\nCongratulations to the ", data.teams[champion].colors, ", ", data.season, " Limmie Futures Cup Champions!\n" )
        end
    end
end

function generateWikiTemplate( txtfile )
    txtfile:write( "<includeonly>{{Sysop-edit|WikiLFL}}</includeonly>\n===[[", data.season, " Limmie Futures League season|", data.season, " ", data.schedule[thisweek].title, "]]===\n{|\n" )
    for num, game in ipairs( data.schedule[thisweek].games ) do
        txtfile:write( "{{LFLgame|color=" )
        if num % 2 == 1 then
            txtfile:write( "FFFFFF" )
        else
            txtfile:write( "C0C0C0" )
        end
        if game.title then
            txtfile:write( "|title=", game.title )
        end
        --[[
        if game.conference then
            txtfile:write( "|conf=", game.conference )
        end
        --]]
        if game.away.seed then
            txtfile:write( "|awayseed=", game.away.seed )
        end
        txtfile:write( "|awayplanet=", data.teams[game.away.team].planet, "|awaynickname=", data.teams[game.away.team].nickname, "|awayreg=", game.away.regulation )
        if game.away.overtime then
            txtfile:write( "|awayOT=", game.away.overtime )
        end
        if game.away.shootout then
            txtfile:write( "|awaySO=", game.away.shootout )
        end
        if game.home.seed then
            txtfile:write( "|homeseed=", game.home.seed )
        end
        txtfile:write( "|homeplanet=", data.teams[game.home.team].planet, "|homenickname=", data.teams[game.home.team].nickname, "|homereg=", game.home.regulation )
        if game.home.overtime then
            txtfile:write( "|homeOT=", game.home.overtime )
        end
        if game.home.shootout then
            txtfile:write( "|homeSO=", game.home.shootout )
        end
        if game.location then
            txtfile:write( "|location=", game.location )
        end
        txtfile:write( "}}\n" )
    end
    txtfile:write( "|}\n" )
end

--[[
function generateWikiSeasonArticle( txtfile )

end
--]]

function saveData(save)
    -- save:write( 'data = {\n\tseason = "', data.season, '",\n\thomefield = { consolation = "', data.homefield.consolation, '", futuresCup = "', data.homefield.futuresCup, '" },\n\tteams = {\n' )
    save:write( 'data = {\n\tseason = "', data.season, '",\n\tteams = {\n' )
    for team, info in pairs( data.teams ) do
        save:write( '\t\t["', team, '"] = {\n\t\t\tplanet = "', info.planet, '", nickname = "', info.nickname, --[[ '", conference = "', info.conference, ]] '",\n\t\t\tcolors = "', info.colors, '",\n\t\t\ttag = ' )
        if info.tag then
            save:write( '"', info.tag, '"' )
        else
            save:write( 'nil' )
        end
        save:write( '\n\t\t},\n' )
    end
    save:write( '\t},\n\tschedule = {\n' )
    for week, info in ipairs( data.schedule ) do
        save:write( '\t\t[', week, '] = {\n\t\t\ttitle = "', info.title, '", display = ' )
        if info.display then
            save:write( 'true' )
        else
            save:write( 'false' )
        end
        save:write( ', playoffs = ', info.playoffs, ',\n\t\t\tgames = {\n' )
        for num, game in ipairs( info.games ) do
            save:write( '\t\t\t\t[', num, '] = {\n\t\t\t\t\ttitle = ' )
            if game.title then
                save:write( '"', game.title, '"' )
            else
                save:write( 'nil' )
            end
            --[[
            if info.playoffs >= 2 then
                save:write( ', conference = ' )
                if game.conference then
                    save:write( '"', game.conference, '"' )
                else
                    save:write( 'nil' )
                end
            end
            --]]
            save:write( ', location = ' )
            if game.location then
                save:write( '"', game.location, '"' )
            else
                save:write( 'nil' )
            end
            save:write( ',\n')
            for _, side in ipairs( { "away", "home" } ) do
                save:write( '\t\t\t\t\t', side, ' = { ' )
                if game[side].seed then
                    save:write( 'seed = ', game[side].seed, ', ' )
                end
                if game[side].team then
                    save:write( 'team = "', game[side].team, '", ' )
                end
                if game[side].bonus then
                    save:write( 'bonus = ', game[side].bonus, ', ' )
                end
                if game[side].regulation then
                    save:write( 'regulation = ', game[side].regulation, ', ' )
                end
                if game[side].overtime then
                    save:write( 'overtime = ', game[side].overtime, ', ' )
                end
                if game[side].shootout then
                    save:write( 'shootout = ', game[side].shootout, ' ' )
                end
                save:write( '},\n' )
            end
            if game.ending then
                save:write( '\t\t\t\t\tending = "', game.ending, '", winner = "', game.winner, '"' )
                if info.playoffs >= 2 then
                    save:write( ', winnerSeed = ', game.winnerSeed, ', loser = "', game.loser, '", loserSeed = ', game.loserSeed )
                end
                save:write( '\n' )
            end
            save:write( '\t\t\t\t},\n' )
        end
        save:write( '\t\t\t},\n' )
        if info.playoffs <= 1 then
            save:write( '\t\t\tteams = {\n' )
            for team, notes in pairs( info.teams ) do
                save:write( '\t\t\t\t["', team, '"] = { opponent = "', notes.opponent, '"' )
                if notes.winner ~= nil then
                    save:write( ', winner = ' )
                    if notes.winner then
                        save:write( 'true' )
                    else
                        save:write( 'false' )
                    end
                    save:write( ', scored = ', notes.scored, ', allowed = ', notes.allowed )
                end
                save:write( ' },\n' )
            end
            save:write( '\t\t\t},\n' )
        end
        save:write( '\t\t},\n' )
    end
    save:write( '\t},\n\tstats = {\n' )
    for statistic, stats in pairs( data.stats ) do
        save:write( '\t\t', statistic, ' = {\n' )
        for key, team in ipairs( stats ) do
            save:write( '\t\t\t[', key, '] = { team = "', team.team, '", points = ', team.points, ', rank = ', team.rank, ' },\n' )
        end
        save:write( '\t\t},\n' )
    end
    save:write( '\t},\n\tstandings = {\n' )
    -- for conference, list in pairs( data.standings ) do
        -- save:write( '\t\t', conference, ' = {\n' )
        for rank, team in ipairs( data.standings ) do   -- for rank, team in ipairs( list ) do
            save:write( '\t\t\t[', rank, '] = { team = "', team.team, '", wins = ', team.wins, ', losses = ', team.losses, --[[ ', confWins = ', team.confWins, ', confLosses = ', team.confLosses, ]]
                ', h2hWins = ', team.h2hWins, ', pointDiff = ', team.pointDiff, ', pointsAllowed = ', team.pointsAllowed, ', randomNumber = ', team.randomNumber, ' },\n' )
        end
        -- save:write( '\t\t},\n' )
    -- end
    save:write( '\t}\n}' )
end

function killProgram()
    io.write( string.format( "\nProgram complete! Elapsed time: %.3f seconds", os.clock() - timer ) )
    while true do
        io.write( '\nType "exit" to close: ' )
        local x = io.read()
        if x == "exit" then
            os.exit()
        end
    end
end

function main()
    initialize()
    rollScores()
    calculateStats()
    calculateStandings()
    generateOutput()
    killProgram()
end

main()
--
Advertisement