def __init__(self, name, componentDict,output=1): if isinstance(componentDict, dict): self.components = ComponentDict() for item in componentDict: self.components[item] = componentDict[item] elif not isinstance(componentDict, ComponentDict): raise TypeError else: self.components = componentDict self.output = output super(CraftedItem,self).__init__(name)
class CraftedItem(Item): def __init__(self, name, componentDict,output=1): if isinstance(componentDict, dict): self.components = ComponentDict() for item in componentDict: self.components[item] = componentDict[item] elif not isinstance(componentDict, ComponentDict): raise TypeError else: self.components = componentDict self.output = output super(CraftedItem,self).__init__(name) def create(self, count, debug=False): if not isinstance(count,int): raise TypeError # Create a new dictionary populated only with the components # required to craft the object required_items = dict.fromkeys(self.components.keys(),0) # Make sure we have a multiple of the minimum output while (count % self.output) != 0: count += 1 # Number of 'crafting' actions to create the total output: iterations = count/self.output for component in required_items: required_items[component] = self.components[component] * iterations return (count, required_items) def getRequirementsFor(self, count, recurse=True, debug=False): true_count,components = self.create(count, debug) output = "In order to make %d %s, you will need \n" % (true_count, self.name) for component in components: output += "\t%d\t%s\n" % (components[component],component) if recurse: if isinstance(component,CraftedItem): subcount,subcomponents = component.create(components[component], debug) output += "\t\tRequires:\n" for sub in subcomponents: output += "\t\t%d\t%s\n" % (subcomponents[sub],sub) return output