From a45ddf84fe80d95cd06621591558900b263e876e Mon Sep 17 00:00:00 2001 From: Dan Feeney Date: Tue, 20 Sep 2016 00:43:19 -0500 Subject: [PATCH] override the default `msg_contents` on ContribRPRoom to accept a `mapping` of format tokens to objects which will have `get_display_name` called --- evennia/contrib/rpsystem.py | 43 ++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/evennia/contrib/rpsystem.py b/evennia/contrib/rpsystem.py index 02e8363e6e..485ec38c5f 100644 --- a/evennia/contrib/rpsystem.py +++ b/evennia/contrib/rpsystem.py @@ -1342,7 +1342,48 @@ class ContribRPRoom(ContribRPObject): """ Dummy inheritance for rooms. """ - pass + def msg_contents(self, message, exclude=None, from_obj=None, mapping=None, **kwargs): + """ + Emits a message to all objects inside this object. + + Args: + message (str): Message to send. + exclude (list, optional): A list of objects not to send to. + from_obj (Object, optional): An object designated as the + "sender" of the message. See `DefaultObject.msg()` for + more info. + mapping (dict, optional): A dictionary of token-object + pairs allowng display name substitution. See Notes + + Kwargs: + Keyword arguments will be passed on to `obj.msg()` for all + messaged objects. + + Notes: + The `mapping` argument is required if `message` contains + {}-style format syntax. The keys of `mapping` should match + named format tokens, and its values will have their + `get_display_name()` function called for each object in + the room before substitution. + + Example: + Say char is a Character object and npc is an NPC object. + + char.location.msg_contents( + "{attacker} attacks {defender}", + mapping=dict(attacker=char, defender=npc), + exclude=(char, npc)) + """ + contents = self.contents + if exclude: + exclude = make_iter(exclude) + contents = [obj for obj in contents if obj not in exclude] + for obj in contents: + if mapping: + substitutions = {t: sub.get_display_name(obj) for t, sub in mapping.items()} + obj.msg(message.format(**substitutions), from_obj=from_obj, **kwargs) + else: + obj.msg(message, from_obj=from_obj, **kwargs) class ContribRPCharacter(DefaultCharacter, ContribRPObject):