Example #1
0
    def test_creationEvent(self):
        """
        When a new L{Thing} is created via L{ImaginaryWorld.create}, its
        addition to L{ImaginaryWorld.origin} is broadcast to that location.
        """
        store = Store()
        world = ImaginaryWorld(store=store)
        observer = world.create(u"observer")

        # There really needs to be a way to get into the event dispatch
        # system.  It's so hard right now that I'm not even going to try,
        # instead I'll look at some strings that get written to a transport.
        observingPlayer = Player(observer)
        transport = StringTransport()
        observingPlayer.setProtocol(PlayerProtocol(transport))

        # Make another thing for the observer to watch the creation of.
        world.create(u"foo")

        self.assertEquals(transport.value(), "Foo arrives.\n")
Example #2
0
 def play(self, character):
     self.player = Player(character)
     self.player.setProtocol(self)
     self.world.loggedIn(character)
     self._prepareDisplay()
     return 'COMMAND'
Example #3
0
class CharacterSelectionTextServer(TextServerBase):
    """
    L{CharacterSelectionTextServer} presents a simple text menu for selecting
    or creating a character and then enters the selected or created character
    into an Imaginary simulation.

    @ivar motd: Some text which will be displayed at connection setup time.

    @ivar role: The L{Role} which will own any new characters created.

    @ivar world: The L{ImaginaryWorld} which the selected character will be
        entered into.

    @ivar choices: A list of L{Thing}s which represent existing characters
        which may be selected.
    """

    motd = file(util.sibpath(resources.__file__, 'motd')).read() % {
        'pythonVersion': sys.version,
        'twistedVersion': tcopyright.version,
        'imaginaryVersion': imaginaryVersion}

    state = 'SELECT'

    def __init__(self, role, world, choices):
        self.role = role
        self.world = world
        self.choices = choices


    def connectionMade(self):
        TextServerBase.connectionMade(self)
        self.terminal.reset()
        self.write(self.motd)
        self.write('Choose a character: \n')
        self.write('  0) Create\n')
        for n, actor in enumerate(self.choices):
            self.write('  %d) %s\n' % (n + 1, actor.name.encode('utf-8')))
        self.write('> ')


    def line_SELECT(self, line):
        which = int(line)
        if which == 0:
            self.write('Name? ')
            return 'USERNAME'
        else:
            return self.play(self.choices[which - 1])


    def play(self, character):
        self.player = Player(character)
        self.player.setProtocol(self)
        self.world.loggedIn(character)
        self._prepareDisplay()
        return 'COMMAND'


    def line_USERNAME(self, line):
        """
        Handle a username supplied in response to a prompt for one when
        creating a new character.

        This will create a user with the name given by C{line}, made available
        to the role indicated by C{self.role}, and entered into play.
        """
        actor = self.world.create(line)
        self.role.shareItem(actor)
        return self.play(actor)