Jon Solow commited on
Commit
54d6a92
·
1 Parent(s): 0a1f3c3

Move player formatting to module for reuse

Browse files
src/format_player_html.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from load_options import PlayerOption
2
+ from stats import get_stats_map, get_scores_map, get_schedule_with_live
3
+
4
+
5
+ POSITION_LABELS = [
6
+ "QB-1",
7
+ "RB-1",
8
+ "RB-2",
9
+ "WR-1",
10
+ "WR-2",
11
+ "TE-1",
12
+ "K-1",
13
+ "DEF-1",
14
+ ]
15
+
16
+
17
+ def get_roster_html_str(
18
+ week: int, user_map: dict[str, PlayerOption], user_multipliers: dict[int, dict[str, int]]
19
+ ) -> str:
20
+ players_str = ""
21
+ if week != 5:
22
+ for pos_label in POSITION_LABELS:
23
+ week_pos_label = f"{week}-{pos_label}"
24
+ player = user_map.get(week_pos_label, PlayerOption.empty_player(week=week))
25
+ player_stats = get_stats_map().get(week, {}).get(player.gsis_id, {})
26
+ player_multiplier = user_multipliers.get(week, {}).get(player.gsis_id, 1)
27
+ player_score = get_scores_map().get(week, {}).get(player.gsis_id, 0.0)
28
+ players_str += get_player_html_str(player, player_stats, player_score, player_multiplier)
29
+ roster_str = f"""<div className='user__roster'>
30
+ {players_str}
31
+ </div>"""
32
+ return roster_str
33
+
34
+
35
+ def get_player_html_str(
36
+ player_opt: PlayerOption, player_stats: dict[str, float], player_score: float, multiplier: int
37
+ ) -> str:
38
+ score = round(player_score * float(multiplier), 0)
39
+ if player_opt.week and player_opt.team:
40
+ game_map = get_schedule_with_live().get(player_opt.week, {}).get(player_opt.team, {})
41
+ else:
42
+ game_map = {}
43
+ game_status = game_map.get("status") or " "
44
+ if isinstance((team_score := game_map.get("score")), int):
45
+ game_score = f"""{team_score} - {game_map.get("opponent_score")}"""
46
+ else:
47
+ game_score = " "
48
+ player_stats_str = get_player_stats_html_str(player_stats)
49
+
50
+ player_classes = "player"
51
+ if player_opt.gsis_id:
52
+ player_classes += f" player--{player_opt.gsis_id}"
53
+
54
+ if player_opt.team:
55
+ player_classes += f" player--{player_opt.team.upper()}"
56
+
57
+ image_classes = "player__image"
58
+ multiplier_classes = "player__multiplier"
59
+ if multiplier > 1:
60
+ image_classes += f" player__image--{multiplier}"
61
+ multiplier_classes += f" player__multiplier--{multiplier}"
62
+
63
+ if not (player_image_url := player_opt.headshot_url):
64
+ player_image_url = "https://static.www.nfl.com/w_114,h_80,c_fill/league/suxzfdslsj5vpwbin5t8"
65
+ player_classes += " player--hidden"
66
+
67
+ player_str = f"""<div className='{player_classes}'>
68
+ <div className="player__position">{player_opt.position}</div>
69
+ <div className={image_classes}>
70
+ <img className="player__img" src={player_image_url} alt='{player_opt.full_name}' />
71
+ </div>
72
+ <div className="player__name">{player_opt.full_name}</div>
73
+ <div className="player__team">{player_opt.team}</div>
74
+ <span className="player__score">{score: .0f}</span>
75
+ <span className='{multiplier_classes}'>{multiplier}X</span>
76
+ <span className="player__game-status">{game_status}</span>
77
+ <span className="player__game-score">{game_score}</span>
78
+ {player_stats_str}
79
+ </div>"""
80
+
81
+ return player_str
82
+
83
+
84
+ def get_stat_list_item_html_str(stat_key: str, stat_value: str | float) -> str:
85
+ return f"""<div className="stat">
86
+ <div className="stat__key">{stat_key}</div>
87
+ <div className="stat__value">{stat_value:.0f}</div>
88
+ </div>"""
89
+
90
+
91
+ def get_player_stats_html_str(stats_dict: dict[str, float]) -> str:
92
+ sorted_stat_tuple = list(
93
+ filter(lambda x: x[1] != 0, sorted(list(stats_dict.items()), key=lambda x: x[1], reverse=True))
94
+ )
95
+ max_stats = 4
96
+ stat_items_str = "\n".join([get_stat_list_item_html_str(s, v) for s, v in sorted_stat_tuple[:max_stats]])
97
+
98
+ stats_str = f"""<div className="player-stats">{stat_items_str}</div>"""
99
+ return stats_str
100
+
101
+
102
+ def get_user_html_str(
103
+ week: int,
104
+ user_name: str,
105
+ user_map: dict[str, PlayerOption],
106
+ week_score: float,
107
+ place: int,
108
+ user_multipliers: dict[int, dict[str, int]],
109
+ ) -> str:
110
+ user_str = ""
111
+ score_type = "Score"
112
+ roster_html_str = get_roster_html_str(week, user_map, user_multipliers)
113
+ user_str += f"""
114
+ <div className="user">
115
+ <span className="user__place">{place}</span>
116
+ <span className="user__username">{user_name}</span>
117
+ <span className="user__username">{user_name}</span>
118
+ <span className="user__score-type">{score_type}</span>
119
+ <span className="user__score-number">{week_score: .0f}</span>
120
+ {roster_html_str}
121
+ </div>"""
122
+ return user_str
src/pages/11_Scoreboard.py CHANGED
@@ -6,20 +6,11 @@ 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
10
- from stats import get_stats_map, get_scores_map, get_schedule_with_live
11
-
12
-
13
- POSITION_LABELS = [
14
- "QB-1",
15
- "RB-1",
16
- "RB-2",
17
- "WR-1",
18
- "WR-2",
19
- "TE-1",
20
- "K-1",
21
- "DEF-1",
22
- ]
23
 
