import streamlit as st import json import os import time def update_game_state(tag_currency=None, enkephalin=None, threshold=None, game_stats=None, save=True): """ Update game state and ensure UI reflects changes Args: tag_currency: New tag currency value (or None to keep current) enkephalin: New enkephalin value (or None to keep current) threshold: New threshold value (or None to keep current) game_stats: Dict of game stats to update (or None to keep current) save: Whether to save state to disk """ changed = False if tag_currency is not None and tag_currency != st.session_state.tag_currency: st.session_state.tag_currency = tag_currency changed = True if enkephalin is not None and enkephalin != st.session_state.enkephalin: st.session_state.enkephalin = enkephalin changed = True if threshold is not None and threshold != st.session_state.threshold: st.session_state.threshold = threshold changed = True # Update game stats if provided if game_stats is not None: for key, value in game_stats.items(): if key in st.session_state.game_stats and st.session_state.game_stats[key] != value: st.session_state.game_stats[key] = value changed = True if changed: # Increment the state version to trigger UI updates if 'state_version' not in st.session_state: st.session_state.state_version = 0 st.session_state.state_version += 1 # Save if requested if save: save_game_state() def save_game_state(): """Save current game state after scanning""" try: import tag_storage success = tag_storage.update_tag_storage_from_session(st.session_state) # Additionally save other game data game_data = { "threshold": st.session_state.threshold, "tag_currency": st.session_state.tag_currency, "enkephalin": st.session_state.enkephalin, "purchased_upgrades": st.session_state.purchased_upgrades, "achievements": list(st.session_state.achievements), "game_stats": st.session_state.game_stats, "tag_power_bonus": st.session_state.tag_power_bonus if hasattr(st.session_state, 'tag_power_bonus') else 0, "coin_multiplier": st.session_state.coin_multiplier if hasattr(st.session_state, 'coin_multiplier') else 1.0, "unlocked_combinations": list(st.session_state.unlocked_combinations) if hasattr(st.session_state, 'unlocked_combinations') else [], "combination_bonuses": st.session_state.combination_bonuses if hasattr(st.session_state, 'combination_bonuses') else {"threshold_reduction": 0, "coin_bonus": 0}, "sacrificed_tags": st.session_state.sacrificed_tags if hasattr(st.session_state, 'sacrificed_tags') else {}, "saved_time": time.strftime("%Y-%m-%d %H:%M:%S") } with open(tag_storage.get_storage_path("game_state.json"), "w") as f: json.dump(game_data, f, indent=2) return success except Exception as e: print(f"Error saving game state: {e}") import traceback traceback.print_exc() return False def force_update_ui(): """Force a UI update by incrementing state version""" if 'state_version' not in st.session_state: st.session_state.state_version = 0 st.session_state.state_version += 1