Jon Solow commited on
Commit
4bfd7b8
·
1 Parent(s): b7c2e71

Implement scoreboard with hidden players

Browse files
src/data_storage.py CHANGED
@@ -131,7 +131,7 @@ def get_all_users(columns_included: list[str] = ["user_id", "name", "email"]):
131
  return all_users
132
 
133
 
134
- def get_all_rosters():
135
  with get_db_connection() as con:
136
  cur = con.cursor()
137
  all_rosters = cur.execute("select * from user_rosters").fetchall()
 
131
  return all_users
132
 
133
 
134
+ def get_all_rosters() -> list[tuple[int, str, str]]:
135
  with get_db_connection() as con:
136
  cur = con.cursor()
137
  all_rosters = cur.execute("select * from user_rosters").fetchall()
src/load_options.py CHANGED
@@ -40,7 +40,11 @@ class PlayerOption:
40
  def empty_player(cls, week: int | None = None):
41
  return cls(full_name="", gsis_id="", headshot_url="", position="", team="", gametime=None, week=week)
42
 
43
- def is_locked(self):
 
 
 
 
44
  if not self.gametime:
45
  return False
46
  else:
@@ -123,3 +127,15 @@ def load_options():
123
  modify_defensive_players_to_be_team_defense(df_rosters)
124
  player_options = player_options_from_df(df_rosters)
125
  return player_options
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  def empty_player(cls, week: int | None = None):
41
  return cls(full_name="", gsis_id="", headshot_url="", position="", team="", gametime=None, week=week)
42
 
43
+ @classmethod
44
+ def hidden_player(cls, week: int | None = None):
45
+ return cls(full_name="Hidden", gsis_id="", headshot_url="", position="", team="", gametime=None, week=week)
46
+
47
+ def is_locked(self) -> bool:
48
  if not self.gametime:
49
  return False
50
  else:
 
127
  modify_defensive_players_to_be_team_defense(df_rosters)
128
  player_options = player_options_from_df(df_rosters)
129
  return player_options
130
+
131
+
132
+ @st.cache_data(ttl=60 * 60 * 24)
133
+ def get_map_week_player_id_option() -> dict[int, dict[str, PlayerOption]]:
134
+ options_pos_week_map = load_options()
135
+ options_week_id_map: dict[int, dict[str, PlayerOption]] = {k: {} for k in PLAYOFF_WEEK_TO_NAME.keys()}
136
+
137
+ for _, pos_map in options_pos_week_map.items():
138
+ for week, pos_week_opt_list in pos_map.items():
139
+ for player_opt in pos_week_opt_list:
140
+ options_week_id_map[week][player_opt.gsis_id] = player_opt
141
+ return options_week_id_map
src/pages/11_Scoreboard.py CHANGED
@@ -4,8 +4,21 @@ import streamlit as st
4
  from config import DEFAULT_ICON
5
  from shared_page import common_page_config
6
 
7
- from data_storage import get_all_users
8
  from domain.playoffs import CURRENT_PLAYOFF_WEEK, PLAYOFF_WEEK_TO_NAME
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
 
11
  def display_user_names():
@@ -20,9 +33,55 @@ def get_users_df():
20
  return all_users
21
 
22
 
23
- def get_masked_rosters(week: int):
24
- # TODO
25
- return pd.DataFrame()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
 
28
  def display_rosters():
@@ -37,9 +96,7 @@ def display_rosters():
37
  format_func=lambda x: PLAYOFF_WEEK_TO_NAME[x],
38
  )
39
 
40
- rosters = get_masked_rosters(week_selected)
41
- if len(rosters):
42
- st.write(rosters)
43
 
44
 
45
  def get_page():
 
4
  from config import DEFAULT_ICON
5
  from shared_page import common_page_config
6
 
7
+ from data_storage import get_all_users, get_all_rosters
8
  from domain.playoffs import CURRENT_PLAYOFF_WEEK, PLAYOFF_WEEK_TO_NAME
9
+ from load_options import get_map_week_player_id_option, PlayerOption, display_player
10
+
11
+
12
+ POSITION_LABELS = [
13
+ "QB-1",
14
+ "RB-1",
15
+ "RB-2",
16
+ "WR-1",
17
+ "WR-2",
18
+ "TE-1",
19
+ "K-1",
20
+ "DEF-1",
21
+ ]
22
 
23
 
24
  def display_user_names():
 
33
  return all_users
34
 
35
 
36
+ @st.cache_data(ttl=60 * 60)
37
+ def load_masked_rosters() -> dict[int, dict[str, PlayerOption]]:
38
+ options_map = get_map_week_player_id_option()
39
+ roster_user_position_map: dict[int, dict[str, PlayerOption]] = {}
40
+ for user_id, position_id, player_id in get_all_rosters():
41
+ if user_id not in roster_user_position_map:
42
+ roster_user_position_map[user_id] = {}
43
+ week = int(position_id[0])
44
+
45
+ player = PlayerOption.empty_player(week=week)
46
+ if selected_player := options_map[week].get(player_id):
47
+ if selected_player.is_locked():
48
+ player = selected_player
49
+ else:
50
+ player = PlayerOption.hidden_player(week=week)
51
+ roster_user_position_map[user_id][position_id] = player
52
+
53
+ return roster_user_position_map
54
+
55
+
56
+ def display_user_row(week: int, user_name: str, user_map: dict[str, PlayerOption]):
57
+ selection_cols = st.columns(1 + len(POSITION_LABELS))
58
+ # first col is name
59
+ with selection_cols[0]:
60
+ st.write(user_name)
61
+ for pos_label, st_col in zip(POSITION_LABELS, selection_cols[1:]):
62
+ week_pos_label = f"{week}-{pos_label}"
63
+ with st_col:
64
+ player = user_map.get(week_pos_label, PlayerOption.empty_player(week=week))
65
+ display_player(player)
66
+
67
+
68
+ def display_roster_headers():
69
+ selection_cols = st.columns(1 + len(POSITION_LABELS))
70
+
71
+ with selection_cols[0]:
72
+ st.write("User Name")
73
+ for pos_label, st_col in zip(POSITION_LABELS, selection_cols[1:]):
74
+ with st_col:
75
+ st.write(pos_label.split("-")[0])
76
+
77
+
78
+ def display_masked_rosters(week: int):
79
+ rosters = load_masked_rosters()
80
+ users = get_users_df()
81
+ display_roster_headers()
82
+ for row in users.itertuples():
83
+ user_roster_map = rosters.get(row.user_id, {})
84
+ display_user_row(week, row.name, user_roster_map)
85
 
86
 
87
  def display_rosters():
 
96
  format_func=lambda x: PLAYOFF_WEEK_TO_NAME[x],
97
  )
98
 
99
+ display_masked_rosters(week_selected)
 
 
100
 
101
 
102
  def get_page():