awacke1 commited on
Commit
a2141e7
·
verified ·
1 Parent(s): 039b051

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +221 -214
app.py CHANGED
@@ -3,273 +3,266 @@ import streamlit as st
3
  import streamlit.components.v1 as components
4
  import os
5
  import json
6
- import sqlite3 # Use SQLite for robust state management
7
  import uuid
8
- import math
9
- import time # For potential delays if needed
10
  from streamlit_js_eval import streamlit_js_eval # For JS communication
11
 
12
  # --- Constants ---
13
- DB_FILE = "world_state_v3.db" # Use a new DB file name to ensure fresh start
14
- PLOT_WIDTH = 50.0
15
- PLOT_DEPTH = 50.0
16
-
17
- # --- Database Setup ---
18
- def init_db():
19
- """Initializes the SQLite database and tables."""
20
- st.write(f"DEBUG: Initializing Database '{os.path.abspath(DB_FILE)}'...")
21
- try:
22
- # Ensure connection closes even if errors occur during table creation
23
- with sqlite3.connect(DB_FILE, timeout=10) as conn: # Added timeout
24
- cursor = conn.cursor()
25
- # Enable WAL mode for potentially better concurrency - might help?
26
- # cursor.execute('PRAGMA journal_mode=WAL;')
27
- # Plots table
28
- cursor.execute('''
29
- CREATE TABLE IF NOT EXISTS plots (
30
- grid_x INTEGER NOT NULL, grid_z INTEGER NOT NULL, name TEXT,
31
- last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
32
- PRIMARY KEY (grid_x, grid_z)
33
- )
34
- ''')
35
- # Objects table
36
- cursor.execute('''
37
- CREATE TABLE IF NOT EXISTS objects (
38
- obj_id TEXT PRIMARY KEY, plot_grid_x INTEGER NOT NULL, plot_grid_z INTEGER NOT NULL,
39
- type TEXT NOT NULL, pos_x REAL NOT NULL, pos_y REAL NOT NULL, pos_z REAL NOT NULL,
40
- rot_x REAL DEFAULT 0.0, rot_y REAL DEFAULT 0.0, rot_z REAL DEFAULT 0.0, rot_order TEXT DEFAULT 'XYZ',
41
- FOREIGN KEY (plot_grid_x, plot_grid_z) REFERENCES plots(grid_x, grid_z) ON DELETE CASCADE
42
- )
43
- ''')
44
- cursor.execute('CREATE INDEX IF NOT EXISTS idx_objects_plot ON objects (plot_grid_x, plot_grid_z)')
45
- print(f"DEBUG: Database {DB_FILE} initialized/checked.") # To console
46
- except sqlite3.Error as e:
47
- st.exception(f"CRITICAL DATABASE INIT ERROR: {e}")
48
- st.stop()
49
-
50
- init_db() # Run initialization at the start
51
-
52
- # --- Helper Functions (Database Operations - Robust Connections) ---
53
-
54
- # *** REMOVED @st.cache_data - Load fresh every time ***
55
- def load_world_state_from_db():
56
- """Loads all plot metadata and object data fresh from the SQLite DB."""
57
- session_id = st.runtime.scriptrunner.get_script_run_ctx().session_id[:5] # Short ID for logging
58
- st.write(f"DEBUG [{session_id}]: Executing load_world_state_from_db...")
59
- plots_metadata = []
60
- all_initial_objects_world = []
61
-
62
- try:
63
- # Ensure connection is opened and closed for each load operation
64
- with sqlite3.connect(DB_FILE, timeout=10) as conn:
65
- conn.row_factory = sqlite3.Row
66
- cursor = conn.cursor()
67
-
68
- # 1. Load Plot Metadata
69
- cursor.execute("SELECT grid_x, grid_z, name FROM plots ORDER BY grid_x, grid_z")
70
- plot_rows = cursor.fetchall()
71
- st.write(f"DEBUG [{session_id}]: DB Read Found {len(plot_rows)} plot rows.")
72
- if not plot_rows:
73
- st.write(f"DEBUG [{session_id}]: No plots found in DB.")
74
-
75
- plot_keys = set() # To efficiently map objects later
76
- for row in plot_rows:
77
- gx, gz = row['grid_x'], row['grid_z']
78
- plots_metadata.append({
79
- 'id': f"plot_X{gx}_Z{gz}", 'grid_x': gx, 'grid_z': gz,
80
- 'name': row['name'] or f"Plot ({gx},{gz})",
81
- 'x_offset': gx * PLOT_WIDTH, 'z_offset': gz * PLOT_DEPTH
82
- })
83
- plot_keys.add((gx, gz))
84
-
85
- # 2. Load All Objects
86
- cursor.execute("SELECT obj_id, plot_grid_x, plot_grid_z, type, pos_x, pos_y, pos_z, rot_x, rot_y, rot_z, rot_order FROM objects")
87
- object_rows = cursor.fetchall()
88
- st.write(f"DEBUG [{session_id}]: DB Read Found {len(object_rows)} object rows.")
89
-
90
- # 3. Combine and Calculate World Coordinates
91
- objects_loaded_count = 0
92
- for row in object_rows:
93
- plot_key = (row['plot_grid_x'], row['plot_grid_z'])
94
- # Ensure the object belongs to a known plot (data integrity check)
95
- if plot_key in plot_keys:
96
- obj_data = dict(row) # Convert row object to dict
97
- # Find corresponding plot metadata for offset calculation
98
- plot_meta = next((p for p in plots_metadata if p['grid_x'] == row['plot_grid_x'] and p['grid_z'] == row['plot_grid_z']), None)
99
- if plot_meta:
100
- world_obj_data = obj_data.copy()
101
- world_obj_data['pos_x'] = obj_data['pos_x'] + plot_meta['x_offset']
102
- world_obj_data['pos_z'] = obj_data['pos_z'] + plot_meta['z_offset']
103
- world_obj_data['pos_y'] = obj_data['pos_y'] # Y is already world Y
104
- all_initial_objects_world.append(world_obj_data)
105
- objects_loaded_count += 1
106
- else:
107
- print(f"WARN: Object {obj_data['obj_id']} references non-existent plot {plot_key}") # To console
108
- else:
109
- print(f"WARN: Object {obj_data['obj_id']} references unknown plot {plot_key}") # To console
110
-
111
-
112
- st.write(f"DEBUG [{session_id}]: DB Load Complete. Processed {len(plots_metadata)} plots, {objects_loaded_count} objects.")
113
-
114
- except sqlite3.Error as e:
115
- st.exception(f"Database load error for session {session_id}: {e}")
116
- return [], [] # Return empty on error
117
-
118
- return plots_metadata, all_initial_objects_world
119
 
 
 
