def ProcessBRPage(filename, con):
    dateStr = filename.split('_')[-1]
    gameDate = date(int(dateStr[:4]),int(dateStr[4:6]),int(dateStr[6:8]))
    html = open(filename).read().decode('utf-8').replace('·','*')
    pbp = PlayByPlay()
    pbp.inning = ''
    playNum = 1
    playInd = 0
    
    #Game Stuff
    b = GameTeamsParser()
    b.feed(html)
    pbp.hAbb = ConvertTeamAbb('BR',b.homeTeamAbb)
    pbp.aAbb = ConvertTeamAbb('BR',b.awayTeamAbb)
    pbp.hTeam = GetTeamfromAbb(pbp.hAbb, con)
    pbp.aTeam = GetTeamfromAbb(pbp.aAbb, con)
    currentGame = Game(pbp.hTeam, pbp.aTeam, gameDate, con)
    b = GameTimeParser()
    b.feed(html)
    currentGame.time = b.time
    lengthStr = b.gamelen.split(':')
    currentGame.gameLength += int(lengthStr[0]) * 60 + int(lengthStr[1])
    b = GameWeatherParser()
    b.feed(html)
    currentGame.temp = int(re.search(': -?([0-9]{2}) F', b.weather).group(1))
    currentGame.windSpeed = int(re.search('Wind ([0-9]{1,2})mph', b.weather, flags=re.IGNORECASE).group(1))
    windDir = re.search('mph ([^,]*),', b.weather, flags=re.IGNORECASE).group(1)
    if windDir == 'out to Centerfield':
        windDir = 'tocf'
    elif windDir == 'out to Rightfield':
        windDir = 'torf'
    elif windDir == 'out to Leftfield':
        windDir = 'tolf'
    elif windDir == 'from Left to Right':
        windDir = 'ltor'
    elif windDir == 'from Right to Left':
        windDir = 'rtol'
    elif windDir == 'in from Centerfield':
        windDir = 'fromcf'
    elif windDir == 'in from Rightfield':
        windDir = 'fromrf'
    elif windDir == 'in from Leftfield':
        windDir = 'fromlf'
    weatherStr = b.weather.split(',')
    if len(weatherStr) < 4:
        precip = 'unknown'
        sky = weatherStr[-1].replace('.','').lower()
    else:
        precip = weatherStr[-1].replace('.','').lower()
        sky = weatherStr[-2].lower()
    field = b.field.lower() if (b.field != '') else 'unknown'
    currentGame.weather = 'Field = '+field+', Prec = '+precip+', Sky = '+sky
    b = GameUmpParser()
    b.feed(html)
    currentGame.homeUmp = b.homeump
    
    #Lineups
    b = GameWinLossSaveParser()
    b.feed(html)
    winPitch = abb(b.winPitch)
    lossPitch = abb(b.lossPitch)
    savePitch = '' if len(b.savePitch) == 0 else abb(b.savePitch)
    b = BRLineupParser()
    b.feed(html)
    for line in b.lineup:
        if len(line) == 8 and line[0].isdigit():
            aBatNum = int(line[0])
            aUID = str(line[1])
            aName = str(line[2])
            aPos = str(line[3]).strip()
            hBatNum = int(line[4])
            hUID = str(line[5])
            hName = str(line[6])
            hPos = str(line[7]).strip()
            if aPos == 'P':
                pbp.aPitcher = abb(aName)
            if hPos == 'P':
                pbp.hPitcher = abb(hName)
            pbp.lineup[abb(aName)] = Lineup(123, pbp.aTeam, aName, aBatNum, aPos, aUID, 'BR', con)
            pbp.lineup[abb(hName)] = Lineup(123, pbp.hTeam, hName, hBatNum, hPos, hUID, 'BR', con)
        elif len(line) == 6:
            pbp.aPitcher = abb(str(line[1]))
            pbp.hPitcher = abb(str(line[4]))
            
    #Pitch Rosters
    bp = BRPitcherParser()
    bp.feed(html)
    pitchIDLookup = {}
    erLookup = {}
    for pitcher in bp.roster:
        uid = str(pitcher[0])
        name = str(pitcher[1])
        pitchIDLookup[abb(name)] = uid
        er = int(pitcher[-2])
        erLookup[abb(name)] = er
        team = pbp.aTeam if pitcher[-1] == 'A' else pbp.hTeam
        pbp.pitchers[abb(name)] = PitchRoster(123, team, name, uid, 'BR', con)
        pbp.pitchers[abb(name)].IP = 0
        pbp.pitchers[abb(name)].pitcherRole = 'Starter'

    #Hitter ID Lookup
    bb = BRBatterParser()
    bb.feed(html)
    batID = {}
    for x in bb.batID.keys():
        batID[abb(x)] = bb.batID[x]
        for y in pbp.lineup.keys():
            if abb(x) == y:
                break
            elif abb(x)[:11] == y:
                pbp.lineup[abb(x)] = pbp.lineup[y]
                del pbp.lineup[y]

    #Plays
    b = BRPlayParser()
    b.feed(html)
    for playNum in range(1, len(b.plays)+1):
        pbp.plays[playInd] = Play()
        pbp.plays[playInd].hitterID = b.plays[playNum][7]
        pbp.plays[playInd].pitcherID = b.plays[playNum][8]

        #Inning Stuff
        innPre = 'Top' if b.plays[playNum][0][0] == 't' else 'Bot'
        prevInn = pbp.inning
        pbp.inning = innPre + ' ' + str(b.plays[playNum][0][1])
        pbp.plays[playInd].inning = pbp.inning
        if pbp.inning != prevInn:
            pbp.outs = 0
            pbp.firstBase = None
            pbp.secondBase = None
            pbp.thirdBase = None
        pbp.plays[playInd].startSit = pbp.ReturnSit()


        #Pitches
        pitchStr = b.plays[playNum][4]
        if len(pitchStr.split(' ')) > 1:
            pbp.plays[playInd].pitchSeq = pitchStr.split(' ')[1]
            pbp.plays[playInd].strikes = int(pitchStr.split('-')[1][0])
            pbp.plays[playInd].balls = int(pitchStr.split('-')[0][-1])
        pbp.plays[playInd].resultOuts = b.plays[playNum][5].count('O')
        pbp.outs += b.plays[playNum][5].count('O')
        pbp.plays[playInd].runsScored = b.plays[playNum][5].count('R')
        if pbp.ProcessBRPlay(b.plays[playNum][11], playInd):
            pbp.lineup[b.plays[playNum][7]].RBI += pbp.plays[playInd].runsScored

        pbp.plays[playInd].endSit = pbp.ReturnSit()
        pbp.plays[playInd].playNum = playInd + 1

        #Subs
        if playNum in b.subs.keys():
            subs = b.subs[playNum].split(';')
            for sub in subs:
                #Pitching Change
                if re.search('(.*) replaces (.*) pitching', sub) is not None:
                    newP = re.search('(.*) replaces (.*) pitching', sub).group(1)
                    p = pbp.aPitcher if b.plays[playNum+1][0][0] == 'b' else pbp.hPitcher
                    pbp.pitchers[p].IP += int(b.plays[playNum+1][0][-1])-1
                    for x in pbp.pitchers.keys():
                        if pbp.pitchers[x].team == pbp.pitchers[p].team and x != p:
                            pbp.pitchers[p].IP -= float(pbp.pitchers[x].IP)
                    if int(pbp.outs) in range(1,3):
                        pbp.pitchers[p].IP += int(pbp.outs) / 3.0
                    if b.plays[playNum+1][0][0] == 'b':
                        pbp.aPitcher = abb(newP)
                        team = pbp.aTeam
                    else:
                        pbp.hPitcher = abb(newP)
                        team = pbp.hTeam
                    pbp.pitchers[abb(newP)] = PitchRoster(123, team, newP, pitchIDLookup[abb(newP)], 'BR', con)
                    pbp.pitchers[abb(newP)].pitcherRole = 'Reliever'
                    batnum = int(re.search('batting ([0-9])',sub).group(1)) \
                        if re.search('batting ([0-9])', sub) is not None else 0
                    pbp.lineup[abb(newP)] = Lineup(123, team, newP, batnum, 'P', pitchIDLookup[abb(newP)], 'BR', con)

                #Pinch Hitter
                if re.search('(.*) pinch hits for (.*)', sub) is not None:
                    newP = re.search('(.*) pinch hits for (.*)', sub).group(1)
                    team = pbp.hTeam if b.plays[playNum+1][0][0] == 'b' else pbp.aTeam
                    batnum = int(re.search('batting ([0-9])', sub).group(1)) \
                        if re.search('batting ([0-9])', sub) is not None else 0
                    pbp.lineup[abb(newP)] = Lineup(123, team, newP, batnum, 'PH', bb.batID[newP], 'BR', con)

                #Defensive Sub
                if re.search('(.*) replaces (.*) playing (.*) batting', sub) is not None:
                    newP = re.search('(.*) replaces (.*) playing (.*) batting', b.subs[playNum]).group(1)
                    team = pbp.aTeam if b.plays[playNum+1][0][0] == 'b' else pbp.hTeam
                    batnum = int(re.search('batting ([0-9])', b.subs[playNum]).group(1)) \
                        if re.search('batting ([0-9])', b.subs[playNum]) is not None else 0
                    pos = re.search('(.*) replaces (.*) playing (.*) batting', b.subs[playNum]).group(3)
                    pbp.lineup[abb(newP)] = Lineup(123, team, newP, batnum, pos, bb.batID[newP], 'BR', con)

                #Pinch Runner
                if re.search('(.*) pinch runs for', b.subs[playNum]) is not None:
                    newP = re.search('(.*) pinch runs for', b.subs[playNum]).group(1)
                    team = pbp.hTeam if b.plays[playNum+1][0][0] == 'b' else pbp.aTeam
                    batnum = int(re.search('batting ([0-9])', b.subs[playNum]).group(1)) \
                        if re.search('batting ([0-9])', b.subs[playNum]) is not None else 0
                    pbp.lineup[abb(newP)] = Lineup(123, team, newP, batnum, 'PR', bb.batID[newP], 'BR', con)
                    playInd += 1
                    pbp.plays[playInd] = Play()
                    pbp.plays[playInd].hitterID = abb(re.search('(.*) pinch runs for (.*) \(', b.subs[playNum]).group(1))
                    pbp.plays[playInd].pitcherID = abb(pbp.aPitcher) if pbp.inning[0] == 'B' else abb(pbp.hPitcher)
                    pbp.plays[playInd].inning = pbp.inning
                    pbp.plays[playInd].startSit = pbp.plays[playInd].endSit = pbp.ReturnSit()
                    pbp.plays[playInd].playType = 'Pinch Runner'
                    replacee = re.search('(.*) pinch runs for (.*) \(', b.subs[playNum]).group(2)
                    if pbp.firstBase != None and pbp.firstBase[1] == abb(replacee):
                        pbp.firstBase = [playInd, abb(newP)]
                    elif pbp.secondBase != None and pbp.secondBase[1] == abb(replacee):
                        pbp.secondBase = [playInd, abb(newP)]
                    elif pbp.thirdBase != None and pbp.thirdBase[1] == abb(replacee):
                        pbp.thirdBase = [playInd, abb(newP)]


        playInd += 1

    #Calculate IP for Last Pitchers
    currentGame.InsertBlankGame(con)
    currentGame.totalInnings = int(pbp.inning.split(' ')[-1])
    pbp.pitchers[pbp.hPitcher].IP += currentGame.totalInnings
    pbp.pitchers[pbp.aPitcher].IP += currentGame.totalInnings
    if pbp.inning[:3] == 'Top':
        pbp.pitchers[pbp.aPitcher].IP -= 1
    elif pbp.inning[:3] == 'Bot':
        pbp.pitchers[pbp.aPitcher].IP -= float(3 - int(pbp.outs)) / 3.0

    for x in pbp.pitchers.keys():
        if pbp.pitchers[x].team == pbp.pitchers[pbp.hPitcher].team and x != pbp.hPitcher:
            pbp.pitchers[pbp.hPitcher].IP -= float(pbp.pitchers[x].IP)
        if pbp.pitchers[x].team == pbp.pitchers[pbp.aPitcher].team and x != pbp.aPitcher:
            pbp.pitchers[pbp.aPitcher].IP -= float(pbp.pitchers[x].IP)

    #Insert Game, Plays, Pitchers, and Lineups into DB
    for x in pbp.plays.values():
        if x.playType not in (
        'No Play', 'Stolen Base', 'Caught Stealing', 'Pick Off', 'Balk', 'Passed Ball', 'Wild Pitch',
        'Defensive Indifference', 'Error on Foul', 'Unknown Runner Activity'):
            x.CalcPitches()
            pbp.lineup[x.hitterID].PA += 1
            pbp.pitchers[x.pitcherID].ContactStrikes += x.contactX
            pbp.pitchers[x.pitcherID].SwingStrikes += x.swingX
            pbp.pitchers[x.pitcherID].LookStrikes += x.lookX
            pbp.pitchers[x.pitcherID].Strikes += x.lookX + x.swingX + x.contactX
            pbp.pitchers[x.pitcherID].Balls += x.balls
            pbp.pitchers[x.pitcherID].pitchCount += x.lookX + x.swingX + x.contactX + x.balls
            if x.ballType in ('Ground Ball', 'Bunt Ground Ball'):
                pbp.pitchers[x.pitcherID].GB += 1
            elif x.ballType in ('Line Drive', 'Bunt Line Drive'):
                pbp.pitchers[x.pitcherID].LD += 1
            elif x.ballType in ('Fly Ball', 'Pop Up', 'Bunt Pop'):
                pbp.pitchers[x.pitcherID].FB += 1
            if x.playType in (
            'Strikeout', 'Out', 'Double Play', 'Triple Play', "Fielders Choice", 'Reach On Error', 'Single',
            'Double', 'Ground Rule Double', 'Triple', 'Home Run'):
                pbp.lineup[x.hitterID].AB += 1
                if x.playType in ('Single', 'Double', 'Ground Rule Double', 'Triple', 'Home Run'):
                    pbp.lineup[x.hitterID].Hits += 1
                    pbp.pitchers[x.pitcherID].Hits += 1
                    if x.playType == 'Single':
                        pbp.lineup[x.hitterID].Single += 1
                    elif x.playType in ('Double', 'Ground Rule Double'):
                        pbp.lineup[x.hitterID].Double += 1
                    elif x.playType == 'Triple':
                        pbp.lineup[x.hitterID].Triple += 1
                    elif x.playType == 'Home Run':
                        pbp.lineup[x.hitterID].HR += 1
                elif x.playType == 'Strikeout':
                    pbp.pitchers[x.pitcherID].K += 1
            else:
                if x.playType in ('Walk', 'Intentional Walk'):
                    pbp.pitchers[x.pitcherID].BB += 1
                    pbp.lineup[x.hitterID].BB += 1
                    pbp.pitchers[x.pitcherID].pitchCount += 1
                elif x.playType == 'Hit By Pitch':
                    pbp.pitchers[x.pitcherID].HBP += 1
                    pbp.lineup[x.hitterID].HBP += 1
        if x.inning[:3] == 'Top':
            if x.hit:
                currentGame.awayHits += 1
            if x.runScored:
                pbp.lineup[x.hitterID].Runs += 1
            currentGame.awayRuns += x.runsScored
        else:
            if x.hit:
                currentGame.homeHits += 1
            if x.runScored:
                pbp.lineup[x.hitterID].Runs += 1
            currentGame.homeRuns += x.runsScored
        x.gameKey = currentGame.gameKey
        x.hitterID = batID[x.hitterID]
        x.pitcherID = pitchIDLookup[x.pitcherID]
        x.InsertPlay('BR', con)
    if currentGame.homeRuns > currentGame.awayRuns:
        currentGame.homeTeamWin = True
    elif currentGame.homeRuns == currentGame.awayRuns:
        currentGame.tie = True
    currentGame.UpdateStats(con)

    # Go Through Pitchers
    for x in pbp.pitchers.keys():
        if x == winPitch:
            pbp.pitchers[x].Win = True
        elif x == lossPitch:
            pbp.pitchers[x].Loss = True
        elif x == savePitch:
            pbp.pitchers[x].Save = True

        if pbp.pitchers[x].IP == 9.0:
            pbp.pitchers[x].CG = True
            if pbp.pitchers[x].Runs == 0:
                pbp.pitchers[x].SO = True
                if pbp.pitchers[x].Hits == 0:
                    pbp.pitchers[x].NH = True
        pbp.pitchers[x].gameKey = currentGame.gameKey
        pbp.pitchers[x].earnedRuns = erLookup[x]
        pbp.pitchers[x].InsertRosterRow(con)

    # Go Through Hitters
    for x in pbp.lineup.keys():
        pbp.lineup[x].game = currentGame.gameKey
        pbp.lineup[x].InsertLineupRow(con)

    return currentGame, pbp
