|
import streamlit as st |
|
import streamlit.components.v1 as components |
|
|
|
from config import DEFAULT_ICON |
|
from shared_page import common_page_config |
|
|
|
from domain.playoffs import ( |
|
PLAYOFF_WEEK_TO_NAME, |
|
CURRENT_PLAYOFF_WEEK, |
|
) |
|
from login import check_password |
|
from data_storage import update_selection, get_user_team |
|
|
|
from load_options import load_options, PlayerOption, display_player |
|
|
|
|
|
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, |
|
) |
|
|
|
|
|
def format_player_option(player_opt: PlayerOption) -> str: |
|
return f"{player_opt.full_name} - {player_opt.team}" |
|
|
|
|
|
def position_cell( |
|
week: str, |
|
pos_str: str, |
|
pos_idx: int, |
|
options_map: dict[str, dict[int, list[PlayerOption]]], |
|
existing_selection_map, |
|
team_filter: list[str], |
|
): |
|
pos_label = f"{week}-{pos_str}-{pos_idx}" |
|
selected_id = existing_selection_map.get(pos_label) |
|
options_list = options_map[pos_str][int(week)] |
|
if team_filter: |
|
options_list = [x for x in options_list if x.team in team_filter or str(selected_id) == str(x.gsis_id)] |
|
non_locked_options = [x for x in options_list if not x.is_locked()] |
|
disabled = False |
|
|
|
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: |
|
|
|
options = [] |
|
selected_option_idx = 0 |
|
disabled = True |
|
elif int(week) < CURRENT_PLAYOFF_WEEK or selected_player.is_locked(): |
|
|
|
options = [selected_player] |
|
selected_option_idx = 0 |
|
disabled = True |
|
else: |
|
options = non_locked_options |
|
|
|
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, st.session_state["db_client"]) |
|
st.rerun() |
|
|
|
|
|
def week_selections(week, player_options, existing_selections, team_filter: list[str]): |
|
selection_cols = st.columns(8) |
|
|
|
with selection_cols[0]: |
|
position_cell(week, "QB", 1, player_options, existing_selections, team_filter) |
|
with selection_cols[1]: |
|
position_cell(week, "RB", 1, player_options, existing_selections, team_filter) |
|
with selection_cols[2]: |
|
position_cell(week, "RB", 2, player_options, existing_selections, team_filter) |
|
with selection_cols[3]: |
|
position_cell(week, "WR", 1, player_options, existing_selections, team_filter) |
|
with selection_cols[4]: |
|
position_cell(week, "WR", 2, player_options, existing_selections, team_filter) |
|
with selection_cols[5]: |
|
position_cell(week, "TE", 1, player_options, existing_selections, team_filter) |
|
with selection_cols[6]: |
|
position_cell(week, "K", 1, player_options, existing_selections, team_filter) |
|
with selection_cols[7]: |
|
position_cell(week, "DEF", 1, player_options, existing_selections, team_filter) |
|
|
|
|
|
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() |
|
try: |
|
logged_in = check_password() |
|
except Exception: |
|
st.write("Sorry, error logging in. Please try again.") |
|
st.stop() |
|
|
|
if not logged_in: |
|
st.write("Sorry, you must be logged in first to play") |
|
st.stop() |
|
|
|
st.title(page_title) |
|
|
|
if st.button("Refresh Data"): |
|
st.rerun() |
|
try: |
|
existing_selections = get_user_team(st.session_state["logged_in_user"], st.session_state["db_client"]) |
|
except Exception: |
|
st.write("Sorry error occurred loading team. Please try refreshing page.") |
|
st.stop() |
|
|
|
player_options = load_options() |
|
|
|
team_options_for_filter = sorted([x.team for x in player_options["DEF"][CURRENT_PLAYOFF_WEEK]]) |
|
team_filter = st.multiselect("Filter for NFL teams", team_options_for_filter) |
|
|
|
for week in range(1, 5): |
|
existing_week_selections = {k: v for k, v in existing_selections.items() if k[0] == str(week)} |
|
st.header(PLAYOFF_WEEK_TO_NAME[week]) |
|
if week < CURRENT_PLAYOFF_WEEK: |
|
with st.expander("Show Previous Week"): |
|
week_selections(week, player_options, existing_week_selections, team_filter) |
|
else: |
|
week_selections(week, player_options, existing_week_selections, team_filter) |
|
|
|
set_selectbox_readonly() |
|
|
|
|
|
if __name__ == "__main__": |
|
get_page() |
|
|