From 4f41582127f85e06d246ccd88a84f24dd33689e7 Mon Sep 17 00:00:00 2001 From: Vincent Le Goff Date: Mon, 27 Mar 2017 17:04:31 -0700 Subject: [PATCH] Update Object.announce_move_* to take advantage of mapping --- evennia/objects/objects.py | 73 +++++++++++++++++++++++++++++--------- 1 file changed, 57 insertions(+), 16 deletions(-) diff --git a/evennia/objects/objects.py b/evennia/objects/objects.py index 96539afce8..23eb55aef8 100644 --- a/evennia/objects/objects.py +++ b/evennia/objects/objects.py @@ -1162,7 +1162,7 @@ class DefaultObject(with_metaclass(TypeclassBase, ObjectDB)): # return has_perm(self, destination, "can_move") return True - def announce_move_from(self, destination): + def announce_move_from(self, destination, msg=None): """ Called if the move is to be announced. This is called while we are still standing in the old @@ -1170,25 +1170,51 @@ class DefaultObject(with_metaclass(TypeclassBase, ObjectDB)): Args: destination (Object): The place we are going to. + msg (str, optional): a replacement message. + + You can override this method and call its parent with a + message to simply change the default message. In the string, + you can use the following as mappings (between braces): + character: the character who is moving. + exit: the exit from which the character is moving (if found). + origin: the location of the character before the move. + destination: the location of the character after moving. """ if not self.location: return - string = "%s is leaving %s, heading for %s." - location = self.location - for obj in self.location.contents: - if obj != self: - obj.msg(string % (self.get_display_name(obj), - location.get_display_name(obj) if location else "nowhere", - destination.get_display_name(obj))) + if msg: + string = msg + else: + string = "{character} is leaving {origin}, heading for {destination}." - def announce_move_to(self, source_location): + location = self.location + exits = [o for o in location.contents if o.location is location and o.destination is destination] + mapping = { + "character": self, + "exit": exits[0] if exits else "somwhere", + "origin": location or "nowhere", + "destination": destination or "nowhere", + } + + location.msg_contents(string, exclude=(self, ), mapping=mapping) + + def announce_move_to(self, source_location, msg=None): """ Called after the move if the move was not quiet. At this point we are standing in the new location. Args: source_location (Object): The place we came from + msg (str, optional): the replacement message if location. + + You can override this method and call its parent with a + message to simply change the default message. In the string, + you can use the following as mappings (between braces): + character: the character who is moving. + exit: the exit from which the character is moving (if found). + origin: the location of the character before the move. + destination: the location of the character after moving. """ @@ -1199,13 +1225,28 @@ class DefaultObject(with_metaclass(TypeclassBase, ObjectDB)): self.location.msg(string) return - string = "%s arrives to %s%s." - location = self.location - for obj in self.location.contents: - if obj != self: - obj.msg(string % (self.get_display_name(obj), - location.get_display_name(obj) if location else "nowhere", - " from %s" % source_location.get_display_name(obj) if source_location else "")) + if source_location: + if msg: + string = msg + else: + string = "{character} arrives to {destination} from {origin}." + else: + string = "{character} arrives to {destination}." + + origin = source_location + destination = self.location + exits = [] + if origin: + exits = [o for o in destination.contents if o.location is destination and o.destination is origin] + + mapping = { + "character": self, + "exit": exits[0] if exits else "somewhere", + "origin": origin or "nowhere", + "destination": destination or "nowhere", + } + + destination.msg_contents(string, exclude=(self, ), mapping=mapping) def at_after_move(self, source_location): """