List stories when cannot find

This commit is contained in:
Jared Miller 2026-02-09 16:14:21 -05:00
parent 6308248d14
commit bc69f46d1a
Signed by: shmup
GPG key ID: 22B5C6D66A38B06C
2 changed files with 31 additions and 3 deletions

View file

@ -15,23 +15,51 @@ _STORY_EXTENSIONS = (".z3", ".z5", ".z8", ".zblorb")
def _find_story(name: str) -> pathlib.Path | None: def _find_story(name: str) -> pathlib.Path | None:
"""Find a story file by name in content/stories/.""" """Find a story file by name in content/stories/."""
# exact match first
for ext in _STORY_EXTENSIONS: for ext in _STORY_EXTENSIONS:
path = _stories_dir / f"{name}{ext}" path = _stories_dir / f"{name}{ext}"
if path.exists(): if path.exists():
return path 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 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: async def cmd_play(player: Player, args: str) -> None:
"""Start playing an interactive fiction game.""" """Start playing an interactive fiction game."""
game_name = args.strip().lower() game_name = args.strip().lower()
if not game_name: 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 return
story_path = _find_story(game_name) story_path = _find_story(game_name)
if not story_path: 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 return
# Create and start IF session # Create and start IF session

View file

@ -50,7 +50,7 @@ async def test_play_unknown_story(player):
await cmd_play(player, "nosuchgame") await cmd_play(player, "nosuchgame")
player.writer.write.assert_called() player.writer.write.assert_called()
output = player.writer.write.call_args[0][0] 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() assert "nosuchgame" in output.lower()