120
 
121
- def save_plot_data_to_db(target_grid_x, target_grid_z, objects_data_list):
122
- """Saves object data list to DB for a specific plot. Overwrites existing objects."""
123
- st.write(f"DEBUG: Executing save_plot_data_to_db for plot ({target_grid_x},{target_grid_z})...")
124
- plot_x_offset = target_grid_x * PLOT_WIDTH
125
- plot_z_offset = target_grid_z * PLOT_DEPTH
126
- # Use a generic name or allow passing one if needed later
127
- plot_name = f"Plot ({target_grid_x},{target_grid_z})"
128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129
  if not isinstance(objects_data_list, list):
130
- st.error("Save Error: Invalid object data format (expected list).")
131
  return False
132
 
133
- try:
134
- # Use 'with' for automatic transaction handling and closing
135
- with sqlite3.connect(DB_FILE, timeout=10) as conn:
136
- cursor = conn.cursor()
137
-
138
- # 1. Upsert Plot (ensures plot exists, updates timestamp)
139
- cursor.execute('''
140
- INSERT INTO plots (grid_x, grid_z, name) VALUES (?, ?, ?)
141
- ON CONFLICT(grid_x, grid_z) DO UPDATE SET last_updated = CURRENT_TIMESTAMP
142
- ''', (target_grid_x, target_grid_z, plot_name))
143
- # st.write(f"DEBUG: Upserted plot metadata for ({target_grid_x},{target_grid_z})")
144
-
145
- # 2. Delete ALL existing objects for this specific plot
146
- cursor.execute("DELETE FROM objects WHERE plot_grid_x = ? AND plot_grid_z = ?", (target_grid_x, target_grid_z))
147
- # st.write(f"DEBUG: Deleted {cursor.rowcount} old objects for plot.")
148
-
149
- # 3. Insert the new objects
150
- objects_to_insert = []
151
- for obj in objects_data_list:
152
- pos = obj.get('position', {})
153
- rot = obj.get('rotation', {})
154
- obj_type = obj.get('type', 'Unknown')
155
- obj_id = obj.get('obj_id', str(uuid.uuid4()))
156
-
157
- if not all(k in pos for k in ['x', 'y', 'z']) or obj_type == 'Unknown': continue
158
-
159
- rel_x = pos.get('x', 0.0) - plot_x_offset
160
- rel_z = pos.get('z', 0.0) - plot_z_offset
161
- rel_y = pos.get('y', 0.0)
162
-
163
- objects_to_insert.append((
164
- obj_id, target_grid_x, target_grid_z, obj_type,
165
- rel_x, rel_y, rel_z,
166
- rot.get('_x', 0.0), rot.get('_y', 0.0), rot.get('_z', 0.0), rot.get('_order', 'XYZ')
167
- ))
168
-
169
- if objects_to_insert:
170
- cursor.executemany('''
171
- INSERT OR REPLACE INTO objects
172
- (obj_id, plot_grid_x, plot_grid_z, type, pos_x, pos_y, pos_z, rot_x, rot_y, rot_z, rot_order)
173
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
174
- ''', objects_to_insert) # Use INSERT OR REPLACE based on obj_id primary key
175
- st.write(f"DEBUG: Inserted/Replaced {len(objects_to_insert)} objects.")
176
- else:
177
- st.write(f"DEBUG: No objects provided to insert for plot ({target_grid_x},{target_grid_z}). Plot cleared.")
178
-
179
- # Commit happens automatically when 'with' block exits without error
180
 
