Skip to content

Instantly share code, notes, and snippets.

@slavfox
Created September 12, 2020 17:38
Show Gist options
  • Save slavfox/e9f03377cb307303f512400a0689bbbe to your computer and use it in GitHub Desktop.
Save slavfox/e9f03377cb307303f512400a0689bbbe to your computer and use it in GitHub Desktop.
STRIKEOUT = 200
HIT = 200
HOMERUN = 1000
SHUTOUT = 10_000
import requests
from collections import defaultdict
# saved full S6 game_events list from the datablase
with open("game_events.json") as f:
import json
game_events = json.load(f)
payouts = defaultdict(
lambda: {"hits": 0, "homeruns": 0, "shutouts": 0, "strikeouts": 0}
)
# {game_uuid: {"home": pitcher_uuid, "away": pitcher_uuid}
gameid_to_pitchers = {}
# Run through the events once to get the pitchers for each game
for event in game_events["game_events"]:
if event["pitcher_id"]:
if event["top_of_inning"]:
gameid_to_pitchers.setdefault(event["game_id"], {})["home"] = event[
"pitcher_id"
]
else:
gameid_to_pitchers.setdefault(event["game_id"], {})["away"] = event[
"pitcher_id"
]
for event in game_events["game_events"]:
if event["event_type"] == "STRIKEOUT":
payouts[event["pitcher_id"]]["strikeouts"] += 1
elif event["event_type"] in {"SINGLE", "DOUBLE", "TRIPLE", "HOME_RUN"}:
payouts[event["batter_id"]]["hits"] += 1
if event["event_type"] == "HOME_RUN":
payouts[event["batter_id"]]["homeruns"] += 1
if event["is_last_game_event"] is True:
if int(event["home_score"]) == 0:
payouts[gameid_to_pitchers[event["game_id"]]["away"]]["shutouts"] += 1
elif int(event["away_score"]) == 0:
payouts[gameid_to_pitchers[event["game_id"]]["home"]]["shutouts"] += 1
from itertools import zip_longest
# thanks, stack overflow
def grouper(iterable, n, fillvalue=None):
args = [iter(iterable)] * n
return zip_longest(*args, fillvalue=fillvalue)
player_names_to_payouts = {}
payouts.pop("UNKNOWN", None) # lol
def payout(player):
return (
(player["hits"] * HIT)
+ (player["strikeouts"] * STRIKEOUT)
+ (player["homeruns"] * HOMERUN)
+ (player["shutouts"] * SHUTOUT)
)
for chunk in grouper([*payouts.keys()], 50):
response = requests.get(
"https://blaseball.com/database/players",
params={"ids": ",".join(filter(None, chunk))},
)
for player in response.json():
payouts[player["id"]]["name"] = player["name"]
payouts[player["id"]]["payout"] = payout(payouts[player["id"]])
import csv
with open("pendant_payouts.csv", "w", newline="") as csvfile:
writer = csv.DictWriter(
csvfile,
fieldnames=["name", "hits", "strikeouts", "homeruns", "shutouts", "payout"],
)
writer.writeheader()
writer.writerows(payouts.values())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment