Example #1
0
    def get_character_actions(
            self,
            character: "CharacterModel") -> typing.List[CharacterActionLink]:
        available_cheats = self._get_available_cheats(character)
        action_links: typing.List[CharacterActionLink] = []

        if "increase_ap" in available_cheats:
            action_links.append(
                CharacterActionLink(
                    name="(Triche) S'ajouter des PA",
                    link=get_character_action_url(
                        character_id=character.id,
                        action_type=ActionType.CHEATS,
                        action_description_id=self._description.id,
                        query_params={"cheat_id": "increase_ap"},
                    ),
                ))

        if "reduce_tiredness" in available_cheats:
            action_links.append(
                CharacterActionLink(
                    name="(Triche) Réduire la fatigue",
                    link=get_character_action_url(
                        character_id=character.id,
                        action_type=ActionType.CHEATS,
                        action_description_id=self._description.id,
                        query_params={"cheat_id": "reduce_tiredness"},
                    ),
                ))

        return action_links
Example #2
0
 def get_base_url(self, character: "CharacterModel") -> str:
     return get_character_action_url(
         character_id=character.id,
         action_type=ActionType.BUILD,
         action_description_id=self._description.id,
         query_params={},
     )
Example #3
0
    def get_character_actions(
            self,
            character: "CharacterModel") -> typing.List[CharacterActionLink]:
        character_actions: typing.List[CharacterActionLink] = []
        accept_resources_ids = [
            rd.id for rd in self._description.properties["accept_resources"]
        ]

        for resource in self._kernel.game.world_manager.get_resource_on_or_around(
                world_row_i=character.world_row_i,
                world_col_i=character.world_col_i,
                zone_row_i=character.zone_row_i,
                zone_col_i=character.zone_col_i,
                material_type=self._kernel.game.config.liquid_material_id,
        ):
            if resource.id in accept_resources_ids:
                query_params = self.input_model(resource_id=resource.id)
                character_actions.append(
                    CharacterActionLink(
                        name=f"Drink {resource.name}",
                        link=get_character_action_url(
                            character_id=character.id,
                            action_type=ActionType.DRINK_RESOURCE,
                            action_description_id=self._description.id,
                            query_params=self.input_model_serializer.dump(
                                query_params),
                        ),
                        cost=self.get_cost(character),
                    ))

        return character_actions
Example #4
0
    def perform(self, character: "CharacterModel",
                input_: input_model) -> Description:
        character_doc = self._character_lib.get_document(character.id)
        resource, resource_extraction_description, cost_per_unit = self._get_resource_and_cost(
            character_doc, input_)

        if input_.quantity is None:
            unit_name = self._kernel.translation.get(resource.unit)

            return Description(
                title=f"Récupérer du {resource.name}",
                items=[
                    Part(
                        is_form=True,
                        form_values_in_query=True,
                        form_action=get_character_action_url(
                            character_id=character.id,
                            action_type=ActionType.COLLECT_RESOURCE,
                            action_description_id=self._description.id,
                            query_params=self.input_model_serializer.dump(
                                input_),
                        ),
                        items=[
                            Part(
                                label=
                                f"Quantité (coût: {cost_per_unit} par {unit_name}) ?",
                                type_=Type.NUMBER,
                                name="quantity",
                            )
                        ],
                    )
                ],
            )

        self._kernel.resource_lib.add_resource_to(
            character_id=character_doc.id,
            resource_id=input_.resource_id,
            quantity=input_.quantity,
            commit=False,
        )

        cost = self.get_cost(character, input_=input_)
        if cost is None:
            raise RollingError("Cost compute should not be None !")

        self._kernel.character_lib.reduce_action_points(character.id,
                                                        cost,
                                                        commit=False)
        self._kernel.server_db_session.commit()

        return Description(
            title=
            f"{input_.quantity} {self._kernel.translation.get(resource.unit)} récupéré",
            footer_links=[
                Part(is_link=True,
                     go_back_zone=True,
                     label="Retourner à l'écran de déplacements")
            ],
        )
Example #5
0
 def _get_url(self,
              character: "CharacterModel",
              input_: typing.Optional[LearnKnowledgeModel] = None) -> str:
     return get_character_action_url(
         character_id=character.id,
         action_type=ActionType.LEARN_KNOWLEDGE,
         query_params=self.input_model_serializer.dump(input_)
         if input_ else {},
         action_description_id=self._description.id,
     )
Example #6
0
 def get_character_actions(
     self, character: "CharacterModel"
 ) -> typing.List[CharacterActionLink]:
     return [
         CharacterActionLink(
             name=f"Commencer {self._description.name}",
             link=get_character_action_url(
                 character_id=character.id,
                 action_type=ActionType.BEGIN_STUFF_CONSTRUCTION,
                 action_description_id=self._description.id,
                 query_params={},
             ),
             cost=self.get_cost(character),
             group_name=self._description.properties["link_group_name"],
         )
     ]
Example #7
0
    def get_character_actions(
        self, character: "CharacterModel"
    ) -> typing.List[CharacterActionLink]:
        try:
            self.check_is_possible(character)
        except ImpossibleAction:
            return []

        return [
            CharacterActionLink(
                name=self._description.name,
                link=get_character_action_url(
                    character_id=character.id,
                    action_type=ActionType.SEARCH_FOOD,
                    action_description_id=self._description.id,
                    query_params={},
                ),
                group_name="Chercher de la nourriture",
                cost=self.get_cost(character),
            )
        ]
