Spaces:
Sleeping
Sleeping
Update engine/live_game_engine.py
Browse files- engine/live_game_engine.py +57 -3
engine/live_game_engine.py
CHANGED
|
@@ -18,6 +18,48 @@ def format_status(inning_half: str, current_inning: int | None, fallback: str =
|
|
| 18 |
return fallback
|
| 19 |
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
def enrich_game_from_live_feed(game: dict[str, Any], feed: dict[str, Any]) -> dict[str, Any]:
|
| 22 |
out = dict(game)
|
| 23 |
|
|
@@ -59,8 +101,18 @@ def enrich_game_from_live_feed(game: dict[str, Any], feed: dict[str, Any]) -> di
|
|
| 59 |
batter = matchup.get("batter", {}) or {}
|
| 60 |
pitcher = matchup.get("pitcher", {}) or {}
|
| 61 |
|
| 62 |
-
|
| 63 |
-
out["
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
out["away_score"] = away_team_stats.get("runs")
|
| 66 |
out["home_score"] = home_team_stats.get("runs")
|
|
@@ -110,4 +162,6 @@ def enrich_game_from_live_feed(game: dict[str, Any], feed: dict[str, Any]) -> di
|
|
| 110 |
out["away_win_prob"] = None
|
| 111 |
out["home_win_prob"] = None
|
| 112 |
|
| 113 |
-
return out
|
|
|
|
|
|
|
|
|
| 18 |
return fallback
|
| 19 |
|
| 20 |
|
| 21 |
+
def _player_name_from_id(feed: dict[str, Any], player_id: Any) -> str:
|
| 22 |
+
if player_id is None or player_id == "":
|
| 23 |
+
return ""
|
| 24 |
+
players = (feed.get("gameData", {}) or {}).get("players", {}) or {}
|
| 25 |
+
key = f"ID{player_id}"
|
| 26 |
+
player = players.get(key, {}) or {}
|
| 27 |
+
return str(player.get("fullName", "") or "").strip()
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def _extract_upcoming_hitters(
|
| 31 |
+
feed: dict[str, Any],
|
| 32 |
+
inning_half: str,
|
| 33 |
+
current_batter_id: Any,
|
| 34 |
+
) -> tuple[str, str, str]:
|
| 35 |
+
live_data = feed.get("liveData", {}) or {}
|
| 36 |
+
boxscore = live_data.get("boxscore", {}) or {}
|
| 37 |
+
teams = boxscore.get("teams", {}) or {}
|
| 38 |
+
|
| 39 |
+
batting_side = "home" if str(inning_half).strip().lower() == "bottom" else "away"
|
| 40 |
+
team_box = teams.get(batting_side, {}) or {}
|
| 41 |
+
|
| 42 |
+
batter_ids = team_box.get("batters", []) or []
|
| 43 |
+
batter_ids = [str(x) for x in batter_ids if x is not None and str(x).strip() != ""]
|
| 44 |
+
|
| 45 |
+
current_id = str(current_batter_id) if current_batter_id is not None else ""
|
| 46 |
+
|
| 47 |
+
if not batter_ids:
|
| 48 |
+
return "", "", ""
|
| 49 |
+
|
| 50 |
+
if current_id in batter_ids:
|
| 51 |
+
idx = batter_ids.index(current_id)
|
| 52 |
+
rotated = batter_ids[idx + 1 :] + batter_ids[:idx]
|
| 53 |
+
else:
|
| 54 |
+
rotated = batter_ids[:]
|
| 55 |
+
|
| 56 |
+
on_deck = _player_name_from_id(feed, rotated[0]) if len(rotated) >= 1 else ""
|
| 57 |
+
in_hole = _player_name_from_id(feed, rotated[1]) if len(rotated) >= 2 else ""
|
| 58 |
+
three_away = _player_name_from_id(feed, rotated[2]) if len(rotated) >= 3 else ""
|
| 59 |
+
|
| 60 |
+
return on_deck, in_hole, three_away
|
| 61 |
+
|
| 62 |
+
|
| 63 |
def enrich_game_from_live_feed(game: dict[str, Any], feed: dict[str, Any]) -> dict[str, Any]:
|
| 64 |
out = dict(game)
|
| 65 |
|
|
|
|
| 101 |
batter = matchup.get("batter", {}) or {}
|
| 102 |
pitcher = matchup.get("pitcher", {}) or {}
|
| 103 |
|
| 104 |
+
batter_id = batter.get("id")
|
| 105 |
+
out["batter_name"] = str(batter.get("fullName", "") or "").strip()
|
| 106 |
+
out["pitcher_name"] = str(pitcher.get("fullName", "") or "").strip()
|
| 107 |
+
|
| 108 |
+
on_deck_name, in_hole_name, three_away_name = _extract_upcoming_hitters(
|
| 109 |
+
feed=feed,
|
| 110 |
+
inning_half=inning_half,
|
| 111 |
+
current_batter_id=batter_id,
|
| 112 |
+
)
|
| 113 |
+
out["on_deck_name"] = on_deck_name
|
| 114 |
+
out["in_hole_name"] = in_hole_name
|
| 115 |
+
out["three_away_name"] = three_away_name
|
| 116 |
|
| 117 |
out["away_score"] = away_team_stats.get("runs")
|
| 118 |
out["home_score"] = home_team_stats.get("runs")
|
|
|
|
| 162 |
out["away_win_prob"] = None
|
| 163 |
out["home_win_prob"] = None
|
| 164 |
|
| 165 |
+
return out
|
| 166 |
+
|
| 167 |
+

|