Files
ages/agame/room.py
Alek Ratzloff dd2128beb1 Initial commit with a mostly working engine.
Basic commands are being parsed. I think the only weird part is the
'use' command because it needs to possibly target two things. A tiny
test example is provided in __main__, this will probably be broken out
later.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
2021-11-18 11:31:21 -08:00

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)