File size: 5,235 Bytes
54d6a92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83551c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54d6a92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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