def building_order(the_line, groups, debug=False): results = order_block.default_line_results(the_line) building_dict = the_line.the_world.buildings() city_dict = the_line.the_world.cities() buildings_lookup = the_line.the_world.buildings_lookup(lower=True) buildings_requirements = the_line.the_world.building_requirements() cities_lookup = the_line.the_world.cities_lookup(lower=True) the_team = the_line.the_world.teams()[the_line.block.team] # DB checks #------------------------ # _find_building if groups['building'].lower() not in buildings_lookup: if debug: results['debug'].append("Failed at _find_building") return order_block.fail(results, "there is no building by the name of '%s'" % groups['building']) else: the_building = building_dict[buildings_lookup[groups['building'].lower()]] # _find_city if groups['city'].lower() not in cities_lookup: if debug: results['debug'].append("Failed at _find_city") return order_block.fail(results, "there is no city by the name of '%s'" % groups['city']) else: the_city = city_dict[cities_lookup[groups['city'].lower()]] # _dead_city if the_city.dead > 0: if debug: results['debug'].append("Failed at _dead_city") return order_block.fail(results, "there is no living city by the name of '%s' (found id of %d)" % (groups['city'], the_city.id)) # if the_line.the_world._cities[974].buildings != {'0':None}: # print(the_line.content, the_line.block.team, the_line.block.title_name) # if the_city.id == 974: # print("") # print(the_line.content) # # # for k, v in city_dict.items(): # # if not v.dead: # # print(v.name, v.buildings) # # print("XXYYZZ") # the_line.the_world.cursor.execute("ROLLBACK") # the_line.the_world.cursor.execute("ROLLBACK") # exit() # Rule checks #------------------------ results = order_block.default_line_results(the_line, "%s could not be built at %s because" % (the_building.name, the_city.name)) # _swamp if the_city.terrain == map_data.terrain.index("swamp"): if the_building.wall or the_building.name == "Castle": if debug: results['debug'].append("Failed at _swamp") return order_block.fail(results, "%s is on a swamp" % the_city.name) # _ownership if the_city.team != the_line.block.team: if debug: results['debug'].append("Failed at _ownership") return order_block.fail(results, "%s is not your city" % the_city.name) # _nomadic if the_city.nomadic: if debug: results['debug'].append("Failed at _nomadic") return order_block.fail(results, "%s is nomadic" % the_city.name) amount = the_city.buildings_amount.get(the_building.id, 0) completion = the_city.buildings.get(the_building.id, 0) # _wall_points if the_building.wall and the_city.wall_points_used >= 1: if debug: results['debug'].append("Failed at _wall_points") return order_block.fail(results, "%s is already constructing a wall this year" % the_city.name) # _economy_points if the_building.economy and the_city.economy_points_used >= 1: if debug: results['debug'].append("Failed at _economy_points") return order_block.fail(results, "%s is already constructing an economic building this year" % the_city.name) # _building_points if not the_building.wall and not the_building.economy and the_city.building_points_used >= 1: if debug: results['debug'].append("Failed at _building_points") return order_block.fail(results, "%s is already constructing a building this year" % the_city.name) # _build_limit if completion == 0 and the_building.limit_per_city > 0: instances = amount # Is it used for an upgrade? if the_building.id in buildings_requirements: for b in buildings_requirements[the_building.id]: if b in the_city.buildings_amount: instances += the_city.buildings_amount[b] if b in the_city.buildings and the_city.buildings[b] > 0: instances += 1 if instances >= the_building.limit_per_city: if debug: results['debug'].append("Failed at _build_limit") return order_block.fail(results, "you have reached the limit allowed in one city") # _upgrade_requirements if completion == 0 and the_building.upgrades > -1: if the_city.buildings_amount.get(the_building.upgrades, 0) < 1: if debug: results['debug'].append("Failed at _upgrade_requirements") results['debug'].append("Required {building} (id: {id})".format( building=building_dict[the_building.upgrades].name, id=the_building.upgrades) ) results['debug'].append("the_city.buildings_amount: %s" % str(the_city.buildings_amount)) results['debug'].append("the_city.buildings: %s" % str(the_city.buildings)) return order_block.fail(results, "the required building (%s) is not complete there" % building_dict[the_building.upgrades].name) # Get cost results['cost'] = res_dict.Res_dict(the_building.cost_per_turn) if completion == 0: results['cost'] += res_dict.Res_dict(the_building.cost_up_front) # Check affordability affordability = the_team.resources.affordable(results['cost'])[0] if not affordability: if debug: results['debug'].append("Failed at _affordability") results['debug'].append("Cost: %s" % str(results['cost'])) results['debug'].append("Team resources: %s" % str(the_team.resources)) return order_block.fail_cost(results) # EXECUTION #------------------------ # Check we've got a DB row ready to update if completion == 0 and amount == 0: the_line.try_query(building_f.check_row_exists(building_id=the_building.id, city_id=the_city.id)) # Completion if the_team.resources.get("Stone") > 0: new_completion = completion + 100 else: new_completion = completion + 50 new_completion = min(new_completion, the_building.build_time) # Completion percentage completion_percentage = int(100 * (new_completion/the_building.build_time)) completion_percentage = min(completion_percentage, 100) # Queries results['queries'].append("-- Building %s at %s for team:%d" % (the_building.name, the_city.name, the_team.id)) results['queries'].extend(building_f.completion_query(the_city, the_building, new_completion)) # Apply cost the_team.resources -= results['cost'].discrete() # Result results['results'].append("%s is %s%% of the way through it's %s" % (the_city.name, completion_percentage, the_building.name)) # Update city points if the_building.wall: the_city.wall_points_used += 1 else: the_city.building_points_used += 1 return order_block.success(results)
def test_check_row_exists(self): for city_id, building_id, expected in self.check_row_exists_sets: r = building_f.check_row_exists(city_id=city_id, building_id=building_id) self.assertEqual(expected, r)