Exemplo n.º 1
0
    def _modify_ingredients(self, ingredient_type: str, inventory: Inventory,
                            *argv: Ingredient):
        # check input
        if ingredient_type in ("Bun", "Patty", "Wrap"):
            total_amount = 0
            for ingredient in argv:
                total_amount += ingredient.amount
                # if more than max limit, reject
                if ingredient_type in self._max_limit.keys():
                    if (total_amount + self.getSumInMain(ingredient_type)
                        ) > self._max_limit[ingredient_type]:
                        print(
                            f"{ingredient_type} are more than the max amount!")
                        self._errors[
                            ingredient_type] = f"{ingredient_type} are more than the max amount!"
                        return
                if not inventory.is_available(ingredient.name,
                                              ingredient.amount):
                    print(f"{ingredient.name} is not enough in the inventory!")
                    self._errors[
                        ingredient.
                        name] = f"{ingredient.name} is not enough in the inventory!"
                    return
                self._ingredients[ingredient_type][
                    ingredient.name] = Ingredient(
                        ingredient.name,
                        ingredient.amount,
                        additional_price=ingredient.additional_price)
                ## At this stage we should remove the error from the list
                self._errors.pop(ingredient_type, None)
                self._errors.pop(ingredient.name, None)
                total_amount = 0
        elif ingredient_type == "Other":
            for ingredient in argv:
                # if more than max limit, reject
                if ingredient.name in self._max_limit.keys():
                    if ingredient.amount > self._max_limit[ingredient.name]:
                        print(
                            f"{ingredient.name} are more than the max amount!")
                        return f"{ingredient.name} are more than the max amount!"
                if not inventory.is_available(ingredient.name,
                                              ingredient.amount):
                    print(f"{ingredient.name} is not enough in the inventory!")
                    self._errors[
                        ingredient.
                        name] = f"{ingredient.name} is not enough in the inventory!"
                    return
                self._ingredients[ingredient_type][
                    ingredient.name] = Ingredient(
                        ingredient.name,
                        ingredient.amount,
                        additional_price=ingredient.additional_price)
                self._errors.pop(ingredient.name, None)
        else:
            print("invalid ingredient type!")

        self.calculate_price()
Exemplo n.º 2
0
 def update_order_inventory(self, inventory: Inventory):
     for items_list in self.items.values():
         for item in items_list:
             for ingredient_list in item._ingredients.values():
                 if ingredient_list.__class__.__name__ == "Ingredient":
                     inventory.update_stock(ingredient_list.name,
                                            -ingredient_list.amount)
                 else:
                     for ingredient in ingredient_list.values():
                         assert (
                             ingredient.__class__.__name__ == "Ingredient")
                         if not isNaN(ingredient.amount):
                             inventory.update_stock(ingredient.name,
                                                    -ingredient.amount)
Exemplo n.º 3
0
 def setup_method(self):
     self.inventory = Inventory()
     self.inventory.add_ingredient("Patty", 1, 11)
     self.inventory.add_ingredient("Falafel", 1, 11)
     self.inventory.add_ingredient("Cheese", 1, 11)
     self.inventory.add_ingredient("Tomato", 1, 11)
     self.inventory.add_ingredient("Carrot", 1, 11)
     self.inventory.add_ingredient("Lettuce", 1, 11)
     self.inventory.add_ingredient("Chilli", 1, 11)
     self.inventory.add_ingredient("Beef Patty", 1, 11)
     self.inventory.add_side("Fruit", 1, 11)
     self.inventory.add_side("Fries", 1, 11)
     self.inventory.add_side("Cake", 1, 11)
     self.inventory.add_drink("Juice", 1, 11)
     self.inventory.add_drink("Water", 1, 11)
     self.inventory.add_drink("Soda", 1, 11)
Exemplo n.º 4
0
 def _check_availability(self, inventory: Inventory):
     for ingredient in self._ingredients.values():
         if ingredient.amount == 0:
             self._is_available = False
             return
         else:
             if (not isNaN(ingredient.amount)
                     and not inventory.is_available(ingredient.name,
                                                    ingredient.amount)):
                 self._is_available = False
                 return
     self._is_available = True
