Imports DigitalScout.App.Data Public Class GameStateRecord Public inning As Integer Public inningTop As Boolean Public batterPlayerID As Guid? Public batterBats As Integer Public battingOrderPosition As Integer Public pitcherPlayerID As Guid? Public pitcherThrows As Integer Public defensePlayerID?(MAX_PBP_DEFENDERS) As Guid Public outs As Integer Public balls As Integer Public strikes As Integer Public numberOfPitches As Integer Public numberOfStrikes As Integer Public nextBatterUp As Integer Public homeLineupPlayerID?(MAX_LINEUP_PLAYERS) As Guid Public homeLineupPosition?(MAX_LINEUP_PLAYERS) As Integer Public homeLastBatterUp As Integer Public visitorLineupPlayerID?(MAX_LINEUP_PLAYERS) As Guid Public visitorLineupPosition?(MAX_LINEUP_PLAYERS) As Integer Public visitorLastBatterUp As Integer Public firstBasePlayers?(MAX_PLAYERS_ON_BASE) As Guid Public firstBaseEvents?(MAX_PLAYERS_ON_BASE) As Guid Public secondBasePlayers?(MAX_PLAYERS_ON_BASE) As Guid Public secondBaseEvents?(MAX_PLAYERS_ON_BASE) As Guid Public thirdBasePlayers?(MAX_PLAYERS_ON_BASE) As Guid Public thirdBaseEvents?(MAX_PLAYERS_ON_BASE) As Guid Sub GameState_Initialize() Dim i As Integer With Me .inning = 1 .inningTop = True .batterPlayerID = Nothing For i = 1 To MAX_PBP_DEFENDERS .defensePlayerID(i) = Nothing Next i .outs = 0 .balls = 0 .strikes = 0 '.nextBatterUp = 0 For i = 1 To MAX_LINEUP_PLAYERS .homeLineupPlayerID(i) = Nothing .homeLineupPosition(i) = Nothing .visitorLineupPlayerID(i) = Nothing .visitorLineupPosition(i) = Nothing Next i .homeLastBatterUp = 0 .visitorLastBatterUp = 0 For i = 1 To MAX_PLAYERS_ON_BASE .firstBasePlayers(i) = Nothing .firstBaseEvents(i) = Nothing .secondBasePlayers(i) = Nothing .secondBaseEvents(i) = Nothing .thirdBasePlayers(i) = Nothing .thirdBaseEvents(i) = Nothing Next i End With End Sub Sub GameState_PutRunnerOn(ByVal base As Integer, ByVal playerID As Guid?, ByVal eventID As Guid?) If base = 1 Then BasePlayers_Add(Me.firstBasePlayers, Me.firstBaseEvents, playerID, eventID) BasePlayers_Remove(Me.secondBasePlayers, Me.secondBaseEvents, playerID) BasePlayers_Remove(Me.thirdBasePlayers, Me.thirdBaseEvents, playerID) End If If base = 2 Then BasePlayers_Remove(Me.firstBasePlayers, Me.firstBaseEvents, playerID) BasePlayers_Add(Me.secondBasePlayers, Me.secondBaseEvents, playerID, eventID) BasePlayers_Remove(Me.thirdBasePlayers, Me.thirdBaseEvents, playerID) End If If base = 3 Then BasePlayers_Remove(Me.firstBasePlayers, Me.firstBaseEvents, playerID) BasePlayers_Remove(Me.secondBasePlayers, Me.secondBaseEvents, playerID) BasePlayers_Add(Me.thirdBasePlayers, Me.thirdBaseEvents, playerID, eventID) End If If base = 4 Then BasePlayers_Remove(Me.firstBasePlayers, Me.firstBaseEvents, playerID) BasePlayers_Remove(Me.secondBasePlayers, Me.secondBaseEvents, playerID) BasePlayers_Remove(Me.thirdBasePlayers, Me.thirdBaseEvents, playerID) End If End Sub Sub GameState_Get(ByVal contestID As Guid) Dim g As New Data.Contest Dim gPref As New Data.Baseball.ContestPreference Dim hLineup As New Data.Baseball.Lineup Dim vLineup As New Data.Baseball.Lineup Dim hGuid, vGuid As Guid Dim i As Integer Dim posUpperLimit As Integer Dim sql As String Dim advanceBatter As Boolean Try If BASEBALL_PROGRAM Then posUpperLimit = 9 Else posUpperLimit = 11 End If Me.GameState_Initialize() g.Get(conn, contestID) gPref.GetByContestId(conn, contestID) Contest.GetHomeAndVisitor(conn, contestID, hGuid, vGuid) hLineup.GetByContestAndTeamID(conn, contestID, hGuid) vLineup.GetByContestAndTeamID(conn, contestID, vGuid) ' fill in the lineup arrays For i = 1 To MAX_LINEUP_PLAYERS Me.homeLineupPlayerID(i) = hLineup.playerID(i - 1) Me.homeLineupPosition(i) = hLineup.playerPosition(i - 1) Me.visitorLineupPlayerID(i) = vLineup.playerID(i - 1) Me.visitorLineupPosition(i) = vLineup.playerPosition(i - 1) Next ' read through the events Dim playList As List(Of Guid) Dim pl As New Data.Baseball.Play playList = SDFDatabase.GuidList(conn, "baseballplays", "playid", "contestid = '" + contestID.ToString + _ "'", "").ToList For Each pID In playList pl.Get(conn, pID) advanceBatter = False If pl.PlayType = PLAY_TYPE_NEW_INNING Then Me.inning = pl.Inning Me.inningTop = pl.InningTop Me.outs = 0 Me.balls = 0 Me.strikes = 0 BasePlayers_Clear(Me.firstBasePlayers, Me.firstBaseEvents) BasePlayers_Clear(Me.secondBasePlayers, Me.secondBaseEvents) BasePlayers_Clear(Me.thirdBasePlayers, Me.thirdBaseEvents) End If If pl.PlayType = PLAY_TYPE_CHANGE_GAME_STATE Then Me.inning = pl.Inning Me.inningTop = pl.InningTop Me.outs = Val(Mid(pl.DefensiveSequence, 1, 1)) Me.balls = Val(Mid(pl.DefensiveSequence, 2, 1)) Me.strikes = Val(Mid(pl.DefensiveSequence, 3, 1)) BasePlayers_Clear(Me.firstBasePlayers, Me.firstBaseEvents) If Val(Mid(pl.DefensiveSequence, 4, 1)) <> 0 Then BasePlayers_Add(Me.firstBasePlayers, Me.firstBaseEvents, TEAM_PLAYER_ID, Nothing) End If BasePlayers_Clear(Me.secondBasePlayers, Me.secondBaseEvents) If Val(Mid(pl.DefensiveSequence, 5, 1)) <> 0 Then BasePlayers_Add(Me.secondBasePlayers, Me.secondBaseEvents, TEAM_PLAYER_ID, Nothing) End If BasePlayers_Clear(Me.thirdBasePlayers, Me.thirdBaseEvents) If Val(Mid(pl.DefensiveSequence, 6, 1)) <> 0 Then BasePlayers_Add(Me.thirdBasePlayers, Me.thirdBaseEvents, TEAM_PLAYER_ID, Nothing) End If End If If pl.PlayType = PLAY_TYPE_PITCH Then If pl.PitchResult = PITCH_RESULT_BALL Or pl.PitchResult = PITCH_RESULT_BALL_CHECK_SWING Or _ pl.PitchResult = PITCH_RESULT_INTENTIONAL_BALL Or pl.PitchResult = PITCH_RESULT_PITCHOUT Then Me.balls = Me.balls + 1 If Me.balls >= gPref.ballsPerWalk Then Me.balls = 0 Me.strikes = 0 advanceBatter = True End If End If If pl.PitchResult = PITCH_RESULT_STRIKE_SWINGING Or pl.PitchResult = PITCH_RESULT_STRIKE_CALLED Then Me.strikes = Me.strikes + 1 If Me.strikes >= gPref.strikesPerStrikeout And BitIsOff(pl.PitchFlags, PITCH_FLAGS_K_ON_BASE) Then Me.balls = 0 Me.strikes = 0 Me.outs = Me.outs + 1 advanceBatter = True End If If Me.strikes >= gPref.strikesPerStrikeout And BitIsOn(pl.PitchFlags, PITCH_FLAGS_K_ON_BASE) Then Me.balls = 0 Me.strikes = 0 advanceBatter = True End If End If If pl.PitchResult = PITCH_RESULT_FOUL_BALL Then If Me.strikes < gPref.strikesPerStrikeout - 1 Then Me.strikes = Me.strikes + 1 End If End If If pl.PitchResult = PITCH_RESULT_BALL_IN_PLAY Then Me.balls = 0 Me.strikes = 0 advanceBatter = True End If If pl.PitchResult = PITCH_RESULT_HIT_BY_PITCH Then Me.balls = 0 Me.strikes = 0 advanceBatter = True End If If Me.inningTop Then Me.visitorLastBatterUp = pl.BattingOrderPosition - 1 Else Me.homeLastBatterUp = pl.BattingOrderPosition - 1 End If End If If pl.PlayType = PLAY_TYPE_ON_BASE Then If pl.BallInPlayResult = BALL_IN_PLAY_RESULT_OUT Or pl.BallInPlayResult = BALL_IN_PLAY_RESULT_DP Or _ pl.BallInPlayResult = BALL_IN_PLAY_RESULT_SF Or pl.BallInPlayResult = BALL_IN_PLAY_RESULT_SH Then ' out Me.balls = 0 Me.strikes = 0 Me.outs = Me.outs + 1 advanceBatter = True ElseIf pl.BallInPlayResult >= BALL_IN_PLAY_RESULT_HRO Then ' homerun out Me.balls = 0 Me.strikes = 0 Me.outs = Me.outs + 1 advanceBatter = True ElseIf pl.BallInPlayResult >= BALL_IN_PLAY_RESULT_1B And pl.BallInPlayResult <= BALL_IN_PLAY_RESULT_HR Then ' single, double, triple, or homerun Me.balls = 0 Me.strikes = 0 Me.GameState_PutRunnerOn(pl.BallInPlayResult, pl.PlayerID, pl.PlayID) advanceBatter = True Else If pl.OnBaseResult = ON_BASE_RESULT_OUT Then BasePlayers_Remove(Me.firstBasePlayers, Me.firstBaseEvents, pl.PlayerID) BasePlayers_Remove(Me.secondBasePlayers, Me.secondBaseEvents, pl.PlayerID) BasePlayers_Remove(Me.thirdBasePlayers, Me.thirdBaseEvents, pl.PlayerID) Me.outs = Me.outs + 1 ElseIf pl.OnBaseResult = ON_BASE_RESULT_PLACED_ON_FIRST_BASE Then Me.balls = 0 Me.strikes = 0 Me.GameState_PutRunnerOn(1, pl.PlayerID, pl.PlayID) advanceBatter = True ElseIf BitIsOn(pl.OnBaseFlags, ON_BASE_FLAGS_CATCHERS_INTERFERENCE) Then Me.balls = 0 Me.strikes = 0 Me.GameState_PutRunnerOn(1, pl.PlayerID, pl.PlayID) advanceBatter = True Else ' the player was placed on base or advanced a base Me.GameState_PutRunnerOn(pl.FinalBase, pl.PlayerID, pl.PlayID) End If End If End If If pl.PlayType = PLAY_TYPE_SUBSTITUTION Then If pl.PlayerTeamID = hGuid Then Me.homeLineupPlayerID(pl.BattingOrderPosition) = pl.PlayerID Me.homeLineupPosition(pl.BattingOrderPosition) = pl.DefenseResult Else Me.visitorLineupPlayerID(pl.BattingOrderPosition) = pl.PlayerID Me.visitorLineupPosition(pl.BattingOrderPosition) = pl.DefenseResult End If ' take the removed player off the base paths BasePlayers_Remove(Me.firstBasePlayers, Me.firstBaseEvents, pl.RemovedPlayerID) BasePlayers_Remove(Me.secondBasePlayers, Me.secondBaseEvents, pl.RemovedPlayerID) BasePlayers_Remove(Me.thirdBasePlayers, Me.thirdBaseEvents, pl.RemovedPlayerID) End If If advanceBatter Then If Me.inningTop Then Me.visitorLastBatterUp = pl.BattingOrderPosition Else Me.homeLastBatterUp = pl.BattingOrderPosition End If End If Next ' figure out the next batter If Me.inningTop Then i = Me.visitorLastBatterUp + 1 If i > MAX_LINEUP_PLAYERS Then Me.batterPlayerID = Me.visitorLineupPlayerID(1) Else If Me.visitorLineupPlayerID(i) Is Nothing Then i = 1 Me.batterPlayerID = Me.visitorLineupPlayerID(i) End If Else i = Me.homeLastBatterUp + 1 If i > MAX_LINEUP_PLAYERS Then Me.batterPlayerID = Me.homeLineupPlayerID(1) Else If Me.homeLineupPlayerID(i) Is Nothing Then i = 1 Me.batterPlayerID = Me.homeLineupPlayerID(i) End If End If Me.nextBatterUp = i ' fill in the defense array If Me.inningTop Then For i = 1 To MAX_LINEUP_PLAYERS If Me.homeLineupPosition(i) >= 1 And Me.homeLineupPosition(i) <= posUpperLimit Then Me.defensePlayerID(Me.homeLineupPosition(i)) = Me.homeLineupPlayerID(i) End If Next Else For i = 1 To MAX_LINEUP_PLAYERS If Me.visitorLineupPosition(i) >= 1 And Me.visitorLineupPosition(i) <= posUpperLimit Then Me.defensePlayerID(Me.visitorLineupPosition(i)) = Me.visitorLineupPlayerID(i) End If Next End If ' get the number of pitches and number of strikes sql = "contestid = '" + contestID.ToString + _ "' and pitcherplayerid = '" + Me.defensePlayerID(1).ToString + _ "' and playtype = " + PLAY_TYPE_PITCH.ToString Me.numberOfPitches = SDFDatabase.RecordCount(conn, "baseballplays", "playid", sql) sql = sql + " and pitchresult >= " + PITCH_RESULT_STRIKE_SWINGING.ToString Me.numberOfStrikes = SDFDatabase.RecordCount(conn, "baseballplays", "playid", sql) Catch ex As Exception Log("GameState_Get error: " + ex.ToString) End Try End Sub End Class