Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.07 KB | None | 0 0
  1. class MessageMixins(object):
  2.    
  3.     def pronoun_sub(self, template, player, iobj=None, location=None):
  4.  
  5.         if not template:
  6.                 return ""
  7.  
  8.         if not player:
  9.             player = self
  10.  
  11.         if not location:
  12.             location = self.location or self
  13.        
  14.         # Handle a single-char sub.
  15.         def replace_char(match):
  16.             m = match.group(1)
  17.             caps = m.isupper()
  18.             m=m.lower().lstrip("%")
  19.  
  20.             if m == 'd':
  21.                 # The direct object, this.
  22.                 return capstr(self.name) if caps else self.name
  23.             elif m == 'n':
  24.                 # The enactor, player.
  25.                 if player:
  26.                     return capstr(player.name) if caps else player.name
  27.                 else:
  28.                     return "#-1 NO PLAYER"
  29.             elif m == 'i':
  30.                 # The indirect object, secondary target of an action. Usually None.
  31.                 if iobj:
  32.                     return capstr(iobj.name) if caps else iobj.name
  33.                 return ""
  34.             elif m == 'l':
  35.                 # Location
  36.                 if location:
  37.                     return capstr(location.name) if caps else location.name
  38.                 return ""
  39.             elif m == 'a':
  40.                 # area/zone
  41.                 if location:
  42.                     area = location.db.area
  43.                     if area:
  44.                         return capstr(area.full_name()) if caps else area.full_name()
  45.                 return ""
  46.             elif m == 't':
  47.                 # Typename e.g. place, object, character, exit, etc. Possibly useful?
  48.                 return capstr(self.typename) if caps else self.typename.lower()
  49.             elif m == 'z':
  50.                 # toplevel area/zone
  51.                 if location:
  52.                     area = location.db.area
  53.                     if area:
  54.                         return capstr(area.toplevel_area().db_key) if caps else area.toplevel_area().db_key
  55.                 return ""
  56.             elif m == '#':
  57.                 # dbref of the direct object.
  58.                 return self.dbref
  59.             elif m == 'c':
  60.                 # outermost container (usually a room)
  61.                 r = room(self)
  62.                 if r:
  63.                     return capstr(r.name) if caps else r.name
  64.                 return "#-1"
  65.             elif m in "opqrs":
  66.                 # Pronouns
  67.                 pro = self.pronoun(f"p{m}")
  68.                 if pro:
  69.                     return capstr(pro) if caps else pro
  70.                 return ""
  71.  
  72.             # Not a substitution. Return as-is.
  73.             return match.group(0)
  74.  
  75.         # Handle attribute sub.
  76.         def replace_var(match):
  77.             m = match.group(1).lower()
  78.  
  79.             if m.startswith("db."):
  80.                 m = m.lstrip("db.")
  81.                 if self.attributes.has(m):
  82.                     return str(self.attributes.get(m))
  83.                 else:
  84.                     return ""
  85.             elif hasattr(self, m):
  86.                 a = getattr(self, m)
  87.                 return str(a) if a else ""
  88.             else:
  89.                 return ""
  90.  
  91.             # return match.group(0)
  92.  
  93.         # Handle method sub.
  94.         def replace_fun(match):
  95.             m = match.group(1).lower()
  96.             try:
  97.                 fun = getattr(self, m)
  98.             except:
  99.                 fun = None
  100.  
  101.             if fun and callable(fun):
  102.                 return str(fun())
  103.  
  104.             return match.group(0)
  105.  
  106.         # Single character subs.
  107.         #regexp = re.compile(r"(%\w)+")
  108.         regexp = re.compile(r"(%[A-Za-z#!]+)")
  109.         out = regexp.sub(replace_char, template)
  110.  
  111.         # Attribute sub.
  112.         regexp = re.compile(r"%\(([A-Za-z0-9_.]+)\)")
  113.         out = regexp.sub(replace_var, out)
  114.  
  115.         #regexp = re.compile(r"%p\([a-zA-Z]+\)")
  116.         #out = regexp.sub(replace_player, template)
  117.  
  118.         # Method sub.
  119.         #regexp = re.compile(r"%[fF]\((\w+)\)")
  120.         #out = regexp.sub(replace_fun, out)
  121.  
  122.         return out
  123.  
  124.  
  125.     def list_messages(self, player):
  126.  
  127.         # Get all the messages defined for this type of object.
  128.         messages = getattr(MESS, f"{self.typename.upper()}_MESSAGES", None)
  129.         if not messages:
  130.             player.msg("No messages have been configured for that type of object.")
  131.             return
  132.  
  133.         # Loop through all of the defined messages for this object type.
  134.         player.msg(f"Messages on |C{self.name_and_dbref}|n:")
  135.         for x in messages:
  136.             # First, attempt to find the message in a db attr on the object.
  137.             atr = self.attributes.get(x, None)
  138.             default = False
  139.             if not atr:
  140.                 # Get the message from the defaults defined in messages.py.
  141.                 atr = getattr(MESS, x.upper(), None)
  142.                 default = True
  143.  
  144.             # Display the message.
  145.             player.msg(f"  |w{x}|n: {atr if atr else ''} {'[|Cdefault|n]' if default and atr else ''}")
  146.  
  147.         # Display the "end of data" marker.
  148.         player.msg("---")
  149.  
  150.  
  151.     def generate_message(self, message, caller, **kwargs):
  152.         # Grab the msg attribute either from the object or the messages module.
  153.         a = self.attributes.get(message, None) or getattr(MESS, message.upper(), None)
  154.         if not a:
  155.             return
  156.  
  157.         # Override loc?
  158.         loc = kwargs.get("location", caller.location)
  159.  
  160.         # Define an indirect object?
  161.         iobj = kwargs.get("iobj", None)
  162.  
  163.         # Don't show o-msgs?
  164.         no_omsg = kwargs.get("no_omsg", False)
  165.  
  166.         # O-msgs are the versions of the message intended to show others in the same location. Most of the time
  167.         # we want both but there are some occasions where the targeted version is all we want.
  168.         if not no_omsg:            
  169.             msg = f"{caller.name} {self.pronoun_sub(a, caller, iobj=iobj, location=loc)}"
  170.             msg and loc.msg_contents(msg, exclude=[caller], **{"from_obj":caller})
  171.         else:
  172.             msg = self.pronoun_sub(a, caller, iobj=iobj, location=loc)
  173.             msg and caller.msg(msg)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement