Spaces:
Sleeping
Sleeping
Deploy Myco from CI
Browse files- game/engine.py +1 -81
game/engine.py
CHANGED
|
@@ -1,16 +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 |
-
"""Choose a mushroom discovery from the catalog."""
|
| 11 |
-
mushrooms = tuple(load_mushrooms() if catalog is None else catalog)
|
| 12 |
-
if not mushrooms:
|
| 13 |
-
raise ValueError("Myco needs at least one mushroom in the catalog.")
|
| 14 |
from __future__ import annotations
|
| 15 |
|
| 16 |
import random
|
|
@@ -34,22 +23,6 @@ def choose_mushroom(catalog: Sequence[Mushroom] | None = None) -> Mushroom:
|
|
| 34 |
return random.choice(mushrooms)
|
| 35 |
|
| 36 |
|
| 37 |
-
def discover_mushroom(collection=None, catalog=None):
|
| 38 |
-
"""Discover a mushroom and return the updated discovery state."""
|
| 39 |
-
current_collection = _safe_collection(collection)
|
| 40 |
-
mushroom = choose_mushroom(catalog)
|
| 41 |
-
current = mushroom.to_dict()
|
| 42 |
-
message = (
|
| 43 |
-
f"We found a {mushroom.name}! Ask me about it, then decide whether to "
|
| 44 |
-
f"eat, collect, or study it. Your MycoDex has {len(current_collection)} entries."
|
| 45 |
-
)
|
| 46 |
-
history = _append_assistant(welcome_history(), message)
|
| 47 |
-
history = welcome_history()
|
| 48 |
-
history.append(
|
| 49 |
-
_assistant_message(
|
| 50 |
-
f"We found a {mushroom.name}! Ask me about it, then decide whether to "
|
| 51 |
-
f"eat, collect, or study it. Your MycoDex has {len(current_collection)} entries."
|
| 52 |
-
)
|
| 53 |
def discover_mushroom(
|
| 54 |
collection: CollectionState | None, catalog: Sequence[Mushroom] | None = None
|
| 55 |
) -> tuple[Mushroom, MushroomState, ChatHistory]:
|
|
@@ -70,7 +43,6 @@ def discover_mushroom(
|
|
| 70 |
return mushroom, current, history
|
| 71 |
|
| 72 |
|
| 73 |
-
def myco_reply(message=None, history=None, current=None):
|
| 74 |
def myco_reply(
|
| 75 |
message: str | None,
|
| 76 |
history: ChatHistory | None,
|
|
@@ -95,42 +67,8 @@ def myco_reply(
|
|
| 95 |
answer = "Let's explore first. The forest usually reveals a mushroom when we move slowly and listen."
|
| 96 |
|
| 97 |
return "", [*next_history, _assistant_message(answer)]
|
| 98 |
-
next_history.append(_assistant_message(answer))
|
| 99 |
-
return "", next_history
|
| 100 |
-
|
| 101 |
|
| 102 |
-
def collect_current(current=None, collection=None, history=None):
|
| 103 |
-
"""Add the current mushroom to the MycoDex if it is not already collected."""
|
| 104 |
-
current_collection = _safe_collection(collection)
|
| 105 |
-
current_history = _safe_history(history)
|
| 106 |
|
| 107 |
-
if current is None:
|
| 108 |
-
return current_collection, _append_assistant(
|
| 109 |
-
current_history,
|
| 110 |
-
"Let's discover a mushroom before updating the MycoDex.",
|
| 111 |
-
)
|
| 112 |
-
|
| 113 |
-
if collection_contains(current_collection, current["name"]):
|
| 114 |
-
return current_collection, _append_assistant(
|
| 115 |
-
current_history,
|
| 116 |
-
f"{current['name']} is already safe in your MycoDex.",
|
| 117 |
-
)
|
| 118 |
-
|
| 119 |
-
updated_collection = [*current_collection, current]
|
| 120 |
-
return updated_collection, _append_assistant(
|
| 121 |
-
current_history,
|
| 122 |
-
f"Added {current['name']} to the MycoDex. Nice find!",
|
| 123 |
-
)
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
def study_current(current=None, history=None):
|
| 127 |
-
"""Study the current mushroom to reveal one cautious field observation."""
|
| 128 |
-
current_history = _safe_history(history)
|
| 129 |
-
if current is None:
|
| 130 |
-
return None, _append_assistant(
|
| 131 |
-
current_history,
|
| 132 |
-
"Let's find a mushroom before we study anything.",
|
| 133 |
-
)
|
| 134 |
def collect_current(
|
| 135 |
current: MushroomState | None,
|
| 136 |
collection: CollectionState | None,
|
|
@@ -174,17 +112,6 @@ def study_current(
|
|
| 174 |
f"I studied the {current['name']} closely. New MycoDex note: "
|
| 175 |
f"magic is now marked as **{studied['magic']}**. Still no tasting without certainty!"
|
| 176 |
)
|
| 177 |
-
return studied, _append_assistant(current_history, message)
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
def eat_current(current=None, history=None):
|
| 181 |
-
"""Handle the Eat choice safely with Myco's companion personality."""
|
| 182 |
-
current_history = _safe_history(history)
|
| 183 |
-
if current is None:
|
| 184 |
-
return _append_assistant(
|
| 185 |
-
current_history,
|
| 186 |
-
"Let's discover a mushroom before making snack decisions.",
|
| 187 |
-
)
|
| 188 |
return studied, [*current_history, {"role": "assistant", "content": message}]
|
| 189 |
|
| 190 |
|
|
@@ -207,10 +134,6 @@ def eat_current(current: MushroomState | None, history: ChatHistory | None) -> C
|
|
| 207 |
f"Its edible field is still **{mushroom.edible}**, "
|
| 208 |
"so the winning move is to study or collect it instead of eating it."
|
| 209 |
)
|
| 210 |
-
return _append_assistant(current_history, message)
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
def _assistant_message(content):
|
| 214 |
next_history.append({"role": "assistant", "content": message})
|
| 215 |
return next_history
|
| 216 |
|
|
@@ -220,24 +143,21 @@ def _assistant_message(content: str) -> dict:
|
|
| 220 |
return {"role": "assistant", "content": content}
|
| 221 |
|
| 222 |
|
| 223 |
-
def _append_assistant(history, content):
|
| 224 |
"""Return chat history with one assistant message appended."""
|
| 225 |
return [*_safe_history(history), _assistant_message(content)]
|
| 226 |
|
| 227 |
|
| 228 |
-
def _safe_collection(collection):
|
| 229 |
def _safe_collection(collection: CollectionState | None) -> CollectionState:
|
| 230 |
"""Return a copy of the collection, tolerating an uninitialized Gradio state."""
|
| 231 |
return list(collection or [])
|
| 232 |
|
| 233 |
|
| 234 |
-
def _safe_history(history):
|
| 235 |
def _safe_history(history: ChatHistory | None) -> ChatHistory:
|
| 236 |
"""Return a copy of chat history, tolerating an uninitialized Gradio state."""
|
| 237 |
return list(history or welcome_history())
|
| 238 |
|
| 239 |
|
| 240 |
-
def _study_hint(rarity):
|
| 241 |
def _study_hint(rarity: str) -> str:
|
| 242 |
"""Return a premium-feeling but cautious study result by rarity."""
|
| 243 |
if rarity == "Legendary":
|
|
|
|
| 1 |
"""Core Myco gameplay actions independent from Gradio."""
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
import random
|
|
|
|
| 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 |
return mushroom, current, history
|
| 44 |
|
| 45 |
|
|
|
|
| 46 |
def myco_reply(
|
| 47 |
message: str | None,
|
| 48 |
history: ChatHistory | None,
|
|
|
|
| 67 |
answer = "Let's explore first. The forest usually reveals a mushroom when we move slowly and listen."
|
| 68 |
|
| 69 |
return "", [*next_history, _assistant_message(answer)]
|
|
|
|
|
|
|
|
|
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
def collect_current(
|
| 73 |
current: MushroomState | None,
|
| 74 |
collection: CollectionState | None,
|
|
|
|
| 112 |
f"I studied the {current['name']} closely. New MycoDex note: "
|
| 113 |
f"magic is now marked as **{studied['magic']}**. Still no tasting without certainty!"
|
| 114 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
return studied, [*current_history, {"role": "assistant", "content": message}]
|
| 116 |
|
| 117 |
|
|
|
|
| 134 |
f"Its edible field is still **{mushroom.edible}**, "
|
| 135 |
"so the winning move is to study or collect it instead of eating it."
|
| 136 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
next_history.append({"role": "assistant", "content": message})
|
| 138 |
return next_history
|
| 139 |
|
|
|
|
| 143 |
return {"role": "assistant", "content": content}
|
| 144 |
|
| 145 |
|
| 146 |
+
def _append_assistant(history: ChatHistory, content: str) -> ChatHistory:
|
| 147 |
"""Return chat history with one assistant message appended."""
|
| 148 |
return [*_safe_history(history), _assistant_message(content)]
|
| 149 |
|
| 150 |
|
|
|
|
| 151 |
def _safe_collection(collection: CollectionState | None) -> CollectionState:
|
| 152 |
"""Return a copy of the collection, tolerating an uninitialized Gradio state."""
|
| 153 |
return list(collection or [])
|
| 154 |
|
| 155 |
|
|
|
|
| 156 |
def _safe_history(history: ChatHistory | None) -> ChatHistory:
|
| 157 |
"""Return a copy of chat history, tolerating an uninitialized Gradio state."""
|
| 158 |
return list(history or welcome_history())
|
| 159 |
|
| 160 |
|
|
|
|
| 161 |
def _study_hint(rarity: str) -> str:
|
| 162 |
"""Return a premium-feeling but cautious study result by rarity."""
|
| 163 |
if rarity == "Legendary":
|