Exemplo n.º 5
0
	def __init__(self, world):
		self.image = None
		self.gore = None
		self.position = Vector2(0,0)
		self.world = world
		self.group = None
		self.ai = AIController()
		self.abilities = [Ability(self, Ability.lookup["Attack"])]
		self.singular = 'unknown'
		self.sight = 5
		self.health = self.maxHealth = 30
		self.inventory = Inventory(self, world.items)
		self.deathTimer = 50
		self.lastDamage = 0
		self.hostile = False
		self.lastAttacker = None
		self.name = None
		self.action = None
		self.lastTarget = None
		self.observers = []
		self.quickcast = None
Exemplo n.º 6
0
 def setup_method(self):
     self.inventory = Inventory()
Exemplo n.º 7
0
class TestUpdateItemInInventory():
    def setup_method(self):
        self.inventory = Inventory()
        self.inventory.add_ingredient("Patty", 1, 11)
        self.inventory.add_ingredient("Falafel", 1, 11)
        self.inventory.add_ingredient("Cheese", 1, 11)
        self.inventory.add_ingredient("Tomato", 1, 11)
        self.inventory.add_ingredient("Carrot", 1, 11)
        self.inventory.add_ingredient("Lettuce", 1, 11)
        self.inventory.add_ingredient("Chilli", 1, 11)
        self.inventory.add_ingredient("Beef Patty", 1, 11)
        self.inventory.add_side("Fruit", 1, 11)
        self.inventory.add_side("Fries", 1, 11)
        self.inventory.add_side("Cake", 1, 11)
        self.inventory.add_drink("Juice", 1, 11)
        self.inventory.add_drink("Water", 1, 11)
        self.inventory.add_drink("Soda", 1, 11)

    def test_add_to_item(self):
        #add to an ingredient
        self.inventory.update_item("Patty", 10)
        assert self.inventory.ingredients[0].total_quantity == 21

        #add to a side
        self.inventory.update_item("Fruit", 10)
        assert self.inventory.sides[0].total_quantity == 21

        #add to a drink
        self.inventory.update_item("Water", 10)
        assert self.inventory.drinks[1].total_quantity == 21

    def test_remove_from_item(self):
        #add to an ingredient
        self.inventory.update_item("Carrot", -10)
        assert self.inventory.ingredients[4].total_quantity == 1

        #add to a side
        self.inventory.update_item("Cake", -10)
        assert self.inventory.sides[2].total_quantity == 1

        #add to a drink
        self.inventory.update_item("Juice", -10)
        assert self.inventory.drinks[0].total_quantity == 1

    def test_remove_too_much_from_item(self):
        with pytest.raises(QuantityError):
            self.inventory.update_item("Cheese", -20)
            self.inventory.update_item("Fries", -20)
            self.inventory.update_item("Soda", -20)

    def test_remove_non_integer(self):
        with pytest.raises(ItemQuantityError):
            self.inventory.update_item("Cheese", -2.5)
            self.inventory.update_item("Fries", -2.50)
            self.inventory.update_item("Soda", -2.60)

    def test_get_item(self):
        item = self.inventory.get_ing("Cheese")
        assert item.name == "Cheese"
Exemplo n.º 8
0
class TestAddItemToInventory():
    def setup_method(self):
        self.inventory = Inventory()

    #add ingredient
    def test_add_ingredient_with_limit(self):
        self.inventory.add_ingredient("Patty", 2, 50, 2)
        assert len(self.inventory.ingredients) == 1

    #add ingredient
    def test_add_ingredient_without_limit(self):
        self.inventory.add_ingredient("Patty", 2, 50)
        assert len(self.inventory.ingredients) == 1

    #add side
    def test_add_side(self):
        self.inventory.add_side("Fries", 1, 10)
        assert len(self.inventory.sides) == 1

    #add drink
    def test_add_drink(self):
        self.inventory.add_drink("Juice", 3, 10)
        assert len(self.inventory.drinks) == 1

    #   As the same function is at the beginning of each ingredient, side,
    #   and drink; the tests to check for name, price, quantity, and limit
    #   errors have been split among the three

    #add ingredient with invalid names
    def test_add_ingredient_invalid_names(self):
        with pytest.raises(ItemNameError):
            self.inventory.add_ingredient(10, 1, 1)
        with pytest.raises(ItemNameError):
            self.inventory.add_ingredient("1Patty", 1, 1)

    #add sides with invalid price
    def test_add_side_invalid_price(self):
        with pytest.raises(ItemPriceError):
            self.inventory.add_side("Fries", -5, 1)

    #add side with invalid quantity
    def test_add_side_invalid_quantity(self):
        with pytest.raises(ItemQuantityError):
            self.inventory.add_side("Fries", 1, -1)

    #add drink with invalid limit
    def test_add_drink_invalid(self):
        with pytest.raises(ItemLimitError):
            self.inventory.add_drink("Juice", 1, 10, 0)
            self.inventory.add_drink("Juice", 1, 0, -5)
