Spaces:
Sleeping

byte-vortex commited on
Commit
439da30
Β·
verified Β·
1 Parent(s): c82213f

Deploy Myco from CI

Browse files
Files changed (1) hide show
  1. ui/9-renderers.py +60 -60
ui/9-renderers.py CHANGED
@@ -11,7 +11,6 @@ from models.mushroom import Mushroom
11
  import json
12
  import ast
13
 
14
-
15
  POISONOUS_MUSHROOM_NAMES = {
16
  "Ghost Gill", "Pepper Pixie", "Ruby Knuckle", "Clockwork Chanterelle",
17
  }
@@ -25,27 +24,23 @@ def hero_markdown() -> str:
25
  <div class="myco-hero">
26
  <div class="myco-mascot">πŸ„</div>
27
  <div>
28
- <p class="eyebrow">Tiny AI mushroom companion</p>
29
  <h1>Myco</h1>
30
  <p>Follow an AI mushroom companion through a glowing forest. Every discovery starts with Myco's intuition.</p>
31
  </div>
32
  </div>
33
  """
34
 
35
- def bubble(content):
36
- return f"""
37
- <div style="
38
- font-size: 12px;
39
- padding: 12px;
40
- border-radius: 28px;
41
- background: linear-gradient(135deg, #1e1e1e 0%, #0f2027 50%, #203a43 100%);
42
- color: #b6ff9a;
43
- font-family: monospace;
44
- ">
45
- {content} by Myco AI πŸ„
46
- </div>
47
- """
48
-
49
 
50
  def home_forest_markdown() -> str:
51
  return """
@@ -130,7 +125,7 @@ def forest_scene(
130
  current: "MushroomState | None",
131
  collection: "CollectionState | None",
132
  position: "tuple[int,int] | None" = None,
133
- active_mushrooms: "list | None" = None,
134
  ) -> str:
135
  import time
136
  count = len(_safe_collection(collection))
@@ -138,11 +133,7 @@ def forest_scene(
138
  pos_x, pos_y = _safe_position(position)
139
  pos_cls = f"pos-{pos_x}-{pos_y}"
140
 
141
- # Catch active_mushrooms if packed inside the current dictionary
142
- if current is not None and not active_mushrooms:
143
- active_mushrooms = current.get("active_mushrooms", [])
144
-
145
- # Safe defaults
146
  mushroom_name = "Hidden mushroom"
147
  rarity = "common"
148
  sprite = "✨"
@@ -157,24 +148,15 @@ def forest_scene(
157
  state_cls = "state-waiting"
158
 
159
  if current is not None:
160
- mushroom_name = current.get("name") or "Mystery cluster"
161
-
162
- # SAFEGUARD: Prevent NoneType.lower() crash
163
- raw_rarity = current.get("rarity")
164
- rarity = (raw_rarity if raw_rarity else "Common").lower()
165
-
166
  game_over = current.get("game_over") == "Yes"
167
-
168
- # SAFEGUARD: Handle None values in string formatting safely
169
- emoji = current.get("event_emoji") or "✨"
170
- title = current.get("event_title") or "Strange Omen"
171
- event_label = f"{emoji} {title}"
172
-
173
- encounter = current.get("encounter") or "Go pick them up!"
174
- mystery_label = current.get("mystery_title") or "The Wrong Memory"
175
- score_total = current.get("score_total") or "0"
176
- health = current.get("health") or "3"
177
- reward_text = current.get("reward_text") or "Dodge the poison, collect the rest!"
178
  state_cls = _scene_state_class(current)
179
 
180
  if game_over:
@@ -187,6 +169,7 @@ def forest_scene(
187
  status = f"A {rarity} {mushroom_name} breaks through the moss!"
188
  omen = "πŸ„ Study it before picking!"
189
 
 
190
  active_sprites = _build_active_sprites(active_mushrooms or [])
191
  anim_key = str(int(time.time() * 1000))[-6:]
192
 
@@ -201,24 +184,23 @@ def forest_scene(
201
  <span>{escape(event_label)}</span>
202
  <span>🧩 {escape(mystery_label)}</span>
203
  </div>
204
-
205
  <div class="scene-stage" aria-label="Magical forest scene">
206
  <span class="tree tree-left">🌲</span>
207
  <span class="tree tree-right">🌲</span>
208
  <span class="grass grass-one">🌿</span>
209
  <span class="grass grass-two">πŸƒ</span>
210
- <span class="myco-player" title="Myco">πŸ„</span>
211
  <span class="mushroom-sprite" title="{escape(mushroom_name)}">{escape(sprite)}</span>
212
  {active_sprites}
213
  <span class="sparkle sparkle-one">✨</span>
214
  <span class="sparkle sparkle-two">✦</span>
215
- <span class="wood-log">πŸͺ΅</span>
216
  </div>
217
-
218
  <div class="forest-map" aria-label="Forest grid">
219
  {_movement_tiles(pos_x, pos_y, active_mushrooms)}
220
  </div>
221
-
222
  <div class="scene-caption">
223
  <strong>{escape(str(status))}</strong>
224
  <span>{escape(str(omen))}</span>
@@ -236,28 +218,46 @@ def forest_scene(
236
  # ---------------------------------------------------------------------------
237
 
238
  def _build_active_sprites(active_mushrooms: list) -> str:
 
 
 
 
239
  sprites = []
240
  for m in active_mushrooms:
241
  if not isinstance(m, dict):
242
  continue
243
  if m.get("picked") == "Yes":
244
  continue
245
-
246
- # SAFEGUARD: Prevent int(None) crash
247
- raw_x = m.get("mush_x")
248
- raw_y = m.get("mush_y")
249
- mx = int(raw_x) if raw_x is not None else 1
250
- my = int(raw_y) if raw_y is not None else 1
251
-
252
- mx = max(0, min(2, mx))
253
- my = max(0, min(2, my))
254
  is_poison = m.get("poison") == "Yes"
255
- icon = "☠️" if is_poison else ("πŸ„β€πŸŸ«" if m.get("rarity") == "Legendary" else "πŸ„")
256
- css_class = f"roam-sprite roam-{mx}-{my}" + (" roam-poison" if is_poison else "")
257
- name = escape(str(m.get("name", "?")))
258
- sprites.append(f'<span class="{css_class}" title="{name}">{icon}</span>')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
259
  return "\n ".join(sprites)
260
-
261
 
262
  # ---------------------------------------------------------------------------
263
  # Helpers
@@ -284,7 +284,7 @@ def _movement_tiles(active_x, active_y, active_mushrooms=None):
284
  mx, my = int(m.get("mush_x", -1)), int(m.get("mush_y", -1))
285
  if 0 <= mx <= 2 and 0 <= my <= 2:
286
  mush_coords[(mx, my)] = m
287
-
288
  tiles = []
289
  for y in range(3):
290
  for x in range(3):
@@ -302,7 +302,7 @@ def _movement_tiles(active_x, active_y, active_mushrooms=None):
302
  icon = "Β·"
303
  tiles.append(f'<span class="{css}">{icon}</span>')
304
  return "".join(tiles)
305
-
306
 
307
  def _scene_state_class(current):
308
  if not current:
 
11
  import json
12
  import ast
13
 
 
14
  POISONOUS_MUSHROOM_NAMES = {
15
  "Ghost Gill", "Pepper Pixie", "Ruby Knuckle", "Clockwork Chanterelle",
16
  }
 
24
  <div class="myco-hero">
25
  <div class="myco-mascot">πŸ„</div>
26
  <div>
27
+ <p class="eyebrow">Tiny AI forest game</p>
28
  <h1>Myco</h1>
29
  <p>Follow an AI mushroom companion through a glowing forest. Every discovery starts with Myco's intuition.</p>
30
  </div>
31
  </div>
32
  """
33
 
34
+ def myco_talking_markdown(text: str) -> str:
35
+ safe_text = escape(str(text))
36
+ return f'''
37
+ <div class="myco-speech-container">
38
+ <div class="myco-bubble">
39
+ <span class="bubble-icon">πŸ’¬</span>
40
+ <p class="myco-thought">{safe_text}</p>
41
+ </div>
42
+ </div>
43
+ '''
 
 
 
 
44
 
45
  def home_forest_markdown() -> str:
46
  return """
 
125
  current: "MushroomState | None",
126
  collection: "CollectionState | None",
127
  position: "tuple[int,int] | None" = None,
128
+ active_mushrooms: "list | None" = None, # ← NEW: list of mushroom dicts
129
  ) -> str:
130
  import time
131
  count = len(_safe_collection(collection))
 
133
  pos_x, pos_y = _safe_position(position)
134
  pos_cls = f"pos-{pos_x}-{pos_y}"
135
 
136
+ # Safe defaults β€” all variables set before any conditional
 
 
 
 
137
  mushroom_name = "Hidden mushroom"
138
  rarity = "common"
139
  sprite = "✨"
 
148
  state_cls = "state-waiting"
149
 
150
  if current is not None:
151
+ mushroom_name = current.get("name", "Mystery cluster")
152
+ rarity = current.get("rarity", "Common").lower()
 
 
 
 
153
  game_over = current.get("game_over") == "Yes"
154
+ event_label = f"{current.get('event_emoji','✨')} {current.get('event_title','Strange Omen')}"
155
+ encounter = current.get("encounter", "Go pick them up!")
156
+ mystery_label = current.get("mystery_title", "The Wrong Memory")
157
+ score_total = current.get("score_total", "0")
158
+ health = current.get("health", "3")
159
+ reward_text = current.get("reward_text", "Dodge the poison, collect the rest!")
 
 
 
 
 
160
  state_cls = _scene_state_class(current)
161
 
162
  if game_over:
 
169
  status = f"A {rarity} {mushroom_name} breaks through the moss!"
170
  omen = "πŸ„ Study it before picking!"
171
 
172
+ # Build roaming mushroom sprites from active_mushrooms
173
  active_sprites = _build_active_sprites(active_mushrooms or [])
174
  anim_key = str(int(time.time() * 1000))[-6:]
175
 
 
184
  <span>{escape(event_label)}</span>
185
  <span>🧩 {escape(mystery_label)}</span>
186
  </div>
187
+
188
  <div class="scene-stage" aria-label="Magical forest scene">
189
  <span class="tree tree-left">🌲</span>
190
  <span class="tree tree-right">🌲</span>
191
  <span class="grass grass-one">🌿</span>
192
  <span class="grass grass-two">πŸƒ</span>
193
+ <span class="myco-player" title="Myco" style="left:{["14%","41%","67%"][pos_x]};bottom:{["62%","36%","13%"][pos_y]};">πŸ„</span>
194
  <span class="mushroom-sprite" title="{escape(mushroom_name)}">{escape(sprite)}</span>
195
  {active_sprites}
196
  <span class="sparkle sparkle-one">✨</span>
197
  <span class="sparkle sparkle-two">✦</span>
 
198
  </div>
199
+
200
  <div class="forest-map" aria-label="Forest grid">
201
  {_movement_tiles(pos_x, pos_y, active_mushrooms)}
202
  </div>
203
+
204
  <div class="scene-caption">
205
  <strong>{escape(str(status))}</strong>
206
  <span>{escape(str(omen))}</span>
 
218
  # ---------------------------------------------------------------------------
219
 
220
  def _build_active_sprites(active_mushrooms: list) -> str:
221
+ """Render roaming mushrooms with inline styles so position works inside Gradio components."""
222
+ # Grid positions: x=0->10%, x=1->37%, x=2->63% / y=0->58%, y=1->32%, y=2->9%
223
+ LEFT = ["10%", "37%", "63%"]
224
+ BOTTOM= ["58%", "32%", "9%"]
225
  sprites = []
226
  for m in active_mushrooms:
227
  if not isinstance(m, dict):
228
  continue
229
  if m.get("picked") == "Yes":
230
  continue
231
+ mx = max(0, min(2, int(m.get("mush_x", 1))))
232
+ my = max(0, min(2, int(m.get("mush_y", 1))))
 
 
 
 
 
 
 
233
  is_poison = m.get("poison") == "Yes"
234
+ rarity = m.get("rarity", "Common")
235
+ if is_poison:
236
+ icon = "☠️"
237
+ color = "rgba(255,62,91,1)"
238
+ extra = "animation:roam-bob 2s ease-in-out infinite,poison-pulse 1.2s ease-in-out infinite;"
239
+ elif rarity == "Legendary":
240
+ icon = "πŸ„β€πŸŸ«"
241
+ color = "rgba(255,215,0,1)"
242
+ extra = "animation:roam-bob 2s ease-in-out infinite,legendary-pulse 1s ease-in-out infinite;"
243
+ elif rarity == "Rare":
244
+ icon = "πŸ„"
245
+ color = "rgba(138,220,255,1)"
246
+ extra = "animation:roam-bob 2s ease-in-out infinite;"
247
+ else:
248
+ icon = "πŸ„"
249
+ color = "rgba(255,255,255,0.3)"
250
+ extra = "animation:roam-bob 2s ease-in-out infinite;"
251
+ name = escape(str(m.get("name", "?")))
252
+ style = (
253
+ f"position:absolute;font-size:2.4rem;z-index:2;line-height:1;"
254
+ f"left:{LEFT[mx]};bottom:{BOTTOM[my]};"
255
+ f"filter:drop-shadow(0 0 8px {color});"
256
+ f"transition:left .3s,bottom .3s;{extra}"
257
+ )
258
+ sprites.append(f'<span style="{style}" title="{name}">{icon}</span>')
259
  return "\n ".join(sprites)
260
+
261
 
262
  # ---------------------------------------------------------------------------
263
  # Helpers
 
284
  mx, my = int(m.get("mush_x", -1)), int(m.get("mush_y", -1))
285
  if 0 <= mx <= 2 and 0 <= my <= 2:
286
  mush_coords[(mx, my)] = m
287
+
288
  tiles = []
289
  for y in range(3):
290
  for x in range(3):
 
302
  icon = "Β·"
303
  tiles.append(f'<span class="{css}">{icon}</span>')
304
  return "".join(tiles)
305
+
306
 
307
  def _scene_state_class(current):
308
  if not current: