[Reality] speaking/listening interfaces

Eric Wong reality@twistedmatrix.com
Fri, 5 Sep 2003 15:09:19 -0700


--sm4nu43k4a2Rpi4c
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

Hello,

Is the mailing list back?

I'm attaching two files, speaking.py and listening.py.  Do these do the
right thing to create interfaces/adapters?  speaking.py defines a basic
interace for talking or whispering in a room, listening.py defines an
interface for listening to things said.

The latest thing that I find a little confusing is using the variable
'actor' to mean the adapter to the interface.  It probably makes perfect
sense in the component model, but it seems a little ambiguous when
talking about mud/mush/moo/virtual worlds.

How does the unit testing work?  I looked over test_reality.py and I'm
not sure how to use it (actually run the unit tests) or add new tests.

Eric


--sm4nu43k4a2Rpi4c
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="speaking.py"

"""
This is the speaking (whisper, say, tell and shout) interface/adapter
for Twisted Reality.  Things need to have the listening interface to
actually receive the message.
"""

from twisted.python import components
from reality.text import english
from reality import actions, things, listening


################################################################
################################################################ speak/say
################################################################
class Speak( actions.NoTargetAction ):

    onlyBroadcastToActor = 1

    def __init__( self, adapter, text ):
        """
        Note this takes two args (the interface adapter and the text
        spoken), even though normally NoTargetAction only takes one.
        """
        super( Speak, self ).__init__( adapter )
        self.text = text

    def formatToActor( self ):
        return "You say: ", self.text

    def doAction( self ):
        """
        Find the other listeners in the room, and tell them what
        was said.
        """
        speaker = self.actor.original
        location = speaker.location
        for o in location.findListeners( self, listening.IListenActor ):
            if o.original is not speaker:
                listening.Listen( o, speaker, self.text ).performAction()


class Speaker( components.Adapter ):
    __implements__ = ISpeakActor

components.registerAdapter( Speaker, things.Actor, ISpeakActor )


class SayParser( english.Subparser ):
    def parse_say( self, player, text ):
        adapter = player.getComponent( ISpeakActor )
        if adapter is None:
            return []
        return [ Speak( adapter, text ) ]

english.registerSubparser( SayParser() )

################################################################
################################################################ whisper
################################################################
IWhisperTarget = listening.IListenActor
class Whisper( actions.TargetAction ):

    def __init__( self, adapter, targetName, text ):
        super( Whisper, self ).__init__( adapter, targetName )
        self.text = text
        whisperer = self.actor.original
        location = whisperer.location

    def formatToActor( self ):
        return "You whisper to ", self.target, ": ", self.text

    def formatToTarget( self ):
        return self.actor, " whispers to you: ", self.text
    
    def formatToOther( self ):
        return None

    def doAction( self ):
        pass

class Whisperer( components.Adapter ):
    __implements__ = IWhisperActor

components.registerAdapter( Whisperer, things.Actor, IWhisperActor )

class WhisperParser( english.Subparser ):
    def parse_whisper( self, player, text ):
        adapter = player.getComponent( IWhisperActor )
        if adapter is None or text.find( " " ) == -1:
            return []
        (targetName, message) = text.split( None, 1 )
        return [ Whisper( adapter, targetName, message ) ]

english.registerSubparser( WhisperParser() )


--sm4nu43k4a2Rpi4c
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="listening.py"

"""
The listening interface/adapter.  Something needs to have this to
'listen' what a speaker says (see speaking.py).
"""

from twisted.python import components
from reality import actions, things

class Listen( actions.NoTargetAction ):
    onlyBroadcastToActor = 1

    def __init__( self, adapter, speaker, text ):
        super( Listen, self ).__init__( adapter )
        self.speaker = speaker
        self.text = text

    def formatToActor( self ):
        return self.speaker, " says: ", str( self.text )

    def doAction( self ):
        pass

class Listener( components.Adapter ):
    __implements__ = IListenActor

components.registerAdapter( Listener, things.Actor, IListenActor )


--sm4nu43k4a2Rpi4c--