@@ -24,17 +24,21 @@ def get_score_stats(db: Session, season: int) -> tuple[float, float]:
2424 Reads from the dynamic match_score_YYYY tables managed by TypeORM.
2525 """
2626 table = f"match_score_{ season } "
27+ breakdown = get_breakdown (season )
2728
2829 try :
2930 result = db .execute (
30- text (f"SELECT total_points FROM { table } WHERE total_points IS NOT NULL" )
31+ text (
32+ f"SELECT { breakdown .total_column } "
33+ f"FROM { table } WHERE { breakdown .total_column } IS NOT NULL"
34+ )
3135 )
3236 scores = [row [0 ] for row in result if row [0 ] is not None ]
3337 except Exception :
38+ db .rollback ()
3439 return 100.0 , 30.0
3540
3641 if not scores :
37- breakdown = get_breakdown (season )
3842 return breakdown .score_mean , breakdown .score_sd
3943
4044 return float (np .mean (scores )), max (1.0 , float (np .std (scores )))
@@ -43,10 +47,22 @@ def get_score_stats(db: Session, season: int) -> tuple[float, float]:
4347def _get_match_scores (db : Session , season : int , event_code : str , match_id : str ) -> dict :
4448 """Fetch match scores from dynamic match_score_YYYY table."""
4549 table = f"match_score_{ season } "
50+ breakdown = get_breakdown (season )
51+
52+ def sum_expr (columns : list [str ]) -> str :
53+ if not columns :
54+ return "0"
55+ return " + " .join (f"COALESCE({ column } , 0)" for column in columns )
56+
4657 try :
4758 result = db .execute (
4859 text (
49- f"SELECT alliance, total_points, total_points_np, auto_points, dc_points, eg_points "
60+ f"SELECT alliance, "
61+ f"{ breakdown .total_column } AS total_points, "
62+ f"{ (breakdown .total_np_column or breakdown .total_column )} AS total_points_np, "
63+ f"{ sum_expr (breakdown .auto_columns )} AS auto_points, "
64+ f"{ sum_expr (breakdown .dc_columns )} AS dc_points, "
65+ f"{ sum_expr (breakdown .endgame_columns )} AS eg_points "
5066 f"FROM { table } WHERE season = :season AND event_code = :event_code AND match_id = :match_id"
5167 ),
5268 {"season" : season , "event_code" : event_code , "match_id" : match_id },
@@ -63,6 +79,7 @@ def _get_match_scores(db: Session, season: int, event_code: str, match_id: str)
6379 }
6480 return scores
6581 except Exception :
82+ db .rollback ()
6683 return {}
6784
6885
@@ -105,11 +122,13 @@ def process_season_epa(db: Session, season: int, force: bool = False) -> Dict:
105122 Match .event_code == event .code ,
106123 Match .has_been_played == True ,
107124 )
108- .order_by (Match .tournament_level , Match .match_num )
125+ .order_by (Match .tournament_level , Match .series , Match . id )
109126 .all ()
110127 )
111128
112129 for match in matches :
130+ match_key = str (match .id )
131+
113132 # Get team participation
114133 tmps = (
115134 db .query (TeamMatchParticipation )
@@ -170,7 +189,7 @@ def process_season_epa(db: Session, season: int, force: bool = False) -> Dict:
170189 EpaTeamMatch .team_number == team_num ,
171190 EpaTeamMatch .season == season ,
172191 EpaTeamMatch .event_code == event .code ,
173- EpaTeamMatch .match_id == match . id ,
192+ EpaTeamMatch .match_id == match_key ,
174193 )
175194 .first ()
176195 )
@@ -179,7 +198,7 @@ def process_season_epa(db: Session, season: int, force: bool = False) -> Dict:
179198 team_number = team_num ,
180199 season = season ,
181200 event_code = event .code ,
182- match_id = match . id ,
201+ match_id = match_key ,
183202 alliance = alliance ,
184203 )
185204 db .add (etm )
@@ -223,7 +242,7 @@ def process_season_epa(db: Session, season: int, force: bool = False) -> Dict:
223242 EpaTeamMatch .team_number == team_num ,
224243 EpaTeamMatch .season == season ,
225244 EpaTeamMatch .event_code == event .code ,
226- EpaTeamMatch .match_id == match . id ,
245+ EpaTeamMatch .match_id == match_key ,
227246 )
228247 .first ()
229248 )
0 commit comments