Files
ages/agame/util.py

43 lines
1.1 KiB
Python
Raw Normal View History

from typing import Iterable, Optional, Sequence, Union, TYPE_CHECKING
import dataclasses
import re
if TYPE_CHECKING:
from agame.item import ItemInst
def search_item_name(seq: Iterable["ItemInst"], item_name: str) -> Optional["ItemInst"]:
for item in seq:
if item.name.lower() == item_name.lower() or item_name.lower() in item.synonyms:
return item
return None
def trigger_help_builder(
actions: Union[str, Sequence[str]], args: Union[str, Sequence[str]] = ()
) -> str:
actions = (actions,) if isinstance(actions, str) else actions
args = (args,) if isinstance(args, str) else args
actions = (
f"(({actions}))"
if isinstance(actions, str)
else "/".join(f"(({a}))" for a in actions)
)
def fmt_arg(a: str):
if "[" in a and "]" in a:
return f"{{{{{a}}}}}"
else:
return a
if args:
args = (
fmt_arg(args)
if isinstance(args, str)
else " ".join(fmt_arg(a) for a in args)
)
return f"{actions} {args}"
else:
return actions