47 lines
1.3 KiB
Python
47 lines
1.3 KiB
Python
|
|
import dataclasses
|
||
|
|
from typing import MutableMapping, Optional, Sequence, Union, TYPE_CHECKING
|
||
|
|
from agame.item import ItemInst
|
||
|
|
from agame.util import search_item_name
|
||
|
|
|
||
|
|
|
||
|
|
__all__ = ("Room",)
|
||
|
|
|
||
|
|
|
||
|
|
@dataclasses.dataclass
|
||
|
|
class Room:
|
||
|
|
id: str
|
||
|
|
name: str
|
||
|
|
desc: str
|
||
|
|
items: MutableMapping[str, ItemInst]
|
||
|
|
|
||
|
|
def __init__(
|
||
|
|
self,
|
||
|
|
id: str,
|
||
|
|
name: str,
|
||
|
|
desc: str,
|
||
|
|
items: Union[Sequence[ItemInst], MutableMapping[str, ItemInst]],
|
||
|
|
):
|
||
|
|
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}
|
||
|
|
|
||
|
|
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)
|