def ProcessRSLog(filename, con):
    text = open(filename,'a')
    text.write('\nid,dunzo')
    text.close()
    text = open(filename,'r')
    pbp = PlayByPlay()
    currentGame = None
    gameDate = date(1500,1,1)
    weather = []
    wp = ''
    lp = ''
    savep = ''
    playInd = 0
    for line in text:
        line = line.replace('!','')
        row = line.split(',')
        rowType = row[0]
        
        #Game
        if rowType == 'info':
            att = row[1]
            if att == 'visteam':
                pbp.aAbb = row[2].strip()
                if pbp.aTeam == 0:
                    pbp.aTeam = GetTeamfromAbb(pbp.aAbb, con)
            elif att == 'hometeam':
                pbp.hAbb = row[2].strip()
                if pbp.hTeam == 0:
                    pbp.hTeam = GetTeamfromAbb(pbp.hAbb, con)
            elif att == 'date':
                datePieces = row[2].split('/')
                gameDate = date(int(datePieces[0]),int(datePieces[1]),int(datePieces[2]))
                currentGame = Game(pbp.hTeam, pbp.aTeam, gameDate, con)
            elif att == 'starttime':
                currentGame.time = row[2].strip()
            elif att == 'winddir':
                currentGame.windDir = row[2].strip()
            elif att == 'windspeed':
                currentGame.windSpeed = int(row[2].strip())
            elif att == 'temp':
                currentGame.temp = int(row[2].strip())
            elif att == 'fieldcond':
                weather.append(row[2].strip())
            elif att == 'precip':
                weather.append(row[2].strip())
            elif att == 'sky':
                weather.append(row[2].strip())
                currentGame.weather = 'Field = '+weather[0]+', Prec = '+weather[1]+', Sky = '+weather[2]
            elif att == 'timeofgame':
                currentGame.gameLength = int(row[2].strip())
            elif att == 'umphome':
                currentGame.homeUmp = row[2].strip()
            elif att == 'wp':
                wp = row[2].strip()
            elif att == 'lp':
                lp = row[2].strip()
            elif att == 'save':
                savep = row[2].strip()
            
        #Lineup and Pitch Roster
        elif rowType == 'start':
            userID = row[1]
            playerName = row[2]
            if int(row[3]) == 0:
                team = pbp.aTeam
            else:
                team = pbp.hTeam
            playerBatNum = row[4]
            playerPos = GetPos(row[5])
            pbp.lineup[userID] = Lineup(123, team, playerName, int(playerBatNum), playerPos, userID, 'RS', con)
            if playerPos == 'P':
                pbp.pitchers[userID] = PitchRoster(123, team, playerName, userID, 'RS', con)
                pbp.pitchers[userID].pitcherRole = 'Starter'
                if int(row[3]) == 0:
                    pbp.aPitcher = userID
                else:
                    pbp.hPitcher = userID
        
        #Plays            
        elif rowType == 'play':
            userID = row[3].strip()
            playInd += 1
            pbp.plays[playInd] = Play()
            pbp.plays[playInd].hitterID = userID
            
            #Set Inning specific variables
            innPre = 'Top' if row[2]=='0' else 'Bot'
            pbp.plays[playInd].pitcherID = pbp.hPitcher if innPre == 'Top' else pbp.aPitcher
            prevInn = pbp.inning
            pbp.inning = innPre +' '+str(row[1])
            pbp.plays[playInd].inning = pbp.inning
            if pbp.inning != prevInn:
                pbp.outs = 0
                pbp.firstBase = None
                pbp.secondBase = None
                pbp.thirdBase = None
            pbp.plays[playInd].startSit = pbp.ReturnSit()
            
            #Deal with Pitches
            pbp.plays[playInd].pitchSeq = row[5]
            pbp.plays[playInd].strikes = int(row[4][1])
            pbp.plays[playInd].balls = int(row[4][0])
            #print(pbp.inning)
            #print ('Before Play', pbp.thirdBase, pbp.secondBase, pbp.firstBase, pbp.outs, row[6])
            pbp.ProcessRSPlay(row[6],playInd)
            #print ('After Play', pbp.thirdBase, pbp.secondBase, pbp.firstBase, pbp.outs, pbp.plays[playInd].playType)
            pbp.plays[playInd].endSit = pbp.ReturnSit()
            #print(pbp.ReturnSit())
            pbp.plays[playInd].playNum = playInd
            
        #Substitutions
        elif rowType == 'sub':
            #Pitching Change
            if int(row[5]) == 1:
                p = pbp.hPitcher if int(row[3]) == 1 else pbp.aPitcher
                pbp.pitchers[p].IP += int(pbp.inning.split(' ')[-1])-1
                for x in pbp.pitchers.keys():
                    if pbp.pitchers[x].team == pbp.pitchers[p].team and x != p:
                        pbp.pitchers[p].IP -= float(pbp.pitchers[x].IP)
                pbp.pitchers[p].IP += int(pbp.outs)/3.0
                if int(row[3]) == 1:
                    pbp.hPitcher = row[1]
                    team = pbp.hTeam
                else:
                    pbp.aPitcher = row[1]
                    team = pbp.aTeam
                pbp.pitchers[row[1]] = PitchRoster(123, team, row[2], row[1], 'RS', con)
                pbp.pitchers[row[1]].pitcherRole = 'Reliever'
                pbp.lineup[row[1]] = Lineup(123, team, row[2], int(row[4]), 'P', row[1], 'RS', con)
               
            #Pinch Hitter
            elif int(row[5]) == 11:
                team = pbp.hTeam if int(row[3])==1 else pbp.aTeam
                pbp.lineup[row[1]] = Lineup(123, team, row[2], int(row[4]), 'PH', row[1], 'RS', con)
                
            #Pinch Runner
            elif int(row[5]) == 12:
                team = pbp.hTeam if int(row[3])==1 else pbp.aTeam
                pbp.lineup[row[1]] = Lineup(123, team, row[2], int(row[4]), 'PR', row[1], 'RS', con)
                playInd += 1
                pbp.plays[playInd] = Play()
                pbp.plays[playInd].hitterID = row[1]
                pbp.plays[playInd].inning = pbp.inning
                pbp.plays[playInd].pitcherID = pbp.hPitcher if pbp.inning[:3] == 'Top' else pbp.aPitcher
                pbp.plays[playInd].startSit = pbp.plays[playInd].endSit = pbp.ReturnSit()
                pbp.plays[playInd].playType = 'Pinch Runner'
                for guy in pbp.lineup.keys():
                    if pbp.lineup[guy].team == team and pbp.lineup[guy].player_bat_num == int(row[4]) and guy != row[1]:
                        replacee = guy
                        if pbp.firstBase != None and pbp.firstBase[1] == replacee:
                            pbp.firstBase = [playInd, row[1]]
                        elif pbp.secondBase != None and pbp.secondBase[1] == replacee:
                            pbp.secondBase = [playInd, row[1]]
                        elif pbp.thirdBase != None and pbp.thirdBase[1] == replacee:
                            pbp.thirdBase = [playInd, row[1]]
                            
            #Defensive Sub
            else:                            
               team = pbp.hTeam if int(row[3])==1 else pbp.aTeam
               playerPos = GetPos(int(row[5]))
               if row[1] not in pbp.lineup.keys():
                   pbp.lineup[row[1]] = Lineup(123, team, row[2], int(row[4]), playerPos, row[1], 'RS', con)
                   
        #Earned Runs
        elif rowType == 'data':
            if row[1] == 'er':
                pbp.pitchers[row[2]].earnedRuns += int(row[3])
                
        elif rowType == 'id':
            print(pbp.aAbb+' at '+pbp.hAbb+' - '+gameDate.isoformat())
            if gameDate != date(1500,1,1):
                currentGame.InsertBlankGame(con)
                currentGame.totalInnings = int(pbp.inning.split(' ')[-1])
                pbp.pitchers[pbp.hPitcher].IP += currentGame.totalInnings
                pbp.pitchers[pbp.aPitcher].IP += currentGame.totalInnings
                if pbp.inning[:3] == 'Top':
                    pbp.pitchers[pbp.aPitcher].IP -= 1
                elif pbp.inning[:3] == 'Bot':
                    pbp.pitchers[pbp.aPitcher].IP -= float(3-int(pbp.outs))/3.0
                    
                #Go through Plays
                for x in pbp.plays.values():
                    if x.playType not in ('No Play','Stolen Base','Caught Stealing','Pick Off','Balk','Passed Ball','Wild Pitch','Defensive Indifference','Error on Foul', 'Unknown Runner Activity'):
                        x.CalcPitches()
                        pbp.lineup[x.hitterID].PA += 1
                        pbp.pitchers[x.pitcherID].ContactStrikes += x.contactX
                        pbp.pitchers[x.pitcherID].SwingStrikes += x.swingX
                        pbp.pitchers[x.pitcherID].LookStrikes += x.lookX
                        pbp.pitchers[x.pitcherID].Strikes += x.lookX + x.swingX + x.contactX
                        pbp.pitchers[x.pitcherID].Balls += x.balls
                        pbp.pitchers[x.pitcherID].pitchCount += x.lookX + x.swingX + x.contactX + x.balls
                        if x.ballType in ('Ground Ball', 'Bunt Ground Ball'):
                            pbp.pitchers[x.pitcherID].GB += 1
                        elif x.ballType in ('Line Drive', 'Bunt Line Drive'):
                            pbp.pitchers[x.pitcherID].LD += 1
                        elif x.ballType in ('Fly Ball','Pop Up','Bunt Pop'):
                            pbp.pitchers[x.pitcherID].FB += 1
                        if x.playType in ('Strikeout','Out','Double Play','Triple Play', "Fielders Choice",'Reach On Error','Single','Double','Ground Rule Double','Triple','Home Run'):
                            pbp.lineup[x.hitterID].AB += 1
                            if x.playType in ('Single', 'Double', 'Ground Rule Double', 'Triple', 'Home Run'):
                                pbp.lineup[x.hitterID].Hits += 1
                                pbp.pitchers[x.pitcherID].Hits += 1
                                if x.playType == 'Single':
                                    pbp.lineup[x.hitterID].Single += 1
                                elif x.playType in ('Double', 'Ground Rule Double'):
                                    pbp.lineup[x.hitterID].Double += 1
                                elif x.playType == 'Triple':
                                    pbp.lineup[x.hitterID].Triple += 1
                                elif x.playType == 'Home Run':
                                    pbp.lineup[x.hitterID].HR += 1
                            elif x.playType == 'Strikeout':
                                pbp.pitchers[x.pitcherID].K += 1
                        else:
                            if x.playType in ('Walk', 'Intentional Walk'):
                                pbp.pitchers[x.pitcherID].BB += 1
                                pbp.lineup[x.hitterID].BB += 1
                                pbp.pitchers[x.pitcherID].pitchCount += 1
                            elif x.playType == 'Hit By Pitch':
                                pbp.pitchers[x.pitcherID].HBP += 1
                                pbp.lineup[x.hitterID].HBP += 1
                    if x.inning[:3] == 'Top':
                        if x.hit:
                            currentGame.awayHits +=1
                        currentGame.awayRuns += x.runsScored
                    else:
                        if x.hit:
                            currentGame.homeHits +=1
                        currentGame.homeRuns += x.runsScored
                    x.gameKey = currentGame.gameKey

                    x.InsertPlay('RS', con)
                if currentGame.homeRuns > currentGame.awayRuns:
                    currentGame.homeTeamWin = True
                elif currentGame.homeRuns == currentGame.awayRuns:
                    currentGame.tie = True

                currentGame.UpdateStats(con)

                #Go Through Pitchers
                for x in pbp.pitchers.keys():
                    if x == wp:
                        pbp.pitchers[x].Win = True
                    elif x == lp:
                        pbp.pitchers[x].Loss = True
                    elif x == savep:
                        pbp.pitchers[x].Save = True
                    if pbp.pitchers[x].team == pbp.pitchers[pbp.hPitcher].team and x != pbp.hPitcher:
                        pbp.pitchers[pbp.hPitcher].IP -= float(pbp.pitchers[x].IP)
                    if pbp.pitchers[x].team == pbp.pitchers[pbp.aPitcher].team and x != pbp.aPitcher:
                        pbp.pitchers[pbp.aPitcher].IP -= float(pbp.pitchers[x].IP)
                    if pbp.pitchers[x].IP == 9.0:
                        pbp.pitchers[x].CG = True
                        if pbp.pitchers[x].Runs == 0:
                            pbp.pitchers[x].SO = True
                            if pbp.pitchers[x].Hits == 0:
                                pbp.pitchers[x].NH = True
                    pbp.pitchers[x].gameKey = currentGame.gameKey
                    pbp.pitchers[x].InsertRosterRow(con)
                    
                #Go Through Hitters
                for x in pbp.lineup.keys():
                    pbp.lineup[x].game = currentGame.gameKey
                    pbp.lineup[x].InsertLineupRow(con)
                    
            #Clear Variables
            rplays = pbp.plays
            rlineup = pbp.lineup
            rpitchers = pbp.pitchers
            rgame = currentGame                    
                
            pbp = PlayByPlay()
            pbp.lineup = {}
            pbp.plays = {}
            pbp.pitchers = {}
            currentGame = None
            gameDate = date(1500,1,1)
            weather = []
            wp = ''
            lp = ''
            savep = ''
            playInd = 0
    return rplays, rlineup, rpitchers, rgame