[sorrows-mudlib] r202 committed - Clean-up of viewed object description creation. These descriptions ar...

0 views
Skip to first unread message

sorrows...@googlecode.com

unread,
Feb 6, 2011, 6:08:56 AM2/6/11
to sorrows-mud...@googlegroups.com
Revision: 202
Author: richard.m.tew
Date: Sun Feb 6 03:07:58 2011
Log: Clean-up of viewed object description creation. These descriptions
are now used in the output of the look command.
http://code.google.com/p/sorrows-mudlib/source/detail?r=202

Added:
/trunk/games/room - simple/world/util.py
Modified:
/trunk/games/room - simple/world/container.py
/trunk/games/room - simple/world/room.py
/trunk/mudlib/commands/look.py
/trunk/mudlib/services/parser.py

=======================================
--- /dev/null
+++ /trunk/games/room - simple/world/util.py Sun Feb 6 03:07:58 2011
@@ -0,0 +1,47 @@
+import game.world
+
+class ViewedObject(object):
+ """
+ Helper class that can be used as a formatting argument.
+
+ This object when passed into a formatting string, can be referred to in
+ different ways through the use of attributes on it.
+ """
+
+ def __init__(self, actor=None, viewer=None, object=None, verb=None):
+ self._actor = actor
+ self._viewer = viewer
+ self._object = object
+ self._verb = verb
+
+ def __getattr__(self, attrName):
+ """
+ s - Short description.
+ S - Short description (capitalised).
+ v - Verb.
+ """
+
+ if attrName in ("s", "S"):
+ if self._object is self._viewer:
+ if attrName == "S":
+ return "You"
+ return "you"
+
+ # If the object is a body, use their short description
directly.
+ text = self._object.shortDescription
+ if isinstance(self._object, game.world.Body):
+ return text
+ # If the viewer is the actor, use the description with "the"
article.
+ if self._viewer is self._actor:
+ return "the "+ text
+ # If the viewer is not the actor, use the description
with "a"/"an" article.
+ if text[0] in ("a", "e", "i", "o", "u"):
+ return "an "+ text
+ return "a "+ text
+
+ if attrName == "v":
+ if self._object is self._viewer:
+ return self._verb
+ return self._verb +"s"
+
+ raise AttributeError("%s instance has no attribute '%s'" %
(self.__class__.__name__, attrName))
=======================================
--- /trunk/games/room - simple/world/container.py Wed Feb 24 00:17:49 2010
+++ /trunk/games/room - simple/world/container.py Sun Feb 6 03:07:58 2011
@@ -1,8 +1,8 @@
-from game.world import Object
-
-class Container(Object):
+import game.world
+
+class Container(game.world.Object):
def __init__(self):
- Object.__init__(self)
+ game.world.Object.__init__(self)

self.contents = []

@@ -13,9 +13,15 @@
self.contents.remove(ob)

def LookString(self, viewer):
- s = Object.LookString(self, viewer)
+ s = game.world.Object.LookString(self, viewer)
if len(self.contents):
- contentsString = ", ".join(ob.shortDescription for ob in
self.contents)
+ contentsString = ""
+ for idx, ob in enumerate(self.contents):
+ if len(self.contents) > 1 and idx == len(self.contents)-1:
+ contentsString += " and "
+ elif idx > 0:
+ contentsString += ", "
+ contentsString
+= "{0.s}".format(game.world.ViewedObject(viewer=viewer, object=ob))
else:
contentsString = "Nothing"
s += "\r\nIt contains: "+ contentsString +"."
=======================================
--- /trunk/games/room - simple/world/room.py Sat Feb 5 05:17:11 2011
+++ /trunk/games/room - simple/world/room.py Sun Feb 6 03:07:58 2011
@@ -1,53 +1,9 @@
import weakref

from mudlib import util
+from game.world import ViewedObject
from game.world import Container, Body, Object

-class ViewedObject(object):
- """
- Helper class that can be used as a formatting argument.
-
- This object when passed into a formatting string, can be referred to in
- different ways through the use of attributes on it.
- """
-
- def __init__(self, actor, viewer, object_, verb=None):
- self._actor = actor
- self._viewer = viewer
- self._object = object_
- self._verb = verb
-
- def __getattr__(self, attrName):
- """
- s - Short description.
- S - Short description (capitalised).
- v - Verb.
- """
-
- if attrName in ("s", "S"):
- if self._object is self._viewer:
- if attrName == "S":
- return "You"
- return "you"
-
- # If the object is a body, use their short description
directly.
- text = self._object.shortDescription
- if isinstance(self._object, Body):
- return text
- # If the viewer is the actor, use the description with "the"
article.
- if self._viewer is self._actor:
- return "the "+ text
- # If the viewer is not the actor, use the description
with "a"/"an" article.
- if text[0] in ("a", "e", "i", "o", "u"):
- return "an "+ text
- return "a "+ text
-
- if attrName == "v":
- if self._object is self._viewer:
- return self._verb
- return self._verb +"s"
-
- raise AttributeError("%s instance has no attribute '%s'" %
(self.__class__.__name__, attrName))

class Room(Container):
def __init__(self):
@@ -82,9 +38,9 @@
for i, arg in enumerate(args):
if type(arg) is tuple:
object, verb = arg
- l[i] = ViewedObject(context.body, viewer, object, verb)
+ l[i] = ViewedObject(actor=context.body, viewer=viewer,
object=object, verb=verb)
elif isinstance(arg, Object):
- l[i] = ViewedObject(context.body, viewer, arg)
+ l[i] = ViewedObject(actor=context.body, viewer=viewer,
object=arg)
else:
l[i] = arg
return l
@@ -112,20 +68,20 @@
self.anArticleObject = Object("apple")

# ViewedObject(ACTOR, VIEWER, OBJECT)
- self.bodyIndirectPerspective = ViewedObject(self.viewingBody,
self.viewingBody, self.viewedBody, "verb")
- self.bodyDirectPerspective = ViewedObject(self.viewingBody,
self.viewingBody, self.viewingBody, "verb")
+ self.bodyIndirectPerspective =
ViewedObject(actor=self.viewingBody, viewer=self.viewingBody,
object=self.viewedBody, verb="verb")
+ self.bodyDirectPerspective = ViewedObject(actor=self.viewingBody,
viewer=self.viewingBody, object=self.viewingBody, verb="verb")

def testObjectArticle(self):
# Watching someone 'take a sword' should result in display of "a
sword".
- aPerspective = ViewedObject(self.viewedBody, self.viewingBody,
self.aArticleObject, "verb")
+ aPerspective = ViewedObject(actor=self.viewedBody,
viewer=self.viewingBody, object=self.aArticleObject, verb="verb")
self.assertEqual(aPerspective.s, "a sword")

# Watching someone 'take an apple' should result in display of "an
apple".
- anPerspective = ViewedObject(self.viewedBody, self.viewingBody,
self.anArticleObject, "verb")
+ anPerspective = ViewedObject(actor=self.viewedBody,
viewer=self.viewingBody, object=self.anArticleObject, verb="verb")
self.assertEqual(anPerspective.s, "an apple")

# Seeing yourself 'take an apple' should result in display of "the
apple".
- thePerspective = ViewedObject(self.viewedBody, self.viewedBody,
self.anArticleObject, "verb")
+ thePerspective = ViewedObject(actor=self.viewedBody,
viewer=self.viewedBody, object=self.anArticleObject, verb="verb")
self.assertEqual(thePerspective.s, "the apple")

def testActorName(self):
=======================================
--- /trunk/mudlib/commands/look.py Wed Feb 24 00:17:49 2010
+++ /trunk/mudlib/commands/look.py Sun Feb 6 03:07:58 2011
@@ -11,3 +11,7 @@
@staticmethod
def syntax_SUBJECT(context, smatches):
context.user.Tell(smatches[0].LookString(context.body))
+
+ @staticmethod
+ def syntax_at_SUBJECT(context, smatches):
+ context.user.Tell(smatches[0].LookString(context.body))
=======================================
--- /trunk/mudlib/services/parser.py Sat Feb 5 05:46:07 2011
+++ /trunk/mudlib/services/parser.py Sun Feb 6 03:07:58 2011
@@ -95,13 +95,17 @@

from game.world import Container

- commandName = context.commandClass.__name__
argString = context.argString
failures = []
+
+ # Check each of the syntaxes that the command supports.
+ commandName = context.commandClass.__name__
for funcName, tokens in self.syntaxByCommand[commandName]:
- args = [ context ]
-
- if len(tokens) == 1:
+ args = []
+
+ if len(tokens) == 1 or len(tokens) == 2:
+ # verb OBJECT / verb preposition OBJECT
+
# Incorrect arguments automatically generates a usage
string.
if argString == "":
failures.append((tokens, ""))
@@ -130,6 +134,7 @@
elif token == "STRING":
args.append(argString)
elif len(tokens) == 3:
+ # OBJECT preposition CONTAINER
preposition = tokens[1]
if preposition.lower() != preposition:
continue
@@ -178,7 +183,7 @@
context.user.Tell("Case %d: no handling for %s" %
(len(tokens), funcName))
continue

- getattr(context.commandClass, funcName)(*args)
+ getattr(context.commandClass, funcName)(context, *args)
break
else:
if len(failures):

Reply all
Reply to author
Forward
0 new messages