From 8e1c455deda6dbe7dfc361650b51c688ae398362 Mon Sep 17 00:00:00 2001 From: josh Date: Sun, 29 Mar 2026 19:37:31 -0400 Subject: [PATCH] fix: correct NHL API situation structure for power play detection The NHL API nests situationDescriptions under situation.homeTeam / situation.awayTeam, not at the top level. The old flat-structure lookup always returned an empty list, silently breaking both the PP indicator on the frontend and the PP bonus in the hype score. Updated get_power_play_info, the _priority_components PP check, and all test fixtures to match the real API shape. Co-Authored-By: Claude Sonnet 4.6 --- app/games.py | 20 +++++++++++++------- tests/test_games.py | 36 +++++++++++++++++++++++++++++------- 2 files changed, 42 insertions(+), 14 deletions(-) diff --git a/app/games.py b/app/games.py index f409f26..ec5524f 100644 --- a/app/games.py +++ b/app/games.py @@ -179,12 +179,16 @@ def get_start_time(game): def get_power_play_info(game, team_name): - if "situation" in game and "situationDescriptions" in game["situation"]: - for situation in game["situation"]["situationDescriptions"]: - if situation == "PP" and game["awayTeam"]["name"]["default"] == team_name: - return f"PP {game['situation']['timeRemaining']}" - elif situation == "PP" and game["homeTeam"]["name"]["default"] == team_name: - return f"PP {game['situation']['timeRemaining']}" + situation = game.get("situation", {}) + if not situation: + return "" + time_remaining = situation.get("timeRemaining", "") + home_descs = situation.get("homeTeam", {}).get("situationDescriptions", []) + away_descs = situation.get("awayTeam", {}).get("situationDescriptions", []) + if "PP" in home_descs and game["homeTeam"]["name"]["default"] == team_name: + return f"PP {time_remaining}" + if "PP" in away_descs and game["awayTeam"]["name"]["default"] == team_name: + return f"PP {time_remaining}" return "" @@ -275,7 +279,9 @@ def _priority_components(game): # ── 8. Power play bonus ─────────────────────────────────────────────── pp_bonus = 0 situation = game.get("situation", {}) - if "PP" in situation.get("situationDescriptions", []): + home_descs = situation.get("homeTeam", {}).get("situationDescriptions", []) + away_descs = situation.get("awayTeam", {}).get("situationDescriptions", []) + if "PP" in home_descs or "PP" in away_descs: if period >= 4: pp_bonus = 200 elif period == 3 and time_remaining <= 300: diff --git a/tests/test_games.py b/tests/test_games.py index a288976..aa98f10 100644 --- a/tests/test_games.py +++ b/tests/test_games.py @@ -131,7 +131,8 @@ class TestGetPowerPlayInfo: def test_returns_pp_info_for_away_team(self): game = make_game(away_name="Bruins") game["situation"] = { - "situationDescriptions": ["PP"], + "awayTeam": {"situationDescriptions": ["PP"]}, + "homeTeam": {"situationDescriptions": ["SH"]}, "timeRemaining": "1:30", } assert get_power_play_info(game, "Bruins") == "PP 1:30" @@ -139,7 +140,8 @@ class TestGetPowerPlayInfo: def test_returns_pp_info_for_home_team(self): game = make_game(home_name="Maple Leafs", away_name="Bruins") game["situation"] = { - "situationDescriptions": ["PP"], + "homeTeam": {"situationDescriptions": ["PP"]}, + "awayTeam": {"situationDescriptions": ["SH"]}, "timeRemaining": "0:45", } assert get_power_play_info(game, "Maple Leafs") == "PP 0:45" @@ -287,7 +289,11 @@ class TestCalculateGamePriority: game_state="LIVE", period=4, seconds_remaining=600, - situation={"situationDescriptions": ["PP"], "timeRemaining": "1:30"}, + situation={ + "homeTeam": {"situationDescriptions": ["PP"]}, + "awayTeam": {"situationDescriptions": ["SH"]}, + "timeRemaining": "1:30", + }, ) assert calculate_game_priority(with_pp) - calculate_game_priority(base) == 200 @@ -301,7 +307,11 @@ class TestCalculateGamePriority: game_state="LIVE", period=3, seconds_remaining=240, - situation={"situationDescriptions": ["PP"], "timeRemaining": "1:30"}, + situation={ + "homeTeam": {"situationDescriptions": ["PP"]}, + "awayTeam": {"situationDescriptions": ["SH"]}, + "timeRemaining": "1:30", + }, ) assert calculate_game_priority(with_pp) - calculate_game_priority(base) == 150 @@ -315,7 +325,11 @@ class TestCalculateGamePriority: game_state="LIVE", period=3, seconds_remaining=600, - situation={"situationDescriptions": ["PP"], "timeRemaining": "1:30"}, + situation={ + "homeTeam": {"situationDescriptions": ["PP"]}, + "awayTeam": {"situationDescriptions": ["SH"]}, + "timeRemaining": "1:30", + }, ) assert calculate_game_priority(with_pp) - calculate_game_priority(base) == 100 @@ -329,7 +343,11 @@ class TestCalculateGamePriority: game_state="LIVE", period=3, seconds_remaining=900, - situation={"situationDescriptions": ["PP"], "timeRemaining": "1:30"}, + situation={ + "homeTeam": {"situationDescriptions": ["PP"]}, + "awayTeam": {"situationDescriptions": ["SH"]}, + "timeRemaining": "1:30", + }, ) assert calculate_game_priority(with_pp) - calculate_game_priority(base) == 50 @@ -343,7 +361,11 @@ class TestCalculateGamePriority: game_state="LIVE", period=1, seconds_remaining=600, - situation={"situationDescriptions": ["PP"], "timeRemaining": "1:30"}, + situation={ + "homeTeam": {"situationDescriptions": ["PP"]}, + "awayTeam": {"situationDescriptions": ["SH"]}, + "timeRemaining": "1:30", + }, ) assert calculate_game_priority(with_pp) - calculate_game_priority(base) == 30