File size: 3,657 Bytes
29b445b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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