def balance(self): s = Stockpile(allowNegative=True) for gainType in self.gain: s.deposit(self.gain[gainType].toList()) for spendType in self.spend: s.withdraw(self.spend[spendType].toList()) return s
class PlayerWarehouse(object): def __init__(self, player): self.player = player self.stockpile = Stockpile() self.gain = { GAIN_TRANSPORT: Stockpile(), GAIN_PRODUCTION: Stockpile(), GAIN_TRADE: Stockpile(), } self.spend = { SPEND_TRANSPORT: Stockpile(), SPEND_PRODUCTION: Stockpile(), SPEND_TRADE: Stockpile(), SPEND_RECRUITMENT: Stockpile(), SPEND_DEVELOPMENT: Stockpile(), SPEND_WORKER_UPKEEP: Stockpile(), } def projectedStockpile(self, gainsUpTo=None, spendsUpTo=None): gainList = WH_GAIN_PRIORITY[:WH_GAIN_PRIORITY.index( gainsUpTo)] if gainsUpTo else WH_GAIN_PRIORITY spendList = WH_SPEND_PRIORITY[:WH_SPEND_PRIORITY.index(spendsUpTo)] if spendsUpTo else WH_SPEND_PRIORITY stockpile = self.stockpile.clone() for gainType in gainList: stockpile.deposit(self.gain[gainType].toList()) for spendType in spendList: stockpile.withdraw(self.spend[spendType].toList()) return stockpile @property def balance(self): s = Stockpile(allowNegative=True) for gainType in self.gain: s.deposit(self.gain[gainType].toList()) for spendType in self.spend: s.withdraw(self.spend[spendType].toList()) return s def getGain(self, gainType): return self.gain[gainType] def getSpend(self, spendType): return self.spend[spendType] def assignGain(self, gainType, resources): self.gain[gainType].assign(resources) def assignSpend(self, spendType, resources): self.spend[spendType].assign(resources) def resetAllGains(self): for gainType in self.gain: self.resetGain(gainType) def resetAllSpends(self): for spendType in self.spend: self.resetSpend(spendType) def resetGain(self, gainType): self.gain[gainType].assign(Stockpile(allowNegative=True).toList()) def resetSpend(self, spendType): self.spend[spendType].assign(Stockpile(allowNegative=True).toList()) def postGain(self, gainType): self.stockpile.deposit(self.gain[gainType].toList()) def postSpend(self, spendType): self.stockpile.withdraw(self.spend[spendType].toList()) def getAmount(self, resourceName): return self.stockpile[resourceName] def listStockpile(self): return self.stockpile.toList() #accepts input of list of resources with quantities, returns lacking for each material #e.g. [(RESOURCE_IRON, 1)] #-> [] if sufficient, [(RESOURCE_IRON, 1)] if lacking def checkLacking(self, materialsWithQuants): lacking = [] for material, quantity in materialsWithQuants: if self.stockpile[material] < quantity: lacking.append((material, quantity-self.stockpile[material])) return lacking def deposit(self, materialsWithQuants): self.stockpile.deposit(materialsWithQuants) def withdraw(self, materialsWithQuants): self.stockpile.withdraw(materialsWithQuants)