This commit is contained in:
2026-05-02 00:57:23 +03:00
parent 90ce6a029d
commit b36b11393f

View File

@@ -40,106 +40,134 @@ RESEARCH_LEVELS = {
def evaluate_blueprints(conn): def evaluate_blueprints(conn):
blueprints = conn.execute('SELECT town_id, blueprint_name FROM town_blueprints WHERE is_active = 1').fetchall() blueprints = conn.execute('SELECT town_id, blueprint_name FROM town_blueprints WHERE is_active = 1').fetchall()
print(f"[blueprint] Active blueprints: {len(blueprints)}")
if not blueprints: if not blueprints:
return return
for row in blueprints: for row in blueprints:
town_id = str(row['town_id']) town_id = str(row['town_id'])
town_row = conn.execute('SELECT data, player_id, town_name FROM town_state WHERE town_id = ?', (town_id,)).fetchone() print(f"[blueprint] Evaluating town_id={town_id}")
town_row = conn.execute(
'SELECT data, player_id, town_name FROM town_state WHERE town_id = ?', (town_id,)
).fetchone()
if not town_row: if not town_row:
print(f"[blueprint] No town_state row found for town_id={town_id} — skipping")
continue continue
player_id = town_row['player_id'] player_id = town_row['player_id']
town_name_db = town_row['town_name'] town_name_db = town_row['town_name']
print(f"[blueprint] Town: {town_name_db}, player_id={player_id}")
try: try:
town = json.loads(town_row['data']) town = json.loads(town_row['data'])
except Exception: except Exception as e:
print(f"[blueprint] Failed to parse town data JSON: {e}")
continue continue
build_queue = town.get('buildingOrder', []) build_queue = town.get('buildingOrder', [])
buildings = town.get('buildings', {}) buildings = town.get('buildings', {})
build_data = town.get('buildData', {}) build_data = town.get('buildData', {})
print(f"[blueprint] buildings keys: {list(buildings.keys())}")
print(f"[blueprint] buildData keys: {list(build_data.keys())}")
print(f"[blueprint] buildingOrder length: {len(build_queue)}")
# Don't queue anything if there's already a pending/executing command in DB # 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() db_pending = conn.execute(
"SELECT id FROM commands WHERE town_id = ? AND type IN ('build', 'research') AND status IN ('pending', 'executing')",
(town_id,)
).fetchall()
if db_pending: if db_pending:
print(f"[blueprint] Already has {len(db_pending)} pending/executing commands — skipping")
continue continue
# Calculate Future Levels based on current + game queue # Calculate Future Levels based on current + game queue
future_levels = {k: v for k, v in buildings.items()} future_levels = {k: v for k, v in buildings.items()}
for q_item in build_queue: for q_item in build_queue:
b_type = q_item.get('building_type') or q_item.get('name') b_type = q_item.get('building_type') or q_item.get('name')
if b_type: if b_type:
future_levels[b_type] = future_levels.get(b_type, 0) + 1 future_levels[b_type] = future_levels.get(b_type, 0) + 1
print(f"[blueprint] future_levels: {future_levels}")
# Find next required building # Find next required building
target_building = None target_building = None
phase_incomplete = False phase_incomplete = False
blocked_phases = 0 blocked_phases = 0
for phase in STANDARD_BLUEPRINT: for phase_idx, phase in enumerate(STANDARD_BLUEPRINT):
incomplete_buildings = [] incomplete_buildings = []
for b_name, req_level in phase.items(): for b_name, req_level in phase.items():
if future_levels.get(b_name, 0) < req_level: if future_levels.get(b_name, 0) < req_level:
incomplete_buildings.append(b_name) incomplete_buildings.append(b_name)
if incomplete_buildings: if incomplete_buildings:
phase_incomplete = True phase_incomplete = True
print(f"[blueprint] Phase {phase_idx} is incomplete. Missing: {incomplete_buildings}")
waiting_for_resources = False waiting_for_resources = False
# Try to find a building in this phase that we can upgrade right now
for b_name in incomplete_buildings: for b_name in incomplete_buildings:
b_info = build_data.get(b_name) b_info = build_data.get(b_name)
if b_info and not b_info.get('has_max_level'): if b_info is None:
# A missing_dependencies object that is empty means dependencies are met print(f"[blueprint] {b_name}: no buildData entry — skipping")
deps = b_info.get('missing_dependencies') continue
has_max = b_info.get('has_max_level', False)
deps = b_info.get('missing_dependencies')
enough = b_info.get('enough_resources')
print(f"[blueprint] {b_name}: has_max={has_max}, deps={deps}, enough_resources={enough}")
if not has_max:
if not deps: if not deps:
if b_info.get('enough_resources') != False: if enough != False:
target_building = b_name target_building = b_name
print(f"[blueprint] -> SELECTED {b_name}")
break break
else: else:
print(f"[blueprint] -> waiting for resources for {b_name}")
waiting_for_resources = True waiting_for_resources = True
if target_building: if target_building:
# Found something we can build right now
break break
elif waiting_for_resources: elif waiting_for_resources:
# We meet the dependencies for at least one building, but lack resources. print(f"[blueprint] Phase {phase_idx}: blocked by resources, stopping lookahead")
# We should wait for resources to accumulate instead of spending them on future phases.
break break
else: else:
# ALL incomplete buildings in this phase are blocked by missing dependencies. print(f"[blueprint] Phase {phase_idx}: all blocked by deps, looking ahead (blocked_phases={blocked_phases+1})")
# We look ahead up to 2 additional phases to build the required dependencies.
blocked_phases += 1 blocked_phases += 1
if blocked_phases > 2: if blocked_phases > 2:
print(f"[blueprint] Too many blocked phases, giving up")
break break
# Handle Academy Tech Research # Handle Academy Tech Research
target_research = None target_research = None
# Only queue research if we aren't about to queue a building, to prevent double-booking
if not target_building: if not target_building:
academy_level = future_levels.get('academy', 0) academy_level = future_levels.get('academy', 0)
researched = town.get('researches', {}) researched = town.get('researches', {})
# 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: for r_name in RESEARCH_LIST:
if not researched.get(r_name): if not researched.get(r_name):
req_level = RESEARCH_LEVELS.get(r_name, 99) req_level = RESEARCH_LEVELS.get(r_name, 99)
if academy_level >= req_level: if academy_level >= req_level:
target_research = r_name target_research = r_name
print(f"[blueprint] -> Research target: {r_name}")
break break
print(f"[blueprint] Final: target_building={target_building}, target_research={target_research}")
if target_building: if target_building:
payload_str = json.dumps({"building_id": target_building}) payload_str = json.dumps({"building_id": target_building})
conn.execute(''' conn.execute('''
INSERT INTO commands (town_id, town_name, type, payload, status, player_id) INSERT INTO commands (town_id, town_name, type, payload, status, player_id)
VALUES (?, ?, ?, ?, ?, ?) VALUES (?, ?, ?, ?, ?, ?)
''', (town_id, town_name_db, 'build', payload_str, 'pending', player_id)) ''', (town_id, town_name_db, 'build', payload_str, 'pending', player_id))
print(f"[blueprint] Inserted build command: {target_building} for {town_name_db}")
elif target_research: elif target_research:
payload_str = json.dumps({"research_id": target_research}) payload_str = json.dumps({"research_id": target_research})
conn.execute(''' conn.execute('''
INSERT INTO commands (town_id, town_name, type, payload, status, player_id) INSERT INTO commands (town_id, town_name, type, payload, status, player_id)
VALUES (?, ?, ?, ?, ?, ?) VALUES (?, ?, ?, ?, ?, ?)
''', (town_id, town_name_db, 'research', payload_str, 'pending', player_id)) ''', (town_id, town_name_db, 'research', payload_str, 'pending', player_id))
print(f"[blueprint] Inserted research command: {target_research} for {town_name_db}")
else:
print(f"[blueprint] Nothing to do for {town_name_db}")