Esempio n. 1
0
	def flatten(self, host_resources="", reset_conditionals=True):
		"""Runs through all the conditionals and turns them into constant values
		
		host_resources is the resources of whatever will be buying this, conditionals rely on this
		"""
		
		if host_resources.__class__ != Res_dict:
			host = Res_dict(host_resources)
		else:
			host = host_resources
		
		for r, v in self.conditionals.items():
			result = re_cache['Swappable_operands'].search(v)
			
			if result == None: continue
			
			res_id		= res_dict_f.get_id(result.groups()[0])
			op			= result.groups()[1]
			amount		= float(result.groups()[2])
			
			# If the host has it then we'll apply the change
			if host.value.get(r, 0) > 0:
				self.value[res_id] = operator[op](self.value[res_id], amount)
		
		# This stops it applying them more than once by mistake
		if reset_conditionals: self.conditionals = {}
		 
		return self
Esempio n. 2
0
	def get(self, key, default = 0):
		if type(key) == str:
			key = res_dict_f.get_id(key)
		
		try:
			v = self.value.get(key, default)
		except Exception as e:
			raise
		
		# Cast it to an int if we can for printing purposes
		if type(v) == float:
			if v == int(v):
				return int(v)
		
		# We have it, yay!
		return v
Esempio n. 3
0
	def affordable(self, cost, overbudget_list=None, verbose=False):
		"""Checks to see if you can pay the price
		
		other is a Res_dict"""
		
		# Ensure that our input is what we want
		if type(cost) != Res_dict: cost = Res_dict(cost)
		cost.flatten(self.value)
		
		# Turn overbudget into a list of integers
		if overbudget_list == None:
			overbudget_list = self.overbudget
		
		overbudget = []
		for o in overbudget_list:
			overbudget.append(res_dict_f.get_id(o))
			
		cant_afford = []
		actual_cost = {}
		
		if verbose:
			print("")
			print("%d: Cost = %s" % (lineno(), cost.value))
			print("%d: Host: %s" % (lineno(), self.value))
		
		for k, r in resource_list.data_dict.items():
			# Set this to 0 so that we don't need to worry about it later
			actual_cost[k] = 0
			
			# Are we not being billed for this or billed zero?
			if cost.get(k, 0) <= 0:
				actual_cost[k] = cost.get(k, 0)
				continue
			
			# If you are allowed to be overbudget on this resource we won't even check it
			if k in overbudget:
				if verbose:
					if self.value[k] > cost.value[k]:
						print("%d: Allowed overbudget on %s" % (lineno(), r.name))
					else:
						print("%d: Can afford %s, can go overbudget" % (lineno(), r.name))
				actual_cost[k] = cost.value[k]
				continue
			
			# We set this to zero to stop null-exception errors
			if k not in self.value:
				self.value[k] = 0
			
			# We can afford this!
			if cost.value[k] <= self.value[k]:
				if verbose:
					print("%d: Can afford %s (%s < %s)" % (lineno(), r.name, cost.value[k], self.value[k]))
				actual_cost[k] += cost.value[k]
				continue
			
			# We can't afford this :(
			if self.value[k] < cost.value[k]:
				
				if verbose:
					print("%d: Can't afford %s" % (lineno(), r.name))
				
				# All is not lost, it may be swappable!
				replacement_k = -1
				
				# Work out amounts now to save duplicating lots of lines of code
				if k not in actual_cost: actual_cost[k] = 0
				
				amount_needed = cost.value[k] - self.value[k]
				k_amount_used = cost.value[k] - amount_needed
				
				# We can't afford this outright, it may however be swappable
				for s in cost.swappables:
					temp_k = -1
					
					# Is it in this swappable block?
					if k not in s:			continue
					
					# Have we already handled it?
					if replacement_k > 0:	continue
					
					# What are we swapping with? One of them is k, the other is it's swap partner
					if s[0] == k:	temp_k = s[1]
					else:			temp_k = s[0]
					
					# temp_k is now what we're looking to swap with
					
					# First lets see if we can go overbudget on temp_k
					if temp_k in overbudget or resource_list.data_list[temp_k].name in overbudget:
						if verbose:
							print("%d: Going overbudget on %s, found in overbudget" % (lineno(),
								resource_list.data_list[temp_k].name))
						
						replacement_k = temp_k
					
					# Failing that we could just see if we can afford it
					elif self.value.get(temp_k, 0) >= amount_needed:
						if verbose:
							print("%d: Going overbudget on %s, found in we had enough spare" % (lineno(),
								resource_list.data_list[temp_k].name))
						
						replacement_k = temp_k
						
					# If we've found a replacement, we need to alter the costs
					if replacement_k > 0:
						if replacement_k not in actual_cost:
							actual_cost[replacement_k] = 0
						
						actual_cost[replacement_k] += amount_needed
						actual_cost[k] += k_amount_used
						continue
				
				# We've gotten this far, that means no solution was found
				if replacement_k < 1:
					if verbose:
						print("%d: Could not find a swap for %s, could not afford cost" % (lineno(),
							resource_list.data_list[k].name))
					return False, {}
		
		return True, actual_cost
Esempio n. 4
0
	def set(self, key, value):
		if type(key) == str:
			key = res_dict_f.get_id(key)
		self.value[key] = value
Esempio n. 5
0
	def __getitem__(self, key):
		if type(key) == str:
			return self.value[res_dict_f.get_id(key)]
		else:
			return self.value[key]