181
- st.success(f"Plot ({target_grid_x},{target_grid_z}) saved successfully to DB.")
 
 
 
182
  return True
183
-
184
- except sqlite3.Error as e:
185
- st.exception(f"DATABASE SAVE ERROR for plot ({target_grid_x},{target_grid_z}): {e}")
186
  return False
187
 
188
  # --- Page Config ---
189
- st.set_page_config( page_title="DB Synced World Builder v3", layout="wide")
190
 
191
  # --- Initialize Session State ---
192
  if 'selected_object' not in st.session_state: st.session_state.selected_object = 'None'
 
193
  if 'js_save_data_result' not in st.session_state: st.session_state.js_save_data_result = None
194
- # No refresh counter needed
195
 
196
- # --- Load World State From DB (Fresh on each run/rerun) ---
197
- plots_metadata, all_initial_objects = load_world_state_from_db()
 
 
 
 
 
 
198
 
199
  # --- Sidebar ---
200
  with st.sidebar:
201
  st.title("🏗️ World Controls")
202
 
203
- # Refresh Button (Just reruns the script)
204
- if st.button("🔄 Refresh World View", key="refresh_button"):
205
- st.info("Reloading world state from database...")
206
- st.rerun() # Rerun forces call to load_world_state_from_db
207
-
208
  st.header("Navigation (Plots)")
209
- # ... (Navigation button code unchanged - uses latest plots_metadata) ...
210
  st.caption("Click to teleport player to a plot.")
211
- max_cols = 2; cols = st.columns(max_cols); col_idx = 0
 
 
 
212
  sorted_plots_for_nav = sorted(plots_metadata, key=lambda p: (p['grid_x'], p['grid_z']))
213
  for plot in sorted_plots_for_nav:
214
  button_label = f"➡️ {plot.get('name', plot['id'])} ({plot['grid_x']},{plot['grid_z']})"
215
  if cols[col_idx].button(button_label, key=f"nav_{plot['id']}"):
