Пример #1
0
    def login_user(self, p: packet.LoginPacket):
        username, password = p.payloads[0].value, p.payloads[1].value
        if not models.User.objects.filter(username=username):
            self.outgoing.append(
                packet.DenyPacket("I don't know anybody by that name"))
            return

        user = models.User.objects.get(username=username)
        player = models.Player.objects.get(user=user)

        if self.server.is_logged_in(player.pk):
            self.outgoing.append(
                packet.DenyPacket(
                    f"{username} is already inhabiting this realm."))
            return

        if not pbkdf2.verify_password(user.password, password):
            self.outgoing.append(packet.DenyPacket("Incorrect password"))
            return

        # The user exists in the database so retrieve the player and entity objects
        self.username = user.username
        self.player_info = player
        self.player_instance = models.InstancedEntity.objects.get(
            entity=self.player_info.entity)
        self.player_instance = self.server.instances[self.player_instance.pk]

        self.outgoing.append(packet.OkPacket())
        self.move_rooms(self.player_instance.room.id)
    def _GETLOGIN(self, p: packet.LoginPacket):
        username: str = p.payloads[0].value
        if username in self.users.keys():
            self.sendPacket(packet.DenyPacket("username already taken"))
            return
        self.sendPacket(packet.OkPacket())
        self.username = username
        self.users[username] = self

        for name, protocol in self.users.items():
            protocol.sendPacket(
                packet.ChatPacket(f"{self.username} has arrived!"))

        self.state = self._CHAT
Пример #3
0
    def register_user(self, p: packet.RegisterPacket):
        username, password = p.payloads[0].value, p.payloads[1].value

        if models.User.objects.filter(username=username):
            self.outgoing.append(
                packet.DenyPacket("Somebody else already goes by that name"))
            return

        password = pbkdf2.hash_password(password)

        # Save the new user
        user = models.User(username=username, password=password)
        try:
            user.save()
        except DataError as e:
            self.outgoing.append(packet.DenyPacket("Error. Value too long."))
            return

        # Create and save a new entity
        entity = models.Entity(typename='Player', name=username)
        entity.save()

        # Create and save a new instance
        initial_room = models.Room.objects.first()
        if not initial_room:
            self.outgoing.append(
                packet.DenyPacket("Error. Please try again later."))
            raise ObjectDoesNotExist(
                "Initial room not loaded. Did you run manage.py loaddata data.json?"
            )
        instance = models.InstancedEntity(entity=entity,
                                          room=initial_room,
                                          y=0,
                                          x=0)
        instance.save()

        # Create and save a new container (inventory)
        container = models.Container()
        container.save()

        # Create and save a new player
        player = models.Player(user=user, entity=entity, inventory=container)
        player.save()

        # adding instance to server
        self.server.instances[instance.pk] = instance

        self.outgoing.append(packet.OkPacket())
Пример #4
0
    def logout(self, p: packet.LogoutPacket):
        username = p.payloads[0].value
        if username == self.username:
            # Tell our client it's OK to log out
            self.outgoing.append(packet.OkPacket())

            # tell everyone we're leaving
            if self.player_instance:
                self.broadcast(packet.GoodbyePacket(self.player_instance.pk))

            self.logged_in = False
            self.player_instance = None
            self.player_info = None
            self.roommap = None
            self.username = ""
            self.visible_instances = set()
            self.state = self.GET_ENTRY

            if self.actionloop:
                self.server.remove_deferred(self.actionloop)
                self.actionloop = None
Пример #5
0
    def move_rooms(self, dest_roomid: Optional[int]):
        print(f"\nmove_rooms(dest_roomid={dest_roomid})\n")

        if self.logged_in:
            # Tell people in the current (old) room we are leaving
            self.broadcast(packet.GoodbyePacket(self.player_instance.pk))

            # Reset visible entities (so things don't "follow" us between rooms)
            self.visible_instances = set()

        self.logged_in = True

        # Tell our client we're ready to switch rooms so it can reinitialise itself and wait for data again.
        self.outgoing.append(packet.MoveRoomsPacket(dest_roomid))

        # Move db instance to the new room
        self.player_instance.room_id = dest_roomid

        room = self.player_instance.room
        self.roommap = maps.Room(room.pk, room.name, room.file_name)

        self.outgoing.append(packet.OkPacket())
        self.establish_player_in_room()