Modified:
/trunk/games/room - simple/commands/drop.py
/trunk/games/room - simple/commands/put.py
/trunk/games/room - simple/commands/take.py
/trunk/games/room - simple/world/room.py
/trunk/mudlib/commands/say.py
/trunk/mudlib/services/parser.py
=======================================
--- /trunk/games/room - simple/commands/drop.py Thu Mar 4 22:09:46 2010
+++ /trunk/games/room - simple/commands/drop.py Sat Feb 5 05:17:11 2011
@@ -9,4 +9,4 @@
def syntax_SUBJECTB(context, matches):
for ob in matches:
ob.MoveTo(context.room)
- context.room.Message("{0.S} {0.v} {1.s}.", (context.body,
context.verb), ob)
+ context.room.Message(context, "{0.S} {0.v} {1.s}.",
(context.body, context.verb), ob)
=======================================
--- /trunk/games/room - simple/commands/put.py Thu Mar 4 22:09:46 2010
+++ /trunk/games/room - simple/commands/put.py Sat Feb 5 05:17:11 2011
@@ -10,4 +10,4 @@
container = omatches[0]
for ob in smatches:
ob.MoveTo(container)
- context.room.Message("{0.S} {0.v} {1.s} in {2.s}.",
(context.body, context.verb), ob, container)
+ context.room.Message(context, "{0.S} {0.v} {1.s} in {2.s}.",
(context.body, context.verb), ob, container)
=======================================
--- /trunk/games/room - simple/commands/take.py Thu Mar 4 22:09:46 2010
+++ /trunk/games/room - simple/commands/take.py Sat Feb 5 05:17:11 2011
@@ -9,7 +9,7 @@
def syntax_SUBJECTR(context, matches):
for ob in matches:
ob.MoveTo(context.body)
- context.room.Message("{0.S} {0.v} {1.s}.", (context.body,
context.verb), ob)
+ context.room.Message(context, "{0.S} {0.v} {1.s}.",
(context.body, context.verb), ob)
# CMD MILESTONE 2: GET ITEM FROM CONTAINER
@@ -18,7 +18,7 @@
for ob in smatches:
container = ob.container
ob.MoveTo(context.body)
- context.room.Message("{0.S} {0.v} {1.s} from {2.s}.",
(context.body, context.verb), ob, container)
+ context.room.Message(context, "{0.S} {0.v} {1.s} from {2.s}.",
(context.body, context.verb), ob, container)
# CMD MILESTONE 3: GET ITEM FROM PERSON
# CMD MILESTONE 3: TAKE ITEM FROM PERSON
=======================================
--- /trunk/games/room - simple/world/room.py Wed Feb 24 00:17:49 2010
+++ /trunk/games/room - simple/world/room.py Sat Feb 5 05:17:11 2011
@@ -3,32 +3,49 @@
from mudlib import util
from game.world import Container, Body, Object
-class ViewedActor:
- def __init__(self, actor, viewer, verb=None):
+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 SetViewer(self, viewer):
- self._viewer = viewer
-
def __getattr__(self, attrName):
- attrNameLower = attrName.lower()
-
- if attrNameLower == "s":
- if self._actor is self._viewer:
- v = "you"
- if attrNameLower != attrName:
- v = v.capitalize()
- else:
- v = self._actor.shortDescription
- return v
- elif attrNameLower == "v":
- if self._actor is self._viewer:
- v = self._verb
- else:
- v = self._verb +"s"
- return v
+ """
+ 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))
@@ -59,47 +76,65 @@
return s
- def Message(self, msgfmt, *args):
- args = list(args)
-
+ def Message(self, context, msgfmt, *args):
def SetArgsViewer(args, viewer):
+ l = list(args)
for i, arg in enumerate(args):
if type(arg) is tuple:
- actor, verb = arg
- args[i] = ViewedActor(actor, viewer, verb)
+ object, verb = arg
+ l[i] = ViewedObject(context.body, viewer, object, verb)
elif isinstance(arg, Object):
- args[i] = ViewedActor(arg, viewer)
- elif isinstance(arg, ViewedActor):
- args[i].SetViewer(viewer)
-
+ l[i] = ViewedObject(context.body, viewer, arg)
+ else:
+ l[i] = arg
+ return l
+
for ob in self.contents:
if isinstance(ob, Body):
- SetArgsViewer(args, ob)
- ob.user.Tell(msgfmt.format(*args))
+ formatArgs = SetArgsViewer(args, ob)
+ ob.user.Tell(msgfmt.format(*formatArgs))
import unittest
-class ViewedActorTest(unittest.TestCase):
+class ViewedObjectTest(unittest.TestCase):
+ longMessage = True
+
def setUp(self):
- actor = self.actor = Body(None, None)
- actor.SetShortDescription("dwarf")
-
- self.otherViewer = Body(None, None)
-
- self.va = ViewedActor(actor, self.otherViewer, "verb")
-
- def test_ActorName(self):
- self.failUnless(self.va.s == "dwarf", "Viewer does not see actor
by their short description as it is")
- self.failUnless(self.va.S == "dwarf", "Viewer does not see actor
by their short description as it is")
-
- self.va.SetViewer(self.va._actor)
-
- self.failUnless(self.va.s == "you", "Actor does not see themselves
as the lowercase 'you'")
- self.failUnless(self.va.S == "You", "Actor does not see themselves
as the capitalised 'you'")
-
- def test_Verb(self):
- self.failUnless(self.va.v == "verbs", "Viewer does not see the
verb in the correct form")
-
- self.va.SetViewer(self.va._actor)
-
- self.failUnless(self.va.v == "verb", "Actor does not see the verb
in the correct form")
+ # Viewing body.
+ self.viewingBody = Body(None, None)
+
+ # Viewed body.
+ self.viewedBody = Body(None, None)
+ self.viewedBody.SetShortDescription("dwarf")
+
+ # Viewed "a" article object / "an" article object.
+ self.aArticleObject = Object("sword")
+ 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")
+
+ def testObjectArticle(self):
+ # Watching someone 'take a sword' should result in display of "a
sword".
+ aPerspective = ViewedObject(self.viewedBody, self.viewingBody,
self.aArticleObject, "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")
+ 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")
+ self.assertEqual(thePerspective.s, "the apple")
+
+ def testActorName(self):
+ self.assertEqual(self.bodyIndirectPerspective.s, "dwarf", "Actor
does not see viewed body by the short description")
+ self.assertEqual(self.bodyIndirectPerspective.S, "dwarf", "Actor
does not see viewed body by the uncapitalised short description")
+
+ self.assertEqual(self.bodyDirectPerspective.s, "you", "Actor does
not see themselves as the lowercase 'you'")
+ self.assertEqual(self.bodyDirectPerspective.S, "You", "Actor does
not see themselves as the capitalised 'you'")
+
+ def testVerb(self):
+ self.assertEqual(self.bodyIndirectPerspective.v, "verbs", "Viewer
does not see the verb in the correct form")
+ self.assertEqual(self.bodyDirectPerspective.v, "verb", "Actor does
not see the verb in the correct form")
=======================================
--- /trunk/mudlib/commands/say.py Wed Feb 24 00:17:49 2010
+++ /trunk/mudlib/commands/say.py Sat Feb 5 05:17:11 2011
@@ -5,4 +5,4 @@
@staticmethod
def syntax_STRING(context, string):
- context.room.Message("{0.S} {0.v}: {1}.", (context.body,
context.verb), string)
+ context.room.Message(context, "{0.S} {0.v}: {1}.", (context.body,
context.verb), string)
=======================================
--- /trunk/mudlib/services/parser.py Mon Mar 22 01:50:29 2010
+++ /trunk/mudlib/services/parser.py Sat Feb 5 05:17:11 2011
@@ -5,35 +5,10 @@
distributiveDeterminers = set([ "all", "every" ])
cardinalNumberDeterminers = set([
- "one",
- "two",
- "three",
- "four",
- "five",
- "six",
- "seven",
- "eight",
- "nine",
- "ten",
- "eleven",
- "twelve",
- "thirteen",
- "fourteen",
- "fifteen",
- "sixteen",
- "seventeen",
- "eighteen",
- "nineteen",
- "twenty",
- "thirty",
- "fourty",
- "fifty",
- "sixty",
- "seventy",
- "eighty",
- "ninety",
- "hundred",
- "thousand",
+ "one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
+ "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen",
+ "seventeen", "eighteen", "nineteen", "twenty", "thirty", "fourty", "fifty",
+ "sixty", "seventy", "eighty", "ninety", "hundred", "thousand",
])
determiners = distributiveDeterminers | cardinalNumberDeterminers