Jon Solow commited on
Commit
7181bcd
·
1 Parent(s): f81bfa4

Implement points against and team win stats

Browse files
Files changed (2) hide show
  1. src/domain/playoffs.py +2 -0
  2. src/stats.py +43 -3
src/domain/playoffs.py CHANGED
@@ -74,4 +74,6 @@ PLAYOFF_TEAM_DEF_PLAYER: list[tuple[teams.NFLTeam, str]] = [
74
  ]
75
 
76
  SHORT_TEAM_NAMES_TO_DEFENSE_PLAYER_ID = {t.team_short_name: p for t, p in PLAYOFF_TEAM_DEF_PLAYER}
 
77
  ROSTER_TEAM_NAMES_TO_DEFENSE_PLAYER_ID = {t.rosters_short_name: p for t, p in PLAYOFF_TEAM_DEF_PLAYER}
 
 
74
  ]
75
 
76
  SHORT_TEAM_NAMES_TO_DEFENSE_PLAYER_ID = {t.team_short_name: p for t, p in PLAYOFF_TEAM_DEF_PLAYER}
77
+
78
  ROSTER_TEAM_NAMES_TO_DEFENSE_PLAYER_ID = {t.rosters_short_name: p for t, p in PLAYOFF_TEAM_DEF_PLAYER}
79
+ DEFENSE_PLAYER_ID_TO_ROSTER_TEAM_NAMES = {v: k for k, v in ROSTER_TEAM_NAMES_TO_DEFENSE_PLAYER_ID.items()}
src/stats.py CHANGED
@@ -7,6 +7,7 @@ import streamlit as st
7
  from domain.constants import SEASON
8
  from domain.playoffs import (
9
  SHORT_TEAM_NAMES_TO_DEFENSE_PLAYER_ID,
 
10
  ROSTER_TEAM_NAMES_TO_DEFENSE_PLAYER_ID,
11
  PLAYOFF_TEAM_DEF_PLAYER,
12
  )
@@ -435,8 +436,47 @@ def get_schedule_with_live() -> dict[int, dict[str, dict[str, str | int | pd.Tim
435
  return schedule
436
 
437
 
438
- def add_points_against_stat(stat_map):
439
- pass
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
440
 
441
 
442
  @st.cache_data(ttl=STAT_CACHE_SECONDS)
@@ -452,7 +492,7 @@ def get_stats_map() -> dict[int, dict[str, dict[str, float]]]:
452
  for player_id, player_stats in week_stats.items():
453
  stat_map[week][player_id] = player_stats
454
 
455
- add_points_against_stat(stat_map)
456
  stat_overrides = get_stat_overrides()
457
  # for stat overrides, override at the stat level
458
  for week, week_stats in stat_overrides.items():
 
7
  from domain.constants import SEASON
8
  from domain.playoffs import (
9
  SHORT_TEAM_NAMES_TO_DEFENSE_PLAYER_ID,
10
+ DEFENSE_PLAYER_ID_TO_ROSTER_TEAM_NAMES,
11
  ROSTER_TEAM_NAMES_TO_DEFENSE_PLAYER_ID,
12
  PLAYOFF_TEAM_DEF_PLAYER,
13
  )
 
436
  return schedule
437
 
438
 
439
+ def add_points_against_team_win_stat(stat_map: dict[int, dict[str, dict[str, float]]]):
440
+ schedule = get_schedule_with_live()
441
+
442
+ for week, week_map in stat_map.items():
443
+ for player_id in week_map.keys():
444
+ if team_short_name := DEFENSE_PLAYER_ID_TO_ROSTER_TEAM_NAMES.get(player_id):
445
+ team_game = schedule[week][team_short_name]
446
+ opponent_team = str(team_game["opponent"])
447
+ opponent_player_id = ROSTER_TEAM_NAMES_TO_DEFENSE_PLAYER_ID[opponent_team]
448
+ opponent_def_stats = week_map[opponent_player_id]
449
+
450
+ if isinstance((opponent_score_str := team_game["opponent_score"]), pd.Timestamp):
451
+ # another case not possible but makes mypy happy
452
+ continue
453
+ opponent_score = float(opponent_score_str) # noqa
454
+
455
+ opponent_def_points_scored = (
456
+ opponent_def_stats.get(TWO_PT.key, 0.0) * 2.0
457
+ + opponent_def_stats.get(DEF_TD.key, 0.0) * 6.0
458
+ + opponent_def_stats.get(ST_TD.key, 0.0) * 6.0
459
+ )
460
+ points_allowed = opponent_score - opponent_def_points_scored
461
+
462
+ if points_allowed == 0:
463
+ stat_map[week][player_id].update({PTS_ALLOW_0.key: 1})
464
+ elif points_allowed < 7:
465
+ stat_map[week][player_id].update({PTS_ALLOW_1_6.key: 1})
466
+ elif points_allowed < 14:
467
+ stat_map[week][player_id].update({PTS_ALLOW_7_13.key: 1})
468
+ elif points_allowed < 21:
469
+ stat_map[week][player_id].update({PTS_ALLOW_14_20.key: 1})
470
+ elif points_allowed < 28:
471
+ stat_map[week][player_id].update({PTS_ALLOW_21_27.key: 1})
472
+ elif points_allowed < 35:
473
+ stat_map[week][player_id].update({PTS_ALLOW_28_34.key: 1})
474
+ else:
475
+ stat_map[week][player_id].update({PTS_ALLOW_35_.key: 1})
476
+
477
+ # check for win
478
+ if team_game["status"] == "Win":
479
+ stat_map[week][player_id].update({TEAM_WIN.key: 1})
480
 
481
 
482
  @st.cache_data(ttl=STAT_CACHE_SECONDS)
 
492
  for player_id, player_stats in week_stats.items():
493
  stat_map[week][player_id] = player_stats
494
 
495
+ add_points_against_team_win_stat(stat_map)
496
  stat_overrides = get_stat_overrides()
497
  # for stat overrides, override at the stat level
498
  for week, week_stats in stat_overrides.items():