def receive_resources(self, resources): """ add the resources to the village create a new object resources with this values and return it """ max_resources = self.max_resources_storage() transfered_resources = Resources( food=min( resources.food, max_resources.food - int(self.resources.food), ), wood=min( resources.wood, max_resources.wood - int(self.resources.wood), ), silex=min( resources.silex, max_resources.silex - int(self.resources.silex), ), skin=min( resources.skin, max_resources.skin - int(self.resources.skin), ), ) #need to create a new instance of resources to remember for the report transfered_resources.pk = None transfered_resources.save() self.resources += transfered_resources self.resources.save() self.plan_next_starvation() return transfered_resources
def create_tribe(name, user, carte): """ Initialise une tribe (à utiliser après la création d'un compte) """ tribe = Tribe(name=name, leader=user) tribe.save() resources_initiales = Resources( wood=INIT_TRIBE_RESOURCES['wood'], food=INIT_TRIBE_RESOURCES['food'], silex=INIT_TRIBE_RESOURCES['silex'], skin=INIT_TRIBE_RESOURCES['skin'] ) resources_initiales.save() village = Village.create_village(tribe, first=True) inhabitants = Group.objects.create( position=village.position, village=village, ) village.inhabitants = inhabitants for key in INIT_TRIBE_UNITS: new_pile = UnitStack( unit_type=UnitType.objects.get(identifier__iexact=key), group=inhabitants, number=INIT_TRIBE_UNITS[key], ) new_pile.save() village.update_income() return tribe
def take_resources(self, village_or_group, resources_dict): """ Substract the max resources up to resources_dict content to the village_or_group’s resources and set it in a new resources object which is returned """ for key in resources_dict: resources_dict[key] = max(resources_dict[key], 0) available_resources = village_or_group.resources wanted_resources = Resources.dict_to_resources(resources_dict) if available_resources >= wanted_resources: available_resources -= wanted_resources available_resources.save() wanted_resources.save() return wanted_resources else: received_resources = Resources() for key in wanted_resources: amount = min(wanted_resources[key], available_resources[key]) received_resources[key] = amount available_resources -= amount available_resources.save() received_resources.save() return received_resources()