sim overhaul: more baserunner motion, less double plays, better fielders' choice text

This commit is contained in:
Sakimori 2020-12-27 02:06:43 -05:00
parent bade9c4ede
commit c91ab77b6f
2 changed files with 69 additions and 21 deletions

View File

@ -26,7 +26,7 @@ class appearance_outcomes(Enum):
strikeoutswinging = "strikes out swinging." strikeoutswinging = "strikes out swinging."
groundout = "grounds out to" groundout = "grounds out to"
flyout = "flies out to" flyout = "flies out to"
fielderschoice = "reaches on fielder's choice." fielderschoice = "reaches on fielder's choice. {} is out at {} base." #requires .format(player, base_string)
doubleplay = "grounds into a double play!" doubleplay = "grounds into a double play!"
sacrifice = "hits a sacrifice fly towards" sacrifice = "hits a sacrifice fly towards"
walk = "draws a walk." walk = "draws a walk."
@ -182,19 +182,27 @@ class game(object):
else: else:
outcome["text"] = appearance_outcomes.walk outcome["text"] = appearance_outcomes.walk
if self.bases[1] is not None and hitnum < -1.3 and self.outs != 2: if self.bases[1] is not None and hitnum < -2 and self.outs != 2:
outcome["text"] = appearance_outcomes.doubleplay outcome["text"] = appearance_outcomes.doubleplay
outcome["defender"] = "" outcome["defender"] = ""
for base in self.bases.values(): #for base in self.bases.values():
if base is not None: #if base is not None:
fc_flag = True #fc_flag = True
if fc_flag and self.outs < 2: runners = [(0,self.get_batter())]
if 0 <= hitnum and hitnum < 1.5: for base in range(1,4):
if self.bases[base] == None:
break
runners.append((base, self.bases[base]))
outcome["runners"] = runners #list of consecutive baserunners: (base number, player object)
if self.outs < 2 and len(runners) > 1: #fielder's choice replaces not great groundouts if any forceouts are present
if -1.5 <= hitnum and hitnum < 0.5: #poorly hit groundouts
outcome["text"] = appearance_outcomes.fielderschoice outcome["text"] = appearance_outcomes.fielderschoice
outcome["defender"] = "" outcome["defender"] = ""
elif 2.5 <= hitnum:
if 2.5 <= hitnum: #well hit flyouts can lead to sacrifice flies/advanced runners
if self.bases[2] is not None or self.bases[3] is not None: if self.bases[2] is not None or self.bases[3] is not None:
outcome["advance"] = True outcome["advance"] = True
else: else:
@ -237,15 +245,34 @@ class game(object):
return runs return runs
elif outcome["text"] == appearance_outcomes.fielderschoice: elif outcome["text"] == appearance_outcomes.fielderschoice:
for base in range(3, 0, -1): furthest_base, runner = outcome["runners"].pop() #get furthest baserunner
if self.bases[base] is not None: self.bases[furthest_base] = None
self.fc_out = self.bases[base] outcome["fc_out"] = (runner.name, base_string(furthest_base+1)) #runner thrown out
for movebase in range(base,1,-1): for index in range(0,len(outcome["runners"])):
self.bases[movebase] = self.bases[movebase-1] base, this_runner = outcome["runners"].pop()
break self.bases[base+1] = this_runner #includes batter, at base 0
self.bases[1] = self.get_batter() if self.bases[3] is not None and furthest_base == 1: #fielders' choice with runners on the corners
self.bases[3] = None
return 1
return 0 return 0
elif outcome["text"] == appearance_outcomes.groundout or outcome["text"] == appearance_outcomes.doubleplay:
runs = 0
if self.bases[3] is not None:
runs += 1
self.bases[3] = None
if self.bases[2] is not None:
run_roll = random.gauss(math.erf(random_star_gen("baserunning_stars", self.bases[2])-def_stat)-.5,1.5)
if run_roll > 0:
self.bases[3] = self.bases[2]
self.bases[2] = None
if self.bases[1] is not None: #double plays set this to None before this call
run_roll = random.gauss(math.erf(random_star_gen("baserunning_stars", self.bases[1])-def_stat)-.5,1.5)
if run_roll > 1:
self.bases[2] = self.bases[1]
self.bases[1] = None
return runs
elif outcome["ishit"]: elif outcome["ishit"]:
runs = 0 runs = 0
if outcome["text"] == appearance_outcomes.single: if outcome["text"] == appearance_outcomes.single:
@ -354,6 +381,8 @@ class game(object):
self.get_pitcher().game_stats["outs_pitched"] += 2 self.get_pitcher().game_stats["outs_pitched"] += 2
self.outs += 2 self.outs += 2
self.bases[1] = None self.bases[1] = None
scores_to_add += self.baserunner_check(defender, result)
self.get_batter().game_stats["rbis"] -= scores_to_add #remove the fake rbi from the player in advance
elif result["text"] == appearance_outcomes.fielderschoice: elif result["text"] == appearance_outcomes.fielderschoice:
self.get_pitcher().game_stats["outs_pitched"] += 1 self.get_pitcher().game_stats["outs_pitched"] += 1
@ -373,6 +402,11 @@ class game(object):
self.get_batter().game_stats["strikeouts_taken"] += 1 self.get_batter().game_stats["strikeouts_taken"] += 1
self.get_pitcher().game_stats["strikeouts_given"] += 1 self.get_pitcher().game_stats["strikeouts_given"] += 1
elif result["text"] == appearance_outcomes.groundout:
self.get_pitcher().game_stats["outs_pitched"] += 1
self.outs += 1
scores_to_add += self.baserunner_check(defender, result)
else: else:
self.get_pitcher().game_stats["outs_pitched"] += 1 self.get_pitcher().game_stats["outs_pitched"] += 1
self.outs += 1 self.outs += 1
@ -501,3 +535,13 @@ def search_team(search_term):
this_team = jsonpickle.decode(team_pickle[0], keys=True, classes=team) this_team = jsonpickle.decode(team_pickle[0], keys=True, classes=team)
teams.append(this_team) teams.append(this_team)
return teams return teams
def base_string(base):
if base == 1:
return "first"
elif base == 2:
return "second"
elif base == 3:
return "third"
elif base == 4:
return "fourth"

