Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MessageMixins(object):
- def pronoun_sub(self, template, player, iobj=None, location=None):
- if not template:
- return ""
- if not player:
- player = self
- if not location:
- location = self.location or self
- # Handle a single-char sub.
- def replace_char(match):
- m = match.group(1)
- caps = m.isupper()
- m=m.lower().lstrip("%")
- if m == 'd':
- # The direct object, this.
- return capstr(self.name) if caps else self.name
- elif m == 'n':
- # The enactor, player.
- if player:
- return capstr(player.name) if caps else player.name
- else:
- return "#-1 NO PLAYER"
- elif m == 'i':
- # The indirect object, secondary target of an action. Usually None.
- if iobj:
- return capstr(iobj.name) if caps else iobj.name
- return ""
- elif m == 'l':
- # Location
- if location:
- return capstr(location.name) if caps else location.name
- return ""
- elif m == 'a':
- # area/zone
- if location:
- area = location.db.area
- if area:
- return capstr(area.full_name()) if caps else area.full_name()
- return ""
- elif m == 't':
- # Typename e.g. place, object, character, exit, etc. Possibly useful?
- return capstr(self.typename) if caps else self.typename.lower()
- elif m == 'z':
- # toplevel area/zone
- if location:
- area = location.db.area
- if area:
- return capstr(area.toplevel_area().db_key) if caps else area.toplevel_area().db_key
- return ""
- elif m == '#':
- # dbref of the direct object.
- return self.dbref
- elif m == 'c':
- # outermost container (usually a room)
- r = room(self)
- if r:
- return capstr(r.name) if caps else r.name
- return "#-1"
- elif m in "opqrs":
- # Pronouns
- pro = self.pronoun(f"p{m}")
- if pro:
- return capstr(pro) if caps else pro
- return ""
- # Not a substitution. Return as-is.
- return match.group(0)
- # Handle attribute sub.
- def replace_var(match):
- m = match.group(1).lower()
- if m.startswith("db."):
- m = m.lstrip("db.")
- if self.attributes.has(m):
- return str(self.attributes.get(m))
- else:
- return ""
- elif hasattr(self, m):
- a = getattr(self, m)
- return str(a) if a else ""
- else:
- return ""
- # return match.group(0)
- # Handle method sub.
- def replace_fun(match):
- m = match.group(1).lower()
- try:
- fun = getattr(self, m)
- except:
- fun = None
- if fun and callable(fun):
- return str(fun())
- return match.group(0)
- # Single character subs.
- #regexp = re.compile(r"(%\w)+")
- regexp = re.compile(r"(%[A-Za-z#!]+)")
- out = regexp.sub(replace_char, template)
- # Attribute sub.
- regexp = re.compile(r"%\(([A-Za-z0-9_.]+)\)")
- out = regexp.sub(replace_var, out)
- #regexp = re.compile(r"%p\([a-zA-Z]+\)")
- #out = regexp.sub(replace_player, template)
- # Method sub.
- #regexp = re.compile(r"%[fF]\((\w+)\)")
- #out = regexp.sub(replace_fun, out)
- return out
- def list_messages(self, player):
- # Get all the messages defined for this type of object.
- messages = getattr(MESS, f"{self.typename.upper()}_MESSAGES", None)
- if not messages:
- player.msg("No messages have been configured for that type of object.")
- return
- # Loop through all of the defined messages for this object type.
- player.msg(f"Messages on |C{self.name_and_dbref}|n:")
- for x in messages:
- # First, attempt to find the message in a db attr on the object.
- atr = self.attributes.get(x, None)
- default = False
- if not atr:
- # Get the message from the defaults defined in messages.py.
- atr = getattr(MESS, x.upper(), None)
- default = True
- # Display the message.
- player.msg(f" |w{x}|n: {atr if atr else ''} {'[|Cdefault|n]' if default and atr else ''}")
- # Display the "end of data" marker.
- player.msg("---")
- def generate_message(self, message, caller, **kwargs):
- # Grab the msg attribute either from the object or the messages module.
- a = self.attributes.get(message, None) or getattr(MESS, message.upper(), None)
- if not a:
- return
- # Override loc?
- loc = kwargs.get("location", caller.location)
- # Define an indirect object?
- iobj = kwargs.get("iobj", None)
- # Don't show o-msgs?
- no_omsg = kwargs.get("no_omsg", False)
- # O-msgs are the versions of the message intended to show others in the same location. Most of the time
- # we want both but there are some occasions where the targeted version is all we want.
- if not no_omsg:
- msg = f"{caller.name} {self.pronoun_sub(a, caller, iobj=iobj, location=loc)}"
- msg and loc.msg_contents(msg, exclude=[caller], **{"from_obj":caller})
- else:
- msg = self.pronoun_sub(a, caller, iobj=iobj, location=loc)
- msg and caller.msg(msg)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement