awacke1 commited on
Commit
aeb2145
·
verified ·
1 Parent(s): c71868f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -18
app.py CHANGED
@@ -17,8 +17,8 @@ REFRESH_RATE = 5
17
  INTERACTION_RADIUS = 2
18
  POINTS_PER_INTERACTION = 1
19
 
20
- # Singleton cache for game resources
21
- @st.experimental_singleton
22
  def get_game_images():
23
  """Cache game images to improve performance"""
24
  return {
@@ -27,7 +27,8 @@ def get_game_images():
27
  'other_player': Image.new('RGB', (50, 50), color='red')
28
  }
29
 
30
- @st.experimental_singleton
 
31
  def get_name_components():
32
  """Cache name generation components"""
33
  return {
@@ -37,6 +38,27 @@ def get_name_components():
37
  'is', 'ax', 'on', 'ir', 'ex', 'az', 'er', 'eth', 'ys', 'ix']
38
  }
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  def generate_fantasy_name():
41
  """Generate random fantasy name using cached components"""
42
  name_parts = get_name_components()
@@ -92,18 +114,6 @@ if 'initialized' not in st.session_state:
92
  st.session_state.nearby_players = []
93
  load_game_state()
94
 
95
- @st.experimental_singleton(validate=lambda x: time.time() - x['last_update'] < 60)
96
- def load_all_players():
97
- """Cache and load all player states with validation"""
98
- players = {}
99
- if os.path.exists('players'):
100
- for filename in os.listdir('players'):
101
- if filename.endswith('.json'):
102
- with open(f"players/{filename}", 'r') as f:
103
- player_data = json.load(f)
104
- players[player_data['name']] = player_data
105
- return {'players': players, 'last_update': time.time()}
106
-
107
  def save_player_state():
108
  """Save player state to JSON file"""
109
  if st.session_state.player_name:
@@ -141,7 +151,8 @@ def update_position(direction):
141
 
142
  def update_nearby_players():
143
  """Update list of nearby players and calculate score gains"""
144
- all_players = load_all_players()['players']
 
145
  nearby = []
146
  score_gain = 0
147
 
@@ -164,7 +175,8 @@ def update_nearby_players():
164
 
165
  def create_game_board():
166
  """Create and display the game board"""
167
- all_players = load_all_players()['players']
 
168
  images = get_game_images()
169
 
170
  # Create columns for each row
@@ -247,7 +259,7 @@ def main():
247
  # Clear game state button
248
  if st.sidebar.button("Clear Game State"):
249
  st.query_params.clear()
250
- st.experimental_singleton.clear()
251
  st.rerun()
252
 
253
  # Main game area
 
17
  INTERACTION_RADIUS = 2
18
  POINTS_PER_INTERACTION = 1
19
 
20
+ # Resource cache for game resources - using cache_resource for mutable objects
21
+ @st.cache_resource(show_spinner="Loading game resources...")
22
  def get_game_images():
23
  """Cache game images to improve performance"""
24
  return {
 
27
  'other_player': Image.new('RGB', (50, 50), color='red')
28
  }
29
 
30
+ # Data cache for immutable data
31
+ @st.cache_data
32
  def get_name_components():
33
  """Cache name generation components"""
34
  return {
 
38
  'is', 'ax', 'on', 'ir', 'ex', 'az', 'er', 'eth', 'ys', 'ix']
39
  }
40
 
41
+ @st.cache_data(ttl=5) # Cache for 5 seconds to match refresh rate
42
+ def load_all_players(timestamp):
43
+ """Cache and load all player states with TTL"""
44
+ players = {}
45
+ if os.path.exists('players'):
46
+ for filename in os.listdir('players'):
47
+ if filename.endswith('.json'):
48
+ with open(f"players/{filename}", 'r') as f:
49
+ try:
50
+ player_data = json.load(f)
51
+ if time.time() - player_data['last_update'] < 60:
52
+ players[player_data['name']] = player_data
53
+ except json.JSONDecodeError:
54
+ continue
55
+ return {'players': players, 'last_update': time.time()}
56
+
57
+ def clear_caches():
58
+ """Clear all caches properly"""
59
+ st.cache_resource.clear()
60
+ st.cache_data.clear()
61
+
62
  def generate_fantasy_name():
63
  """Generate random fantasy name using cached components"""
64
  name_parts = get_name_components()
 
114
  st.session_state.nearby_players = []
115
  load_game_state()
116
 
 
 
 
 
 
 
 
 
 
 
 
 
117
  def save_player_state():
118
  """Save player state to JSON file"""
119
  if st.session_state.player_name:
 
151
 
152
  def update_nearby_players():
153
  """Update list of nearby players and calculate score gains"""
154
+ current_time = time.time()
155
+ all_players = load_all_players(current_time)['players']
156
  nearby = []
157
  score_gain = 0
158
 
 
175
 
176
  def create_game_board():
177
  """Create and display the game board"""
178
+ current_time = time.time()
179
+ all_players = load_all_players(current_time)['players']
180
  images = get_game_images()
181
 
182
  # Create columns for each row
 
259
  # Clear game state button
260
  if st.sidebar.button("Clear Game State"):
261
  st.query_params.clear()
262
+ clear_caches()
263
  st.rerun()
264
 
265
  # Main game area