Spaces:
Sleeping

byte-vortex commited on
Commit
e448469
·
verified ·
1 Parent(s): 2974ecc

Deploy Myco from CI

Browse files
Files changed (4) hide show
  1. game/engine.py +74 -0
  2. ui/gradio_app.py +46 -0
  3. ui/renderers.py +53 -0
  4. ui/styles.py +112 -0
game/engine.py CHANGED
@@ -1,5 +1,12 @@
1
  """Core Myco gameplay actions independent from Gradio."""
2
 
 
 
 
 
 
 
 
3
  from __future__ import annotations
4
 
5
  import random
@@ -23,6 +30,17 @@ def choose_mushroom(catalog: Sequence[Mushroom] | None = None) -> Mushroom:
23
  return random.choice(mushrooms)
24
 
25
 
 
 
 
 
 
 
 
 
 
 
 
26
  def discover_mushroom(
27
  collection: CollectionState | None, catalog: Sequence[Mushroom] | None = None
28
  ) -> tuple[Mushroom, MushroomState, ChatHistory]:
@@ -43,6 +61,7 @@ def discover_mushroom(
43
  return mushroom, current, history
44
 
45
 
 
46
  def myco_reply(
47
  message: str | None,
48
  history: ChatHistory | None,
@@ -70,6 +89,38 @@ def myco_reply(
70
  return "", next_history
71
 
72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  def collect_current(
74
  current: MushroomState | None,
75
  collection: CollectionState | None,
@@ -113,6 +164,17 @@ def study_current(
113
  f"I studied the {current['name']} closely. New MycoDex note: "
114
  f"magic is now marked as **{studied['magic']}**. Still no tasting without certainty!"
115
  )
 
 
 
 
 
 
 
 
 
 
 
116
  return studied, [*current_history, {"role": "assistant", "content": message}]
117
 
118
 
@@ -135,6 +197,10 @@ def eat_current(current: MushroomState | None, history: ChatHistory | None) -> C
135
  f"Its edible field is still **{mushroom.edible}**, "
136
  "so the winning move is to study or collect it instead of eating it."
137
  )
 
 
 
 
138
  next_history.append({"role": "assistant", "content": message})
139
  return next_history
140
 
@@ -144,16 +210,24 @@ def _assistant_message(content: str) -> dict:
144
  return {"role": "assistant", "content": content}
145
 
146
 
 
 
 
 
 
 
147
  def _safe_collection(collection: CollectionState | None) -> CollectionState:
148
  """Return a copy of the collection, tolerating an uninitialized Gradio state."""
149
  return list(collection or [])
150
 
151
 
 
152
  def _safe_history(history: ChatHistory | None) -> ChatHistory:
153
  """Return a copy of chat history, tolerating an uninitialized Gradio state."""
154
  return list(history or welcome_history())
155
 
156
 
 
157
  def _study_hint(rarity: str) -> str:
158
  """Return a premium-feeling but cautious study result by rarity."""
159
  if rarity == "Legendary":
 
1
  """Core Myco gameplay actions independent from Gradio."""
2
 
3
+ import random
4
+
5
+ from game.catalog import load_mushrooms
6
+ from game.state import collection_contains, mushroom_from_state, welcome_history
7
+
8
+
9
+ def choose_mushroom(catalog=None):
10
  from __future__ import annotations
11
 
12
  import random
 
30
  return random.choice(mushrooms)
31
 
32
 
33
+ def discover_mushroom(collection=None, catalog=None):
34
+ """Discover a mushroom and return the updated discovery state."""
35
+ current_collection = _safe_collection(collection)
36
+ mushroom = choose_mushroom(catalog)
37
+ current = mushroom.to_dict()
38
+ history = welcome_history()
39
+ history.append(
40
+ _assistant_message(
41
+ f"We found a {mushroom.name}! Ask me about it, then decide whether to "
42
+ f"eat, collect, or study it. Your MycoDex has {len(current_collection)} entries."
43
+ )
44
  def discover_mushroom(
45
  collection: CollectionState | None, catalog: Sequence[Mushroom] | None = None
46
  ) -> tuple[Mushroom, MushroomState, ChatHistory]:
 
61
  return mushroom, current, history
62
 
63
 
64
+ def myco_reply(message=None, history=None, current=None):
65
  def myco_reply(
66
  message: str | None,
67
  history: ChatHistory | None,
 
89
  return "", next_history
90
 
91
 
92
+ def collect_current(current=None, collection=None, history=None):
93
+ """Add the current mushroom to the MycoDex if it is not already collected."""
94
+ current_collection = _safe_collection(collection)
95
+ current_history = _safe_history(history)
96
+
97
+ if current is None:
98
+ return current_collection, _append_assistant(
99
+ current_history,
100
+ "Let's discover a mushroom before updating the MycoDex.",
101
+ )
102
+
103
+ if collection_contains(current_collection, current["name"]):
104
+ return current_collection, _append_assistant(
105
+ current_history,
106
+ f"{current['name']} is already safe in your MycoDex.",
107
+ )
108
+
109
+ updated_collection = [*current_collection, current]
110
+ return updated_collection, _append_assistant(
111
+ current_history,
112
+ f"Added {current['name']} to the MycoDex. Nice find!",
113
+ )
114
+
115
+
116
+ def study_current(current=None, history=None):
117
+ """Study the current mushroom to reveal one cautious field observation."""
118
+ current_history = _safe_history(history)
119
+ if current is None:
120
+ return None, _append_assistant(
121
+ current_history,
122
+ "Let's find a mushroom before we study anything.",
123
+ )
124
  def collect_current(
125
  current: MushroomState | None,
126
  collection: CollectionState | None,
 
164
  f"I studied the {current['name']} closely. New MycoDex note: "
165
  f"magic is now marked as **{studied['magic']}**. Still no tasting without certainty!"
166
  )
167
+ return studied, _append_assistant(current_history, message)
168
+
169
+
170
+ def eat_current(current=None, history=None):
171
+ """Handle the Eat choice safely with Myco's companion personality."""
172
+ current_history = _safe_history(history)
173
+ if current is None:
174
+ return _append_assistant(
175
+ current_history,
176
+ "Let's discover a mushroom before making snack decisions.",
177
+ )
178
  return studied, [*current_history, {"role": "assistant", "content": message}]
179
 
180
 
 
197
  f"Its edible field is still **{mushroom.edible}**, "
198
  "so the winning move is to study or collect it instead of eating it."
199
  )
200
+ return _append_assistant(current_history, message)
201
+
202
+
203
+ def _assistant_message(content):
204
  next_history.append({"role": "assistant", "content": message})
205
  return next_history
206
 
 
210
  return {"role": "assistant", "content": content}
211
 
212
 
213
+ def _append_assistant(history, content):
214
+ """Return chat history with one assistant message appended."""
215
+ return [*_safe_history(history), _assistant_message(content)]
216
+
217
+
218
+ def _safe_collection(collection):
219
  def _safe_collection(collection: CollectionState | None) -> CollectionState:
220
  """Return a copy of the collection, tolerating an uninitialized Gradio state."""
221
  return list(collection or [])
222
 
223
 
224
+ def _safe_history(history):
225
  def _safe_history(history: ChatHistory | None) -> ChatHistory:
226
  """Return a copy of chat history, tolerating an uninitialized Gradio state."""
227
  return list(history or welcome_history())
228
 
229
 
230
+ def _study_hint(rarity):
231
  def _study_hint(rarity: str) -> str:
232
  """Return a premium-feeling but cautious study result by rarity."""
233
  if rarity == "Legendary":
ui/gradio_app.py CHANGED
@@ -7,6 +7,22 @@ import gradio as gr
7
  from game.engine import collect_current, discover_mushroom, eat_current, myco_reply, study_current
8
  from game.state import CollectionState, MushroomState, welcome_history
9
  from models.mushroom import Mushroom
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  from ui.renderers import dex_markdown, hero_markdown, mushroom_card, progress_markdown
11
 
12
 
@@ -29,6 +45,16 @@ def add_to_dex(
29
  current: MushroomState | None,
30
  collection: CollectionState,
31
  history: list[dict[str, str]],
 
 
 
 
 
 
 
 
 
 
32
  ) -> tuple[CollectionState, str, list[dict[str, str]], str]:
33
  """Gradio callback for collecting the current mushroom."""
34
  updated_collection, updated_history = collect_current(current, collection, history)
@@ -38,6 +64,16 @@ def add_to_dex(
38
  def study_mushroom(
39
  current: MushroomState | None,
40
  history: list[dict[str, str]],
 
 
 
 
 
 
 
 
 
 
41
  ) -> tuple[MushroomState | None, str, list[dict[str, str]]]:
42
  """Gradio callback for studying the current mushroom."""
43
  updated_current, updated_history = study_current(current, history)
@@ -67,6 +103,8 @@ def build_app() -> gr.Blocks:
67
 
68
  gr.HTML(hero_markdown())
69
 
 
 
70
  with gr.Row():
71
  with gr.Column(scale=2):
72
  discovery = gr.Markdown(mushroom_card(None), elem_id="discovery-card")
@@ -90,6 +128,7 @@ def build_app() -> gr.Blocks:
90
  explore_button.click(
91
  explore,
92
  inputs=[collection],
 
93
  outputs=[discovery, current_mushroom, chat, dex, progress],
94
  )
95
  ask_button.click(ask_myco, inputs=[prompt, chat, current_mushroom], outputs=[prompt, chat])
@@ -97,6 +136,13 @@ def build_app() -> gr.Blocks:
97
  collect_button.click(
98
  add_to_dex,
99
  inputs=[current_mushroom, collection, chat],
 
 
 
 
 
 
 
100
  outputs=[collection, dex, chat, progress],
101
  )
102
  study_button.click(study_mushroom, inputs=[current_mushroom, chat], outputs=[current_mushroom, discovery, chat])
 
7
  from game.engine import collect_current, discover_mushroom, eat_current, myco_reply, study_current
8
  from game.state import CollectionState, MushroomState, welcome_history
9
  from models.mushroom import Mushroom
10
+ from ui.renderers import dex_markdown, forest_scene, hero_markdown, mushroom_card, progress_markdown
11
+
12
+
13
+ def explore(
14
+ collection: CollectionState,
15
+ ) -> tuple[str, str, MushroomState, list[dict[str, str]], str, str]:
16
+ """Gradio callback for the Start Exploring button."""
17
+ mushroom, current, history = discover_mushroom(collection)
18
+ return (
19
+ forest_scene(current, collection),
20
+ mushroom_card(mushroom),
21
+ current,
22
+ history,
23
+ dex_markdown(collection),
24
+ progress_markdown(collection),
25
+ )
26
  from ui.renderers import dex_markdown, hero_markdown, mushroom_card, progress_markdown
27
 
28
 
 
45
  current: MushroomState | None,
46
  collection: CollectionState,
47
  history: list[dict[str, str]],
48
+ ) -> tuple[CollectionState, str, str, list[dict[str, str]], str]:
49
+ """Gradio callback for collecting the current mushroom."""
50
+ updated_collection, updated_history = collect_current(current, collection, history)
51
+ return (
52
+ updated_collection,
53
+ forest_scene(current, updated_collection),
54
+ dex_markdown(updated_collection),
55
+ updated_history,
56
+ progress_markdown(updated_collection),
57
+ )
58
  ) -> tuple[CollectionState, str, list[dict[str, str]], str]:
59
  """Gradio callback for collecting the current mushroom."""
60
  updated_collection, updated_history = collect_current(current, collection, history)
 
64
  def study_mushroom(
65
  current: MushroomState | None,
66
  history: list[dict[str, str]],
67
+ collection: CollectionState,
68
+ ) -> tuple[MushroomState | None, str, str, list[dict[str, str]]]:
69
+ """Gradio callback for studying the current mushroom."""
70
+ updated_current, updated_history = study_current(current, history)
71
+ return (
72
+ updated_current,
73
+ forest_scene(updated_current, collection),
74
+ mushroom_card_from_state(updated_current),
75
+ updated_history,
76
+ )
77
  ) -> tuple[MushroomState | None, str, list[dict[str, str]]]:
78
  """Gradio callback for studying the current mushroom."""
79
  updated_current, updated_history = study_current(current, history)
 
103
 
104
  gr.HTML(hero_markdown())
105
 
106
+ scene = gr.HTML(forest_scene(None, []))
107
+
108
  with gr.Row():
109
  with gr.Column(scale=2):
110
  discovery = gr.Markdown(mushroom_card(None), elem_id="discovery-card")
 
128
  explore_button.click(
129
  explore,
130
  inputs=[collection],
131
+ outputs=[scene, discovery, current_mushroom, chat, dex, progress],
132
  outputs=[discovery, current_mushroom, chat, dex, progress],
133
  )
134
  ask_button.click(ask_myco, inputs=[prompt, chat, current_mushroom], outputs=[prompt, chat])
 
136
  collect_button.click(
137
  add_to_dex,
138
  inputs=[current_mushroom, collection, chat],
139
+ outputs=[collection, scene, dex, chat, progress],
140
+ )
141
+ study_button.click(
142
+ study_mushroom,
143
+ inputs=[current_mushroom, chat, collection],
144
+ outputs=[current_mushroom, scene, discovery, chat],
145
+ )
146
  outputs=[collection, dex, chat, progress],
147
  )
148
  study_button.click(study_mushroom, inputs=[current_mushroom, chat], outputs=[current_mushroom, discovery, chat])
ui/renderers.py CHANGED
@@ -2,6 +2,10 @@
2
 
3
  from __future__ import annotations
4
 
 
 
 
 
5
  from game.progression import AREA_UNLOCKS, current_area, next_area, unlocked_areas
6
  from game.state import CollectionState, EMPTY_DEX
7
  from models.mushroom import Mushroom
@@ -21,6 +25,55 @@ def hero_markdown() -> str:
21
  """
22
 
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  def mushroom_card(mushroom: Mushroom | None) -> str:
25
  """Render the current mushroom discovery as markdown."""
26
  if mushroom is None:
 
2
 
3
  from __future__ import annotations
4
 
5
+ from html import escape
6
+
7
+ from game.progression import AREA_UNLOCKS, current_area, next_area, unlocked_areas
8
+ from game.state import CollectionState, EMPTY_DEX, MushroomState
9
  from game.progression import AREA_UNLOCKS, current_area, next_area, unlocked_areas
10
  from game.state import CollectionState, EMPTY_DEX
11
  from models.mushroom import Mushroom
 
25
  """
26
 
27
 
28
+ def forest_scene(current: MushroomState | None, collection: CollectionState | None) -> str:
29
+ """Render a game-like forest scene for the current discovery."""
30
+ count = len(_safe_collection(collection))
31
+ area = current_area(count)
32
+
33
+ if current is None:
34
+ mushroom_name = "Hidden mushroom"
35
+ rarity = "Common"
36
+ sprite = "✨"
37
+ status = "Click Start Exploring to reveal what is hiding under the moss."
38
+ else:
39
+ mushroom_name = current.get("name", "Mystery mushroom")
40
+ rarity = current.get("rarity", "Common")
41
+ sprite = _mushroom_sprite(rarity)
42
+ status = f"A {rarity.lower()} {mushroom_name} has appeared!"
43
+
44
+ return f'''
45
+ <div class="forest-scene rarity-{escape(rarity.lower())}">
46
+ <div class="scene-hud">
47
+ <span>🧭 {escape(area.name)}</span>
48
+ <span>📖 {count} MycoDex finds</span>
49
+ </div>
50
+ <div class="scene-stage" aria-label="Magical forest scene">
51
+ <span class="tree tree-left">🌲</span>
52
+ <span class="tree tree-right">🌳</span>
53
+ <span class="grass grass-one">🌿</span>
54
+ <span class="grass grass-two">🍃</span>
55
+ <span class="myco-player" title="Myco">🍄</span>
56
+ <span class="mushroom-sprite" title="{escape(mushroom_name)}">{sprite}</span>
57
+ <span class="sparkle sparkle-one">✨</span>
58
+ <span class="sparkle sparkle-two">✦</span>
59
+ </div>
60
+ <div class="scene-caption">
61
+ <strong>{escape(status)}</strong>
62
+ <span>Myco is waiting for your next choice.</span>
63
+ </div>
64
+ </div>
65
+ '''
66
+
67
+
68
+ def _mushroom_sprite(rarity: str) -> str:
69
+ """Return a simple scene sprite based on rarity."""
70
+ if rarity == "Legendary":
71
+ return "🍄‍🟫"
72
+ if rarity == "Rare":
73
+ return "🌌"
74
+ return "🍄"
75
+
76
+
77
  def mushroom_card(mushroom: Mushroom | None) -> str:
78
  """Render the current mushroom discovery as markdown."""
79
  if mushroom is None:
ui/styles.py CHANGED
@@ -43,6 +43,118 @@ APP_CSS = """
43
  margin-bottom: 0;
44
  color: #31533f;
45
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  #discovery-card,
47
  #mycodex,
48
  #progress-panel {
 
43
  margin-bottom: 0;
44
  color: #31533f;
45
  }
46
+
47
+ .forest-scene {
48
+ position: relative;
49
+ overflow: hidden;
50
+ min-height: 18rem;
51
+ padding: 1rem;
52
+ border-radius: 28px;
53
+ color: #f9ffe9;
54
+ background:
55
+ radial-gradient(circle at 50% 72%, rgba(255, 239, 173, 0.26), transparent 8rem),
56
+ radial-gradient(circle at 18% 24%, rgba(134, 225, 153, 0.22), transparent 10rem),
57
+ linear-gradient(180deg, #123626 0%, #245c3c 54%, #523b20 100%);
58
+ border: 1px solid rgba(225, 255, 209, 0.25);
59
+ box-shadow: 0 18px 48px rgba(8, 30, 20, 0.26);
60
+ }
61
+ .scene-hud,
62
+ .scene-caption {
63
+ position: relative;
64
+ z-index: 2;
65
+ display: flex;
66
+ justify-content: space-between;
67
+ gap: 0.75rem;
68
+ padding: 0.7rem 0.85rem;
69
+ border-radius: 999px;
70
+ background: rgba(13, 38, 28, 0.68);
71
+ backdrop-filter: blur(6px);
72
+ }
73
+ .scene-stage {
74
+ position: relative;
75
+ height: 11rem;
76
+ margin: 0.75rem 0;
77
+ border-radius: 22px;
78
+ background:
79
+ linear-gradient(transparent 58%, rgba(106, 79, 33, 0.62) 59%),
80
+ radial-gradient(ellipse at bottom, rgba(190, 255, 180, 0.24), transparent 58%);
81
+ }
82
+ .scene-stage span {
83
+ position: absolute;
84
+ line-height: 1;
85
+ filter: drop-shadow(0 8px 12px rgba(0, 0, 0, 0.25));
86
+ }
87
+ .tree-left {
88
+ left: 7%;
89
+ bottom: 18%;
90
+ font-size: 5rem;
91
+ }
92
+ .tree-right {
93
+ right: 8%;
94
+ bottom: 20%;
95
+ font-size: 4.75rem;
96
+ }
97
+ .grass-one {
98
+ left: 27%;
99
+ bottom: 16%;
100
+ font-size: 2.5rem;
101
+ }
102
+ .grass-two {
103
+ right: 30%;
104
+ bottom: 18%;
105
+ font-size: 2.25rem;
106
+ }
107
+ .myco-player {
108
+ left: 42%;
109
+ bottom: 15%;
110
+ font-size: 4rem;
111
+ animation: myco-bob 2.4s ease-in-out infinite;
112
+ }
113
+ .mushroom-sprite {
114
+ right: 37%;
115
+ bottom: 24%;
116
+ font-size: 3.4rem;
117
+ animation: discovery-pop 1.6s ease-out both;
118
+ }
119
+ .sparkle-one {
120
+ left: 35%;
121
+ top: 20%;
122
+ font-size: 1.8rem;
123
+ animation: twinkle 1.8s ease-in-out infinite;
124
+ }
125
+ .sparkle-two {
126
+ right: 34%;
127
+ top: 26%;
128
+ font-size: 1.6rem;
129
+ animation: twinkle 2.2s ease-in-out infinite reverse;
130
+ }
131
+ .rarity-rare .mushroom-sprite,
132
+ .rarity-legendary .mushroom-sprite {
133
+ text-shadow: 0 0 24px rgba(193, 247, 255, 0.9);
134
+ }
135
+ .scene-caption {
136
+ align-items: center;
137
+ border-radius: 18px;
138
+ }
139
+ .scene-caption span {
140
+ color: #d9f4d4;
141
+ }
142
+ @keyframes myco-bob {
143
+ 0%, 100% { transform: translateY(0); }
144
+ 50% { transform: translateY(-0.45rem); }
145
+ }
146
+ @keyframes discovery-pop {
147
+ 0% { opacity: 0; transform: scale(0.25) translateY(1rem); }
148
+ 70% { opacity: 1; transform: scale(1.12) translateY(-0.25rem); }
149
+ 100% { opacity: 1; transform: scale(1) translateY(0); }
150
+ }
151
+ @keyframes twinkle {
152
+ 0%, 100% { opacity: 0.35; transform: scale(0.85); }
153
+ 50% { opacity: 1; transform: scale(1.15); }
154
+ }
155
+ #discovery-card,
156
+ #mycodex,
157
+ #progress-panel {
158
  #discovery-card,
159
  #mycodex,
160
  #progress-panel {