-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsync.py
More file actions
125 lines (105 loc) · 4.93 KB
/
sync.py
File metadata and controls
125 lines (105 loc) · 4.93 KB
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
"""
UI synchronization module for the Bingo application.
"""
import logging
from nicegui import ui
from src.config.constants import CLOSED_HEADER_TEXT, HEADER_TEXT
from src.core.game_logic import board_views, header_label, is_game_closed
from src.utils.text_processing import get_line_style_for_lines, split_phrase_into_lines
def sync_board_state():
"""
Update tile styles in every board view (e.g., home and stream).
Also handles the game closed state to ensure consistency across views.
"""
try:
# If game is closed, make sure all views reflect that
if is_game_closed:
# Update header state (this should happen automatically through binding)
import src.core.game_logic
from src.core.game_logic import current_header_text
if hasattr(src.core.game_logic, "current_header_text"):
src.core.game_logic.current_header_text = CLOSED_HEADER_TEXT
# Hide all board views
for view_key, (container, _) in board_views.items():
container.style("display: none;")
container.update()
# Make sure controls row is showing only the Start New Game button
from src.core.game_logic import controls_row, reopen_game
if controls_row:
# Check if controls row has been already updated
if (
controls_row.default_slot
and len(controls_row.default_slot.children) != 1
):
controls_row.clear()
with controls_row:
with ui.button(
"", icon="autorenew", on_click=reopen_game
).classes("rounded-full w-12 h-12") as new_game_btn:
ui.tooltip("Start New Game")
return
else:
# Ensure header state is correct when game is open (should happen automatically through binding)
import src.core.game_logic
from src.core.game_logic import current_header_text
if hasattr(src.core.game_logic, "current_header_text"):
src.core.game_logic.current_header_text = HEADER_TEXT
# Normal update if game is not closed
# Update tile styles in every board view (e.g., home and stream)
for view_key, (container, tile_buttons_local) in board_views.items():
update_tile_styles(tile_buttons_local)
# Safely run JavaScript to resize text
try:
# Add a slight delay to ensure DOM updates have propagated
js_code = """
setTimeout(function() {
if (typeof fitty !== 'undefined') {
fitty('.fit-text', { multiLine: true, minSize: 10, maxSize: 1000 });
fitty('.fit-text-small', { multiLine: true, minSize: 10, maxSize: 72 });
}
}, 50);
"""
ui.run_javascript(js_code)
except Exception as e:
logging.debug(
f"JavaScript execution failed (likely disconnected client): {e}"
)
except Exception as e:
logging.debug(f"Error in sync_board_state: {e}")
def update_tile_styles(tile_buttons_dict: dict):
"""
Update styles for each tile and its text labels based on the clicked_tiles set.
"""
from src.config.constants import (
FREE_SPACE_TEXT,
TILE_CLICKED_BG_COLOR,
TILE_CLICKED_TEXT_COLOR,
TILE_UNCLICKED_BG_COLOR,
TILE_UNCLICKED_TEXT_COLOR,
)
from src.core.game_logic import board, clicked_tiles
for (r, c), tile in tile_buttons_dict.items():
# tile is a dict with keys "card" and "labels"
phrase = board[r][c]
if (r, c) in clicked_tiles:
new_card_style = f"background-color: {TILE_CLICKED_BG_COLOR}; color: {TILE_CLICKED_TEXT_COLOR}; border: none; outline: 3px solid {TILE_CLICKED_TEXT_COLOR};"
new_label_color = TILE_CLICKED_TEXT_COLOR
else:
new_card_style = f"background-color: {TILE_UNCLICKED_BG_COLOR}; color: {TILE_UNCLICKED_TEXT_COLOR}; border: none;"
new_label_color = TILE_UNCLICKED_TEXT_COLOR
# Update the card style.
tile["card"].style(new_card_style)
tile["card"].update()
# Recalculate the line count for the current phrase.
lines = split_phrase_into_lines(phrase)
line_count = len(lines)
# Recalculate label style based on the new color.
new_label_style = get_line_style_for_lines(line_count, new_label_color)
# Update all label elements for this tile.
for label_info in tile["labels"]:
lbl = label_info["ref"]
# Reapply the stored base classes.
lbl.classes(label_info["base_classes"])
# Update inline style (which may now use a new color due to tile click state).
lbl.style(new_label_style)
lbl.update()