toktoktheeo

Object

Apr 1st, 2022
1,781
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.28 KB | None | 0 0
  1. class ObjectParent:
  2.     """
  3.    This is a mixin that can be used to override *all* entities inheriting at
  4.    some distance from DefaultObject (Objects, Exits, Characters and Rooms).
  5.  
  6.    Just add any method that exists on `DefaultObject` to this class. If one
  7.    of the derived classes has itself defined that same hook already, that will
  8.    take precedence.
  9.  
  10.    """
  11.     # populated by `return_apperance`
  12.     appearance_template = "\n----------<|w{name}|n>{desc}{exits}{characters}{things}"
  13.  
  14.     def return_appearance(self, looker, **kwargs):
  15.         """
  16.        Main callback used by 'look' for the object to describe itself.
  17.        This formats a description. By default, this looks for the `appearance_template`
  18.        string set on this class and populates it with formatting keys
  19.            'name', 'desc', 'exits', 'characters', 'things' as well as
  20.            (currently empty) 'header'/'footer'.
  21.  
  22.        Args:
  23.            looker (Object): Object doing the looking.
  24.            **kwargs (dict): Arbitrary, optional arguments for users
  25.                overriding the call. This is passed into the helper
  26.                methods and into `get_display_name` calls.
  27.  
  28.        Returns:
  29.            str: The description of this entity. By default this includes
  30.                the entity's name, description and any contents inside it.
  31.  
  32.        Notes:
  33.            To simply change the layout of how the object displays itself (like
  34.            adding some line decorations or change colors of different sections),
  35.            you can simply edit `.appearance_template`. You only need to override
  36.            this method (and/or its helpers) if you want to change what is passed
  37.            into the template or want the most control over output.
  38.  
  39.        """
  40.  
  41.         if not looker:
  42.             return ""
  43.  
  44.         # ourselves
  45.         name = self.get_display_name(looker, **kwargs)
  46.         desc = self.db.desc or "Description non définie."
  47.  
  48.         # contents
  49.         content_names_map = self.get_content_names(looker, **kwargs)
  50.         exits = list_to_string(content_names_map["exits"])
  51.         characters = list_to_string(content_names_map["characters"])
  52.         things = list_to_string(content_names_map["things"])
  53.  
  54.         # populate the appearance_template string. It's a good idea to strip it and
  55.         # let the client add any extra spaces instead.
  56.         return self.appearance_template.format(
  57.             name=f"{name}",
  58.             desc=f"\n{desc}\n",
  59.             exits=f"\n|wExits:|n {exits}" if exits else "",
  60.             characters=f"\n|wPersonnages:|n {characters}" if characters else "",
  61.             things=f"\n|wAgents:|n {things}.\n" if things else "",
  62.         )
  63.  
  64.     def at_say(
  65.         self,
  66.         message,
  67.         msg_self=None,
  68.         msg_location=None,
  69.         receivers=None,
  70.         msg_receivers=None,
  71.         **kwargs,
  72.     ):
  73.         """
  74.        Display the actual say (or whisper) of self.
  75.  
  76.        This hook should display the actual say/whisper of the object in its
  77.        location.  It should both alert the object (self) and its
  78.        location that some text is spoken.  The overriding of messages or
  79.        `mapping` allows for simple customization of the hook without
  80.        re-writing it completely.
  81.  
  82.        Args:
  83.            message (str): The message to convey.
  84.            msg_self (bool or str, optional): If boolean True, echo `message` to self. If a string,
  85.                return that message. If False or unset, don't echo to self.
  86.            msg_location (str, optional): The message to echo to self's location.
  87.            receivers (Object or iterable, optional): An eventual receiver or receivers of the
  88.                message (by default only used by whispers).
  89.            msg_receivers(str): Specific message to pass to the receiver(s). This will parsed
  90.                with the {receiver} placeholder replaced with the given receiver.
  91.        Keyword Args:
  92.            whisper (bool): If this is a whisper rather than a say. Kwargs
  93.                can be used by other verbal commands in a similar way.
  94.            mapping (dict): Pass an additional mapping to the message.
  95.  
  96.        Notes:
  97.  
  98.  
  99.            Messages can contain {} markers. These are substituted against the values
  100.            passed in the `mapping` argument.
  101.  
  102.                msg_self = 'You say: "{speech}"'
  103.                msg_location = '{object} says: "{speech}"'
  104.                msg_receivers = '{object} whispers: "{speech}"'
  105.  
  106.            Supported markers by default:
  107.                {self}: text to self-reference with (default 'You')
  108.                {speech}: the text spoken/whispered by self.
  109.                {object}: the object speaking.
  110.                {receiver}: replaced with a single receiver only for strings meant for a specific
  111.                    receiver (otherwise 'None').
  112.                {all_receivers}: comma-separated list of all receivers,
  113.                                 if more than one, otherwise same as receiver
  114.                {location}: the location where object is.
  115.  
  116.        """
  117.         msg_type = "say"
  118.         if kwargs.get("whisper", False):
  119.             # whisper mode
  120.             msg_type = "whisper"
  121.             msg_self = (
  122.                 '{self} whisper to {all_receivers}, "|n{speech}|n"'
  123.                 if msg_self is True
  124.                 else msg_self
  125.             )
  126.             msg_receivers = msg_receivers or '{object} whispers: "|n{speech}|n"'
  127.             msg_location = None
  128.         else:
  129.             msg_self = '{self} dites, "|n{speech}|n"' if msg_self is True else msg_self
  130.             msg_location = msg_location or '{object} dit, "{speech}"'
  131.             msg_receivers = msg_receivers or message
  132.  
  133.         custom_mapping = kwargs.get("mapping", {})
  134.         receivers = make_iter(receivers) if receivers else None
  135.         location = self.location
  136.  
  137.         if msg_self:
  138.             self_mapping = {
  139.                 "self": "Vous",
  140.                 "object": self.get_display_name(self),
  141.                 "location": location.get_display_name(self) if location else None,
  142.                 "receiver": None,
  143.                 "all_receivers": ", ".join(recv.get_display_name(self) for recv in receivers)
  144.                 if receivers
  145.                 else None,
  146.                 "speech": message,
  147.             }
  148.             self_mapping.update(custom_mapping)
  149.             self.msg(text=(msg_self.format(**self_mapping), {"type": msg_type}), from_obj=self)
  150.  
  151.         if receivers and msg_receivers:
  152.             receiver_mapping = {
  153.                 "self": "Vous",
  154.                 "object": None,
  155.                 "location": None,
  156.                 "receiver": None,
  157.                 "all_receivers": None,
  158.                 "speech": message,
  159.             }
  160.             for receiver in make_iter(receivers):
  161.                 individual_mapping = {
  162.                     "object": self.get_display_name(receiver),
  163.                     "location": location.get_display_name(receiver),
  164.                     "receiver": receiver.get_display_name(receiver),
  165.                     "all_receivers": ", ".join(recv.get_display_name(recv) for recv in receivers)
  166.                     if receivers
  167.                     else None,
  168.                 }
  169.                 receiver_mapping.update(individual_mapping)
  170.                 receiver_mapping.update(custom_mapping)
  171.                 receiver.msg(
  172.                     text=(msg_receivers.format(**receiver_mapping), {"type": msg_type}),
  173.                     from_obj=self,
  174.                 )
  175.  
  176.         if self.location and msg_location:
  177.             location_mapping = {
  178.                 "self": "Vous",
  179.                 "object": self,
  180.                 "location": location,
  181.                 "all_receivers": ", ".join(str(recv) for recv in receivers) if receivers else None,
  182.                 "receiver": None,
  183.                 "speech": message,
  184.             }
  185.             location_mapping.update(custom_mapping)
  186.             exclude = []
  187.             if msg_self:
  188.                 exclude.append(self)
  189.             if receivers:
  190.                 exclude.extend(receivers)
  191.             self.location.msg_contents(
  192.                 text=(msg_location, {"type": msg_type}),
  193.                 from_obj=self,
  194.                 exclude=exclude,
  195.                 mapping=location_mapping,
  196.             )
  197.  
Advertisement
Add Comment
Please, Sign In to add comment