From fadf3933aff1cee8db6d39253d178bf031efe25c Mon Sep 17 00:00:00 2001 From: Greg Taylor Date: Tue, 17 Jul 2007 14:39:10 +0000 Subject: [PATCH] Move the presentation part of the 'look' command out to the new scripting system. You can now make different scripted objects appear vastly different by overriding basicobject's return_appearance (or any of its submethods). Also fixed word wrapping, and we are now wrapping descriptions to 78 characters by default. We'll see how gracefully it handles this, and we might eventually apply it to all output. --- apps/objects/models.py | 13 ++++++++----- commands/general.py | 36 +++++------------------------------- functions_general.py | 19 ++++++------------- scripts/basicobject.py | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 49 deletions(-) diff --git a/apps/objects/models.py b/apps/objects/models.py index 7c8c65e8c4..75bd6dca76 100755 --- a/apps/objects/models.py +++ b/apps/objects/models.py @@ -93,6 +93,7 @@ class Object(models.Model): zone = models.ForeignKey('self', related_name="obj_zone", blank=True, null=True) home = models.ForeignKey('self', related_name="obj_home", blank=True, null=True) type = models.SmallIntegerField(choices=defines_global.OBJECT_TYPES) + # TODO: Move description to an attribute.s description = models.TextField(blank=True, null=True) location = models.ForeignKey('self', related_name="obj_location", blank=True, null=True) flags = models.TextField(blank=True, null=True) @@ -301,22 +302,24 @@ class Object(models.Model): self.description = new_desc self.save() - def get_description(self, no_parsing=False, wrap_width=False): + def get_description(self, no_parsing=False, wrap_text=True): """ Returns an object's ANSI'd description. """ try: + # Evaluate ANSI and stuff? if no_parsing: retval = self.description else: retval = ansi.parse_ansi(self.description) - - if wrap_width: - # TODO: Broken for some reason? Returning None. - return functions_general.word_wrap(retval, width=wrap_width) + + # Default to a 78 character wrap. + if wrap_text: + return functions_general.word_wrap(retval) else: return retval except: + # No description attribute present, return empty string. return "" def get_flags(self): diff --git a/commands/general.py b/commands/general.py index f6b770307c..b6193f9d25 100644 --- a/commands/general.py +++ b/commands/general.py @@ -112,37 +112,11 @@ def cmd_look(cdat): if not target_obj: return - retval = "%s\r\n%s" % ( - target_obj.get_name(), - target_obj.get_description(), - ) - session.msg(retval) - - con_players = [] - con_things = [] - con_exits = [] - - for obj in target_obj.get_contents(): - if obj.is_player(): - if obj != pobject and obj.is_connected_plr(): - con_players.append(obj) - elif obj.is_exit(): - con_exits.append(obj) - else: - con_things.append(obj) - - if con_players: - session.msg("%sPlayers:%s" % (ansi.ansi["hilite"], ansi.ansi["normal"],)) - for player in con_players: - session.msg('%s' %(player.get_name(),)) - if con_things: - session.msg("%sContents:%s" % (ansi.ansi["hilite"], ansi.ansi["normal"],)) - for thing in con_things: - session.msg('%s' %(thing.get_name(),)) - if con_exits: - session.msg("%sExits:%s" % (ansi.ansi["hilite"], ansi.ansi["normal"],)) - for exit in con_exits: - session.msg('%s' %(exit.get_name(),)) + # SCRIPT: Get the item's appearance from the scriptlink. + session.msg(target_obj.get_scriptlink().return_appearance({ + "target_obj": target_obj, + "pobject": pobject + })) # SCRIPT: Call the object's script's a_desc() method. target_obj.get_scriptlink().a_desc(pobject) diff --git a/functions_general.py b/functions_general.py index 11b90e9cc8..38293e23ae 100644 --- a/functions_general.py +++ b/functions_general.py @@ -1,3 +1,4 @@ +import textwrap from twisted.python import log import session_mgr @@ -132,17 +133,9 @@ def announce_all(message, with_ann_prefix=True): def word_wrap(text, width=78): """ - A word-wrap function that preserves existing line breaks - and most spaces in the text. Expects that existing line - breaks are posix newlines (\n). - - Function originally by Mike Brown + Wrap text to a certain number of characters. + + text: (str) The text to wrap. + width: (int) The number of characters to wrap to. """ - return reduce(lambda line, word, width=width: '%s%s%s' % - (line, - ' \n'[(len(line)-line.rfind('\n')-1 - + len(word.split('\n',1)[0] - ) >= width)], - word), - text.split(' ') - ) + return '\r\n'.join(textwrap.wrap(text, width)) diff --git a/scripts/basicobject.py b/scripts/basicobject.py index f3fcaf8119..ea5ffe356b 100644 --- a/scripts/basicobject.py +++ b/scripts/basicobject.py @@ -3,6 +3,8 @@ This will be the base object type/interface that all scripts are derived from by default. It will have the necessary outline for developers to sub-class and override. """ +import ansi + class BasicObject: def __init__(self, source_obj): """ @@ -22,6 +24,42 @@ class BasicObject: #print "SCRIPT TEST: %s looked at %s." % (actor, self.source_obj) pass + def return_appearance(self, values): + target_obj = values["target_obj"] + pobject = values["pobject"] + retval = "\r\n%s\r\n%s" % ( + target_obj.get_name(), + target_obj.get_description(), + ) + + con_players = [] + con_things = [] + con_exits = [] + + for obj in target_obj.get_contents(): + if obj.is_player(): + if obj != pobject and obj.is_connected_plr(): + con_players.append(obj) + elif obj.is_exit(): + con_exits.append(obj) + else: + con_things.append(obj) + + if con_players: + retval += "\n\r%sPlayers:%s" % (ansi.ansi["hilite"], ansi.ansi["normal"],) + for player in con_players: + retval +='\n\r%s' %(player.get_name(),) + if con_things: + retval += "\n\r%sContents:%s" % (ansi.ansi["hilite"], ansi.ansi["normal"],) + for thing in con_things: + retval += '\n\r%s' %(thing.get_name(),) + if con_exits: + retval += "\n\r%sExits:%s" % (ansi.ansi["hilite"], ansi.ansi["normal"],) + for exit in con_exits: + retval += '\n\r%s' %(exit.get_name(),) + + return retval + def a_get(self, actor): """ Perform this action when someone uses the GET command on the object.