подсветка команда в турнирке

This commit is contained in:
2025-10-27 19:50:05 +03:00
parent 48c5f552ec
commit cbcad5e525

104
visual.py
View File

@@ -203,8 +203,6 @@ def process_player_data(team_json, player_index):
"time": str(player_data["AvgCarPlayedTime"]),
}
return [season_total, season_avg, career_total, career_avg], player_data
@@ -440,15 +438,9 @@ cached_referee = st.session_state.get("referee")
# standings — может не быть тега/файла
league_tag = None
if isinstance(cached_game_online, dict):
league_tag = (cached_game_online.get("league") or {}).get(
"tag"
)
league_tag = (cached_game_online.get("league") or {}).get("tag")
print(cached_game_online.get("comp").get("name"))
comp_name = (
(cached_game_online.get("comp") or {})
.get("name")
.replace(" ", "_")
)
comp_name = (cached_game_online.get("comp") or {}).get("name").replace(" ", "_")
if league_tag:
load_data_from_json(f"standings_{league_tag}_{comp_name}")
cached_standings = (
@@ -580,7 +572,7 @@ if isinstance(cached_game_online, dict):
t1 = result.get("team1") or {}
t2 = result.get("team2") or {}
if t1.get("logo"):
col1.image(t1["logo"], width=100)
team1_name = t1.get("name") or ""
@@ -834,6 +826,7 @@ columns_game = [
if cached_team1 and cached_team2:
team1_data = process_team_data(cached_team1, columns_game)
team2_data = process_team_data(cached_team2, columns_game)
# Добавляем звездочку, если pts > PTS_Career_High
def _get_first_number(x):
"""Безопасно вытащить число из строки/значения (например '12 (60%)' -> 12)."""
@@ -843,19 +836,21 @@ if cached_team1 and cached_team2:
s = str(x)
# заберём ведущие число/знак (поддержим +/-)
import re
m = re.search(r"[-+]?\d+(\.\d+)?", s)
return float(m.group(0)) if m else None
except Exception:
return None
CAREER_HIGH_KEYS = {
"pts": ["PTS_Career_High", "CareerHighPoints", "career_high_pts"],
"ast": ["AST_Career_High", "CareerHighAssist", "career_high_ast"],
"stl": ["STL_Career_High", "CareerHighSteal", "career_high_stl"],
"blk": ["BLK_Career_High", "CareerHighBlocks", "career_high_blk"],
"reb": ["REB_Career_High", "CareerHighRebound", "career_high_reb"],
# если нужно — добавь ещё пары "df_column": ["possible_key1","possible_key2"...]
}
"pts": ["PTS_Career_High", "CareerHighPoints", "career_high_pts"],
"ast": ["AST_Career_High", "CareerHighAssist", "career_high_ast"],
"stl": ["STL_Career_High", "CareerHighSteal", "career_high_stl"],
"blk": ["BLK_Career_High", "CareerHighBlocks", "career_high_blk"],
"reb": ["REB_Career_High", "CareerHighRebound", "career_high_reb"],
# если нужно — добавь ещё пары "df_column": ["possible_key1","possible_key2"...]
}
def _build_career_high_map(cached_team_list):
"""Вернёт словарь: player_id -> {stat_key: value} для всех доступных максимумов."""
out = {}
@@ -878,7 +873,9 @@ if cached_team1 and cached_team2:
def _ensure_id_column(df, cached_team_list):
"""Присвоить игрокам id в том же порядке, что и в списке cached_team."""
try:
ids = [p.get("id") if isinstance(p, dict) else None for p in cached_team_list][:len(df)]
ids = [
p.get("id") if isinstance(p, dict) else None for p in cached_team_list
][: len(df)]
if "id" not in df.columns:
df["id"] = ids
else:
@@ -914,18 +911,25 @@ if cached_team1 and cached_team2:
df[col] = new_vals # теперь это текст для отображения
STAR_COLUMNS = [
"pts", "ast", "stl", "blk", "reb",
]
"pts",
"ast",
"stl",
"blk",
"reb",
]
team1_data["_pts_num"] = pd.to_numeric(team1_data["pts"], errors="coerce")
team1_data["_kpi_num"] = pd.to_numeric(team1_data["kpi"], errors="coerce")
team2_data["_pts_num"] = pd.to_numeric(team2_data["pts"], errors="coerce")
team2_data["_kpi_num"] = pd.to_numeric(team2_data["kpi"], errors="coerce")
def highlight_max_by_refcol(df, view_col, ref_col):
ref = pd.to_numeric(df[ref_col], errors="coerce")
mx = ref.max()
return [("background-color: green" if (pd.notna(v) and v == mx and v > 0) else "")
for v in ref]
return [
("background-color: green" if (pd.notna(v) and v == mx and v > 0) else "")
for v in ref
]
_mark_star_for_columns(team1_data, cached_team1, STAR_COLUMNS)
_mark_star_for_columns(team2_data, cached_team2, STAR_COLUMNS)
@@ -943,18 +947,34 @@ if cached_team1 and cached_team2:
# .apply(highlight_max, subset="kpi")
# )
team1_styled = (
team1_data[columns_game].style
.apply(highlight_grey, axis=1)
.apply(highlight_foul, subset="foul")
.apply(lambda _: highlight_max_by_refcol(team1_data, "pts", "_pts_num"), axis=0, subset=["pts"])
.apply(lambda _: highlight_max_by_refcol(team1_data, "kpi", "_kpi_num"), axis=0, subset=["kpi"])
team1_data[columns_game]
.style.apply(highlight_grey, axis=1)
.apply(highlight_foul, subset="foul")
.apply(
lambda _: highlight_max_by_refcol(team1_data, "pts", "_pts_num"),
axis=0,
subset=["pts"],
)
.apply(
lambda _: highlight_max_by_refcol(team1_data, "kpi", "_kpi_num"),
axis=0,
subset=["kpi"],
)
)
team2_styled = (
team2_data[columns_game].style
.apply(highlight_grey, axis=1)
team2_data[columns_game]
.style.apply(highlight_grey, axis=1)
.apply(highlight_foul, subset="foul")
.apply(lambda _: highlight_max_by_refcol(team2_data, "pts", "_pts_num"), axis=0, subset=["pts"])
.apply(lambda _: highlight_max_by_refcol(team2_data, "kpi", "_kpi_num"), axis=0, subset=["kpi"])
.apply(
lambda _: highlight_max_by_refcol(team2_data, "pts", "_pts_num"),
axis=0,
subset=["pts"],
)
.apply(
lambda _: highlight_max_by_refcol(team2_data, "kpi", "_kpi_num"),
axis=0,
subset=["kpi"],
)
)
def get_player_all_game(player_data_1):
@@ -1238,16 +1258,10 @@ if cached_standings:
def highlight_teams(s):
try:
t1 = (
((cached_game_online or {}).get("result") or {})
.get("team1", {})
.get("teamId")
)
t2 = (
((cached_game_online or {}).get("result") or {})
.get("team2", {})
.get("teamId")
)
t1 = (cached_game_online or {}).get("team1", {}).get("teamId")
t2 = (cached_game_online or {}).get("team2", {}).get("teamId")
if s.iloc[0] in (t1, t2):
return ["background-color: #FF4B4B"] * len(s)
if s.iloc[0] in (t1, t2):
return ["background-color: #FF4B4B"] * len(s)
except Exception:
@@ -1278,7 +1292,7 @@ if cached_standings:
column_config={"logo": st.column_config.ImageColumn("logo")},
hide_index=True,
height=610,
width="content"
width="content",
)