216
- target_x = plot['x_offset'] + PLOT_WIDTH/2; target_z = plot['z_offset'] + PLOT_DEPTH/2
217
- try: streamlit_js_eval(js_code=f"teleportPlayer({target_x}, {target_z});", key=f"teleport_{plot['id']}")
218
- except Exception as e: st.error(f"Teleport command failed: {e}")
 
 
 
 
 
219
  col_idx = (col_idx + 1) % max_cols
 
220
  st.markdown("---")
221
 
222
  # --- Object Placement ---
223
  st.header("Place Objects")
224
- # ... (Object selection unchanged) ...
225
  object_types = ["None", "Simple House", "Tree", "Rock", "Fence Post"]
226
  current_object_index = object_types.index(st.session_state.selected_object) if st.session_state.selected_object in object_types else 0
227
- selected_object_type_widget = st.selectbox( "Select Object:", options=object_types, index=current_object_index, key="selected_object_widget")
228
- if selected_object_type_widget != st.session_state.selected_object: st.session_state.selected_object = selected_object_type_widget
 
 
 
 
 
229
  st.markdown("---")
230
 
231
  # --- Saving ---
232
  st.header("Save Work")
233
- st.caption("Saves ALL objects in the player's current plot to the central database.")
234
- if st.button("💾 Save Current Plot", key="save_button"):
235
- js_get_data_code = "getSaveDataAndPosition();"
 
236
  streamlit_js_eval(js_code=js_get_data_code, key="js_save_processor")
237
- st.rerun()
238
 
239
 
240
  # --- Process Save Data ---
241
  save_data_from_js = st.session_state.get("js_save_processor", None)
242
 
243
  if save_data_from_js is not None:
244
- st.write("DEBUG: Processing save request from JS...") # Debug
245
  save_processed_successfully = False
246
  try:
 
247
  payload = json.loads(save_data_from_js) if isinstance(save_data_from_js, str) else save_data_from_js
 
248
  if isinstance(payload, dict) and 'playerPosition' in payload and 'objectsToSave' in payload:
249
  player_pos = payload['playerPosition']
250
- objects_to_save = payload['objectsToSave'] # World coords from JS
251
- if isinstance(objects_to_save, list):
 
 
252
  target_grid_x = math.floor(player_pos.get('x', 0.0) / PLOT_WIDTH)
253
- target_grid_z = math.floor(player_pos.get('z', 0.0) / PLOT_DEPTH)
254
- # --- Save the data to SQLite DB ---
255
- save_ok = save_plot_data_to_db(target_grid_x, target_grid_z, objects_to_save)
256
- if save_ok: save_processed_successfully = True # Flag success
257
- else: st.error(f"Failed DB save for plot ({target_grid_x},{target_grid_z}).")
258
- else: st.error("Save Error: 'objectsToSave' format invalid (expected list).")
259
- else: st.error("Save Error: Invalid payload structure received.")
260
- except json.JSONDecodeError: st.error("Save Error: Failed to decode JSON data from client.")
261
- except Exception as e: st.exception(f"Save Error: An unexpected error occurred: {e}")
262
-
263
- # Clear the trigger data from session state ALWAYS after processing attempt
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
264
  st.session_state.js_save_processor = None
265
- # Rerun ONLY if save was flagged successful to reload the state
266
  if save_processed_successfully:
267
  st.rerun()
268
 
269
 
270
  # --- Main Area ---
271
- st.header("Database Synced 3D World")
272
- st.caption(f"DB: '{os.path.abspath(DB_FILE)}'. Plots loaded: {len(plots_metadata)}. Use 'Refresh' button to sync.")
273
 
274
  # --- Load and Prepare HTML ---
275
  html_file_path = 'index.html'
@@ -279,21 +272,35 @@ try:
279
  with open(html_file_path, 'r', encoding='utf-8') as f:
280
  html_template = f.read()
281
 
282
- # Inject data loaded fresh from DB
283
  js_injection_script = f"""
284
  <script>
285
  window.ALL_INITIAL_OBJECTS = {json.dumps(all_initial_objects)};
286
- window.PLOTS_METADATA = {json.dumps(plots_metadata)};
287
  window.SELECTED_OBJECT_TYPE = {json.dumps(st.session_state.selected_object)};
288
  window.PLOT_WIDTH = {json.dumps(PLOT_WIDTH)};
289
  window.PLOT_DEPTH = {json.dumps(PLOT_DEPTH)};
290
- console.log("Streamlit State Injected:", {{ /* Basic logging */ }});
 
 
 
 
 
 
291
  </script>
292
  """
