diff --git a/app.py b/app.py index d152067..1f0cd2c 100644 --- a/app.py +++ b/app.py @@ -5,6 +5,12 @@ from db import init_db, get_db from routes.api import api from routes.dashboard import dashboard from routes.auth import auth +import logging + +logging.basicConfig( + level=logging.WARNING, + format='%(asctime)s [%(name)s] %(levelname)s: %(message)s' +) # Initialise DB schema when the app starts init_db() diff --git a/blueprint_engine.py b/blueprint_engine.py index 3436f13..be1d923 100644 --- a/blueprint_engine.py +++ b/blueprint_engine.py @@ -1,4 +1,7 @@ import json +import logging + +log = logging.getLogger(__name__) STANDARD_BLUEPRINT = [ {"barracks": 1, "farm": 3, "lumber": 2, "stoner": 2, "ironer": 2, "storage": 2, "main": 2, "temple": 1}, @@ -40,39 +43,39 @@ RESEARCH_LEVELS = { def evaluate_blueprints(conn): blueprints = conn.execute('SELECT town_id, blueprint_name FROM town_blueprints WHERE is_active = 1').fetchall() - print(f"[blueprint] Active blueprints: {len(blueprints)}") + log.warning(f"[blueprint] Active blueprints: {len(blueprints)}") if not blueprints: return for row in blueprints: town_id = str(row['town_id']) - print(f"[blueprint] Evaluating town_id={town_id}") + log.warning(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: - print(f"[blueprint] No town_state row found for town_id={town_id} — skipping") + log.warning(f"[blueprint] No town_state row found for town_id={town_id} — skipping") continue player_id = town_row['player_id'] town_name_db = town_row['town_name'] - print(f"[blueprint] Town: {town_name_db}, player_id={player_id}") + log.warning(f"[blueprint] Town: {town_name_db}, player_id={player_id}") try: town = json.loads(town_row['data']) except Exception as e: - print(f"[blueprint] Failed to parse town data JSON: {e}") + log.warning(f"[blueprint] Failed to parse town data JSON: {e}") continue build_queue = town.get('buildingOrder', []) buildings = town.get('buildings', {}) 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)}") + log.warning(f"[blueprint] buildings keys: {list(buildings.keys())}") + log.warning(f"[blueprint] buildData keys: {list(build_data.keys())}") + log.warning(f"[blueprint] buildingOrder length: {len(build_queue)}") # Don't queue anything if there's already a pending/executing command in DB db_pending = conn.execute( @@ -80,7 +83,7 @@ def evaluate_blueprints(conn): (town_id,) ).fetchall() if db_pending: - print(f"[blueprint] Already has {len(db_pending)} pending/executing commands — skipping") + log.warning(f"[blueprint] Already has {len(db_pending)} pending/executing commands — skipping") continue # Calculate Future Levels based on current + game queue @@ -90,7 +93,7 @@ def evaluate_blueprints(conn): if b_type: future_levels[b_type] = future_levels.get(b_type, 0) + 1 - print(f"[blueprint] future_levels: {future_levels}") + log.warning(f"[blueprint] future_levels: {future_levels}") # Find next required building target_building = None @@ -105,39 +108,39 @@ def evaluate_blueprints(conn): if incomplete_buildings: phase_incomplete = True - print(f"[blueprint] Phase {phase_idx} is incomplete. Missing: {incomplete_buildings}") + log.warning(f"[blueprint] Phase {phase_idx} is incomplete. Missing: {incomplete_buildings}") waiting_for_resources = False for b_name in incomplete_buildings: b_info = build_data.get(b_name) if b_info is None: - print(f"[blueprint] {b_name}: no buildData entry — skipping") + log.warning(f"[blueprint] {b_name}: no buildData entry — skipping") 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}") + log.warning(f"[blueprint] {b_name}: has_max={has_max}, deps={deps}, enough_resources={enough}") if not has_max: if not deps: if enough != False: target_building = b_name - print(f"[blueprint] -> SELECTED {b_name}") + log.warning(f"[blueprint] -> SELECTED {b_name}") break else: - print(f"[blueprint] -> waiting for resources for {b_name}") + log.warning(f"[blueprint] -> waiting for resources for {b_name}") waiting_for_resources = True if target_building: break elif waiting_for_resources: - print(f"[blueprint] Phase {phase_idx}: blocked by resources, stopping lookahead") + log.warning(f"[blueprint] Phase {phase_idx}: blocked by resources, stopping lookahead") break else: - print(f"[blueprint] Phase {phase_idx}: all blocked by deps, looking ahead (blocked_phases={blocked_phases+1})") + log.warning(f"[blueprint] Phase {phase_idx}: all blocked by deps, looking ahead (blocked_phases={blocked_phases+1})") blocked_phases += 1 if blocked_phases > 2: - print(f"[blueprint] Too many blocked phases, giving up") + log.warning(f"[blueprint] Too many blocked phases, giving up") break # Handle Academy Tech Research @@ -150,10 +153,10 @@ def evaluate_blueprints(conn): req_level = RESEARCH_LEVELS.get(r_name, 99) if academy_level >= req_level: target_research = r_name - print(f"[blueprint] -> Research target: {r_name}") + log.warning(f"[blueprint] -> Research target: {r_name}") break - print(f"[blueprint] Final: target_building={target_building}, target_research={target_research}") + log.warning(f"[blueprint] Final: target_building={target_building}, target_research={target_research}") if target_building: payload_str = json.dumps({"building_id": target_building}) @@ -161,13 +164,13 @@ def evaluate_blueprints(conn): INSERT INTO commands (town_id, town_name, type, payload, status, player_id) VALUES (?, ?, ?, ?, ?, ?) ''', (town_id, town_name_db, 'build', payload_str, 'pending', player_id)) - print(f"[blueprint] Inserted build command: {target_building} for {town_name_db}") + log.warning(f"[blueprint] Inserted build command: {target_building} for {town_name_db}") 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_name_db, 'research', payload_str, 'pending', player_id)) - print(f"[blueprint] Inserted research command: {target_research} for {town_name_db}") + log.warning(f"[blueprint] Inserted research command: {target_research} for {town_name_db}") else: - print(f"[blueprint] Nothing to do for {town_name_db}") + log.warning(f"[blueprint] Nothing to do for {town_name_db}")