Example #8
0
    def get_character_actions(
        self, character: "CharacterModel"
    ) -> typing.List[CharacterActionLink]:
        try:
            self.check_is_possible(character)
        except ImpossibleAction:
            pass

        build_id = self._description.properties["build_id"]
        build_description = self._kernel.game.config.builds[build_id]
        return [
            CharacterActionLink(
                name=build_description.name,
                link=get_character_action_url(
                    character_id=character.id,
                    action_type=ActionType.BEGIN_BUILD,
                    action_description_id=self._description.id,
                    query_params={},
                ),
                cost=self.get_cost(character, input_=None),
            )
        ]
Example #9
0
    def get_character_actions(
            self,
            character: "CharacterModel") -> typing.List[CharacterActionLink]:
        inspect_zone_positions = get_on_and_around_coordinates(
            character.zone_row_i, character.zone_col_i)
        character_actions: typing.List[CharacterActionLink] = []

        for row_i, col_i in inspect_zone_positions:
            for resource in self._kernel.game.world_manager.get_resources_at(
                    world_row_i=character.world_row_i,
                    world_col_i=character.world_col_i,
                    zone_row_i=row_i,
                    zone_col_i=col_i,
            ):
                tile_type = self._kernel.tile_maps_by_position[(
                    character.world_row_i,
                    character.world_col_i)].source.geography.rows[row_i][col_i]
                query_params = self.input_model(resource_id=resource.id,
                                                row_i=row_i,
                                                col_i=col_i)
                character_actions.append(
                    CharacterActionLink(
                        name=f"Récupérer {resource.name} sur {tile_type.name}",
                        link=get_character_action_url(
                            character_id=character.id,
                            action_type=ActionType.COLLECT_RESOURCE,
                            action_description_id=self._description.id,
                            query_params=self.input_model_serializer.dump(
                                query_params),
                        ),
                        cost=None,
                        merge_by=(ActionType.COLLECT_RESOURCE, resource.id),
                        group_name="Ramasser du matériel ou des ressources",
                    ))

        return character_actions
Example #10
0
    def perform(self, character: "CharacterModel", input_: BeginStuffModel) -> Description:
        if not input_.description:
            require_txts = []
            for consume in self._description.properties["consume"]:
                if "resource" in consume:
                    resource_id = consume["resource"]
                    resource_description = self._kernel.game.config.resources[resource_id]
                    quantity_str = quantity_to_str(
                        consume["quantity"], resource_description.unit, self._kernel
                    )
                    require_txts.append(f"{quantity_str} de {resource_description.name}")

                elif "stuff" in consume:
                    stuff_id = consume["stuff"]
                    stuff_properties = self._kernel.game.stuff_manager.get_stuff_properties_by_id(
                        stuff_id
                    )
                    require_txts.append(f"{consume['quantity']} de {stuff_properties.name}")

            return Description(
                title=f"Commencer {self._description.name}",
                items=[
                    Part(
                        is_form=True,
                        form_values_in_query=True,
                        form_action=get_character_action_url(
                            character_id=character.id,
                            action_type=ActionType.BEGIN_STUFF_CONSTRUCTION,
                            query_params={},
                            action_description_id=self._description.id,
                        ),
                        items=[Part(text="Consommera :")]
                        + [Part(text=txt) for txt in require_txts]
                        + [
                            Part(
                                label=f"Vous pouvez fournir une description de l'objet",
                                type_=Type.STRING,
                                name="description",
                                default_value=self._description.properties["default_description"],
                            )
                        ],
                    )
                ],
            )

        for consume in self._description.properties["consume"]:
            if "resource" in consume:
                resource_id = consume["resource"]
                self._kernel.resource_lib.reduce_carried_by(
                    character.id,
                    resource_id=resource_id,
                    quantity=consume["quantity"],
                    commit=False,
                )

            elif "stuff" in consume:
                stuff_id = consume["stuff"]
                carried_stuffs = self._kernel.stuff_lib.get_carried_by(
                    character.id, stuff_id=stuff_id
                )
                for i in range(consume["quantity"]):
                    self._kernel.stuff_lib.destroy(carried_stuffs[i].id, commit=False)

        stuff_id = self._description.properties["produce_stuff_id"]
        stuff_properties = self._kernel.game.stuff_manager.get_stuff_properties_by_id(stuff_id)
        stuff_doc = self._kernel.stuff_lib.create_document_from_stuff_properties(
            properties=stuff_properties,
            world_row_i=character.world_row_i,
            world_col_i=character.world_col_i,
            zone_row_i=character.zone_row_i,
            zone_col_i=character.zone_col_i,
        )
        stuff_doc.description = input_.description or ""
        stuff_doc.carried_by_id = character.id
        stuff_doc.ap_spent = 0.0
        stuff_doc.ap_required = self._description.properties["craft_ap"]
        stuff_doc.under_construction = True
        self._kernel.stuff_lib.add_stuff(stuff_doc, commit=False)
        self._kernel.character_lib.reduce_action_points(
            character.id, cost=self.get_cost(character), commit=False
        )
        self._kernel.server_db_session.commit()

        return Description(
            title=f"{stuff_properties.name} commencé",
            footer_links=[
                Part(is_link=True, go_back_zone=True, label="Retourner à l'écran de déplacements"),
                Part(
                    is_link=True,
                    label="Voir l'objet commencé",
                    form_action=DESCRIBE_LOOK_AT_STUFF_URL.format(
                        character_id=character.id, stuff_id=stuff_doc.id
                    ),
                    classes=["primary"],
                ),
            ],
            force_back_url=f"/_describe/character/{character.id}/on_place_actions",
        )