293
  html_content_with_state = html_template.replace('</head>', js_injection_script + '\n</head>', 1)
294
 
295
- # Embed HTML Component
296
- components.html( html_content_with_state, height=750, scrolling=False )
297
-
298
- except FileNotFoundError: st.error(f"CRITICAL ERROR: Could not find file '{html_file_path}'.")
299
- except Exception as e: st.exception(f"HTML prep/render error: {e}")
 
 
 
 
 
 
 
 
 
3
  import streamlit.components.v1 as components
4
  import os
5
  import json
6
+ import pandas as pd
7
  import uuid
8
+ import math # For floor function
9
+ # from PIL import Image, ImageDraw # No longer needed for minimap
10
  from streamlit_js_eval import streamlit_js_eval # For JS communication
11
 
12
  # --- Constants ---
13
+ SAVE_DIR = "saved_worlds"
14
+ PLOT_WIDTH = 50.0 # Width of each plot in 3D space
15
+ PLOT_DEPTH = 50.0 # Depth of each plot (can be same as width)
16
+ CSV_COLUMNS = ['obj_id', 'type', 'pos_x', 'pos_y', 'pos_z', 'rot_x', 'rot_y', 'rot_z', 'rot_order']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
+ # --- Ensure Save Directory Exists ---
19
+ os.makedirs(SAVE_DIR, exist_ok=True)
20
 
21
+ # --- Helper Functions ---
 
 
 
 
 
 
22
 
23
+ @st.cache_data(ttl=3600) # Cache plot list
24
+ def load_plot_metadata():
25
+ """Scans save dir for plot_X*_Z*.csv, sorts, calculates metadata."""
26
+ plots = []
27
+ plot_files = []
28
+ try:
29
+ plot_files = [f for f in os.listdir(SAVE_DIR) if f.endswith(".csv") and f.startswith("plot_X")]
30
+ except FileNotFoundError:
31
+ st.error(f"Save directory '{SAVE_DIR}' not found.")
32
+ return []
33
+ except Exception as e:
34
+ st.error(f"Error listing save directory '{SAVE_DIR}': {e}")
35
+ return []
36
+
37
+ # Parse filenames to get grid coordinates
38
+ parsed_plots = []
39
+ for filename in plot_files:
40
+ try:
41
+ parts = filename[:-4].split('_') # Remove .csv
42
+ grid_x = int(parts[1][1:]) # Extract number after X
43
+ grid_z = int(parts[2][1:]) # Extract number after Z
44
+ # Extract name if present (parts after Z coordinate)
45
+ plot_name = " ".join(parts[3:]) if len(parts) > 3 else f"Plot ({grid_x},{grid_z})"
46
+
47
+ parsed_plots.append({
48
+ 'id': filename[:-4], # Use filename base as unique ID
49
+ 'filename': filename,
50
+ 'grid_x': grid_x,
51
+ 'grid_z': grid_z,
52
+ 'name': plot_name,
53
+ 'x_offset': grid_x * PLOT_WIDTH,
54
+ 'z_offset': grid_z * PLOT_DEPTH # Use PLOT_DEPTH for Z offset
55
+ })
56
+ except (IndexError, ValueError):
57
+ st.warning(f"Could not parse grid coordinates from filename: {filename}. Skipping.")
58
+ continue
59
+
60
+ # Sort primarily by X, then by Z
61
+ parsed_plots.sort(key=lambda p: (p['grid_x'], p['grid_z']))
62
+
63
+ return parsed_plots
64
+
65
+ def load_plot_objects(filename, x_offset, z_offset):
66
+ """Loads objects from a CSV, applying the plot's world offsets."""
67
+ file_path = os.path.join(SAVE_DIR, filename)
68
+ objects = []
69
+ try:
70
+ df = pd.read_csv(file_path)
71
+ # Check required columns
72
+ if not all(col in df.columns for col in ['type', 'pos_x', 'pos_y', 'pos_z']):
73
+ st.warning(f"CSV '{filename}' missing essential columns. Skipping.")
74
+ return []
75
+ # Add defaults for optional columns
76
+ df['obj_id'] = df.get('obj_id', pd.Series([str(uuid.uuid4()) for _ in range(len(df))]))
77
+ for col, default in [('rot_x', 0.0), ('rot_y', 0.0), ('rot_z', 0.0), ('rot_order', 'XYZ')]:
78
+ if col not in df.columns: df[col] = default
79
+
80
+ for _, row in df.iterrows():
81
+ obj_data = row.to_dict()
82
+ # Apply world offset
83
+ obj_data['pos_x'] += x_offset
84
+ obj_data['pos_z'] += z_offset # Apply Z offset too
85
+ objects.append(obj_data)
86
+ return objects
87
+ except FileNotFoundError:
88
+ st.error(f"File not found during object load: {filename}")
89
+ return []
90
+ except pd.errors.EmptyDataError:
91
+ return [] # Empty file is valid
92
+ except Exception as e:
93
+ st.error(f"Error loading objects from {filename}: {e}")
94
+ return []
95
+
96
+ def save_plot_data(filename, objects_data_list, plot_x_offset, plot_z_offset):
97
+ """Saves object data list to a CSV, making positions relative to plot origin."""
98
+ file_path = os.path.join(SAVE_DIR, filename)
99
+ relative_objects = []
100
  if not isinstance(objects_data_list, list):
