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 <noreply@anthropic.com>
This commit is contained in:
20
app/games.py
20
app/games.py
@@ -179,12 +179,16 @@ def get_start_time(game):
|
|||||||
|
|
||||||
|
|
||||||
def get_power_play_info(game, team_name):
|
def get_power_play_info(game, team_name):
|
||||||
if "situation" in game and "situationDescriptions" in game["situation"]:
|
situation = game.get("situation", {})
|
||||||
for situation in game["situation"]["situationDescriptions"]:
|
if not situation:
|
||||||
if situation == "PP" and game["awayTeam"]["name"]["default"] == team_name:
|
return ""
|
||||||
return f"PP {game['situation']['timeRemaining']}"
|
time_remaining = situation.get("timeRemaining", "")
|
||||||
elif situation == "PP" and game["homeTeam"]["name"]["default"] == team_name:
|
home_descs = situation.get("homeTeam", {}).get("situationDescriptions", [])
|
||||||
return f"PP {game['situation']['timeRemaining']}"
|
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 ""
|
return ""
|
||||||
|
|
||||||
|
|
||||||
@@ -275,7 +279,9 @@ def _priority_components(game):
|
|||||||
# ── 8. Power play bonus ───────────────────────────────────────────────
|
# ── 8. Power play bonus ───────────────────────────────────────────────
|
||||||
pp_bonus = 0
|
pp_bonus = 0
|
||||||
situation = game.get("situation", {})
|
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:
|
if period >= 4:
|
||||||
pp_bonus = 200
|
pp_bonus = 200
|
||||||
elif period == 3 and time_remaining <= 300:
|
elif period == 3 and time_remaining <= 300:
|
||||||
|
|||||||
@@ -131,7 +131,8 @@ class TestGetPowerPlayInfo:
|
|||||||
def test_returns_pp_info_for_away_team(self):
|
def test_returns_pp_info_for_away_team(self):
|
||||||
game = make_game(away_name="Bruins")
|
game = make_game(away_name="Bruins")
|
||||||
game["situation"] = {
|
game["situation"] = {
|
||||||
"situationDescriptions": ["PP"],
|
"awayTeam": {"situationDescriptions": ["PP"]},
|
||||||
|
"homeTeam": {"situationDescriptions": ["SH"]},
|
||||||
"timeRemaining": "1:30",
|
"timeRemaining": "1:30",
|
||||||
}
|
}
|
||||||
assert get_power_play_info(game, "Bruins") == "PP 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):
|
def test_returns_pp_info_for_home_team(self):
|
||||||
game = make_game(home_name="Maple Leafs", away_name="Bruins")
|
game = make_game(home_name="Maple Leafs", away_name="Bruins")
|
||||||
game["situation"] = {
|
game["situation"] = {
|
||||||
"situationDescriptions": ["PP"],
|
"homeTeam": {"situationDescriptions": ["PP"]},
|
||||||
|
"awayTeam": {"situationDescriptions": ["SH"]},
|
||||||
"timeRemaining": "0:45",
|
"timeRemaining": "0:45",
|
||||||
}
|
}
|
||||||
assert get_power_play_info(game, "Maple Leafs") == "PP 0:45"
|
assert get_power_play_info(game, "Maple Leafs") == "PP 0:45"
|
||||||
@@ -287,7 +289,11 @@ class TestCalculateGamePriority:
|
|||||||
game_state="LIVE",
|
game_state="LIVE",
|
||||||
period=4,
|
period=4,
|
||||||
seconds_remaining=600,
|
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
|
assert calculate_game_priority(with_pp) - calculate_game_priority(base) == 200
|
||||||
|
|
||||||
@@ -301,7 +307,11 @@ class TestCalculateGamePriority:
|
|||||||
game_state="LIVE",
|
game_state="LIVE",
|
||||||
period=3,
|
period=3,
|
||||||
seconds_remaining=240,
|
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
|
assert calculate_game_priority(with_pp) - calculate_game_priority(base) == 150
|
||||||
|
|
||||||
@@ -315,7 +325,11 @@ class TestCalculateGamePriority:
|
|||||||
game_state="LIVE",
|
game_state="LIVE",
|
||||||
period=3,
|
period=3,
|
||||||
seconds_remaining=600,
|
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
|
assert calculate_game_priority(with_pp) - calculate_game_priority(base) == 100
|
||||||
|
|
||||||
@@ -329,7 +343,11 @@ class TestCalculateGamePriority:
|
|||||||
game_state="LIVE",
|
game_state="LIVE",
|
||||||
period=3,
|
period=3,
|
||||||
seconds_remaining=900,
|
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
|
assert calculate_game_priority(with_pp) - calculate_game_priority(base) == 50
|
||||||
|
|
||||||
@@ -343,7 +361,11 @@ class TestCalculateGamePriority:
|
|||||||
game_state="LIVE",
|
game_state="LIVE",
|
||||||
period=1,
|
period=1,
|
||||||
seconds_remaining=600,
|
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
|
assert calculate_game_priority(with_pp) - calculate_game_priority(base) == 30
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user