1+ # Page definitions
2+ from nicegui import ui
3+ import logging
4+ from typing import Dict , Tuple , List , Set , Any
5+
6+ from src .config .constants import (
7+ HOME_BG_COLOR , STREAM_BG_COLOR
8+ )
9+ from src .core .board import (
10+ board , clicked_tiles , board_views ,
11+ generate_board , reset_board , generate_new_board ,
12+ toggle_tile , today_seed
13+ )
14+ from src .core .win_patterns import check_winner , process_win_notifications
15+ from src .core .phrases import check_phrases_file_change
16+ from src .ui .styling import setup_head
17+ from src .ui .components import create_header , create_board_controls
18+ from src .ui .board_view import build_board , update_tile_styles
19+ from src .utils .javascript import setup_javascript , run_fitty_js
20+
21+ # Global variable for the seed label
22+ seed_label = None
23+
24+ def toggle_tile_handler (row : int , col : int ):
25+ """Handle tile click events."""
26+ toggle_tile (row , col )
27+
28+ # Check for win conditions
29+ new_patterns = check_winner (clicked_tiles )
30+ process_win_notifications (new_patterns )
31+
32+ # Update all board views
33+ sync_board_state ()
34+
35+ def sync_board_state ():
36+ """Synchronize the board state across all views."""
37+ try :
38+ # Update tile styles in every board view
39+ for view_key , (container , tile_buttons_local ) in board_views .items ():
40+ update_tile_styles (board , clicked_tiles , tile_buttons_local )
41+ container .update ()
42+
43+ # Run fitty to resize text
44+ run_fitty_js ()
45+ except Exception as e :
46+ logging .debug (f"Error in sync_board_state: { e } " )
47+
48+ def create_board_view (background_color : str , is_global : bool ):
49+ """Create a board view (home or stream)."""
50+ # Setup page head elements
51+ setup_head (background_color )
52+ setup_javascript ()
53+
54+ # Create header
55+ create_header ()
56+
57+ # Create board container
58+ if is_global :
59+ container = ui .element ("div" ).classes ("home-board-container flex justify-center items-center w-full" )
60+ try :
61+ ui .run_javascript ("document.querySelector('.home-board-container').id = 'board-container'" )
62+ except Exception as e :
63+ logging .debug (f"Setting board container ID failed: { e } " )
64+ else :
65+ container = ui .element ("div" ).classes ("stream-board-container flex justify-center items-center w-full" )
66+ try :
67+ ui .run_javascript ("document.querySelector('.stream-board-container').id = 'board-container-stream'" )
68+ except Exception as e :
69+ logging .debug (f"Setting stream container ID failed: { e } " )
70+
71+ if is_global :
72+ # For home view, use global state
73+ global seed_label
74+ tile_buttons_dict = {}
75+ build_board (container , board , clicked_tiles , tile_buttons_dict , toggle_tile_handler )
76+ board_views ["home" ] = (container , tile_buttons_dict )
77+
78+ # Add phrase file watcher
79+ try :
80+ check_timer = ui .timer (1 , check_phrases_file_change )
81+ except Exception as e :
82+ logging .warning (f"Error setting up timer: { e } " )
83+
84+ # Add board controls
85+ seed_label = create_board_controls (reset_board , generate_new_board , today_seed )
86+ else :
87+ # For stream view, create local state
88+ local_tile_buttons = {}
89+ build_board (container , board , clicked_tiles , local_tile_buttons , toggle_tile_handler )
90+ board_views ["stream" ] = (container , local_tile_buttons )
91+
92+ @ui .page ("/" )
93+ def home_page ():
94+ """Home page with interactive board."""
95+ create_board_view (HOME_BG_COLOR , True )
96+ try :
97+ # Create a timer that deactivates when the client disconnects
98+ timer = ui .timer (0.1 , sync_board_state )
99+ except Exception as e :
100+ logging .warning (f"Error creating timer: { e } " )
101+
102+ @ui .page ("/stream" )
103+ def stream_page ():
104+ """Stream overlay page."""
105+ create_board_view (STREAM_BG_COLOR , False )
106+ try :
107+ # Create a timer that deactivates when the client disconnects
108+ timer = ui .timer (0.1 , sync_board_state )
109+ except Exception as e :
110+ logging .warning (f"Error creating timer: { e } " )
111+
112+ def setup_pages ():
113+ """Initialize all pages."""
114+ # Make sure the page routes are registered
115+ # The decorators should handle registration, but we include the functions here
116+ # to ensure they're imported properly
117+ home_page
118+ stream_page
0 commit comments