def totalRequiredMaterials(self): stockpile = Stockpile() for ruleName, quantity in self.productionLines.items(): rule = getProductionRule(ruleName) consumed = self.requiredMaterials(rule) stockpile.deposit(consumed.toList()) return stockpile
def execute(self, game): maps = game.maps stockpile = Stockpile() mapProvince = maps.findProvince(self.province) if self.outputType == QUERY_RESOURCE_OUTPUT_ACTUAL: total = mapProvince.actualResourceOutput else: total = mapProvince.maxResourceOutput stockpile.deposit(total) return stockpile.toList()
def execute(self, game): maps = game.maps nation = game.getPlayerNation(self.player) stockpile = Stockpile() for province in nation.provinces: if not nation.capitalConnected[province]: continue mapProvince = maps.findProvince(province) if self.outputType == QUERY_RESOURCE_OUTPUT_ACTUAL: total = mapProvince.actualResourceOutput else: total = mapProvince.maxResourceOutput stockpile.deposit(total) return stockpile.toList()
def lineMaxCapacity(self, rule, resources): stockpile = Stockpile(resources) c = ResourceConstraint(rule.materialsWithQuant) count = 0 while c.isSatisfied(stockpile): c.satisfy(stockpile) count += 1 return count
def handle(self, player, game): warehouse = game.getPlayerWarehouse(player) transportedResources = QueryAllProvinceResourceOutput(player).execute( game) transportableResources = QueryAllProvinceResourceOutput( player, outputType=QUERY_RESOURCE_OUTPUT_MAX).execute(game) return { 'respType': RESP_RES_BAL, 'civilians': game.getPlayerCivilians(player).toDict(), 'warehouse': warehouse.stockpile.toDict(), 'warehouseBalance': warehouse.balance.toDict(), 'account': game.getPlayerBankAccount(player).toDict(), 'transportedResources': Stockpile(transportedResources).toDict(), 'transportableResources': Stockpile(transportableResources).toDict(), 'industry': game.getPlayerIndustry(player).toDict(), 'industryMaxCapacity': QueryPlayerIndustryMaxCap(player).execute(game), 'labour': QueryPlayerLabour(player).execute(game), }
def execute(self, game): player = self.player civilians = game.getPlayerCivilians(player) warehouse = game.getPlayerWarehouse(player) sp = warehouse.projectedStockpile(spendsUpTo=SPEND_DEVELOPMENT) devSpend = Stockpile() for id, assgn in civilians.allActiveAssignments: resourceCost = assgn.perTurnResourceCost(game, player) rc = ResourceConstraint(resourceCost) if rc.isSatisfied(sp): rc.satisfy(sp) devSpend.deposit(rc.resources) assgn.progressBlocked = False else: assgn.progressBlocked = True logging.info('Assignment %s blocked' % (assgn)) warehouse.assignSpend(SPEND_DEVELOPMENT, devSpend.toList())
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 actualResourceOutput(self): stockpile = Stockpile() for terrainTile in self.mapTerrainTiles: resources = terrainTile.actualResourcesProduced stockpile.deposit(resources) return stockpile.toList()
def resetSpend(self, spendType): self.spend[spendType].assign(Stockpile(allowNegative=True).toList())
def resetGain(self, gainType): self.gain[gainType].assign(Stockpile(allowNegative=True).toList())
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(), }
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)
def producedResources(self): stockpile = Stockpile() for ruleName, quantity in self.productionLines.items(): rule = getProductionRule(ruleName) stockpile[rule.productName] += quantity return stockpile
def lineSpare(self, rule, resources): stockpile = Stockpile(resources) requiredMat = self.requiredMaterials(rule) c = ResourceConstraint(rule.materialsWithQuant) newRequired = requiredMat.deposit(c.resources) return stockpile.greaterEqualTo(newRequired, [resource for resource, quantity in c.resources])
def lineSatisfied(self, rule, resources): stockpile = Stockpile(resources) requiredMat = self.requiredMaterials(rule) c = ResourceConstraint(rule.materialsWithQuant) return stockpile.greaterEqualTo(requiredMat, [resource for resource, quantity in c.resources])
def requiredMaterials(self, rule): return Stockpile(ResourceConstraint(rule.materialsWithQuant).resources) * self.employing(rule)