Jon Solow
commited on
Commit
·
7737702
1
Parent(s):
e11a10a
Implement player multipliers and fix bug on scoring that will be present once week 2 players are set
Browse files- src/pages/11_Scoreboard.py +50 -14
src/pages/11_Scoreboard.py
CHANGED
@@ -48,24 +48,47 @@ def load_masked_rosters() -> dict[int, dict[str, PlayerOption]]:
|
|
48 |
return roster_user_position_map
|
49 |
|
50 |
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
players_str = ""
|
53 |
for pos_label in POSITION_LABELS:
|
54 |
week_pos_label = f"{week}-{pos_label}"
|
55 |
player = user_map.get(week_pos_label, PlayerOption.empty_player(week=week))
|
56 |
player_stats = get_stats_map().get(week, {}).get(player.gsis_id, {})
|
|
|
57 |
player_score = get_scores_map().get(week, {}).get(player.gsis_id, 0.0)
|
58 |
-
players_str += get_player_html_str(player, player_stats, player_score)
|
59 |
roster_str = f"""<div className='user__roster'>
|
60 |
{players_str}
|
61 |
</div>"""
|
62 |
return roster_str
|
63 |
|
64 |
|
65 |
-
def get_player_html_str(player_opt: PlayerOption, player_stats: dict[str, float], score: float) -> str:
|
66 |
-
# TODO
|
67 |
-
multiplier = 1
|
68 |
-
|
69 |
if player_opt.week and player_opt.team:
|
70 |
game_map = get_schedule_with_live().get(player_opt.week, {}).get(player_opt.team, {})
|
71 |
else:
|
@@ -130,11 +153,16 @@ def get_player_stats_html_str(stats_dict: dict[str, float]) -> str:
|
|
130 |
|
131 |
|
132 |
def get_user_html_str(
|
133 |
-
week: int,
|
|
|
|
|
|
|
|
|
|
|
134 |
) -> str:
|
135 |
user_str = ""
|
136 |
score_type = "Score"
|
137 |
-
roster_html_str = get_roster_html_str(week, user_map)
|
138 |
user_str += f"""
|
139 |
<div className="user">
|
140 |
<span className="user__place">{place}</span>
|
@@ -148,23 +176,29 @@ def get_user_html_str(
|
|
148 |
|
149 |
|
150 |
def assemble_user_scores(
|
151 |
-
player_scores_map: dict[int, dict[str, float]],
|
|
|
|
|
152 |
) -> dict[int, dict[int, float]]:
|
153 |
week_user_score_map: dict[int, dict[int, float]] = {w: {} for w in player_scores_map.keys()}
|
154 |
for user_id, user_roster_map in roster_map.items():
|
155 |
-
|
156 |
for roster_key, player_id in user_roster_map.items():
|
157 |
week = int(roster_key[0])
|
158 |
-
|
159 |
-
|
|
|
|
|
|
|
160 |
return week_user_score_map
|
161 |
|
162 |
|
163 |
def display_masked_rosters(week: int):
|
164 |
rosters = load_masked_rosters()
|
|
|
165 |
users = get_users_df()
|
166 |
player_scores = get_scores_map()
|
167 |
-
user_scores = assemble_user_scores(player_scores, rosters)
|
168 |
|
169 |
scoreboard_str = ""
|
170 |
|
@@ -178,7 +212,9 @@ def display_masked_rosters(week: int):
|
|
178 |
user_score = user_scores.get(week, {}).get(row.user_id, 0.0)
|
179 |
user_roster_map = rosters.get(row.user_id, {})
|
180 |
user_place = i + 1
|
181 |
-
scoreboard_str += get_user_html_str(
|
|
|
|
|
182 |
|
183 |
scoreboard_str += """</div>"""
|
184 |
|
|
|
48 |
return roster_user_position_map
|
49 |
|
50 |
|
51 |
+
@st.cache_data(ttl=60 * 1)
|
52 |
+
def get_roster_multipliers(roster_map: dict[int, dict[str, PlayerOption]]) -> dict[int, dict[int, dict[str, int]]]:
|
53 |
+
"""Map of user -> week -> player_id -> multiplier"""
|
54 |
+
multiplier_map: dict[int, dict[int, dict[str, int]]] = {}
|
55 |
+
for user_id, user_roster_map in roster_map.items():
|
56 |
+
user_multipliers: dict[int, dict[str, int]] = {}
|
57 |
+
# iterate through players sorted by week
|
58 |
+
for position_id, player in sorted(user_roster_map.items(), key=lambda x: x[0][0]):
|
59 |
+
if not player.gsis_id:
|
60 |
+
# skip not set players
|
61 |
+
continue
|
62 |
+
week = int(position_id.split("-", 1)[0])
|
63 |
+
if week not in user_multipliers:
|
64 |
+
user_multipliers[week] = {}
|
65 |
+
|
66 |
+
player_previous_multiplier = user_multipliers.get(week - 1, {}).get(player.gsis_id, 0)
|
67 |
+
player_multiplier = player_previous_multiplier + 1
|
68 |
+
user_multipliers[week][player.gsis_id] = player_multiplier
|
69 |
+
|
70 |
+
multiplier_map[user_id] = user_multipliers
|
71 |
+
return multiplier_map
|
72 |
+
|
73 |
+
|
74 |
+
def get_roster_html_str(
|
75 |
+
week: int, user_map: dict[str, PlayerOption], user_multipliers: dict[int, dict[str, int]]
|
76 |
+
) -> str:
|
77 |
players_str = ""
|
78 |
for pos_label in POSITION_LABELS:
|
79 |
week_pos_label = f"{week}-{pos_label}"
|
80 |
player = user_map.get(week_pos_label, PlayerOption.empty_player(week=week))
|
81 |
player_stats = get_stats_map().get(week, {}).get(player.gsis_id, {})
|
82 |
+
player_multiplier = user_multipliers.get(week, {}).get(player.gsis_id, 1)
|
83 |
player_score = get_scores_map().get(week, {}).get(player.gsis_id, 0.0)
|
84 |
+
players_str += get_player_html_str(player, player_stats, player_score, player_multiplier)
|
85 |
roster_str = f"""<div className='user__roster'>
|
86 |
{players_str}
|
87 |
</div>"""
|
88 |
return roster_str
|
89 |
|
90 |
|
91 |
+
def get_player_html_str(player_opt: PlayerOption, player_stats: dict[str, float], score: float, multiplier: int) -> str:
|
|
|
|
|
|
|
92 |
if player_opt.week and player_opt.team:
|
93 |
game_map = get_schedule_with_live().get(player_opt.week, {}).get(player_opt.team, {})
|
94 |
else:
|
|
|
153 |
|
154 |
|
155 |
def get_user_html_str(
|
156 |
+
week: int,
|
157 |
+
user_name: str,
|
158 |
+
user_map: dict[str, PlayerOption],
|
159 |
+
week_score: float,
|
160 |
+
place: int,
|
161 |
+
user_multipliers: dict[int, dict[str, int]],
|
162 |
) -> str:
|
163 |
user_str = ""
|
164 |
score_type = "Score"
|
165 |
+
roster_html_str = get_roster_html_str(week, user_map, user_multipliers)
|
166 |
user_str += f"""
|
167 |
<div className="user">
|
168 |
<span className="user__place">{place}</span>
|
|
|
176 |
|
177 |
|
178 |
def assemble_user_scores(
|
179 |
+
player_scores_map: dict[int, dict[str, float]],
|
180 |
+
roster_map: dict[int, dict[str, PlayerOption]],
|
181 |
+
multiplier_map: dict[int, dict[int, dict[str, int]]],
|
182 |
) -> dict[int, dict[int, float]]:
|
183 |
week_user_score_map: dict[int, dict[int, float]] = {w: {} for w in player_scores_map.keys()}
|
184 |
for user_id, user_roster_map in roster_map.items():
|
185 |
+
user_score_map: dict[int, float] = {w: 0.0 for w in player_scores_map.keys()}
|
186 |
for roster_key, player_id in user_roster_map.items():
|
187 |
week = int(roster_key[0])
|
188 |
+
player_score = player_scores_map.get(week, {}).get(player_id.gsis_id, 0.0)
|
189 |
+
multiplier = float(multiplier_map.get(user_id, {}).get(week, {}).get(player_id.gsis_id, 1))
|
190 |
+
user_score_map[week] += player_score * multiplier
|
191 |
+
for week, week_score in user_score_map.items():
|
192 |
+
week_user_score_map[week][user_id] = week_score
|
193 |
return week_user_score_map
|
194 |
|
195 |
|
196 |
def display_masked_rosters(week: int):
|
197 |
rosters = load_masked_rosters()
|
198 |
+
multipliers = get_roster_multipliers(rosters)
|
199 |
users = get_users_df()
|
200 |
player_scores = get_scores_map()
|
201 |
+
user_scores = assemble_user_scores(player_scores, rosters, multipliers)
|
202 |
|
203 |
scoreboard_str = ""
|
204 |
|
|
|
212 |
user_score = user_scores.get(week, {}).get(row.user_id, 0.0)
|
213 |
user_roster_map = rosters.get(row.user_id, {})
|
214 |
user_place = i + 1
|
215 |
+
scoreboard_str += get_user_html_str(
|
216 |
+
week, row.name, user_roster_map, user_score, user_place, multipliers.get(row.user_id, {})
|
217 |
+
)
|
218 |
|
219 |
scoreboard_str += """</div>"""
|
220 |
|