feat: overhaul hype scoring algorithm
All checks were successful
CI / Lint (push) Successful in 6s
CI / Test (push) Successful in 6s
CI / Build & Push (push) Successful in 15s

- Period base: playoff OT escalates indefinitely (P4=600, P5=750…),
  reg season P4=5-min OT (600), P5=shootout (700)
- Time priority range increased (max ~300 vs old ~120), calibrated
  to period length so 5-min reg season OT reads correctly
- Matchup multiplier inverted: higher period = less weight (any OT
  is exciting regardless of teams)
- Replace unconditional score_total with closeness bonus: rewards
  tight games regardless of goal volume (5-4 == 1-0 at same diff)
- Power play bonus: 30 (P1/P2) → 50/100/150 (P3 by time) → 200 (OT)
- Comeback bonus: one-time pulse (+50/75/100 by period) when trailing
  team scores to within 2 goals; keyed on team names, clears after
  firing, skips intermission

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-29 14:46:10 -04:00
parent 62afc1001e
commit e2d2c7dd97
3 changed files with 304 additions and 34 deletions

View File

@@ -9,6 +9,10 @@ EASTERN = ZoneInfo("America/New_York")
logger = logging.getLogger(__name__)
# Maps (home_team_name, away_team_name) -> (home_score, away_score)
# Used to detect when the trailing team just scored.
_score_cache: dict[tuple[str, str], tuple[int, int]] = {}
def format_record(record):
if record == "N/A":
@@ -39,6 +43,7 @@ def parse_games(scoreboard_data):
"Home Logo": game["homeTeam"]["logo"],
"Away Logo": game["awayTeam"]["logo"],
"Game State": game_state,
"Game Type": game.get("gameType", 2),
"Period": get_period(game),
"Time Remaining": get_time_remaining(game),
"Time Running": game["clock"]["running"]
@@ -47,7 +52,7 @@ def parse_games(scoreboard_data):
"Intermission": game["clock"]["inIntermission"]
if game_state == "LIVE"
else "N/A",
"Priority": calculate_game_priority(game),
"Priority": calculate_game_priority(game) + get_comeback_bonus(game),
"Start Time": get_start_time(game),
"Home Record": format_record(game["homeTeam"]["record"])
if game["gameState"] in ["PRE", "FUT"]
@@ -75,6 +80,52 @@ def parse_games(scoreboard_data):
return sorted(extracted_info, key=lambda x: x["Priority"], reverse=True)
def get_comeback_bonus(game):
"""
Returns a one-time bonus when the trailing team just scored and the game
is still within reach (score_diff <= 2). Updates _score_cache.
Returns 0 for intermission, non-live games, or no cache entry yet.
"""
if game["gameState"] not in ("LIVE", "CRIT"):
return 0
if game["clock"]["inIntermission"]:
return 0
home_name = game["homeTeam"]["name"]["default"]
away_name = game["awayTeam"]["name"]["default"]
key = (home_name, away_name)
home_score = game["homeTeam"]["score"]
away_score = game["awayTeam"]["score"]
period = game.get("periodDescriptor", {}).get("number", 0)
bonus = 0
if key in _score_cache:
prev_home, prev_away = _score_cache[key]
prev_diff = abs(prev_home - prev_away)
new_diff = abs(home_score - away_score)
trailing_scored = (
new_diff < prev_diff
and new_diff <= 2
and (
(prev_home < prev_away and home_score > prev_home)
or (prev_away < prev_home and away_score > prev_away)
)
)
if trailing_scored:
if period >= 4:
bonus = 100
elif period == 3:
bonus = 75
else:
bonus = 50
_score_cache[key] = (home_score, away_score)
return bonus
def convert_game_state(game_state):
state_mapping = {"OFF": "FINAL", "CRIT": "LIVE", "FUT": "PRE"}
return state_mapping.get(game_state, game_state)
@@ -123,55 +174,59 @@ def get_game_outcome(game, game_state):
def calculate_game_priority(game):
# Return 0 if game is in certain states
if game["gameState"] in ["FINAL", "OFF", "PRE", "FUT"]:
return 0
# Get period, time remaining, scores, and other relevant data
period = game.get("periodDescriptor", {}).get("number", 0)
time_remaining = game.get("clock", {}).get("secondsRemaining", 0)
home_score = game["homeTeam"]["score"]
away_score = game["awayTeam"]["score"]
score_difference = abs(home_score - away_score)
score_total = (home_score + away_score) * 20
is_playoff = game.get("gameType", 2) == 3
# Get standings for home and away teams
home_team_standings = get_team_standings(game["homeTeam"]["name"]["default"])
away_team_standings = get_team_standings(game["awayTeam"]["name"]["default"])
# ── 1. Base priority by period ────────────────────────────────────────
if is_playoff:
# Playoffs: P4+ are full 20-min OTs that escalate indefinitely
base_priority = {1: 150, 2: 200, 3: 350}.get(period, 600 + (period - 4) * 150)
else:
# Regular season: P4=5-min OT, P5=shootout
base_priority = {1: 150, 2: 200, 3: 350, 4: 600, 5: 700}.get(period, 150)
# Calculate total values of leagueSequence + leagueL10Sequence for each team
# ── 2. Period length for time calculations ────────────────────────────
if period >= 4:
period_length = 1200 if is_playoff else 300
else:
period_length = 1200
# ── 3. Standings-quality matchup adjustment ───────────────────────────
home_standings = get_team_standings(game["homeTeam"]["name"]["default"])
away_standings = get_team_standings(game["awayTeam"]["name"]["default"])
home_total = (
home_team_standings["league_sequence"]
+ home_team_standings["league_l10_sequence"]
home_standings["league_sequence"] + home_standings["league_l10_sequence"]
)
away_total = (
away_team_standings["league_sequence"]
+ away_team_standings["league_l10_sequence"]
away_standings["league_sequence"] + away_standings["league_l10_sequence"]
)
# Calculate the matchup adjustment factor
matchup_multiplier = {5: 1, 4: 1, 3: 1.50, 2: 1.65, 1: 2}.get(period)
# Higher period = matchup matters less (any OT is exciting regardless of teams)
matchup_multiplier = {1: 2.0, 2: 1.65, 3: 1.50, 4: 1.0}.get(period, 1.0)
matchup_adjustment = (home_total + away_total) * matchup_multiplier
# Calculate the base priority based on period
base_priority = {5: 650, 4: 600, 3: 300, 2: 200}.get(period, 150)
# Adjust base priority based on score difference
score_differential_adjustment = 0
# ── 4. Score-differential penalty ────────────────────────────────────
if score_difference > 3:
score_differential_adjustment += 500
score_differential_adjustment = 500
elif score_difference > 2:
score_differential_adjustment += 350
score_differential_adjustment = 350
elif score_difference > 1:
score_differential_adjustment += 100
score_differential_adjustment = 100
else:
score_differential_adjustment = 0
if period == 3 and time_remaining <= 300:
score_differential_adjustment = score_differential_adjustment * 2
score_differential_adjustment *= 2
base_priority -= score_differential_adjustment
# Adjust base priority based on certain conditions
# ── 5. Late-3rd urgency bonus ─────────────────────────────────────────
if period == 3 and time_remaining <= 720:
if score_difference == 0:
base_priority += 100
@@ -184,24 +239,46 @@ def calculate_game_priority(game):
elif score_difference == 1:
base_priority += 30
# Calculate time priority
time_multiplier = {4: 2, 3: 2, 2: 1.5}.get(period, 0.75)
time_priority = ((1200 - time_remaining) / 20) * time_multiplier
# ── 6. Closeness bonus (replaces unconditional score_total) ──────────
# Rewards tight games regardless of total goals scored
closeness_bonus = {0: 80, 1: 50, 2: 20}.get(score_difference, 0)
# ── 7. Time priority ─────────────────────────────────────────────────
# Calibrated to period length so deep-into-period signal is meaningful
# for both 5-min reg season OT and 20-min playoff OT
time_multiplier = {4: 5, 3: 5, 2: 3}.get(period, 2.0 if period >= 5 else 1.5)
time_priority = ((period_length - time_remaining) / 20) * time_multiplier
# ── 8. Power play bonus ───────────────────────────────────────────────
pp_bonus = 0
situation = game.get("situation", {})
if "PP" in situation.get("situationDescriptions", []):
if period >= 4:
pp_bonus = 200
elif period == 3 and time_remaining <= 300:
pp_bonus = 150
elif period == 3 and time_remaining <= 720:
pp_bonus = 100
elif period == 3:
pp_bonus = 50
else:
pp_bonus = 30
logger.debug(
"priority components — base: %s, time: %s, matchup: %s, score_total: %s",
"priority components — base: %s, time: %.0f, matchup: %.0f, "
"closeness: %s, pp: %s",
base_priority,
time_priority,
matchup_adjustment,
score_total,
closeness_bonus,
pp_bonus,
)
# Calculate the final priority
final_priority = int(
base_priority + time_priority - matchup_adjustment + score_total
base_priority + time_priority - matchup_adjustment + closeness_bonus + pp_bonus
)
# Pushes the games that are in intermission to the bottom, but retains their sort
# Pushes intermission games to the bottom, retains relative sort order
if game["clock"]["inIntermission"]:
return -2000 - time_remaining