예제 #1
0
class Profile:
    class ProfileDoesNotExistsError(Exception):
        pass

    pmc: ProfileModel
    scav: ProfileModel

    hideout: Hideout
    quests: Quests
    inventory: PlayerInventory
    encyclopedia: Encyclopedia
    mail: Mail

    def __init__(
        self,
        profile_dir: Path,
        profile_id: str,
        encyclopedia_factory: Callable[..., Encyclopedia],
        hideout_factory: Callable[..., Hideout],
        quests_factory: Callable[..., Quests],
        notifier_service: NotifierService,
    ):
        self.__encyclopedia_factory = encyclopedia_factory
        self.__hideout_factory = hideout_factory
        self.__quests_factory = quests_factory
        self.__notifier_service = notifier_service

        self.profile_dir = profile_dir
        self.profile_id = profile_id

        self.pmc_profile_path = self.profile_dir.joinpath("pmc_profile.json")
        self.scav_profile_path = self.profile_dir.joinpath("scav_profile.json")

    def add_insurance(self, item: Item, trader: TraderType) -> None:
        # TODO: Move this function into IInsuranceService
        self.pmc.InsuredItems.append(
            ItemInsurance(item_id=item.id, trader_id=trader.value)
        )

    def receive_experience(self, amount: int) -> None:
        self.pmc.Info.Experience += amount

    def read(self) -> None:
        if any(
            not path.exists()
            for path in (self.pmc_profile_path, self.scav_profile_path)
        ):
            raise Profile.ProfileDoesNotExistsError

        self.pmc = ProfileModel.parse_file(self.pmc_profile_path)
        self.scav = ProfileModel.parse_file(self.scav_profile_path)

        self.encyclopedia = self.__encyclopedia_factory(profile=self)
        self.inventory = PlayerInventory(profile=self)
        self.inventory.read()

        self.quests = self.__quests_factory(profile=self)

        self.hideout = self.__hideout_factory(profile=self)
        self.hideout.read()

        self.mail = Mail(profile=self, notifier_service=self.__notifier_service)
        self.mail.read()

    def write(self) -> None:
        self.hideout.write()
        self.mail.write()
        self.inventory.write()

        atomic_write(self.pmc.json(exclude_defaults=True), self.pmc_profile_path)
        atomic_write(self.scav.json(exclude_defaults=True), self.scav_profile_path)

    def update(self) -> None:
        self.hideout.update()
예제 #2
0
파일: conftest.py 프로젝트: socek/jet_py
def inventory(player_profile: Profile) -> PlayerInventory:
    inventory = PlayerInventory(player_profile)
    inventory.read()
    return inventory
예제 #3
0
파일: conftest.py 프로젝트: socek/jet_py
 def _make_inventory(inventory_path: str) -> PlayerInventory:
     target_inventory = InventoryModel.parse_file(inventory_path)
     with patch.object(player_profile.pmc, "Inventory", target_inventory):
         inventory = PlayerInventory(player_profile)
         inventory.read()
     return inventory
예제 #4
0
파일: profile.py 프로젝트: socek/jet_py
class Profile:
    # pylint: disable=too-many-instance-attributes
    # Disabling that in case of profile is reasonable

    class ProfileDoesNotExistsError(Exception):
        pass

    pmc: ProfileModel
    scav: ProfileModel

    hideout: Hideout
    quests: Quests
    inventory: PlayerInventory
    encyclopedia: Encyclopedia
    mail: Mail

    def __init__(self, profile_id: str):
        self.profile_id = profile_id

        self.profile_dir = root_dir.joinpath("resources", "profiles",
                                             profile_id)

        self.pmc_profile_path = self.profile_dir.joinpath("pmc_profile.json")
        self.scav_profile_path = self.profile_dir.joinpath("scav_profile.json")

    @staticmethod
    def exists(profile_id: str) -> bool:
        return root_dir.joinpath("resources", "profiles", profile_id).exists()

    def add_insurance(self, item: Item, trader: TraderType) -> None:
        self.pmc.InsuredItems.append(
            ItemInsurance(item_id=item.id, trader_id=trader.value))

        #  Todo remove insurance from items that aren't present in inventory after raid

    def receive_experience(self, amount: int) -> None:
        self.pmc.Info.Experience += amount

    @inject
    def read(
        self,
        notifier_service: NotifierService = Provide[
            AppContainer.notifier.service]
    ) -> None:
        if any(not path.exists()
               for path in (self.pmc_profile_path, self.scav_profile_path)):
            raise Profile.ProfileDoesNotExistsError

        self.pmc = ProfileModel.parse_file(self.pmc_profile_path)
        self.scav = ProfileModel.parse_file(self.scav_profile_path)

        self.encyclopedia = Encyclopedia(profile=self)
        self.inventory = PlayerInventory(profile=self)
        self.inventory.read()

        self.quests = Quests(profile=self)

        self.hideout = Hideout(profile=self)
        self.hideout.read()

        self.mail = Mail(self, notifier_service)
        self.mail.read()

    def write(self) -> None:
        self.hideout.write()
        self.mail.write()
        self.inventory.write()

        atomic_write(self.pmc.json(exclude_defaults=True),
                     self.pmc_profile_path)
        atomic_write(self.scav.json(exclude_defaults=True),
                     self.scav_profile_path)

    def update(self) -> None:
        self.hideout.update()

    def __enter__(self) -> Profile:
        self.read()
        return self

    def __exit__(
        self,
        exc_type: Optional[Type[BaseException]],
        exc_val: Optional[BaseException],
        exc_tb: Optional[TracebackType],
    ) -> None:
        if exc_type is None:
            self.write()