Exemplo n.º 9
0
def create_inventory():
    inventory = Inventory()
    inventory.add_ingredient("Patty", 1, 11)
    inventory.add_ingredient("Falafel", 1, 11)
    inventory.add_ingredient("Cheese", 1, 11)
    inventory.add_ingredient("Tomato", 1, 11)
    inventory.add_ingredient("Carrot", 1, 11)
    inventory.add_ingredient("Lettuce", 1, 11)
    inventory.add_ingredient("Chilli", 1, 11)
    inventory.add_side("Fruit", 1, 11)
    inventory.add_side("Fries", 1, 11)
    inventory.add_side("Cake", 1, 11)
    inventory.add_drink("Juice", 1, 11)
    inventory.add_drink("Water", 1, 11)
    inventory.add_drink("Soda", 1, 11)
    return inventory
def generate_inventory():
    '''
    init a inventory
    '''
    inventory = Inventory()
    '''
    mains
    '''
    fish_patties = Patties("fish_patties", 2, 40, "mains")
    chicken_patties = Patties("chicken_patties", 1.5, 50, "mains")
    tomato = Ingredient("tomato", 0.5, 300, "mains")
    buns = Buns("buns", 0.5, 400, "buns")
    lettuce = Ingredient("lettuce", 0.5, 100, "mains")
    '''
    sides
    '''
    salad = Sides_inventory("salad", 10000, "sides", 0, 0, 0)
    fries = Sides_inventory("fries", 10000, "sides", 0, 0, 0)
    '''
    drinks
    '''
    can_coca = Can_drink("can_coca", 2.5, 100, "drinks")
    bottle_coca = Bottle_drink("bottle_coca", 3.5, 100, "drinks")

    coca = Drinks_inventory("coca", 30000, "drinks", 0, 0, 0)
    orange = Drinks_inventory("orangejuice", 20000, "drinks", 0, 0, 0)
    sprite = Drinks_inventory("sprite", 35000, "drinks", 0, 0, 0)
    '''
    sundaes
    '''
    chocolate = Sundae_inventory("chocolate", 10000, "sundaes", 0, 0, 0)
    strawberry = Sundae_inventory("strawberry", 10000, "sundaes", 0, 0, 0)
    '''
    add food item to inventory
    '''
    inventory.add_food(can_coca)
    inventory.add_food(salad)
    inventory.add_food(fries)
    inventory.add_food(fish_patties)
    inventory.add_food(chicken_patties)
    inventory.add_food(bottle_coca)
    inventory.add_food(tomato)
    inventory.add_food(lettuce)
    inventory.add_food(buns)
    inventory.add_food(coca)
    inventory.add_food(orange)
    inventory.add_food(sprite)
    inventory.add_food(chocolate)
    inventory.add_food(strawberry)
    return inventory
