131 lines
5.3 KiB
Python
131 lines
5.3 KiB
Python
import json
|
|
|
|
STANDARD_BLUEPRINT = [
|
|
{"barracks": 1, "farm": 3, "lumber": 2, "stoner": 2, "ironer": 2, "storage": 2, "main": 2, "temple": 1},
|
|
{"barracks": 1, "farm": 3, "lumber": 3, "stoner": 3, "ironer": 3, "storage": 6, "main": 8},
|
|
{"farm": 8, "lumber": 8, "ironer": 8, "stoner": 8, "market": 5, "temple": 5, "barracks": 5},
|
|
{"academy": 13},
|
|
{"storage": 12, "farm": 12},
|
|
{"main": 25},
|
|
{"storage": 21, "farm": 15},
|
|
{"lumber": 15, "stoner": 10, "ironer": 12},
|
|
{"docks": 10},
|
|
{"academy": 30},
|
|
{"farm": 20, "storage": 25},
|
|
{"market": 15, "trade_office": 1, "hide": 10},
|
|
{"market": 30, "farm": 35, "thermal": 1, "academy": 36},
|
|
{"farm": 45, "storage": 35, "lumber": 40, "ironer": 40, "stoner": 40},
|
|
{"temple": 30}
|
|
]
|
|
|
|
RESEARCH_LIST = [
|
|
"booty", "pottery", "architecture", "building_crane",
|
|
"shipwright", "plow", "mathematics", "combat_experience",
|
|
"strong_wine", "take_over", "colonize_ship"
|
|
]
|
|
|
|
RESEARCH_LEVELS = {
|
|
"booty": 7,
|
|
"pottery": 7,
|
|
"architecture": 10,
|
|
"building_crane": 13,
|
|
"shipwright": 13,
|
|
"plow": 22,
|
|
"mathematics": 25,
|
|
"combat_experience": 34,
|
|
"strong_wine": 34,
|
|
"take_over": 28,
|
|
"colonize_ship": 13
|
|
}
|
|
|
|
def evaluate_blueprints(conn):
|
|
blueprints = conn.execute('SELECT town_id, blueprint_name FROM town_blueprints WHERE is_active = 1').fetchall()
|
|
if not blueprints:
|
|
return
|
|
|
|
for row in blueprints:
|
|
town_id = str(row['town_id'])
|
|
town_row = conn.execute('SELECT data FROM town_state WHERE town_id = ?', (town_id,)).fetchone()
|
|
if not town_row:
|
|
continue
|
|
|
|
try:
|
|
town = json.loads(town_row['data'])
|
|
except Exception:
|
|
continue
|
|
|
|
build_queue = town.get('buildingOrder', [])
|
|
buildings = town.get('buildings', {})
|
|
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()
|
|
if db_pending:
|
|
continue
|
|
|
|
# Calculate Future Levels based on current + game queue
|
|
future_levels = {k: v for k, v in buildings.items()}
|
|
for q_item in build_queue:
|
|
b_type = q_item.get('building_type') or q_item.get('name')
|
|
if b_type:
|
|
future_levels[b_type] = future_levels.get(b_type, 0) + 1
|
|
|
|
# Find next required building
|
|
target_building = None
|
|
phase_incomplete = False
|
|
blocked_phases = 0
|
|
|
|
for phase in STANDARD_BLUEPRINT:
|
|
incomplete_buildings = []
|
|
for b_name, req_level in phase.items():
|
|
if future_levels.get(b_name, 0) < req_level:
|
|
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 b_info.get('can_upgrade') == True:
|
|
target_building = b_name
|
|
break
|
|
|
|
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', {})
|
|
# 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):
|
|
req_level = RESEARCH_LEVELS.get(r_name, 99)
|
|
if academy_level >= req_level:
|
|
target_research = r_name
|
|
break
|
|
|
|
if target_building:
|
|
payload_str = json.dumps({"building_id": target_building})
|
|
conn.execute('''
|
|
INSERT INTO commands (town_id, town_name, type, payload, status, player_id)
|
|
VALUES (?, ?, ?, ?, ?, ?)
|
|
''', (town_id, town.get('town_name'), 'build', payload_str, 'pending', town.get('player_id')))
|
|
elif target_research:
|
|
payload_str = json.dumps({"research_id": target_research})
|
|
conn.execute('''
|
|
INSERT INTO commands (town_id, town_name, type, payload, status, player_id)
|
|
VALUES (?, ?, ?, ?, ?, ?)
|
|
''', (town_id, town.get('town_name'), 'research', payload_str, 'pending', town.get('player_id')))
|