-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameState.py
452 lines (329 loc) · 14.8 KB
/
GameState.py
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
from FieldGameState import *
from Player import *
import AtBatResult
from Globals import *
DEBUG_GAMESTATE=True
########################
#
#
#
#
########################
class GameState:
def __init__(self, HomeTeam, AwayTeam, playerDB):
#this is a reference to the global object
#that acts as the caching interface to persistent player
#objects
self.__playerDB = playerDB
self.__fieldGameState = DefensiveFieldState(BasesState(), 0)
self.__HomeTeam = HomeTeam
self.__AwayTeam = AwayTeam
self.__inning = 1
self.__homeTeamUp = False
self.__gameEventLog = []
def __str__(self):
header = "--- H:%d A:%d in %s of %d %d outs ---\n" % (self.__HomeTeam.getRunsScored(),
self.__AwayTeam.getRunsScored(),
"bottom" if self.__homeTeamUp else "top",
self.__inning,
self.__fieldGameState.getNumOuts())
second = " %d \n" % self.__fieldGameState.playerOn(2)
thirdfirst = " %d %d %d\n" % (self.__fieldGameState.playerOn(3),
self._getPitcherGUID(),
self.__fieldGameState.playerOn(1))
home = " %d \n" % self.__fieldGameState.playerOn(0)
return header + second + thirdfirst + home
def _getNextBatterGUID(self):
offenseTeamObj = self._getOffenseTeamObject()
return offenseTeamObj.getNextBatterGUID()
def _getPlayerAbilities(self, guid):
player = self.__playerDB.getObjectHandle(guid)
if player != None:
return player.getPlayerAbilities()
return None
def _getPlayerPitcherSkills(self, guid):
player = self.__playerDB.getObjectHandle(guid)
if player != None:
return player.getPlayerPitcherSkills()
return None
def _getPlayerBatterSkills(self, guid):
player = self.__playerDB.getObjectHandle(guid)
if player != None:
return player.getPlayerBatterSkills()
return None
def _getPitcherGUID(self):
defenseTeamObj = self._getDefenseTeamObject()
return defenseTeamObj.getCurrentPitcherGUID()
def _appendGameEvents(self, eventList):
self.__gameEventLog += eventList
def _logGameEvent(self, eventStr):
self.__gameEventLog += [eventStr]
def _isScoreEqual(self):
return self.__HomeTeam.getRunsScored() == self.__AwayTeam.getRunsScored()
def _isEndGame(self):
if self.__inning < gsMAXGAMEINNINGS:
return False
if self._isScoreEqual():
return False
if self.__homeTeamUp and \
self.__fieldGameState.getNumOuts() == gsOUTSPERINNING:
return True
if self.__homeTeamUp and \
self.__HomeTeam.getRunsScored() > self.__AwayTeam.getRunsScored():
return True
return False
def _isEndTeamAtBat(self):
return self.__fieldGameState.isChangeSides()
#should only called if we know its the end of the inning
#should return whether end of game
def handleChangeSides(self): #"handleEndInning()
if DEBUG_GAMESTATE:
print "--- CHANGE SIDES --- (homeTeamUp:%s)" % self.__homeTeamUp
#DEBUG
#if not self._isEndTeamAtBat():
# print "Called Handled Change Sides when not isEndTeamAtBat()\n"
if self._isEndGame():
return True
if self.__homeTeamUp:
self._logGameEvent("end of inning %d." % self.__inning)
self.__inning += 1
self.__homeTeamUp = not self.__homeTeamUp
#NEW
self.__fieldGameState.reset()
return self._isEndGame()
def _getOffenseTeamObject(self):
offenseTeam = None
if self.__homeTeamUp:
offenseTeam = self.__HomeTeam
else:
offenseTeam = self.__AwayTeam
return offenseTeam
def _getDefenseTeamObject(self):
defenseTeam = None
if not self.__homeTeamUp:
defenseTeam = self.__HomeTeam
else:
defenseTeam = self.__AwayTeam
return defenseTeam
#who scored is implicit from who is up
#should return whether end of game
def handleTeamScored(self, atBatResultObj):
offenseTeam = self._getOffenseTeamObject()
runnersScoredList = atBatResultObj.getRunnersScored()
#DEBUG
#if runnersScoredList == None or len(runnersScoredList) == 0:
# print "DEBUG: handleTeamScored called but runnersScored list is empty\n"
#need to check for case of HR because if its a walkoff
#all runs score
if atBatResultObj.HomeRun():
offenseTeam.incRunsScored(len(runnersScoredList))
if len(runnersScoredList) == 1:
atBatResultObj.logAtBatEvent("%d hit a HR." % (atBatResultObj.getBatterGUID()))
else:
if len(runnersScoredList) == 4:
atBatResultObj.logAtBatEvent("%d hit a GRANDSLAM HR." % (atBatResultObj.getBatterGUID()))
else:
atBatResultObj.logAtBatEvent("%d hit a %d run HR." % (atBatResultObj.getBatterGUID(), len(runnersScoredList)))
for runnerGUID in runnersScoredList:#[0:-1]:
atBatResultObj.logAtBatEvent("%d scores." % runnerGUID)
plyr = self.__playerDB.getObjectHandle(runnerGUID)
if plyr != None:
plyr.incRunsScored()
plyr = None
return self._isEndGame()
#case of not HR, need to score them one at a time
#because if its the bottom of the ninth the score stop
#incrementing after the winning run comes in
for runnerGUID in runnersScoredList:
offenseTeam.incRunsScored(1)
plyr = self.__playerDB.getObjectHandle(runnerGUID)
if plyr != None:
plyr.incRunsScored()
plyr = None
atBatResultObj.logAtBatEvent("%d scores." % runnerGUID)
if self._isEndGame():
return True
return self._isEndGame()
#pass the number of outs just made
#should return (bool:is end of inning, bool:is end game)
#valid return states:
#(true, true)
#(true, false)
#(false, false)
def _handleOut(self, atBatResultObj):
OLD = """if atBatResultObj.StrikeOut():
self.__outs += 1
elif atBatResultObj.GroundOut():
self.__outs += 1
elif atBatResultObj.FlyOut():
self.__outs += 1
elif atBatResultObj.SacrificeOut():
self.__outs += 1
elif atBatResultObj.DoublePlay():
self.__outs += 2
elif atBatResultObj.TriplePlay():
self.__outs += 3
else:
print "handleOut received a non-Out event (%s)\n" % resultCode"""
return (self._isEndTeamAtBat(), self._isEndGame())
def getGameEvents(self):
return []
#endSim()
#don't reset state because successive
#calls to isSimDone() must
#all be true
def handleEndGame(self):
#generate appropriate events
#update pitcher record
if self.__HomeTeam.getRunsScored() > self.__AwayTeam.getRunsScored():
self.__HomeTeam.incWins()
self.__AwayTeam.incLosses()
else:
self.__HomeTeam.incLosses()
self.__AwayTeam.incWins()
#homePictcherGUID = self.__HomeTeam.getCurrentPitcherGUID()
#homeTeamGS = self.__HomeTeam.getPlayerGameStates
#plyr = self.__playerDB.getObjectHandle(homePitcherGUID)
#update team record
#don't copy Games stats into Players Stats
#person who ran the sim should pass TeamGameState to Franchise object
#print self.__gameEventLog
#print "FINAL SCORE: H:%d A:%d" % (self.__HomeTeam.getRunsScored(), self.__AwayTeam.getRunsScored())
#TODO: return 'GAMEOVER' event
return ['GAMEOVER']
#startSim()
def handleStartGame(self):
#TODO: give the pitchers the start
self.__HomeTeam.incPitcherStarts()
self.__AwayTeam.incPitcherStarts()
self.__fieldGameState.reset()
return ['GAMESTART'] #gameStateEvent('Start Game')
def handleStartAtBat(self):
self.__fieldGameState.setBatterGUID(self._getNextBatterGUID())
if DEBUG_GAMESTATE:
print " +++ HANDLE START AT BAT +++ "
print self
def handleAtBatResult(self, atBatEventObj):
if atBatEventObj.teamScored():
isGameOver = self.handleTeamScored(atBatEventObj)
#can't exit here because game could end
#on SAC out in which case we need to record the out
#handleOut
if atBatEventObj.isOut():
(endTeamAtBat, endGame) = self._handleOut(atBatEventObj)
return (endTeamAtBat, endGame)
return (False, False)
#update the play log
def handleEndAtBat(self, atBatEventObj):
teamObj = self._getOffenseTeamObject()
teamObj.advanceBattingLineup()
if DEBUG_GAMESTATE:
print self
print " --- HANDLE END AT BAT --- : %s" % atBatEventObj
#here is where you update playerGameState Stats
teamObj.updateTeamGameState(atBatEventObj, True)
defTeamObj = self._getDefenseTeamObject()
#print "Updating Defensive Team State"
defTeamObj.updateTeamGameState(atBatEventObj, False)
self._appendGameEvents(atBatEventObj.atBatEventLog())
return
#should be called before handleStartAtBat or after handleEndAtBat()
#so that the 0th element in the list is gsBASEEMPTY
def _getStateForNextAtBatEvent(self):
return self.__fieldGameState#(self.__outs, len([p for p in self.__bases[1:] if p != gsBASEEMPTY]))
#
# IMPLEMENT THE SIM RUNNER INTERFACE
#
#
def generateSimEvent(self):
batterGUID = self._getNextBatterGUID()
batterAbilities = self._getPlayerAbilities(batterGUID)
if batterAbilities == None:
print "could't get batter abilities"
return None
pitcherGUID = self._getPitcherGUID()
pitcherAbilities = self._getPlayerAbilities(pitcherGUID)
if pitcherAbilities == None:
print "could't get pitcher abilities"
return None
atBatEvent = AtBatResult.AtBatResult(self._getNextBatterGUID(),
batterAbilities,
self._getPlayerBatterSkills(batterGUID),
self._getPitcherGUID(),
pitcherAbilities,
self._getPlayerPitcherSkills(pitcherGUID),
self._getStateForNextAtBatEvent())
ret = atBatEvent.simAtBat()
if DEBUG_GAMESTATE:
print "SIM AT BAT RETURN: %s" % ret
return atBatEvent
def initSim(self):
events = self.handleStartGame()
return events
def isSimDone(self):
return self._isEndGame()
def stepSim(self):#, simEvent):
if self.isSimDone():
return "GAMEOVER"
self.handleStartAtBat()
atBatEvent = self.generateSimEvent()
(changeSides, gameOver) = self.handleAtBatResult(atBatEvent)
self.handleEndAtBat(atBatEvent)
if gameOver:
return ["GAMEOVER"] #gameStateEvent().setGameOver()
if changeSides:
self.handleChangeSides()
#if sim finish return True
#TODO: generate a real event
if len(self.__gameEventLog) > 0:
return [self.__gameEventLog[-1]] #gameEvents()
return []
def finishSim(self):
gameStateEvents = self.handleEndGame()
return gameStateEvents
#sets list of playerGUIDs that scored [3,6] in the event object
#if was a HomeRun includes man at bat in list
OLD = """def _handleAdvanceAllRunners(self, atBatResultObj):
runnersScored = []
basesReversed = self.__bases[:]
basesReversed.reverse()
if atBatResultObj.Single():
runnersScored = [basesReversed[0]]
runnersScored = [runner for runner in runnersScored if runner != gsBASEEMPTY]
self.__bases = [gsBASEEMPTY] + self.__bases[0:3]
elif atBatResultObj.Double():
runnersScored = basesReversed[0:2]
runnersScored = [runner for runner in runnersScored if runner != gsBASEEMPTY]
self.__bases = [gsBASEEMPTY]*2 + self.__bases[0:2]
elif atBatResultObj.Triple():
runnersScored = basesReversed[0:3]
runnersScored = [runner for runner in runnersScored if runner != gsBASEEMPTY]
self.__bases = [gsBASEEMPTY]*3 + [self.__bases[0]]
elif atBatResultObj.HomeRun():
runnersScored = basesReversed
runnersScored = [runner for runner in runnersScored if runner != gsBASEEMPTY]
self.__bases = [gsBASEEMPTY]*4
elif atBatResultObj.Walk() or atBatResultObj.HitByPitch():
if not self._manOn(1):
self.__bases[1] = self.__bases[0]
self.__bases[0] = gsBASEEMPTY
elif self._manOn(1) and not self._manOn(2):
self.__bases[2] = self.__bases[1]
self.__bases[1] = self.__bases[0]
self.__bases[0] = gsBASEEMPTY
elif self._manOn(1) and self._manOn(2) and not self._manOn(3):
self.__bases[3] = self.__bases[2]
self.__bases[2] = self.__bases[1]
self.__bases[1] = self.__bases[0]
self.__bases[0] = gsBASEEMPTY
elif self._manOn(1) and self._manOn(2) and self._manOn(3):
runnersScored = [self.__bases[3]]
self.__bases = [gsBASEEMPTY] + self.__bases[0:3]
else:
print "DEBUG: Hit Default Case in _handleAdvanceAllRunners()\n"
elif atBatResultObj.SacrificeOut():
runnersScored = [basesReversed[0]]
runnersScored = [runner for runner in runnersScored if runner != gsBASEEMPTY]
self.__bases = [gsBASEEMPTY]*2 + self.__bases[1:3]
atBatResultObj.setRunnersScored(runnersScored)
return #runnersScored"""