Spaces:
Sleeping

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

Deploy Myco from CI

Browse files
Files changed (1) hide show
  1. game/engine.py +4 -155
game/engine.py CHANGED
@@ -1,12 +1,5 @@
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
@@ -24,36 +17,18 @@ from game.state import (
24
  from models.mushroom import Mushroom
25
 
26
 
27
- def choose_mushroom(catalog: Sequence[Mushroom] | None = None):
28
  def choose_mushroom(catalog: Sequence[Mushroom] | None = None) -> Mushroom:
29
  """Choose a mushroom discovery from the catalog."""
30
  mushrooms = tuple(catalog or load_mushrooms())
31
  return random.choice(mushrooms)
32
 
33
 
34
- def discover_mushroom(collection=None, catalog=None):
35
- """Discover a mushroom and return the updated discovery state."""
36
- current_collection = _safe_collection(collection)
37
- mushroom = choose_mushroom(catalog)
38
- current = mushroom.to_dict()
39
- history = welcome_history()
40
- history.append(
41
- _assistant_message(
42
- f"We found a {mushroom.name}! Ask me about it, then decide whether to "
43
- f"eat, collect, or study it. Your MycoDex has {len(current_collection)} entries."
44
- )
45
- def discover_mushroom(collection: CollectionState | None, catalog: Sequence[Mushroom] | None = None):
46
  def discover_mushroom(
47
  collection: CollectionState | None, catalog: Sequence[Mushroom] | None = None
48
  ) -> tuple[Mushroom, MushroomState, ChatHistory]:
49
  """Discover a mushroom and return the updated discovery state."""
50
- current_collection = _safe_collection(collection)
51
  mushroom = choose_mushroom(catalog)
52
- discovery_count = len(current_collection)
53
- def discover_mushroom(collection: CollectionState, catalog: Sequence[Mushroom] | None = None) -> tuple[Mushroom, MushroomState, ChatHistory]:
54
- """Discover a mushroom and return the updated discovery state."""
55
- mushroom = choose_mushroom(catalog)
56
- discovery_count = len(collection)
57
  current = mushroom.to_dict()
58
  history = welcome_history()
59
  history.append(
@@ -68,9 +43,6 @@ def discover_mushroom(collection: CollectionState, catalog: Sequence[Mushroom] |
68
  return mushroom, current, history
69
 
70
 
71
- def myco_reply(message=None, history=None, current=None):
72
- def myco_reply(message: str | None, history, current: MushroomState | None):
73
- def myco_reply(message: str | None, history: ChatHistory | None, current: MushroomState | None):
74
  def myco_reply(
75
  message: str | None,
76
  history: ChatHistory | None,
@@ -83,13 +55,6 @@ def myco_reply(
83
  return "", current_history
84
 
85
  next_history = [*current_history, {"role": "user", "content": clean_message}]
86
- def myco_reply(message: str, history: ChatHistory, current: MushroomState | None) -> tuple[str, ChatHistory]:
87
- """Generate a lightweight companion reply without requiring an external model."""
88
- clean_message = message.strip()
89
- if not clean_message:
90
- return "", history
91
-
92
- next_history = [*history, {"role": "user", "content": clean_message}]
93
 
94
  if current:
95
  mushroom = mushroom_from_state(current)
@@ -105,44 +70,6 @@ def myco_reply(message: str, history: ChatHistory, current: MushroomState | None
105
  return "", next_history
106
 
107
 
108
- def collect_current(current=None, collection=None, history=None):
109
- """Add the current mushroom to the MycoDex if it is not already collected."""
110
- current_collection = _safe_collection(collection)
111
- current_history = _safe_history(history)
112
-
113
- if current is None:
114
- return current_collection, _append_assistant(
115
- current_history,
116
- "Let's discover a mushroom before updating the MycoDex.",
117
- )
118
-
119
- if collection_contains(current_collection, current["name"]):
120
- return current_collection, _append_assistant(
121
- current_history,
122
- f"{current['name']} is already safe in your MycoDex.",
123
- )
124
-
125
- updated_collection = [*current_collection, current]
126
- return updated_collection, _append_assistant(
127
- current_history,
128
- f"Added {current['name']} to the MycoDex. Nice find!",
129
- )
130
-
131
-
132
- def study_current(current=None, history=None):
133
- """Study the current mushroom to reveal one cautious field observation."""
134
- current_history = _safe_history(history)
135
- if current is None:
136
- return None, _append_assistant(
137
- current_history,
138
- "Let's find a mushroom before we study anything.",
139
- )
140
- next_history.append({"role": "assistant", "content": answer})
141
- return "", next_history
142
-
143
-
144
- def collect_current(current: MushroomState | None, collection: CollectionState | None, history):
145
- def collect_current(current: MushroomState | None, collection: CollectionState | None, history: ChatHistory | None):
146
  def collect_current(
147
  current: MushroomState | None,
148
  collection: CollectionState | None,
@@ -170,8 +97,6 @@ def collect_current(
170
  ]
171
 
172
 
173
- def study_current(current: MushroomState | None, history):
174
- def study_current(current: MushroomState | None, history: ChatHistory | None):
175
  def study_current(
176
  current: MushroomState | None, history: ChatHistory | None
177
  ) -> tuple[MushroomState | None, ChatHistory]:
@@ -182,46 +107,15 @@ def study_current(
182
  *current_history,
183
  {"role": "assistant", "content": "Let's find a mushroom before we study anything."},
184
  ]
185
- collection: CollectionState,
186
- history: ChatHistory,
187
- ) -> tuple[CollectionState, ChatHistory]:
188
- """Add the current mushroom to the MycoDex if it is not already collected."""
189
- if current is None:
190
- return collection, [*history, {"role": "assistant", "content": "Let's discover a mushroom before updating the MycoDex."}]
191
-
192
- if collection_contains(collection, current["name"]):
193
- return collection, [*history, {"role": "assistant", "content": f"{current['name']} is already safe in your MycoDex."}]
194
-
195
- updated_collection = [*collection, current]
196
- return updated_collection, [*history, {"role": "assistant", "content": f"Added {current['name']} to the MycoDex. Nice find!"}]
197
-
198
-
199
- def study_current(current: MushroomState | None, history: ChatHistory) -> tuple[MushroomState | None, ChatHistory]:
200
- """Study the current mushroom to reveal one cautious field observation."""
201
- if current is None:
202
- return None, [*history, {"role": "assistant", "content": "Let's find a mushroom before we study anything."}]
203
 
204
  studied = {**current, "magic": _study_hint(current["rarity"]), "studied": "Yes"}
205
  message = (
206
  f"I studied the {current['name']} closely. New MycoDex note: "
207
  f"magic is now marked as **{studied['magic']}**. Still no tasting without certainty!"
208
  )
209
- return studied, _append_assistant(current_history, message)
210
-
211
-
212
- def eat_current(current=None, history=None):
213
- """Handle the Eat choice safely with Myco's companion personality."""
214
- current_history = _safe_history(history)
215
- if current is None:
216
- return _append_assistant(
217
- current_history,
218
- "Let's discover a mushroom before making snack decisions.",
219
- )
220
  return studied, [*current_history, {"role": "assistant", "content": message}]
221
 
222
 
223
- def eat_current(current: MushroomState | None, history):
224
- def eat_current(current: MushroomState | None, history: ChatHistory | None):
225
  def eat_current(current: MushroomState | None, history: ChatHistory | None) -> ChatHistory:
226
  """Handle the Eat choice safely with Myco's companion personality."""
227
  next_history = _safe_history(history)
@@ -241,54 +135,13 @@ def eat_current(current: MushroomState | None, history: ChatHistory | None) -> C
241
  f"Its edible field is still **{mushroom.edible}**, "
242
  "so the winning move is to study or collect it instead of eating it."
243
  )
244
- return _append_assistant(current_history, message)
245
-
246
-
247
- def _assistant_message(content):
248
- """Return one assistant chat message."""
249
- return {"role": "assistant", "content": content}
250
-
251
-
252
- def _append_assistant(history, content):
253
- """Return chat history with one assistant message appended."""
254
- return [*_safe_history(history), _assistant_message(content)]
255
-
256
-
257
- def _safe_collection(collection):
258
  next_history.append({"role": "assistant", "content": message})
259
  return next_history
260
 
261
 
262
- def _safe_collection(collection: CollectionState | None):
263
- current_history = _safe_history(history)
264
- if current is None:
265
- return [
266
- *current_history,
267
- {"role": "assistant", "content": "Let's discover a mushroom before making snack decisions."},
268
- ]
269
-
270
- mushroom = mushroom_from_state(current)
271
- return [
272
- *current_history,
273
- return studied, [*history, {"role": "assistant", "content": message}]
274
-
275
-
276
- def eat_current(current: MushroomState | None, history: ChatHistory) -> ChatHistory:
277
- """Handle the Eat choice safely with Myco's companion personality."""
278
- if current is None:
279
- return [*history, {"role": "assistant", "content": "Let's discover a mushroom before making snack decisions."}]
280
-
281
- mushroom = mushroom_from_state(current)
282
- return [
283
- *history,
284
- {
285
- "role": "assistant",
286
- "content": (
287
- f"Myco gently blocks the {mushroom.name}. Its edible field is still **{mushroom.edible}**, "
288
- "so the winning move is to study or collect it instead of eating it."
289
- ),
290
- },
291
- ]
292
 
293
 
294
  def _safe_collection(collection: CollectionState | None) -> CollectionState:
@@ -296,15 +149,11 @@ def _safe_collection(collection: CollectionState | None) -> CollectionState:
296
  return list(collection or [])
297
 
298
 
299
- def _safe_history(history):
300
- def _safe_history(history: ChatHistory | None):
301
  def _safe_history(history: ChatHistory | None) -> ChatHistory:
302
  """Return a copy of chat history, tolerating an uninitialized Gradio state."""
303
  return list(history or welcome_history())
304
 
305
 
306
- def _study_hint(rarity):
307
- def _study_hint(rarity: str):
308
  def _study_hint(rarity: str) -> str:
309
  """Return a premium-feeling but cautious study result by rarity."""
310
  if rarity == "Legendary":
 
1
  """Core Myco gameplay actions independent from Gradio."""
2
 
 
 
 
 
 
 
 
3
  from __future__ import annotations
4
 
5
  import random
 
17
  from models.mushroom import Mushroom
18
 
19
 
 
20
  def choose_mushroom(catalog: Sequence[Mushroom] | None = None) -> Mushroom:
21
  """Choose a mushroom discovery from the catalog."""
22
  mushrooms = tuple(catalog or load_mushrooms())
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]:
29
  """Discover a mushroom and return the updated discovery state."""
 
30
  mushroom = choose_mushroom(catalog)
31
+ discovery_count = len(collection or [])
 
 
 
 
32
  current = mushroom.to_dict()
33
  history = welcome_history()
34
  history.append(
 
43
  return mushroom, current, history
44
 
45
 
 
 
 
46
  def myco_reply(
47
  message: str | None,
48
  history: ChatHistory | None,
 
55
  return "", current_history
56
 
57
  next_history = [*current_history, {"role": "user", "content": clean_message}]
 
 
 
 
 
 
 
58
 
59
  if current:
60
  mushroom = mushroom_from_state(current)
 
70
  return "", next_history
71
 
72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  def collect_current(
74
  current: MushroomState | None,
75
  collection: CollectionState | None,
 
97
  ]
98
 
99
 
 
 
100
  def study_current(
101
  current: MushroomState | None, history: ChatHistory | None
102
  ) -> tuple[MushroomState | None, ChatHistory]:
 
107
  *current_history,
108
  {"role": "assistant", "content": "Let's find a mushroom before we study anything."},
109
  ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
 
111
  studied = {**current, "magic": _study_hint(current["rarity"]), "studied": "Yes"}
112
  message = (
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
 
 
 
119
  def eat_current(current: MushroomState | None, history: ChatHistory | None) -> ChatHistory:
120
  """Handle the Eat choice safely with Myco's companion personality."""
121
  next_history = _safe_history(history)
 
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
 
141
 
142
+ def _assistant_message(content: str) -> dict:
143
+ """Return one assistant chat message."""
144
+ return {"role": "assistant", "content": content}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
 
146
 
147
  def _safe_collection(collection: CollectionState | None) -> CollectionState:
 
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":