File size: 8,971 Bytes
d45ec63
4393241
d45ec63
56a9838
d45ec63
 
 
 
4393241
b838c58
 
 
 
 
 
 
1727748
d45ec63
4393241
560823b
 
d45ec63
 
56a9838
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d45ec63
 
 
 
 
 
 
4393241
3a2866a
d45ec63
 
 
 
 
 
 
 
 
4393241
3a2866a
d45ec63
 
 
3a2866a
 
 
 
 
 
 
a8d8ee5
595bd47
3a2866a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f0d0c7
3a2866a
 
 
 
d45ec63
 
b838c58
 
 
 
 
 
 
973503c
d45ec63
 
4393241
3a2866a
 
4f0d0c7
4393241
3a2866a
d45ec63
3a2866a
 
 
4f0d0c7
 
4393241
d862a37
 
4393241
3a2866a
 
 
 
 
 
4f0d0c7
4393241
3a2866a
b838c58
3a2866a
 
d45ec63
 
 
 
 
 
 
 
 
7858dd3
 
 
 
d45ec63
 
3a2866a
 
 
 
255e3c4
3a2866a
eaae921
535ec3b
eaae921
 
 
 
255e3c4
eaae921
255e3c4
eaae921
255e3c4
3e6686b
535ec3b
3a2866a
eaae921
255e3c4
 
535ec3b
255e3c4
eaae921
 
 
 
 
 
3e6686b
 
902260e
255e3c4
3e6686b
 
902260e
535ec3b
3e6686b
255e3c4
 
2d08297
 
c1ae968
 
 
 
 
2d08297
 
560823b
c1ae968
 
2d08297
3e6686b
 
560823b
 
3e6686b
 
d45ec63
 
 
 
2eee45f
fac5a9c
2eee45f
d45ec63
2dc08a5
 
ac6561c
 
560823b
3e6686b
3a2866a
3e6686b
 
 
 
 
 
3a2866a
3e6686b
3a2866a
3e6686b
3a2866a
3e6686b
3a2866a
3e6686b
3a2866a
3e6686b
3a2866a
3e6686b
3a2866a
3e6686b
3a2866a
d45ec63
56a9838
 
d45ec63
 
 
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
from dataclasses import dataclass
import pandas as pd
import streamlit as st
import streamlit.components.v1 as components

from config import DEFAULT_ICON
from shared_page import common_page_config

from domain.constants import SEASON
from domain.playoffs import (
    PLAYOFF_WEEK_TO_NAME,
    CURRENT_PLAYOFF_WEEK,
    ROSTER_WEEK_TO_PLAYOFF_WEEK,
    PLAYOFFS_TEAMS,
    PLAYOFF_TEAM_DEF_PLAYER,
)
from domain.teams import SCHEDULE_NAME_TO_PFR_NAME_MAP
from queries.nflverse.github_data import get_weekly_rosters
from queries.pfr.league_schedule import get_season_time_map
from login import check_password
from data_storage import update_selection, get_user_team


def set_selectbox_readonly():
    components.html(
        """
<script>
    function formatSelectBox() {
        const matches = window.parent.document.querySelectorAll("input");
        matches.forEach((input) => {
            input.setAttribute("readonly", "true");
        });

    }
    formatSelectBox();

</script>
    """,
        width=0,
        height=0,
    )


@dataclass
class PlayerOption:
    full_name: str
    gsis_id: str
    headshot_url: str
    position: str
    team: str
    gametime: pd.Timestamp | None
    week: int | None

    @classmethod
    def from_series(cls, input_series):
        return cls(
            full_name=input_series.full_name,
            gsis_id=input_series.gsis_id,
            headshot_url=input_series.headshot_url,
            position=input_series.position,
            team=input_series.team,
            gametime=input_series.gametime,
            week=int(input_series.week),
        )

    @classmethod
    def empty_player(cls, week: int | None = None):
        return cls(full_name="", gsis_id="", headshot_url="", position="", team="", gametime=None, week=week)

    def is_locked(self):
        if not self.gametime:
            return False
        else:
            date_compare = (pd.Timestamp.now(tz="America/New_York")) + pd.Timedelta(days=0, hours=0)
            return self.gametime < date_compare


def initialize_empty_options_map() -> dict[str, dict[int, list[PlayerOption]]]:
    options_map: dict[str, dict[int, list[PlayerOption]]] = {}
    for pos in ["QB", "RB", "WR", "TE", "K", "DEF"]:
        options_map[pos] = {}
        for week in PLAYOFF_WEEK_TO_NAME.keys():
            options_map[pos][int(week)] = [PlayerOption.empty_player(week=week)]
    return options_map


def player_options_from_df(df_options) -> dict[str, dict[int, list[PlayerOption]]]:
    options_map = initialize_empty_options_map()
    for pos, pos_week_map in options_map.items():
        for week in pos_week_map.keys():
            df_pos_week = df_options[((df_options.week == week) & (df_options.position == pos))]
            if len(df_pos_week) > 0:
                player_options_list = df_pos_week.apply(PlayerOption.from_series, axis=1).tolist()
                options_map[pos][int(week)].extend(player_options_list)
    return options_map


def modify_defensive_players_to_be_team_defense(df_options):
    for team, player_id in PLAYOFF_TEAM_DEF_PLAYER:
        if player_id in df_options.gsis_id.values:
            df_options.loc[df_options.gsis_id == player_id, "position"] = "DEF"
            df_options.loc[df_options.gsis_id == player_id, "full_name"] = team.team_name


@st.cache_data(ttl=60 * 60 * 24)
def load_options():
    df_rosters = get_weekly_rosters()

    # get game schedules
    week_game_times = get_season_time_map(SEASON)
    latest_game_time_defaults = {k: max(v.values()) for k, v in week_game_times.items() if v}

    # sort
    sort_by_cols = ["position", "week", "fantasy_points"]
    df_rosters.sort_values(sort_by_cols, ascending=False, inplace=True)

    # filter data from non-playoffs
    df_rosters = df_rosters[df_rosters.week.isin(ROSTER_WEEK_TO_PLAYOFF_WEEK.keys())]
    df_rosters["week"] = df_rosters["week"].map(ROSTER_WEEK_TO_PLAYOFF_WEEK)
    # set gametime
    if len(df_rosters) == 0:
        return initialize_empty_options_map()
    df_rosters["gametime"] = df_rosters.apply(
        lambda x: week_game_times.get(x.week, {}).get(
            SCHEDULE_NAME_TO_PFR_NAME_MAP[x.team], latest_game_time_defaults.get(x.week, None)
        ),
        axis=1,
    )

    df_rosters["in_playoffs"] = df_rosters.apply(lambda x: x.team in PLAYOFFS_TEAMS[x.week], axis=1)

    df_rosters = df_rosters[df_rosters.in_playoffs]
    modify_defensive_players_to_be_team_defense(df_rosters)
    player_options = player_options_from_df(df_rosters)
    return player_options


def format_player_option(player_opt: PlayerOption) -> str:
    return f"{player_opt.team} - {player_opt.full_name}"


def display_player(player_opt: PlayerOption | None):
    if player_opt:
        if player_opt.headshot_url:
            st.image(player_opt.headshot_url)
        if player_opt.full_name:
            st.write(player_opt.full_name)
            st.write(f"{player_opt.team} - {player_opt.gametime.strftime('%-m/%-d %-I:%M %p')}")


def position_cell(
    week: str, pos_str: str, pos_idx: int, options_map: dict[str, dict[int, list[PlayerOption]]], existing_selection_map
):
    pos_label = f"{week}-{pos_str}-{pos_idx}"
    selected_id = existing_selection_map.get(pos_label)
    options_list = options_map[pos_str][int(week)]
    non_locked_options = [x for x in options_list if not x.is_locked()]
    disabled = False
    # get selected player by id from options
    try:
        selected_player = next(v for v in options_list if str(selected_id) == str(v.gsis_id))
    except Exception:
        selected_player = PlayerOption.empty_player()

    if int(week) > CURRENT_PLAYOFF_WEEK:
        # future week, no options
        options = []
        selected_option_idx = 0
        disabled = True
    elif int(week) < CURRENT_PLAYOFF_WEEK or selected_player.is_locked():
        # past week, can't change
        options = [selected_player]
        selected_option_idx = 0
        disabled = True
    else:
        options = non_locked_options
        # set to index of current select.  This should exist, but try-except for safety
        try:
            selected_option_idx = non_locked_options.index(selected_player)
        except ValueError:
            selected_option_idx = 0

    selected_player_from_box = st.selectbox(
        pos_str,
        options=options,
        format_func=format_player_option,
        index=selected_option_idx,
        key=pos_label,
        disabled=disabled,
    )
    if selected_player_from_box and int(week) == CURRENT_PLAYOFF_WEEK:
        if selected_player_from_box.gsis_id and selected_player_from_box.gsis_id != selected_id:
            if selected_player_from_box.is_locked():
                st.warning("Sorry player's game has already started", icon="🚨")
                display_player(selected_player)
                return
            elif selected_player_from_box.gsis_id in existing_selection_map.values():
                st.warning("Player already in lineup.  Please choose another.", icon="🚨")
                display_player(selected_player)
                return
            else:
                update_and_save_selection(pos_label, selected_player_from_box.gsis_id),
        display_player(selected_player_from_box)
    else:
        display_player(selected_player)


def update_and_save_selection(pos_label: str, selection_id: str):
    update_selection(st.session_state["logged_in_user"], pos_label, selection_id)


def get_page():
    page_title = "Select Your Team"
    st.set_page_config(page_title=page_title, page_icon=DEFAULT_ICON, layout="wide")
    common_page_config()
    if not check_password():
        st.write("Sorry, you must be logged in first to play")
        st.stop()

    st.title(page_title)

    if st.button("Refresh Data"):
        st.rerun()
    existing_selections = get_user_team(st.session_state["logged_in_user"])

    player_options = load_options()

    for week in range(1, 5):
        st.header(PLAYOFF_WEEK_TO_NAME[week])
        selection_cols = st.columns(8)

        with selection_cols[0]:
            position_cell(week, "QB", 1, player_options, existing_selections)
        with selection_cols[1]:
            position_cell(week, "RB", 1, player_options, existing_selections)
        with selection_cols[2]:
            position_cell(week, "RB", 2, player_options, existing_selections)
        with selection_cols[3]:
            position_cell(week, "WR", 1, player_options, existing_selections)
        with selection_cols[4]:
            position_cell(week, "WR", 2, player_options, existing_selections)
        with selection_cols[5]:
            position_cell(week, "TE", 1, player_options, existing_selections)
        with selection_cols[6]:
            position_cell(week, "K", 1, player_options, existing_selections)
        with selection_cols[7]:
            position_cell(week, "DEF", 1, player_options, existing_selections)

    set_selectbox_readonly()


if __name__ == "__main__":
    get_page()