diff --git a/blueprint_engine.py b/blueprint_engine.py index cc0d99e..1b9cec2 100644 --- a/blueprint_engine.py +++ b/blueprint_engine.py @@ -54,9 +54,9 @@ def evaluate_blueprints(conn): except Exception: continue - build_queue = town.get('build_queue', []) + build_queue = town.get('buildingOrder', []) buildings = town.get('buildings', {}) - build_data = town.get('build_data', {}) + build_data = town.get('buildData', {}) # Don't queue anything if there's already a pending/executing command in DB db_pending = conn.execute("SELECT id FROM commands WHERE town_id = ? AND type IN ('build', 'research') AND status IN ('pending', 'executing')", (town_id,)).fetchall() @@ -72,29 +72,45 @@ def evaluate_blueprints(conn): # Find next required building target_building = None + phase_incomplete = False + blocked_phases = 0 + for phase in STANDARD_BLUEPRINT: - incomplete = False + incomplete_buildings = [] for b_name, req_level in phase.items(): if future_levels.get(b_name, 0) < req_level: - incomplete = True + incomplete_buildings.append(b_name) + + if incomplete_buildings: + phase_incomplete = True + # Try to find a building in this phase that we can upgrade right now + for b_name in incomplete_buildings: b_info = build_data.get(b_name) - if b_info and not b_info.get('has_max_level'): - if b_info.get('enough_resources') != False: - target_building = b_name - break - if incomplete: - break + if b_info and b_info.get('can_upgrade') == True: + target_building = b_name + break - # Handle Academy Tech Research if no building was prioritized + if target_building: + # Found something we can build right now + break + else: + # Nothing in this phase can be built (missing dependencies, population, or resources) + # We look ahead up to 2 additional phases to unblock it + blocked_phases += 1 + if blocked_phases > 2: + break + + # Handle Academy Tech Research target_research = None + # Only queue research if we aren't about to queue a building, to prevent double-booking if not target_building: academy_level = future_levels.get('academy', 0) researched = town.get('researches', {}) - research_queue = town.get('research_queue', []) + # We don't have a reliable research queue from the state yet, so we just check what's researched. + # But we should ensure we don't queue multiple of the same. The DB check above handles 'research' pending commands. for r_name in RESEARCH_LIST: - if not researched.get(r_name) and r_name not in [q.get('type') for q in research_queue]: - # Check if academy level is high enough + if not researched.get(r_name): req_level = RESEARCH_LEVELS.get(r_name, 99) if academy_level >= req_level: target_research = r_name