good luck
This commit is contained in:
@@ -1,6 +1,12 @@
|
||||
import logging
|
||||
import sqlite3
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from app.config import DB_PATH
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def process_record(record):
|
||||
if record == "N/A":
|
||||
return "N/A"
|
||||
@@ -9,6 +15,7 @@ def process_record(record):
|
||||
formatted_parts = [part.zfill(2) for part in parts]
|
||||
return "-".join(formatted_parts)
|
||||
|
||||
|
||||
def extract_game_info(scoreboard_data):
|
||||
if not scoreboard_data:
|
||||
return []
|
||||
@@ -16,36 +23,60 @@ def extract_game_info(scoreboard_data):
|
||||
extracted_info = []
|
||||
for game in scoreboard_data.get("games", []):
|
||||
game_state = convert_game_state(game["gameState"])
|
||||
extracted_info.append({
|
||||
"Home Team": game["homeTeam"]["name"]["default"],
|
||||
"Home Score": game["homeTeam"]["score"] if game_state != "PRE" else "N/A",
|
||||
"Away Team": game["awayTeam"]["name"]["default"],
|
||||
"Away Score": game["awayTeam"]["score"] if game_state != "PRE" else "N/A",
|
||||
"Home Logo": game["homeTeam"]["logo"],
|
||||
"Away Logo": game["awayTeam"]["logo"],
|
||||
"Game State": game_state,
|
||||
"Period": process_period(game),
|
||||
"Time Remaining": process_time_remaining(game),
|
||||
"Time Running": game["clock"]["running"] if game_state == "LIVE" else "N/A",
|
||||
"Intermission": game["clock"]["inIntermission"] if game_state == "LIVE" else "N/A",
|
||||
"Priority": calculate_game_priority(game),
|
||||
"Start Time": process_start_time(game),
|
||||
"Home Record": process_record(game["homeTeam"]["record"]) if game["gameState"] in ["PRE", "FUT"] else "N/A",
|
||||
"Away Record": process_record(game["awayTeam"]["record"]) if game["gameState"] in ["PRE", "FUT"] else "N/A",
|
||||
"Home Shots": game["homeTeam"]["sog"] if game["gameState"] not in ["PRE", "FUT"] else 0,
|
||||
"Away Shots": game["awayTeam"]["sog"] if game["gameState"] not in ["PRE", "FUT"] else 0,
|
||||
"Home Power Play": get_power_play_info(game, game["homeTeam"]["name"]["default"]),
|
||||
"Away Power Play": get_power_play_info(game, game["awayTeam"]["name"]["default"]),
|
||||
"Last Period Type": get_game_outcome(game, game_state)
|
||||
})
|
||||
extracted_info.append(
|
||||
{
|
||||
"Home Team": game["homeTeam"]["name"]["default"],
|
||||
"Home Score": game["homeTeam"]["score"]
|
||||
if game_state != "PRE"
|
||||
else "N/A",
|
||||
"Away Team": game["awayTeam"]["name"]["default"],
|
||||
"Away Score": game["awayTeam"]["score"]
|
||||
if game_state != "PRE"
|
||||
else "N/A",
|
||||
"Home Logo": game["homeTeam"]["logo"],
|
||||
"Away Logo": game["awayTeam"]["logo"],
|
||||
"Game State": game_state,
|
||||
"Period": process_period(game),
|
||||
"Time Remaining": process_time_remaining(game),
|
||||
"Time Running": game["clock"]["running"]
|
||||
if game_state == "LIVE"
|
||||
else "N/A",
|
||||
"Intermission": game["clock"]["inIntermission"]
|
||||
if game_state == "LIVE"
|
||||
else "N/A",
|
||||
"Priority": calculate_game_priority(game),
|
||||
"Start Time": process_start_time(game),
|
||||
"Home Record": process_record(game["homeTeam"]["record"])
|
||||
if game["gameState"] in ["PRE", "FUT"]
|
||||
else "N/A",
|
||||
"Away Record": process_record(game["awayTeam"]["record"])
|
||||
if game["gameState"] in ["PRE", "FUT"]
|
||||
else "N/A",
|
||||
"Home Shots": game["homeTeam"]["sog"]
|
||||
if game["gameState"] not in ["PRE", "FUT"]
|
||||
else 0,
|
||||
"Away Shots": game["awayTeam"]["sog"]
|
||||
if game["gameState"] not in ["PRE", "FUT"]
|
||||
else 0,
|
||||
"Home Power Play": get_power_play_info(
|
||||
game, game["homeTeam"]["name"]["default"]
|
||||
),
|
||||
"Away Power Play": get_power_play_info(
|
||||
game, game["awayTeam"]["name"]["default"]
|
||||
),
|
||||
"Last Period Type": get_game_outcome(game, game_state),
|
||||
}
|
||||
)
|
||||
|
||||
# Sort games based on priority
|
||||
return sorted(extracted_info, key=lambda x: x["Priority"], reverse=True)
|
||||
|
||||
|
||||
def convert_game_state(game_state):
|
||||
state_mapping = {"OFF": "FINAL", "CRIT": "LIVE", "FUT": "PRE"}
|
||||
return state_mapping.get(game_state, game_state)
|
||||
|
||||
|
||||
def process_period(game):
|
||||
if game["gameState"] in ["PRE", "FUT"]:
|
||||
return 0
|
||||
@@ -54,6 +85,7 @@ def process_period(game):
|
||||
else:
|
||||
return game["periodDescriptor"]["number"]
|
||||
|
||||
|
||||
def process_time_remaining(game):
|
||||
if game["gameState"] in ["PRE", "FUT"]:
|
||||
return "20:00"
|
||||
@@ -63,17 +95,16 @@ def process_time_remaining(game):
|
||||
time_remaining = game["clock"]["timeRemaining"]
|
||||
return "END" if time_remaining == "00:00" else time_remaining
|
||||
|
||||
|
||||
def process_start_time(game):
|
||||
if game["gameState"] in ["PRE", "FUT"]:
|
||||
utc_time = game["startTimeUTC"]
|
||||
est_time = utc_to_est_time(utc_time)
|
||||
# Check if the hour starts with a zero
|
||||
if est_time.startswith("0"):
|
||||
est_time = est_time[1:] # Drop the leading zero
|
||||
return est_time
|
||||
return est_time.lstrip("0")
|
||||
else:
|
||||
return "N/A"
|
||||
|
||||
|
||||
def get_power_play_info(game, team_name):
|
||||
if "situation" in game and "situationDescriptions" in game["situation"]:
|
||||
for situation in game["situation"]["situationDescriptions"]:
|
||||
@@ -83,14 +114,16 @@ def get_power_play_info(game, team_name):
|
||||
return f"PP {game['situation']['timeRemaining']}"
|
||||
return ""
|
||||
|
||||
|
||||
def get_game_outcome(game, game_state):
|
||||
return game["gameOutcome"]["lastPeriodType"] if game_state == "FINAL" else "N/A"
|
||||
|
||||
|
||||
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)
|
||||
@@ -104,8 +137,14 @@ def calculate_game_priority(game):
|
||||
away_team_standings = get_team_standings(game["awayTeam"]["name"]["default"])
|
||||
|
||||
# Calculate total values of leagueSequence + leagueL10Sequence for each team
|
||||
home_total = home_team_standings["league_sequence"] + home_team_standings["league_l10_sequence"]
|
||||
away_total = away_team_standings["league_sequence"] + away_team_standings["league_l10_sequence"]
|
||||
home_total = (
|
||||
home_team_standings["league_sequence"]
|
||||
+ home_team_standings["league_l10_sequence"]
|
||||
)
|
||||
away_total = (
|
||||
away_team_standings["league_sequence"]
|
||||
+ away_team_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)
|
||||
@@ -123,11 +162,11 @@ def calculate_game_priority(game):
|
||||
score_differential_adjustment += 350
|
||||
elif score_difference > 1:
|
||||
score_differential_adjustment += 100
|
||||
|
||||
|
||||
if period == 3 and time_remaining <= 300:
|
||||
score_differential_adjustment = score_differential_adjustment * 2
|
||||
|
||||
base_priority -= score_differential_adjustment
|
||||
|
||||
base_priority -= score_differential_adjustment
|
||||
|
||||
# Adjust base priority based on certain conditions
|
||||
if period == 3 and time_remaining <= 720:
|
||||
@@ -142,40 +181,51 @@ 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
|
||||
|
||||
print(base_priority)
|
||||
print(time_priority)
|
||||
print(matchup_adjustment)
|
||||
print(score_total)
|
||||
logger.debug(
|
||||
"priority components — base: %s, time: %s, matchup: %s, score_total: %s",
|
||||
base_priority,
|
||||
time_priority,
|
||||
matchup_adjustment,
|
||||
score_total,
|
||||
)
|
||||
|
||||
# Calculate the final priority
|
||||
final_priority = int(base_priority + time_priority - matchup_adjustment + score_total)
|
||||
final_priority = int(
|
||||
base_priority + time_priority - matchup_adjustment + score_total
|
||||
)
|
||||
|
||||
# Pushes the games that are in intermission to the bottom, but retains their sort
|
||||
if game["clock"]["inIntermission"]:
|
||||
return (-2000 - time_remaining)
|
||||
return -2000 - time_remaining
|
||||
|
||||
return final_priority
|
||||
|
||||
|
||||
def get_team_standings(team_name):
|
||||
conn = sqlite3.connect("app/data/nhl_standings.db")
|
||||
conn = sqlite3.connect(DB_PATH)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("""
|
||||
SELECT league_sequence, league_l10_sequence
|
||||
FROM standings
|
||||
cursor.execute(
|
||||
"""
|
||||
SELECT league_sequence, league_l10_sequence
|
||||
FROM standings
|
||||
WHERE team_common_name = ?
|
||||
""", (team_name,))
|
||||
""",
|
||||
(team_name,),
|
||||
)
|
||||
result = cursor.fetchone()
|
||||
conn.close()
|
||||
return {"league_sequence": result[0] if result else 0, "league_l10_sequence": result[1] if result else 0}
|
||||
return {
|
||||
"league_sequence": result[0] if result else 0,
|
||||
"league_l10_sequence": result[1] if result else 0,
|
||||
}
|
||||
|
||||
|
||||
def utc_to_est_time(utc_time):
|
||||
utc_datetime = datetime.strptime(utc_time, "%Y-%m-%dT%H:%M:%SZ")
|
||||
est_offset = timedelta(hours=-4)
|
||||
est_datetime = utc_datetime + est_offset
|
||||
return est_datetime.strftime("%#I:%M %p")
|
||||
return est_datetime.strftime("%I:%M %p")
|
||||
|
||||
Reference in New Issue
Block a user