Exemplo n.º 11
0
class Entity:

	living = "player"
	dead = "body"
	damage = "damage"
	step = "step.wav"
	ouch = "ouch.wav"
	
	def __init__(self, world):
		self.image = None
		self.gore = None
		self.position = Vector2(0,0)
		self.world = world
		self.group = None
		self.ai = AIController()
		self.abilities = [Ability(self, Ability.lookup["Attack"])]
		self.singular = 'unknown'
		self.sight = 5
		self.health = self.maxHealth = 30
		self.inventory = Inventory(self, world.items)
		self.deathTimer = 50
		self.lastDamage = 0
		self.hostile = False
		self.lastAttacker = None
		self.name = None
		self.action = None
		self.lastTarget = None
		self.observers = []
		self.quickcast = None

	def move(self, direction):

		x, y = direction
		oldx, oldy = self.position

		newx = floor(oldx + x)
		newy = floor(oldy + y)

		if not self.world.isLocationPassable(Vector2(newx, newy)):
			return False

		self.position = Vector2(newx, newy)
		
		return True

	def update(self):
		
		done = True
		
		if self.action is not None:
			done = self.action.update()
			if done:
				self.action = None
				
		return done
			

			
	def draw(self, screen, camPos, images, visible):

		tileSize = 32
		relx = self.position[0] - camPos[0]  
		rely = self.position[1] - camPos[1] 
		position = (((relx + 8)*tileSize), ((rely + 8) * tileSize))

		if visible(self.position):
			screen.blit(self.getImage(images), position)
	
		if self.action is not None:
			self.action.draw(screen, camPos, visible)
	
	def getImage(self, images):
			if self.health == 0:
				return images.get(self.__class__.dead, self.position)
			elif time() - self.lastDamage < 0.2:
				return images.get(self.__class__.damage, self.position)
			else:
				return images.get(self.__class__.living, self.position)

	def save(self):
		data = {}
		data['type'] = self.__class__.__name__
		data['position'] = Vector2.save(self.position)
		data['health'] = self.health
		data['deathTimer'] = self.deathTimer
		data['hostile'] = self.hostile
		data['name'] = self.name
		data['inventory'] = self.inventory.save()
		abi = []
		for ability in self.abilities:
			abi.append(ability.save())
		data['abilities'] = abi
		data['ai'] = self.ai.save()
			
		return data

	def load(self, data):
		if 'position' in data.keys():
			self.position = Vector2.load(data['position'])
		if 'health' in data.keys():
			self.health = data['health']
		if 'deathTimer' in data.keys():
			self.deathTimer = data['deathTimer']
		if 'hostile' in data.keys():
			self.hostile = data['hostile']
		if 'name' in data.keys():
			self.name = data['name']
		if 'inventory' in data.keys():
			self.inventory.load(data['inventory'])
		if 'abilities' in data.keys():
			self.abilities = []
			for ability in data['abilities']:
				toAdd = Ability(self)
				toAdd.load(ability)
				self.abilities.append(toAdd)
		if 'ai' in data.keys():
			self.ai.load(data['ai'])
			
	def turn(self):

		for ability in self.abilities:
			ability.update()
	
		if self.health > 0:
			if self.group is not None:
				if self.distance(self.getLeader().position) > 20:
					self.teleportToLeader()
			self.regen()
			self.ai.act()
		else:			
			self.deathTimer -= 1
		
	def inChunk(self, position):
		
		
		posX = position[0] << 4
		posY = position[1] << 4
		
		mobX = self.position[0]
		mobY = self.position[1]
		
		if mobX < posX:
			return False
		
		if mobX > posX + 16:
			return False
		
		if mobY < posY:
			return False
		
		if mobY > posY + 16:
			return False
		
		return True
		
		
	def canSpawn(self, position):
		
		tile = self.world.getTile(position)
		ground = tile.getGround()
		if not ground.passable:
			return False
		
		if not ground.spawnable:
			return False
		
		return True
	
	def partyCanSee(self, position):
		if self.group is None:
			return self.canSee(position)
			
		return self.group.canSee(position)
	
	def canSee(self, target):
		
		relx = abs(self.position[0] - target[0])
		rely = abs(self.position[1] - target[1])

		if relx > self.sight or rely > self.sight:
			return False
		
		if(relx * relx + rely * rely > self.sight * self.sight):
			return False

		if self.world.obscured(self.position, target):
			return False
		
		return True
		
	def inflict(self, attacker, damage):
		
		startHealth = self.health
		
		self.hostile = True
		self.lastAttacker = attacker
		self.health -= damage
		
		if self.health <= 0:
			self.kill()
			
		endHealth = self.health
		
		damageDealt = startHealth - endHealth
		self.world.log.append(attacker.getName() + " hit " + self.getName() + " for " + str(damage) + " damage!")
		
		self.lastDamage = time()
		self.__class__.onDamage(self.world.sounds)
		return damageDealt
	
	def heal(self, healer, amount):
		if not self.isAlive():
			return 0
		
		startHealth = self.health
		self.health += amount
		
		if self.health > self.maxHealth:
			self.health = self.maxHealth
		
		healingDone = self.health - startHealth
		self.world.log.append(healer.getName() + " healed " + self.getName() + " for " + str(healingDone) + "!")
		return healingDone
		
	def kill(self):
		self.world.log.append(self.getName() + ' Died!')
		self.health = 0
		
	def revive(self, reviver):
		
		if self.health != 0:
			return
		
		self.health = 1
		self.world.log.append(self.getName() + ' resurrected by ' + reviver.getName())
		
		
	def distance(self, location):
		relx = abs(self.position[0] - location[0])
		rely = abs(self.position[1] - location[1])
		
		return int(sqrt(relx**2 + rely**2))
	
	def acquireTarget(self):
		
		for e in self.getEnemies():
			if self.canSee(e.position):
				return e
			
	def getAttackable(self):
		
		for e in self.getEnemies():
			if e.isAlive() and self.canSee(e.position) and self.canHit(e.position) :
				return e
			
	def getEnemies(self):
		if self in self.world.friendly:
			return self.world.mobManager.mobs
		else:
			return self.world.friendly
	
	def getFriends(self):
		if self in self.world.friendly:
			return self.world.friendly
		else:
			return self.world.mobManager.mobs
		
	def canHit(self, location, attackRange):
		pos = Vector2(self.position[0], self.position[1])
		pos.center()
		target = Vector2(location)
		target.center()
		pos -= target
		return pos.inRange(attackRange)
			
	def isAlive(self):
		return self.health > 0
	
	def regen(self):
		if self.health >= self.maxHealth:
			return
		
		if randint(0,5) is not 0:
			return
		
		self.health += 1
		
	def setGroup(self, group):
		self.group = group
		
		
	def getLeader(self):
		return self.group.getLeader() if self.group is not None else None
		
	def teleportToLeader(self):
		
		leader = self.getLeader()
		x, y = leader.position
		r = 1
		while(not self.world.isLocationPassable((x, y))):
			x = randint(-r,r) + leader.position[0]
			y = randint(-r,r) + leader.position[1]
			if r is 10:
				print "Failed to find a place to teleport to"
				return
			r += 1
			
		self.position = Vector2(x, y)


	def getName(self):
		if self.name is None:
			return self.__class__.__name__
		else:
			return self.name

	
	def getAction(self):

		if len(self.abilities) == 0:
			return None
		
		for ability in self.abilities:
			
			for e in self.world.getAllEntities():
				
				if not ability.ready():
					continue
				
				if not ability.valid(e):
					continue
				
				return ability.cast(e.position) #ability instance
	
	
	def pocket(self, item):
		self.inventory.pocket(item)
	
	def equip(self):
		pass
		
	def notify(self, event):
		for obs in self.observers:
			obs.notify(self, event)
		
	def __str__(self):
		return self.__class__.__name__ + 'Pos: ' + str(self.position) + ' ' + 'HP: ' + str(self.health)

	@classmethod
	def onDamage(cls, sounds):
		sounds.get("damage.wav").play()
Exemplo n.º 12
0
 def __init__(self):
     self._temp_orders = []
     self._current_orders = []
     self._completed_orders = []
     self._inventory = Inventory()
     self._standard = []
Exemplo n.º 13
0
                 availability=True):
        super().__init__(name, price, "Sides", description, availability)


class Drink(Item):
    def __init__(self,
                 name: str,
                 price: float,
                 description='N/A',
                 availability=True):
        super().__init__(name, price, "Drinks", description, availability)


class Sundae(Item):
    def __init__(self,
                 name: str,
                 price: float,
                 description='N/A',
                 availability=True):
        super().__init__(name, price, "Sundaes", description, availability)


if __name__ == "__main__":
    invent = Inventory()
    invent.add_new_ingredients(Ingredient("Veg Bun", amount=10))
    big_mac = Burger("Big Mac")
    big_mac.add_ingredients("Bun", Ingredient("Veg Bun", additional_price=1))
    # big_mac.set_ingredient_limit("Veg Bun", 0)
    big_mac.modify_buns(invent,
                        Ingredient("Veg Bun", amount=1, additional_price=1))
    print(big_mac)