def handle(self, player, line): # The player arg will be a dummy, since no one is logged in yet. if line.lower() == "new": player.enter_mode(AccountCreateMode(self.protocol)) return if line.lower() == "quit": self.protocol.sendLine("Bye!") self.protocol.transport.loseConnection() return if line.find(" ") == -1: # No space, but not a command we recognize. self.protocol.sendLine("Eh?") self.protocol.sendLine("Log in with your username, a space, and " "your password. Type 'new' to create an " "account, or 'quit' to disconnect.") return # Must be a login attempt (name, password) = line.split(" ", 1) try: player = db.player_by_name(name) except KeyError: # That name is unregistered self.protocol.sendLine("Invalid login.") return if player.hash(password) == player.password: # Associate this protocol with this player, dropping any existing # one. if player.name in factory.allProtocols: factory.allProtocols[player.name].transport.loseConnection() reconnect = True else: reconnect = False factory.allProtocols[player.name] = self.protocol self.protocol.player = player # Drop into normal mode with locks.authority_of(player): self.protocol.sendLine("Hello, {}!".format(player.name)) self.protocol.sendLine("") from muss.commands.world import Look # Exit LoginMode and enter NormalMode player.enter_mode(handler.NormalMode()) Look().execute(player, {"obj": player.location}) if reconnect: player.emit("{} has reconnected.".format(player.name), exceptions=[player]) else: player.emit("{} has connected.".format(player.name), exceptions=[player]) else: # Wrong password self.protocol.sendLine("Invalid login.") return
def new_player(self, name): """ Create a new player with the given name, store it in the database, and return it. """ newbie = PlayerMock(name, "password") newbie.location = self.lobby newbie.enter_mode(handler.NormalMode()) db.store(newbie) return newbie
def handle(self, player, line): # Just as in LoginMode, the player arg will be None since no one is # logged in. if line == 'cancel': player.exit_mode() # Drop back to LoginMode player.mode.greet() return if self.stage == 'name': if line.find(" ") > -1: self.protocol.sendLine("Please type only the username; it may " "not contain any spaces. Try again:") return if db.player_name_taken(line): self.protocol.sendLine("That name is already taken. If it's " "yours, type 'cancel' to log in. " "Otherwise, try another name:") return self.name = line self.protocol.sendLine( "Welcome, {}! Please enter a password.".format(self.name)) self.stage = 'password1' return elif self.stage == 'password1': self.password = line self.protocol.sendLine("Please enter it again.") self.stage = 'password2' return elif self.stage == 'password2': if self.password == line: player = db.Player(self.name, self.password) self.protocol.player = player db.store(player) factory.allProtocols[player.name] = self.protocol with locks.authority_of(player): player.enter_mode(handler.NormalMode()) self.protocol.sendLine("Hello, {}!".format(player.name)) self.protocol.sendLine("") from muss.commands.world import Look Look().execute(player, {"obj": player.location}) player.emit("{} has connected for the first time.".format( player.name), exceptions=[player]) return else: self.protocol.sendLine("Passwords don't match; try again. " "Please enter a password.") self.stage = 'password1' return
def test_destroy(self): apple_uid = self.objects["apple"].uid command = "destroy #{}".format(apple_uid) response = "You destroy #{} (apple).".format(apple_uid) self.assert_response(command, response) self.assertEqual(self.neighbor.send.call_args[0][0], "Player destroys apple.") self.assertRaises(KeyError, db.get, apple_uid) with locks.authority_of(self.objects["frog"]): handler.NormalMode().handle(self.objects["frog"], "drop hat") self.player.send_line("take hat") hat_uid = self.objects["hat"].uid self.assert_response("destroy #{}".format(hat_uid), "You cannot destroy hat.")
def handle(self, player, line): if line.startswith('/'): handler.NormalMode().handle(player, line[1:]) return for name in Chat().nospace_names: if line.startswith(name): arguments = line.split(name, 1)[1] args = Chat.args(player).parseString(arguments).asDict() Chat().execute(player, args) return if line.startswith(Emote.nospace_name): self.channel.pose(player, line[1:]) elif line.startswith(SpacelessEmote.nospace_name): self.channel.semipose(player, line[1:]) else: self.channel.say(player, line)
def handle(self, player, line): """ Check for escapes and emotes, then pass through to say. """ if line.startswith("/"): handler.NormalMode().handle(player, line[1:]) return for command in [Emote, SpacelessEmote, Chat]: for name in command().nospace_names: if line.startswith(name): arguments = line.split(name, 1)[1] args = command.args(player).parseString(arguments).asDict() command().execute(player, args) return args = Say.args(player).parseString(line).asDict() Say().execute(player, args)