Пример #1
0
    def change_weather(self, new_weather: str):
        print(f"Weather changed from {self.weather} to {new_weather}")

        self.broadcast_to_all(packet.WeatherChangePacket(new_weather),
                              state='PLAY')
        self.weather = new_weather

        if self.weather == "Rain":
            self.broadcast_to_all(
                packet.ServerLogPacket("It has begun to rain..."),
                state='PLAY')

        elif self.weather == "Clear":
            self.broadcast_to_all(
                packet.ServerLogPacket("The rain has cleared."), state='PLAY')
Пример #2
0
    def establish_player_in_room(self):
        self.outgoing.append(
            packet.ServerModelPacket('Room',
                                     model_to_dict(self.player_instance.room)))
        self.outgoing.append(
            packet.ServerModelPacket(
                'Instance', create_dict('Instance', self.player_instance)))

        playerdict = model_to_dict(self.player_info)
        playerdict["entity"] = model_to_dict(self.player_info.entity)
        self.outgoing.append(packet.ServerModelPacket('Player', playerdict))

        self.outgoing.append(packet.WeatherChangePacket(self.server.weather))

        # send inventory to player
        items = models.ContainerItem.objects.filter(
            container=self.player_info.inventory)
        for ci in items:
            self.outgoing.append(
                packet.ServerModelPacket('ContainerItem',
                                         create_dict('ContainerItem', ci)))

        self.state = self.PLAY
        self.broadcast(packet.ServerLogPacket(f"{self.username} has arrived."))

        # Tell other players in view that we have arrived
        for proto in self.server.protocols_in_room(
                self.player_instance.room_id):
            proto.process_visible_instances()
Пример #3
0
 def save_all_instances(self):
     for key, instance in self.instances.items():
         if instance.entity.typename == 'Player':
             instance.save()
     print("Saved all player instances to DB")
     self.broadcast_to_all(packet.ServerLogPacket("Game has been saved."),
                           state='PLAY')
Пример #4
0
 def chat(self, p: packet.ChatPacket):
     """
     Broadcasts a chat message which includes this protocol's connected player name.
     Truncates to 80 characters. Cannot be empty.
     """
     message: str = p.payloads[0].value
     if message.strip() != '':
         message: str = f"{self.player_instance.entity.name} says: {message[:80]}"
         self.broadcast(packet.ServerLogPacket(message), include_self=True)
         self.logger.log(message)
Пример #5
0
    def start_gather(self, instance: models.InstancedEntity):
        node = models.ResourceNode.objects.get(entity=instance.entity)

        # check if player has required level and item (e.g. pickaxe)
        if not self.can_gather(node):
            return

        if node.entity.typename == "OreNode":
            self.outgoing.append(
                packet.ServerLogPacket("You begin to mine at the rocks."))
        elif node.entity.typename == "TreeNode":
            self.outgoing.append(
                packet.ServerLogPacket("You begin to chop at the tree."))

        if self.actionloop:
            self.server.remove_deferred(self.actionloop)
            self.actionloop = None
        self.actionloop = self.server.add_deferred(self.attempt_gather,
                                                   self.server.tickrate, True,
                                                   instance, node)
Пример #6
0
    def can_gather(self, node: models.ResourceNode) -> bool:
        requirements = {'OreNode': 'Pickaxe', 'TreeNode': 'Axe'}

        cis = models.ContainerItem.objects.filter(
            container=self.player_info.inventory,
            item__entity__typename=requirements[node.entity.typename])
        if not cis:
            self.outgoing.append(
                packet.ServerLogPacket(
                    f"You do not have a {requirements[node.entity.typename]}.")
            )
            return False

        return True
Пример #7
0
    def depart_other(self, p: packet.GoodbyePacket):
        other_instanceid: int = p.payloads[0].value
        # other_instance: models.InstancedEntity = models.InstancedEntity.objects.get(id=other_instanceid)
        other_instance = self.server.instances[other_instanceid]

        if other_instance in self.visible_instances:
            self.visible_instances.remove(other_instance)

        if other_instance.entity.typename == 'Player':
            self.outgoing.append(
                packet.ServerLogPacket(
                    f"{other_instance.entity.name} has departed."))

        self.outgoing.append(p)
Пример #8
0
    def attempt_gather(self, instance: models.InstancedEntity,
                       node: models.ResourceNode):
        # if node has already been killed (by other player)
        if instance.y == OOB:
            if self.actionloop:
                self.server.remove_deferred(self.actionloop)
                self.actionloop = None
            return

        if not self.can_gather(node):
            return

        # change change based on difficulty
        if random.randint(0, 5) == 0:
            # success
            if self.actionloop:
                self.server.remove_deferred(self.actionloop)
                self.actionloop = None

            dropitems = set(
                models.DropTableItem.objects.filter(droptable=node.droptable))
            for itm in dropitems:
                if random.randint(1, itm.chance) == 1:
                    amt = random.randint(itm.min_amt, itm.max_amt)
                    item = itm.item
                    self.add_item_to_inventory(item, amt)
                    self.outgoing.append(
                        packet.ServerLogPacket(
                            f"You acquire {amt} {item.entity.name}."))

            self.kill_instance(instance)
        else:
            # fail
            self.outgoing.append(
                packet.ServerLogPacket(f"You continue gathering."))
            pass
        pass