List stories when cannot find
This commit is contained in:
parent
6308248d14
commit
bc69f46d1a
2 changed files with 31 additions and 3 deletions
|
|
@ -15,23 +15,51 @@ _STORY_EXTENSIONS = (".z3", ".z5", ".z8", ".zblorb")
|
|||
|
||||
def _find_story(name: str) -> pathlib.Path | None:
|
||||
"""Find a story file by name in content/stories/."""
|
||||
# exact match first
|
||||
for ext in _STORY_EXTENSIONS:
|
||||
path = _stories_dir / f"{name}{ext}"
|
||||
if path.exists():
|
||||
return path
|
||||
# prefix match (e.g. "zork" matches "zork1.z3")
|
||||
if _stories_dir.exists():
|
||||
for path in sorted(_stories_dir.iterdir()):
|
||||
if path.stem.startswith(name) and path.suffix in _STORY_EXTENSIONS:
|
||||
return path
|
||||
return None
|
||||
|
||||
|
||||
def _list_stories() -> list[str]:
|
||||
"""Return available story names."""
|
||||
if not _stories_dir.exists():
|
||||
return []
|
||||
return sorted(
|
||||
p.stem
|
||||
for p in _stories_dir.iterdir()
|
||||
if p.suffix in _STORY_EXTENSIONS
|
||||
)
|
||||
|
||||
|
||||
async def cmd_play(player: Player, args: str) -> None:
|
||||
"""Start playing an interactive fiction game."""
|
||||
game_name = args.strip().lower()
|
||||
if not game_name:
|
||||
await player.send("play what? (try: play zork1)\r\n")
|
||||
stories = _list_stories()
|
||||
if stories:
|
||||
names = ", ".join(stories)
|
||||
await player.send(f"play what? available: {names}\r\n")
|
||||
else:
|
||||
await player.send("play what? (no stories installed)\r\n")
|
||||
return
|
||||
|
||||
story_path = _find_story(game_name)
|
||||
if not story_path:
|
||||
await player.send(f"no story found for '{game_name}'.\r\n")
|
||||
stories = _list_stories()
|
||||
if stories:
|
||||
names = ", ".join(stories)
|
||||
msg = f"no story '{game_name}'. available: {names}\r\n"
|
||||
else:
|
||||
msg = f"no story found for '{game_name}'.\r\n"
|
||||
await player.send(msg)
|
||||
return
|
||||
|
||||
# Create and start IF session
|
||||
|
|
|
|||
|
|
@ -50,7 +50,7 @@ async def test_play_unknown_story(player):
|
|||
await cmd_play(player, "nosuchgame")
|
||||
player.writer.write.assert_called()
|
||||
output = player.writer.write.call_args[0][0]
|
||||
assert "no story found" in output.lower()
|
||||
assert "no story" in output.lower()
|
||||
assert "nosuchgame" in output.lower()
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue