Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -37,7 +37,6 @@ def load_latest_global_save():
|
|
37 |
json_files = [f for f in os.listdir(GLOBAL_SAVES_DIR) if f.endswith(".json")]
|
38 |
if not json_files:
|
39 |
return None
|
40 |
-
# Assuming filenames contain timestamp info, sort them in reverse order.
|
41 |
json_files.sort(reverse=True)
|
42 |
latest_file = json_files[0]
|
43 |
with open(os.path.join(GLOBAL_SAVES_DIR, latest_file), "r", encoding="utf-8") as f:
|
@@ -53,7 +52,6 @@ def consolidate_global_saves():
|
|
53 |
if not json_files:
|
54 |
return None
|
55 |
merged_state = {"timestamp": "", "game_state": [], "player_position": {"x": 0, "y": 0, "z": 0}}
|
56 |
-
# Use a dictionary keyed by obj_id to union objects.
|
57 |
obj_dict = {}
|
58 |
latest_timestamp = None
|
59 |
latest_player_position = {"x": 0, "y": 0, "z": 0}
|
@@ -71,7 +69,6 @@ def consolidate_global_saves():
|
|
71 |
merged_state["timestamp"] = latest_timestamp if latest_timestamp else time.strftime("%Y-%m-%d %H:%M:%S")
|
72 |
merged_state["game_state"] = list(obj_dict.values())
|
73 |
merged_state["player_position"] = latest_player_position
|
74 |
-
# Write the consolidated state with current timestamp.
|
75 |
consolidated_filename = f"consolidated_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
|
76 |
consolidated_path = os.path.join(GLOBAL_SAVES_DIR, consolidated_filename)
|
77 |
with open(consolidated_path, "w", encoding="utf-8") as f:
|
@@ -84,6 +81,39 @@ def consolidate_global_saves():
|
|
84 |
st.error(f"Error deleting old global save {f}: {e}")
|
85 |
return merged_state
|
86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
@st.cache_data(ttl=3600)
|
88 |
def load_plot_metadata():
|
89 |
"""Scans SAVE_DIR for plot files and returns metadata."""
|
@@ -221,7 +251,7 @@ all_initial_objects = []
|
|
221 |
for plot in plots_metadata:
|
222 |
all_initial_objects.extend(load_plot_objects(plot['filename'], plot['x_offset'], plot['z_offset']))
|
223 |
|
224 |
-
# If GameState is
|
225 |
if not game_state.get_state():
|
226 |
game_state.update_state(all_initial_objects)
|
227 |
|
@@ -257,7 +287,7 @@ with st.sidebar:
|
|
257 |
|
258 |
st.markdown("---")
|
259 |
st.header("Save Work (Per Plot)")
|
260 |
-
st.caption("Saves newly placed objects to the current plot.
|
261 |
if st.button("πΎ Save Current Work", key="save_button"):
|
262 |
from streamlit_js_eval import streamlit_js_eval
|
263 |
js_get_data_code = "getSaveDataAndPosition();"
|
@@ -266,7 +296,6 @@ with st.sidebar:
|
|
266 |
|
267 |
st.markdown("---")
|
268 |
st.header("Global Save & Load")
|
269 |
-
# Global Save: Persists the entire game state plus current player position.
|
270 |
if st.button("πΎ Global Save"):
|
271 |
global_save_data = {
|
272 |
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S"),
|
@@ -279,10 +308,10 @@ with st.sidebar:
|
|
279 |
json.dump(global_save_data, f, indent=2)
|
280 |
st.success(f"Global state saved to {default_save_name}")
|
281 |
st.session_state.loaded_global_state = global_save_data
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
|
287 |
st.subheader("π Global Saves")
|
288 |
save_files = sorted([f for f in os.listdir(GLOBAL_SAVES_DIR) if f.endswith(".json")])
|
@@ -350,6 +379,7 @@ if save_data_from_js is not None:
|
|
350 |
st.success(f"Updated existing plot: {target_filename}")
|
351 |
game_state.update_state(objects_to_save)
|
352 |
save_processed_successfully = True
|
|
|
353 |
else:
|
354 |
st.error(f"Failed to save plot data to file: {target_filename}")
|
355 |
else:
|
|
|
37 |
json_files = [f for f in os.listdir(GLOBAL_SAVES_DIR) if f.endswith(".json")]
|
38 |
if not json_files:
|
39 |
return None
|
|
|
40 |
json_files.sort(reverse=True)
|
41 |
latest_file = json_files[0]
|
42 |
with open(os.path.join(GLOBAL_SAVES_DIR, latest_file), "r", encoding="utf-8") as f:
|
|
|
52 |
if not json_files:
|
53 |
return None
|
54 |
merged_state = {"timestamp": "", "game_state": [], "player_position": {"x": 0, "y": 0, "z": 0}}
|
|
|
55 |
obj_dict = {}
|
56 |
latest_timestamp = None
|
57 |
latest_player_position = {"x": 0, "y": 0, "z": 0}
|
|
|
69 |
merged_state["timestamp"] = latest_timestamp if latest_timestamp else time.strftime("%Y-%m-%d %H:%M:%S")
|
70 |
merged_state["game_state"] = list(obj_dict.values())
|
71 |
merged_state["player_position"] = latest_player_position
|
|
|
72 |
consolidated_filename = f"consolidated_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
|
73 |
consolidated_path = os.path.join(GLOBAL_SAVES_DIR, consolidated_filename)
|
74 |
with open(consolidated_path, "w", encoding="utf-8") as f:
|
|
|
81 |
st.error(f"Error deleting old global save {f}: {e}")
|
82 |
return merged_state
|
83 |
|
84 |
+
def perform_global_save():
|
85 |
+
"""
|
86 |
+
Immediately save the current game state (with player position) to a new JSON file,
|
87 |
+
then consolidate all global saves and update session state.
|
88 |
+
"""
|
89 |
+
global_save_data = {
|
90 |
+
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S"),
|
91 |
+
"game_state": game_state.get_state(),
|
92 |
+
"player_position": st.session_state.get("player_position", {"x": 0, "y": 0, "z": 0})
|
93 |
+
}
|
94 |
+
default_save_name = f"save_{time.strftime('%Y%m%d_%H%M%S')}.json"
|
95 |
+
save_file_path = os.path.join(GLOBAL_SAVES_DIR, default_save_name)
|
96 |
+
with open(save_file_path, "w", encoding="utf-8") as f:
|
97 |
+
json.dump(global_save_data, f, indent=2)
|
98 |
+
# Consolidate all global saves into one file and update session state.
|
99 |
+
consolidated_state = consolidate_global_saves()
|
100 |
+
if consolidated_state is not None:
|
101 |
+
st.session_state.loaded_global_state = consolidated_state
|
102 |
+
|
103 |
+
def reload_global_state():
|
104 |
+
"""
|
105 |
+
Reload all global save files, consolidate them, and update the game state.
|
106 |
+
"""
|
107 |
+
consolidated_state = consolidate_global_saves()
|
108 |
+
if consolidated_state is not None:
|
109 |
+
with game_state.lock:
|
110 |
+
game_state.world_state = consolidated_state.get("game_state", [])
|
111 |
+
st.session_state.player_position = consolidated_state.get("player_position", {"x": 0, "y": 0, "z": 0})
|
112 |
+
st.session_state.loaded_global_state = consolidated_state
|
113 |
+
st.success("Global state reloaded!")
|
114 |
+
else:
|
115 |
+
st.warning("No global save found to reload.")
|
116 |
+
|
117 |
@st.cache_data(ttl=3600)
|
118 |
def load_plot_metadata():
|
119 |
"""Scans SAVE_DIR for plot files and returns metadata."""
|
|
|
251 |
for plot in plots_metadata:
|
252 |
all_initial_objects.extend(load_plot_objects(plot['filename'], plot['x_offset'], plot['z_offset']))
|
253 |
|
254 |
+
# If GameState is empty, update it with initial objects.
|
255 |
if not game_state.get_state():
|
256 |
game_state.update_state(all_initial_objects)
|
257 |
|
|
|
287 |
|
288 |
st.markdown("---")
|
289 |
st.header("Save Work (Per Plot)")
|
290 |
+
st.caption("Saves newly placed objects to the current plot.")
|
291 |
if st.button("πΎ Save Current Work", key="save_button"):
|
292 |
from streamlit_js_eval import streamlit_js_eval
|
293 |
js_get_data_code = "getSaveDataAndPosition();"
|
|
|
296 |
|
297 |
st.markdown("---")
|
298 |
st.header("Global Save & Load")
|
|
|
299 |
if st.button("πΎ Global Save"):
|
300 |
global_save_data = {
|
301 |
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S"),
|
|
|
308 |
json.dump(global_save_data, f, indent=2)
|
309 |
st.success(f"Global state saved to {default_save_name}")
|
310 |
st.session_state.loaded_global_state = global_save_data
|
311 |
+
perform_global_save()
|
312 |
+
|
313 |
+
if st.button("π Reload Global State"):
|
314 |
+
reload_global_state()
|
315 |
|
316 |
st.subheader("π Global Saves")
|
317 |
save_files = sorted([f for f in os.listdir(GLOBAL_SAVES_DIR) if f.endswith(".json")])
|
|
|
379 |
st.success(f"Updated existing plot: {target_filename}")
|
380 |
game_state.update_state(objects_to_save)
|
381 |
save_processed_successfully = True
|
382 |
+
perform_global_save()
|
383 |
else:
|
384 |
st.error(f"Failed to save plot data to file: {target_filename}")
|
385 |
else:
|