Spaces:
Sleeping

Myco / game /engine.py
byte-vortex's picture
Deploy Myco from CI
8a1102f verified
Raw History Blame
8.96 kB
"""Core Myco gameplay actions independent from Gradio."""
import random
from game.catalog import load_mushrooms
from game.state import collection_contains, mushroom_from_state, welcome_history
def choose_mushroom(catalog=None):
"""Choose a mushroom discovery from the catalog."""
mushrooms = tuple(load_mushrooms() if catalog is None else catalog)
if not mushrooms:
raise ValueError("Myco needs at least one mushroom in the catalog.")
from __future__ import annotations
import random
from collections.abc import Sequence
from game.catalog import load_mushrooms
from game.state import (
ChatHistory,
CollectionState,
MushroomState,
collection_contains,
mushroom_from_state,
welcome_history,
)
from models.mushroom import Mushroom
def choose_mushroom(catalog: Sequence[Mushroom] | None = None) -> Mushroom:
"""Choose a mushroom discovery from the catalog."""
mushrooms = tuple(catalog or load_mushrooms())
return random.choice(mushrooms)
def discover_mushroom(collection=None, catalog=None):
"""Discover a mushroom and return the updated discovery state."""
current_collection = _safe_collection(collection)
mushroom = choose_mushroom(catalog)
current = mushroom.to_dict()
message = (
f"We found a {mushroom.name}! Ask me about it, then decide whether to "
f"eat, collect, or study it. Your MycoDex has {len(current_collection)} entries."
)
history = _append_assistant(welcome_history(), message)
history = welcome_history()
history.append(
_assistant_message(
f"We found a {mushroom.name}! Ask me about it, then decide whether to "
f"eat, collect, or study it. Your MycoDex has {len(current_collection)} entries."
)
def discover_mushroom(
collection: CollectionState | None, catalog: Sequence[Mushroom] | None = None
) -> tuple[Mushroom, MushroomState, ChatHistory]:
"""Discover a mushroom and return the updated discovery state."""
mushroom = choose_mushroom(catalog)
discovery_count = len(collection or [])
current = mushroom.to_dict()
history = welcome_history()
history.append(
{
"role": "assistant",
"content": (
f"We found a {mushroom.name}! Ask me about it, then decide whether to "
f"eat, collect, or study it. Your MycoDex has {discovery_count} entries."
),
}
)
return mushroom, current, history
def myco_reply(message=None, history=None, current=None):
def myco_reply(
message: str | None,
history: ChatHistory | None,
current: MushroomState | None,
) -> tuple[str, ChatHistory]:
"""Generate a lightweight companion reply without requiring an external model."""
current_history = _safe_history(history)
clean_message = (message or "").strip()
if not clean_message:
return "", current_history
next_history = [*current_history, {"role": "user", "content": clean_message}]
if current:
mushroom = mushroom_from_state(current)
answer = (
f"I think this is **{mushroom.name}**. {mushroom.lore} "
"The MycoDex still marks edible, magic, and danger as unknown, "
"so let's observe it gently instead of tasting anything."
)
else:
answer = "Let's explore first. The forest usually reveals a mushroom when we move slowly and listen."
return "", [*next_history, _assistant_message(answer)]
next_history.append(_assistant_message(answer))
return "", next_history
def collect_current(current=None, collection=None, history=None):
"""Add the current mushroom to the MycoDex if it is not already collected."""
current_collection = _safe_collection(collection)
current_history = _safe_history(history)
if current is None:
return current_collection, _append_assistant(
current_history,
"Let's discover a mushroom before updating the MycoDex.",
)
if collection_contains(current_collection, current["name"]):
return current_collection, _append_assistant(
current_history,
f"{current['name']} is already safe in your MycoDex.",
)
updated_collection = [*current_collection, current]
return updated_collection, _append_assistant(
current_history,
f"Added {current['name']} to the MycoDex. Nice find!",
)
def study_current(current=None, history=None):
"""Study the current mushroom to reveal one cautious field observation."""
current_history = _safe_history(history)
if current is None:
return None, _append_assistant(
current_history,
"Let's find a mushroom before we study anything.",
)
def collect_current(
current: MushroomState | None,
collection: CollectionState | None,
history: ChatHistory | None,
) -> tuple[CollectionState, ChatHistory]:
"""Add the current mushroom to the MycoDex if it is not already collected."""
current_collection = _safe_collection(collection)
current_history = _safe_history(history)
if current is None:
return current_collection, [
*current_history,
{"role": "assistant", "content": "Let's discover a mushroom before updating the MycoDex."},
]
if collection_contains(current_collection, current["name"]):
return current_collection, [
*current_history,
{"role": "assistant", "content": f"{current['name']} is already safe in your MycoDex."},
]
updated_collection = [*current_collection, current]
return updated_collection, [
*current_history,
{"role": "assistant", "content": f"Added {current['name']} to the MycoDex. Nice find!"},
]
def study_current(
current: MushroomState | None, history: ChatHistory | None
) -> tuple[MushroomState | None, ChatHistory]:
"""Study the current mushroom to reveal one cautious field observation."""
current_history = _safe_history(history)
if current is None:
return None, [
*current_history,
{"role": "assistant", "content": "Let's find a mushroom before we study anything."},
]
studied = {**current, "magic": _study_hint(current["rarity"]), "studied": "Yes"}
message = (
f"I studied the {current['name']} closely. New MycoDex note: "
f"magic is now marked as **{studied['magic']}**. Still no tasting without certainty!"
)
return studied, _append_assistant(current_history, message)
def eat_current(current=None, history=None):
"""Handle the Eat choice safely with Myco's companion personality."""
current_history = _safe_history(history)
if current is None:
return _append_assistant(
current_history,
"Let's discover a mushroom before making snack decisions.",
)
return studied, [*current_history, {"role": "assistant", "content": message}]
def eat_current(current: MushroomState | None, history: ChatHistory | None) -> ChatHistory:
"""Handle the Eat choice safely with Myco's companion personality."""
next_history = _safe_history(history)
if current is None:
next_history.append(
{
"role": "assistant",
"content": "Let's discover a mushroom before making snack decisions.",
}
)
return next_history
mushroom = mushroom_from_state(current)
message = (
f"Myco gently blocks the {mushroom.name}. "
f"Its edible field is still **{mushroom.edible}**, "
"so the winning move is to study or collect it instead of eating it."
)
return _append_assistant(current_history, message)
def _assistant_message(content):
next_history.append({"role": "assistant", "content": message})
return next_history
def _assistant_message(content: str) -> dict:
"""Return one assistant chat message."""
return {"role": "assistant", "content": content}
def _append_assistant(history, content):
"""Return chat history with one assistant message appended."""
return [*_safe_history(history), _assistant_message(content)]
def _safe_collection(collection):
def _safe_collection(collection: CollectionState | None) -> CollectionState:
"""Return a copy of the collection, tolerating an uninitialized Gradio state."""
return list(collection or [])
def _safe_history(history):
def _safe_history(history: ChatHistory | None) -> ChatHistory:
"""Return a copy of chat history, tolerating an uninitialized Gradio state."""
return list(history or welcome_history())
def _study_hint(rarity):
def _study_hint(rarity: str) -> str:
"""Return a premium-feeling but cautious study result by rarity."""
if rarity == "Legendary":
return "Strong, mysterious aura"
if rarity == "Rare":
return "Faint magical trace"
return "Tiny spore shimmer"