101
+ st.error("Invalid data format received for saving (expected a list).")
102
  return False
103
 
104
+ for obj in objects_data_list:
105
+ pos = obj.get('position', {})
106
+ rot = obj.get('rotation', {})
107
+ obj_type = obj.get('type', 'Unknown')
108
+ obj_id = obj.get('obj_id', str(uuid.uuid4()))
109
+
110
+ if not all(k in pos for k in ['x', 'y', 'z']) or obj_type == 'Unknown':
111
+ print(f"Skipping malformed object during save prep: {obj}")
112
+ continue
113
+
114
+ relative_obj = {
115
+ 'obj_id': obj_id, 'type': obj_type,
116
+ 'pos_x': pos.get('x', 0.0) - plot_x_offset, # Make relative X
117
+ 'pos_y': pos.get('y', 0.0),
118
+ 'pos_z': pos.get('z', 0.0) - plot_z_offset, # Make relative Z
119
+ 'rot_x': rot.get('_x', 0.0), 'rot_y': rot.get('_y', 0.0), 'rot_z': rot.get('_z', 0.0),
120
+ 'rot_order': rot.get('_order', 'XYZ')
121
+ }
122
+ relative_objects.append(relative_obj)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
 
124
+ try:
125
+ df = pd.DataFrame(relative_objects, columns=CSV_COLUMNS)
126
+ df.to_csv(file_path, index=False)
127
+ st.success(f"Saved {len(relative_objects)} objects to {filename}")
128
  return True
129
+ except Exception as e:
130
+ st.error(f"Failed to save plot data to {filename}: {e}")
 
131
  return False
132
 
133
  # --- Page Config ---
134
+ st.set_page_config( page_title="Infinite World Builder", layout="wide")
135
 
136
  # --- Initialize Session State ---
137
  if 'selected_object' not in st.session_state: st.session_state.selected_object = 'None'
138
+ if 'new_plot_name' not in st.session_state: st.session_state.new_plot_name = "" # No longer used for filename
139
  if 'js_save_data_result' not in st.session_state: st.session_state.js_save_data_result = None
 
140
 
141
+ # --- Load Plot Metadata ---
142
+ # This is now the source of truth for saved plots
143
+ plots_metadata = load_plot_metadata()
144
+
145
+ # --- Load ALL Objects for Rendering ---
146
+ all_initial_objects = []
147
+ for plot in plots_metadata:
148
+ all_initial_objects.extend(load_plot_objects(plot['filename'], plot['x_offset'], plot['z_offset']))
149
 
