2021-11-18 11:31:21 -08:00
|
|
|
import dataclasses
|
|
|
|
|
from typing import MutableMapping, Optional, Sequence, Union, TYPE_CHECKING
|
|
|
|
|
from agame.item import ItemInst
|
|
|
|
|
from agame.util import search_item_name
|
|
|
|
|
|
2021-11-18 20:32:02 -08:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from agame.action import Action
|
|
|
|
|
|
2021-11-18 11:31:21 -08:00
|
|
|
|
|
|
|
|
__all__ = ("Room",)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclasses.dataclass
|
|
|
|
|
class Room:
|
|
|
|
|
id: str
|
|
|
|
|
name: str
|
2021-11-18 16:26:16 -08:00
|
|
|
desc: Union[str, Sequence[str]]
|
2021-11-18 11:31:21 -08:00
|
|
|
items: MutableMapping[str, ItemInst]
|
2021-11-18 20:32:02 -08:00
|
|
|
# Actions that are executed upon teleport or entrance to this room.
|
|
|
|
|
teleport_actions: Sequence["Action"]
|
2021-11-18 11:31:21 -08:00
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
id: str,
|
|
|
|
|
name: str,
|
2021-11-18 16:26:16 -08:00
|
|
|
desc: Union[str, Sequence[str]],
|
2021-11-18 11:31:21 -08:00
|
|
|
items: Union[Sequence[ItemInst], MutableMapping[str, ItemInst]],
|
2021-11-18 20:32:02 -08:00
|
|
|
teleport_actions: Sequence["Action"] = [],
|
2021-11-18 11:31:21 -08:00
|
|
|
):
|
|
|
|
|
self.id = id
|
|
|
|
|
self.name = name
|
|
|
|
|
self.desc = desc
|
|
|
|
|
if isinstance(items, MutableMapping):
|
|
|
|
|
self.items = items
|
|
|
|
|
else:
|
|
|
|
|
self.items = {item.id: item for item in items}
|
2021-11-18 20:32:02 -08:00
|
|
|
self.teleport_actions = teleport_actions
|
2021-11-18 11:31:21 -08:00
|
|
|
|
|
|
|
|
def search_item_name(self, item_name: str) -> Optional[ItemInst]:
|
|
|
|
|
"""
|
|
|
|
|
Searches all item instances in the room for the given item name, also
|
|
|
|
|
checking synonyms. Returns the first item instance found, or none if no
|
|
|
|
|
synonyms or names were found to match.
|
|
|
|
|
"""
|
|
|
|
|
return search_item_name(self.items.values(), item_name)
|
|
|
|
|
|
|
|
|
|
def remove(self, item_id: str) -> Optional[ItemInst]:
|
|
|
|
|
"""
|
|
|
|
|
Removes an item with the given ID from the room, returning it.
|
|
|
|
|
|
|
|
|
|
If it's not present in the room, `None` is returned.
|
|
|
|
|
"""
|
|
|
|
|
return self.items.pop(item_id, None)
|