View File

@ -453,6 +453,10 @@ async def watch_game(channel, game):
if newgame.last_update[0]["defender"] != "": if newgame.last_update[0]["defender"] != "":
punc = ". " punc = ". "
if "fc_out" in newgame.last_update[0].keys():
name, base_string = newgame.last_update[0]['fc_out']
updatestring = f"{newgame.last_update[0]['batter']} {newgame.last_update[0]['text'].value.format(name, base_string)} {newgame.last_update[0]['defender']}{punc}"
else:
updatestring = f"{newgame.last_update[0]['batter']} {newgame.last_update[0]['text'].value} {newgame.last_update[0]['defender']}{punc}" updatestring = f"{newgame.last_update[0]['batter']} {newgame.last_update[0]['text'].value} {newgame.last_update[0]['defender']}{punc}"
if newgame.last_update[1] > 0: if newgame.last_update[1] > 0:
updatestring += f"{newgame.last_update[1]} runs scored!" updatestring += f"{newgame.last_update[1]} runs scored!"
@ -486,7 +490,7 @@ async def watch_game(channel, game):
newgame.gamestate_update_full() newgame.gamestate_update_full()
pause -= 1 pause -= 1
await asyncio.sleep(6) await asyncio.sleep(2)
title_string = f"{newgame.teams['away'].name} at {newgame.teams['home'].name} ended after {newgame.inning-1} innings" title_string = f"{newgame.teams['away'].name} at {newgame.teams['home'].name} ended after {newgame.inning-1} innings"
if (newgame.inning - 1) > newgame.max_innings: #if extra innings if (newgame.inning - 1) > newgame.max_innings: #if extra innings