150
  # --- Sidebar ---
151
  with st.sidebar:
152
  st.title("🏗️ World Controls")
153
 
 
 
 
 
 
154
  st.header("Navigation (Plots)")
 
155
  st.caption("Click to teleport player to a plot.")
156
+ max_cols = 2 # Adjust columns for potentially more buttons
157
+ cols = st.columns(max_cols)
158
+ col_idx = 0
159
+ # Sort buttons by grid coords for logical layout
160
  sorted_plots_for_nav = sorted(plots_metadata, key=lambda p: (p['grid_x'], p['grid_z']))
161
  for plot in sorted_plots_for_nav:
162
  button_label = f"➡️ {plot.get('name', plot['id'])} ({plot['grid_x']},{plot['grid_z']})"
163
  if cols[col_idx].button(button_label, key=f"nav_{plot['id']}"):
164
+ target_x = plot['x_offset']
165
+ target_z = plot['z_offset'] # Use Z offset too
166
+ try:
167
+ # Tell JS where to teleport (center of plot approx)
168
+ js_code = f"teleportPlayer({target_x + PLOT_WIDTH/2}, {target_z + PLOT_DEPTH/2});"
169
+ streamlit_js_eval(js_code=js_code, key=f"teleport_{plot['id']}")
170
+ except Exception as e:
171
+ st.error(f"Failed to send teleport command: {e}")
172
  col_idx = (col_idx + 1) % max_cols
173
+
174
  st.markdown("---")
175
 
176
  # --- Object Placement ---
177
  st.header("Place Objects")
 
178
  object_types = ["None", "Simple House", "Tree", "Rock", "Fence Post"]
179
  current_object_index = object_types.index(st.session_state.selected_object) if st.session_state.selected_object in object_types else 0
180
+ selected_object_type_widget = st.selectbox(
181
+ "Select Object:", options=object_types, index=current_object_index, key="selected_object_widget"
182
+ )
183
+ if selected_object_type_widget != st.session_state.selected_object:
184
+ st.session_state.selected_object = selected_object_type_widget
185
+ # Rerun will happen, JS reloads state via sessionStorage, Python injects new selection
186
+
187
  st.markdown("---")
188
 
189
  # --- Saving ---
190
  st.header("Save Work")
191
+ st.caption("Saves newly placed objects to the plot the player is currently in. If it's a new area, a new plot file is created.")
192
+ if st.button("💾 Save Current Work", key="save_button"):
193
+ # Trigger JS to get data AND player position
194
+ js_get_data_code = "getSaveDataAndPosition();" # JS function needs update
195
  streamlit_js_eval(js_code=js_get_data_code, key="js_save_processor")
196
+ st.rerun() # Rerun to process result
197
 
198
 
199
  # --- Process Save Data ---
200
  save_data_from_js = st.session_state.get("js_save_processor", None)
201
 
202
  if save_data_from_js is not None:
203
+ st.info("Received save data from client...")
204
  save_processed_successfully = False
205
  try:
206
+ # Expecting { playerPosition: {x,y,z}, objectsToSave: [...] }
207
  payload = json.loads(save_data_from_js) if isinstance(save_data_from_js, str) else save_data_from_js
208
+
209
  if isinstance(payload, dict) and 'playerPosition' in payload and 'objectsToSave' in payload:
210
  player_pos = payload['playerPosition']
211
+ objects_to_save = payload['objectsToSave']
212
+
213
+ if isinstance(objects_to_save, list): # Allow saving empty list (clears new objects)
214
+ # Determine target plot based on player position
215
  target_grid_x = math.floor(player_pos.get('x', 0.0) / PLOT_WIDTH)