24
 
25
  def get_users_df():
@@ -74,114 +65,6 @@ def get_roster_multipliers(roster_map: dict[int, dict[str, PlayerOption]]) -> di
74
  return multiplier_map
75
 
76
 
77
- def get_roster_html_str(
78
- week: int, user_map: dict[str, PlayerOption], user_multipliers: dict[int, dict[str, int]]
79
- ) -> str:
80
- players_str = ""
81
- if week != 5:
82
- for pos_label in POSITION_LABELS:
83
- week_pos_label = f"{week}-{pos_label}"
84
- player = user_map.get(week_pos_label, PlayerOption.empty_player(week=week))
85
- player_stats = get_stats_map().get(week, {}).get(player.gsis_id, {})
86
- player_multiplier = user_multipliers.get(week, {}).get(player.gsis_id, 1)
87
- player_score = get_scores_map().get(week, {}).get(player.gsis_id, 0.0)
88
- players_str += get_player_html_str(player, player_stats, player_score, player_multiplier)
89
- roster_str = f"""<div className='user__roster'>
90
- {players_str}
91
- </div>"""
92
- return roster_str
93
-
94
-
95
- def get_player_html_str(
96
- player_opt: PlayerOption, player_stats: dict[str, float], player_score: float, multiplier: int
97
- ) -> str:
98
- score = round(player_score * float(multiplier), 0)
99
- if player_opt.week and player_opt.team:
100
- game_map = get_schedule_with_live().get(player_opt.week, {}).get(player_opt.team, {})
101
- else:
102
- game_map = {}
103
- game_status = game_map.get("status") or " "
104
- if isinstance((team_score := game_map.get("score")), int):
105
- game_score = f"""{team_score} - {game_map.get("opponent_score")}"""
106
- else:
107
- game_score = " "
108
- player_stats_str = get_player_stats_html_str(player_stats)
109
-
110
- player_classes = "player"
111
- if player_opt.gsis_id:
112
- player_classes += f" player--{player_opt.gsis_id}"
113
-
114
- if player_opt.team:
115
- player_classes += f" player--{player_opt.team.upper()}"
116
-
117
- image_classes = "player__image"
118
- multiplier_classes = "player__multiplier"
119
- if multiplier > 1:
120
- image_classes += f" player__image--{multiplier}"
121
- multiplier_classes += f" player__multiplier--{multiplier}"
122
-
123
- if not (player_image_url := player_opt.headshot_url):
124
- player_image_url = "https://static.www.nfl.com/w_114,h_80,c_fill/league/suxzfdslsj5vpwbin5t8"
125
- player_classes += " player--hidden"
126
-
127
- player_str = f"""<div className='{player_classes}'>
128
- <div className="player__position">{player_opt.position}</div>
129
- <div className={image_classes}>
130
- <img className="player__img" src={player_image_url} alt='{player_opt.full_name}' />
131
- </div>
132
- <div className="player__name">{player_opt.full_name}</div>
133
- <div className="player__team">{player_opt.team}</div>
134
- <span className="player__score">{score: .0f}</span>
135
- <span className='{multiplier_classes}'>{multiplier}X</span>
136
- <span className="player__game-status">{game_status}</span>
137
- <span className="player__game-score">{game_score}</span>
138
- {player_stats_str}
139
- </div>"""
140
-
141
- return player_str
142
-
143
-
144
- def get_stat_list_item_html_str(stat_key: str, stat_value: str | float) -> str:
145
- return f"""<div className="stat">
146
- <div className="stat__key">{stat_key}</div>
147
- <div className="stat__value">{stat_value:.0f}</div>
148
- </div>"""
149
-
150
-
151
- def get_player_stats_html_str(stats_dict: dict[str, float]) -> str:
152
- sorted_stat_tuple = list(
153
- filter(lambda x: x[1] != 0, sorted(list(stats_dict.items()), key=lambda x: x[1], reverse=True))
154
- )
155
- max_stats = 4
156
- stat_items_str = "\n".join([get_stat_list_item_html_str(s, v) for s, v in sorted_stat_tuple[:max_stats]])
157
-
158
- stats_str = f"""<div className="player-stats">{stat_items_str}</div>"""
159
- return stats_str
160
-
161
-
162
- def get_user_html_str(
163
- week: int,
164
- user_name: str,
165
- user_map: dict[str, PlayerOption],
166
- week_score: float,
167
- place: int,
168
- user_multipliers: dict[int, dict[str, int]],
169
- ) -> str:
170
- user_str = ""
171
- score_type = "Score"
172
- roster_html_str = get_roster_html_str(week, user_map, user_multipliers)
173
- user_str += f"""
174
- <div className="user">
175
- <span className="user__place">{place}</span>
176
- <span className="user__username">{user_name}</span>
177
- <span className="user__username">{user_name}</span>
178
- <span className="user__score-type">{score_type}</span>
179
- <span className="user__score-number">{week_score: .0f}</span>
180
- {roster_html_str}
181
- </div>"""
182
- return user_str
183
-
184
-
185
  def assemble_user_scores(
186
  player_scores_map: dict[int, dict[str, float]],
187
  roster_map: dict[int, dict[str, PlayerOption]],
 
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 format_player_html import (
10
+ get_user_html_str,
11
+ )
12
  from load_options import get_map_week_player_id_option, PlayerOption
13
+ from stats import get_scores_map
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
 
16
  def get_users_df():
 
65
  return multiplier_map
66
 
67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  def assemble_user_scores(
69
  player_scores_map: dict[int, dict[str, float]],
70
  roster_map: dict[int, dict[str, PlayerOption]],