forked from Zhetadelta/Blaseball-Livestream-Watcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameEventParser.cs
187 lines (162 loc) · 6.44 KB
/
GameEventParser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Blaseball_Livestream
{
/// <summary>
/// Basic parser that takes Game json updates and emits GameEvent json objects
/// PLEASE NOTE this is probably wrong; I'm currently emitting one GameEvent per game update but
/// ultimately we want a Retrosheet-style condensed description of the whole "play" as a single GameEvent
/// </summary>
class GameEventParser
{
// Last state we saw, for comparison
Game m_oldState;
// State tracking for stats not tracked inherently in the state updates
int m_eventIndex = 0;
string m_currentBatter = null;
string m_currentBatterName = null;
string m_currentBatterTeam = null;
string m_currentPitcher = null;
string m_currentPitcherName = null;
string m_currentPitcherTeam = null;
int m_numFouls = 0;
public void StartNewGame(Game initState)
{
m_oldState = initState;
m_eventIndex = 0;
m_currentBatter = null;
m_currentBatterName = null;
m_currentBatterTeam = null;
m_currentPitcher = initState.awayPitcher;
m_currentPitcherName = initState.awayPitcherName;
m_currentPitcherTeam = initState.awayTeam;
m_numFouls = 0;
}
private void UpdateBatterAndPitcher(Game newState)
{
// Pitchers
if(m_oldState.inning != newState.inning || m_oldState.topOfInning != newState.topOfInning)
{
if(newState.topOfInning)
{
m_currentPitcher = newState.awayPitcher;
m_currentPitcherTeam = newState.awayTeam;
}
else
{
m_currentPitcher = newState.homePitcher;
m_currentPitcherTeam = newState.homeTeam;
}
}
// Batters
string batter = "";
string batterTeam = "";
if(newState.topOfInning)
{
batter = newState.awayBatter;
batterTeam = newState.awayTeam;
}
else
{
batter = newState.homeBatter;
batterTeam = newState.homeTeam;
}
// Did the batter change?
if(batter != "" && batter != m_currentBatter)
{
m_currentBatter = batter;
m_currentBatterTeam = batterTeam;
m_numFouls = 0;
}
}
public GameEvent ParseGameUpdate(Game newState)
{
GameEvent currEvent = new GameEvent();
currEvent.gameId = newState._id;
currEvent.eventIndex = m_eventIndex;
currEvent.inning = newState.inning;
currEvent.outsBeforePlay = m_oldState.halfInningOuts;
UpdateBatterAndPitcher(newState);
currEvent.batterId = m_currentBatter;
currEvent.batterTeamId = m_currentBatterTeam;
currEvent.pitcherId = m_currentPitcher;
currEvent.pitcherTeamId = m_currentPitcherTeam;
currEvent.homeScore = newState.homeScore;
currEvent.awayScore = newState.awayScore;
currEvent.homeStrikeCount = newState.homeStrikes;
currEvent.awayStrikeCount = newState.awayStrikes;
currEvent.totalStrikes = newState.atBatStrikes;
currEvent.totalBalls = newState.atBatBalls;
if(newState.lastUpdate.Contains("Foul Ball"))
{
m_numFouls++;
}
currEvent.totalFouls = m_numFouls;
// TODO This doesn't work for the third out - outs never reach 3
currEvent.outsOnPlay = Math.Max(0, newState.halfInningOuts - m_oldState.halfInningOuts);
// Do we need a better method to track RBIs?
// Stealing home can happen, for one thing
currEvent.runsBattedIn = newState.topOfInning ? newState.awayScore - m_oldState.awayScore : newState.homeScore - m_oldState.homeScore;
// Extremely basic single/double/triple/HR detection
if (newState.lastUpdate.Contains("hits a Single"))
{
currEvent.basesHit = 1;
currEvent.batterBaseAfterPlay = 1;
}
else if(newState.lastUpdate.Contains("hits a Double"))
{
currEvent.basesHit = 2;
currEvent.batterBaseAfterPlay = 2;
}
else if(newState.lastUpdate.Contains("hits a Triple"))
{
currEvent.basesHit = 3;
currEvent.batterBaseAfterPlay = 3;
}
else if(newState.lastUpdate.Contains("home run") || newState.lastUpdate.Contains("grand slam"))
{
currEvent.basesHit = 4;
currEvent.batterBaseAfterPlay = 4;
}
// Sacrifice outs
if(newState.lastUpdate.Contains("sacrifice"))
{
currEvent.isSacrificeHit = true;
}
// Double plays
if(newState.lastUpdate.Contains("double play"))
{
currEvent.isDoublePlay = true;
}
// Triple plays
if(newState.lastUpdate.Contains("triple play"))
{
currEvent.isTriplePlay = true;
}
// TODO currEvent.eventType
// TODO currEvent.batterCount
// TODO currEvent.pitchesList
// TODO currEvent.isLeadoff
// TODO currEvent.lineupPosition
// TODO currEvent.battedBallType
// TODO currEvent.baseRunners
// TODO currEvent.isLastEventForAtBat
// Unsure if this is enough
currEvent.isLastGameEvent = newState.gameComplete;
// Currently not supported by the cultural event of Blaseball
currEvent.isPinchHit = false;
currEvent.isWildPitch = false;
currEvent.isBunt = false;
currEvent.errorsOnPlay = 0;
currEvent.isSacrificeFly = false; // I think we can't tell this
// Store original update text for reference
currEvent.additionalContext = newState.lastUpdate;
m_oldState = newState;
m_eventIndex++;
return currEvent;
}
}
}