216
+ target_grid_z = math.floor(player_pos.get('z', 0.0) / PLOT_DEPTH) # Use Z pos too
217
+
218
+ target_filename = f"plot_X{target_grid_x}_Z{target_grid_z}.csv"
219
+ target_plot_x_offset = target_grid_x * PLOT_WIDTH
220
+ target_plot_z_offset = target_grid_z * PLOT_DEPTH
221
+
222
+ st.write(f"Attempting to save plot: {target_filename} (Player at: x={player_pos.get('x', 0):.1f}, z={player_pos.get('z', 0):.1f})")
223
+
224
+ # Check if this plot already exists in metadata (for logging/future logic)
225
+ is_new_plot_file = not os.path.exists(os.path.join(SAVE_DIR, target_filename))
226
+
227
+ save_ok = save_plot_data(target_filename, objects_to_save, target_plot_x_offset, target_plot_z_offset)
228
+
229
+ if save_ok:
230
+ load_plot_metadata.clear() # Clear cache to force reload metadata
231
+ try: # Tell JS to clear its unsaved state
232
+ streamlit_js_eval(js_code="resetNewlyPlacedObjects();", key="reset_js_state")
233
+ except Exception as js_e:
234
+ st.warning(f"Could not reset JS state after save: {js_e}")
235
+
236
+ if is_new_plot_file:
237
+ st.success(f"New plot created and saved: {target_filename}")
238
+ else:
239
+ st.success(f"Updated existing plot: {target_filename}")
240
+ save_processed_successfully = True
241
+ else:
242
+ st.error(f"Failed to save plot data to file: {target_filename}")
243
+ else:
244
+ st.error("Invalid 'objectsToSave' format received (expected list).")
245
+ else:
246
+ st.error("Invalid save payload structure received from client.")
247
+ print("Received payload:", payload) # Log for debugging
248
+
249
+ except json.JSONDecodeError:
250
+ st.error("Failed to decode save data from client.")
251
+ print("Received raw data:", save_data_from_js)
252
+ except Exception as e:
253
+ st.error(f"Error processing save: {e}")
254
+ st.exception(e)
255
+
256
+ # Clear the trigger data from session state
257
  st.session_state.js_save_processor = None
258
+ # Rerun after processing to reflect changes
259
  if save_processed_successfully:
260
  st.rerun()
261
 
262
 
263
  # --- Main Area ---
264
+ st.header("Infinite Shared 3D World")
265
+ st.caption("Move to empty areas to expand the world. Use sidebar 'Save' to save work to current plot.")
266
 
267
  # --- Load and Prepare HTML ---
268
  html_file_path = 'index.html'
 
272
  with open(html_file_path, 'r', encoding='utf-8') as f:
273
  html_template = f.read()
274
 
275
+ # --- Inject Python state into JavaScript ---
276
  js_injection_script = f"""
277
  <script>
278
  window.ALL_INITIAL_OBJECTS = {json.dumps(all_initial_objects)};
279
+ window.PLOTS_METADATA = {json.dumps(plots_metadata)}; // Send plot info to JS
280
  window.SELECTED_OBJECT_TYPE = {json.dumps(st.session_state.selected_object)};
281
  window.PLOT_WIDTH = {json.dumps(PLOT_WIDTH)};
282
  window.PLOT_DEPTH = {json.dumps(PLOT_DEPTH)};
283
+ console.log("Streamlit State Injected:", {{
284
+ selectedObject: window.SELECTED_OBJECT_TYPE,
285
+ initialObjectsCount: window.ALL_INITIAL_OBJECTS ? window.ALL_INITIAL_OBJECTS.length : 0,
286
+ plotCount: window.PLOTS_METADATA ? window.PLOTS_METADATA.length : 0,
287
+ plotWidth: window.PLOT_WIDTH,
288
+ plotDepth: window.PLOT_DEPTH
289
+ }});
290
  </script>
291
  """
292
  html_content_with_state = html_template.replace('</head>', js_injection_script + '\n</head>', 1)
293
 
294
+ # --- Embed HTML Component ---
295
+ components.html(
296
+ html_content_with_state,
297
+ height=750,
298
+ scrolling=False
299
+ )
300
+
301
+ except FileNotFoundError:
302
+ st.error(f"CRITICAL ERROR: Could not find the file '{html_file_path}'.")
303
+ st.warning(f"Make sure `{html_file_path}` is in the same directory as `app.py` and `{SAVE_DIR}` exists.")
304
+ except Exception as e:
305
+ st.error(f"An critical error occurred during HTML preparation or component rendering: {e}")
306
+ st.exception(e)