Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ObjectParent:
- """
- This is a mixin that can be used to override *all* entities inheriting at
- some distance from DefaultObject (Objects, Exits, Characters and Rooms).
- Just add any method that exists on `DefaultObject` to this class. If one
- of the derived classes has itself defined that same hook already, that will
- take precedence.
- """
- # populated by `return_apperance`
- appearance_template = "\n----------<|w{name}|n>{desc}{exits}{characters}{things}"
- def return_appearance(self, looker, **kwargs):
- """
- Main callback used by 'look' for the object to describe itself.
- This formats a description. By default, this looks for the `appearance_template`
- string set on this class and populates it with formatting keys
- 'name', 'desc', 'exits', 'characters', 'things' as well as
- (currently empty) 'header'/'footer'.
- Args:
- looker (Object): Object doing the looking.
- **kwargs (dict): Arbitrary, optional arguments for users
- overriding the call. This is passed into the helper
- methods and into `get_display_name` calls.
- Returns:
- str: The description of this entity. By default this includes
- the entity's name, description and any contents inside it.
- Notes:
- To simply change the layout of how the object displays itself (like
- adding some line decorations or change colors of different sections),
- you can simply edit `.appearance_template`. You only need to override
- this method (and/or its helpers) if you want to change what is passed
- into the template or want the most control over output.
- """
- if not looker:
- return ""
- # ourselves
- name = self.get_display_name(looker, **kwargs)
- desc = self.db.desc or "Description non définie."
- # contents
- content_names_map = self.get_content_names(looker, **kwargs)
- exits = list_to_string(content_names_map["exits"])
- characters = list_to_string(content_names_map["characters"])
- things = list_to_string(content_names_map["things"])
- # populate the appearance_template string. It's a good idea to strip it and
- # let the client add any extra spaces instead.
- return self.appearance_template.format(
- name=f"{name}",
- desc=f"\n{desc}\n",
- exits=f"\n|wExits:|n {exits}" if exits else "",
- characters=f"\n|wPersonnages:|n {characters}" if characters else "",
- things=f"\n|wAgents:|n {things}.\n" if things else "",
- )
- def at_say(
- self,
- message,
- msg_self=None,
- msg_location=None,
- receivers=None,
- msg_receivers=None,
- **kwargs,
- ):
- """
- Display the actual say (or whisper) of self.
- This hook should display the actual say/whisper of the object in its
- location. It should both alert the object (self) and its
- location that some text is spoken. The overriding of messages or
- `mapping` allows for simple customization of the hook without
- re-writing it completely.
- Args:
- message (str): The message to convey.
- msg_self (bool or str, optional): If boolean True, echo `message` to self. If a string,
- return that message. If False or unset, don't echo to self.
- msg_location (str, optional): The message to echo to self's location.
- receivers (Object or iterable, optional): An eventual receiver or receivers of the
- message (by default only used by whispers).
- msg_receivers(str): Specific message to pass to the receiver(s). This will parsed
- with the {receiver} placeholder replaced with the given receiver.
- Keyword Args:
- whisper (bool): If this is a whisper rather than a say. Kwargs
- can be used by other verbal commands in a similar way.
- mapping (dict): Pass an additional mapping to the message.
- Notes:
- Messages can contain {} markers. These are substituted against the values
- passed in the `mapping` argument.
- msg_self = 'You say: "{speech}"'
- msg_location = '{object} says: "{speech}"'
- msg_receivers = '{object} whispers: "{speech}"'
- Supported markers by default:
- {self}: text to self-reference with (default 'You')
- {speech}: the text spoken/whispered by self.
- {object}: the object speaking.
- {receiver}: replaced with a single receiver only for strings meant for a specific
- receiver (otherwise 'None').
- {all_receivers}: comma-separated list of all receivers,
- if more than one, otherwise same as receiver
- {location}: the location where object is.
- """
- msg_type = "say"
- if kwargs.get("whisper", False):
- # whisper mode
- msg_type = "whisper"
- msg_self = (
- '{self} whisper to {all_receivers}, "|n{speech}|n"'
- if msg_self is True
- else msg_self
- )
- msg_receivers = msg_receivers or '{object} whispers: "|n{speech}|n"'
- msg_location = None
- else:
- msg_self = '{self} dites, "|n{speech}|n"' if msg_self is True else msg_self
- msg_location = msg_location or '{object} dit, "{speech}"'
- msg_receivers = msg_receivers or message
- custom_mapping = kwargs.get("mapping", {})
- receivers = make_iter(receivers) if receivers else None
- location = self.location
- if msg_self:
- self_mapping = {
- "self": "Vous",
- "object": self.get_display_name(self),
- "location": location.get_display_name(self) if location else None,
- "receiver": None,
- "all_receivers": ", ".join(recv.get_display_name(self) for recv in receivers)
- if receivers
- else None,
- "speech": message,
- }
- self_mapping.update(custom_mapping)
- self.msg(text=(msg_self.format(**self_mapping), {"type": msg_type}), from_obj=self)
- if receivers and msg_receivers:
- receiver_mapping = {
- "self": "Vous",
- "object": None,
- "location": None,
- "receiver": None,
- "all_receivers": None,
- "speech": message,
- }
- for receiver in make_iter(receivers):
- individual_mapping = {
- "object": self.get_display_name(receiver),
- "location": location.get_display_name(receiver),
- "receiver": receiver.get_display_name(receiver),
- "all_receivers": ", ".join(recv.get_display_name(recv) for recv in receivers)
- if receivers
- else None,
- }
- receiver_mapping.update(individual_mapping)
- receiver_mapping.update(custom_mapping)
- receiver.msg(
- text=(msg_receivers.format(**receiver_mapping), {"type": msg_type}),
- from_obj=self,
- )
- if self.location and msg_location:
- location_mapping = {
- "self": "Vous",
- "object": self,
- "location": location,
- "all_receivers": ", ".join(str(recv) for recv in receivers) if receivers else None,
- "receiver": None,
- "speech": message,
- }
- location_mapping.update(custom_mapping)
- exclude = []
- if msg_self:
- exclude.append(self)
- if receivers:
- exclude.extend(receivers)
- self.location.msg_contents(
- text=(msg_location, {"type": msg_type}),
- from_obj=self,
- exclude=exclude,
- mapping=location_mapping,
- )
Advertisement
Add Comment
Please, Sign In to add comment