| from load_options import PlayerOption | |
| from stats import get_stats_map, get_scores_map, get_schedule_with_live | |
| POSITION_LABELS = [ | |
| "QB-1", | |
| "RB-1", | |
| "RB-2", | |
| "WR-1", | |
| "WR-2", | |
| "TE-1", | |
| "K-1", | |
| "DEF-1", | |
| ] | |
| def get_roster_html_str( | |
| week: int, user_map: dict[str, PlayerOption], user_multipliers: dict[int, dict[str, int]] | |
| ) -> str: | |
| players_str = "" | |
| if week != 5: | |
| for pos_label in POSITION_LABELS: | |
| week_pos_label = f"{week}-{pos_label}" | |
| player = user_map.get(week_pos_label, PlayerOption.empty_player(week=week)) | |
| player_stats = get_stats_map().get(week, {}).get(player.gsis_id, {}) | |
| player_multiplier = user_multipliers.get(week, {}).get(player.gsis_id, 1) | |
| player_score = get_scores_map().get(week, {}).get(player.gsis_id, 0.0) | |
| players_str += get_player_html_str(player, player_stats, player_score, player_multiplier) | |
| roster_str = f"""<div className='user__roster'> | |
| {players_str} | |
| </div>""" | |
| return roster_str | |
| def get_all_position_week_html_str(week: int, player_option_list: list[PlayerOption]) -> str: | |
| player_opts_with_stats = [] | |
| for player in player_option_list: | |
| player_stats = get_stats_map().get(week, {}).get(player.gsis_id, {}) | |
| player_score = get_scores_map().get(week, {}).get(player.gsis_id, 0.0) | |
| player_opts_with_stats.append((player, player_stats, player_score)) | |
| players_str = "" | |
| player_limit = 24 | |
| for player, player_stats, player_score in sorted(player_opts_with_stats, key=lambda x: x[2], reverse=True)[ | |
| :player_limit | |
| ]: | |
| player_multiplier = 1 | |
| if player_stats: | |
| players_str += get_player_html_str(player, player_stats, player_score, player_multiplier) | |
| roster_str = f"""<div className='user__roster'> | |
| {players_str} | |
| </div>""" | |
| return roster_str | |
| def get_player_html_str( | |
| player_opt: PlayerOption, player_stats: dict[str, float], player_score: float, multiplier: int | |
| ) -> str: | |
| score = round(player_score * float(multiplier), 0) | |
| if player_opt.week and player_opt.team: | |
| game_map = get_schedule_with_live().get(player_opt.week, {}).get(player_opt.team, {}) | |
| else: | |
| game_map = {} | |
| game_status = game_map.get("status") or " " | |
| if isinstance((team_score := game_map.get("score")), int): | |
| game_score = f"""{team_score} - {game_map.get("opponent_score")}""" | |
| else: | |
| game_score = " " | |
| player_stats_str = get_player_stats_html_str(player_stats) | |
| player_classes = "player" | |
| if player_opt.gsis_id: | |
| player_classes += f" player--{player_opt.gsis_id}" | |
| if player_opt.team: | |
| player_classes += f" player--{player_opt.team.upper()}" | |
| image_classes = "player__image" | |
| multiplier_classes = "player__multiplier" | |
| if multiplier > 1: | |
| image_classes += f" player__image--{multiplier}" | |
| multiplier_classes += f" player__multiplier--{multiplier}" | |
| if not (player_image_url := player_opt.headshot_url): | |
| player_image_url = "https://static.www.nfl.com/w_114,h_80,c_fill/league/suxzfdslsj5vpwbin5t8" | |
| player_classes += " player--hidden" | |
| player_str = f"""<div className='{player_classes}'> | |
| <div className="player__position">{player_opt.position}</div> | |
| <div className={image_classes}> | |
| <img className="player__img" src={player_image_url} alt='{player_opt.full_name}' /> | |
| </div> | |
| <div className="player__name">{player_opt.full_name}</div> | |
| <div className="player__team">{player_opt.team}</div> | |
| <span className="player__score">{score: .0f}</span> | |
| <span className='{multiplier_classes}'>{multiplier}X</span> | |
| <span className="player__game-status">{game_status}</span> | |
| <span className="player__game-score">{game_score}</span> | |
| {player_stats_str} | |
| </div>""" | |
| return player_str | |
| def get_stat_list_item_html_str(stat_key: str, stat_value: str | float) -> str: | |
| return f"""<div className="stat"> | |
| <div className="stat__key">{stat_key}</div> | |
| <div className="stat__value">{stat_value:.0f}</div> | |
| </div>""" | |
| def get_player_stats_html_str(stats_dict: dict[str, float]) -> str: | |
| sorted_stat_tuple = list( | |
| filter(lambda x: x[1] != 0, sorted(list(stats_dict.items()), key=lambda x: x[1], reverse=True)) | |
| ) | |
| max_stats = 4 | |
| stat_items_str = "\n".join([get_stat_list_item_html_str(s, v) for s, v in sorted_stat_tuple[:max_stats]]) | |
| stats_str = f"""<div className="player-stats">{stat_items_str}</div>""" | |
| return stats_str | |
| def get_user_html_str( | |
| week: int, | |
| user_name: str, | |
| user_map: dict[str, PlayerOption], | |
| week_score: float, | |
| place: int, | |
| user_multipliers: dict[int, dict[str, int]], | |
| ) -> str: | |
| user_str = "" | |
| score_type = "Score" | |
| roster_html_str = get_roster_html_str(week, user_map, user_multipliers) | |
| user_str += f""" | |
| <div className="user"> | |
| <span className="user__place">{place}</span> | |
| <span className="user__username">{user_name}</span> | |
| <span className="user__username">{user_name}</span> | |
| <span className="user__score-type">{score_type}</span> | |
| <span className="user__score-number">{week_score: .0f}</span> | |
| {roster_html_str} | |
| </div>""" | |
| return user_str | |