final fix?
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -78,11 +79,26 @@ def evaluate_blueprints(conn):
|
|||||||
log.warning(f"[blueprint] buildData keys: {list(build_data.keys())}")
|
log.warning(f"[blueprint] buildData keys: {list(build_data.keys())}")
|
||||||
log.warning(f"[blueprint] buildingOrder length: {len(build_queue)}")
|
log.warning(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 valid pending/executing command in DB
|
||||||
db_pending = conn.execute(
|
db_pending = conn.execute(
|
||||||
"SELECT id, type, status FROM commands WHERE town_id = ? AND type IN ('build', 'research') AND status IN ('pending', 'executing')",
|
"SELECT id, type, status, updated_at FROM commands WHERE town_id = ? AND type IN ('build', 'research') AND status IN ('pending', 'executing')",
|
||||||
(town_id,)
|
(town_id,)
|
||||||
).fetchall()
|
).fetchall()
|
||||||
|
|
||||||
|
if db_pending:
|
||||||
|
# Check if any of these are malformed (NULL updated_at = created by old broken blueprint engine)
|
||||||
|
# Auto-delete those so we can re-insert them correctly
|
||||||
|
broken_ids = [r['id'] for r in db_pending if r['updated_at'] is None]
|
||||||
|
if broken_ids:
|
||||||
|
log.warning(f"[blueprint] Deleting {len(broken_ids)} malformed stuck commands {broken_ids} for town {town_id}")
|
||||||
|
conn.execute(f"DELETE FROM commands WHERE id IN ({','.join('?' for _ in broken_ids)})", broken_ids)
|
||||||
|
conn.commit()
|
||||||
|
# Re-check if there are still real pending commands
|
||||||
|
db_pending = conn.execute(
|
||||||
|
"SELECT id, type, status, updated_at FROM commands WHERE town_id = ? AND type IN ('build', 'research') AND status IN ('pending', 'executing')",
|
||||||
|
(town_id,)
|
||||||
|
).fetchall()
|
||||||
|
|
||||||
if db_pending:
|
if db_pending:
|
||||||
details = [(r['id'], r['type'], r['status']) for r in db_pending]
|
details = [(r['id'], r['type'], r['status']) for r in db_pending]
|
||||||
log.warning(f"[blueprint] Already has {len(db_pending)} pending/executing commands — skipping. Commands: {details}")
|
log.warning(f"[blueprint] Already has {len(db_pending)} pending/executing commands — skipping. Commands: {details}")
|
||||||
@@ -161,18 +177,28 @@ def evaluate_blueprints(conn):
|
|||||||
log.warning(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:
|
if target_building:
|
||||||
|
now = datetime.utcnow().isoformat()
|
||||||
|
# Get next position for this town's build queue
|
||||||
|
pos_row = conn.execute(
|
||||||
|
"SELECT MAX(position) as max_pos FROM commands"
|
||||||
|
" WHERE player_id = ? AND town_id = ? AND type = 'build'"
|
||||||
|
" AND status IN ('pending', 'executing')",
|
||||||
|
(str(player_id), str(town_id))
|
||||||
|
).fetchone()
|
||||||
|
position = (pos_row['max_pos'] or 0) + 1
|
||||||
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, position, created_at, updated_at, player_id)
|
||||||
VALUES (?, ?, ?, ?, ?, ?)
|
VALUES (?, ?, ?, ?, 'pending', ?, ?, ?, ?)
|
||||||
''', (town_id, town_name_db, 'build', payload_str, 'pending', player_id))
|
''', (str(town_id), town_name_db, 'build', payload_str, position, now, now, str(player_id)))
|
||||||
log.warning(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:
|
elif target_research:
|
||||||
|
now = datetime.utcnow().isoformat()
|
||||||
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, created_at, updated_at, player_id)
|
||||||
VALUES (?, ?, ?, ?, ?, ?)
|
VALUES (?, ?, ?, ?, 'pending', ?, ?, ?)
|
||||||
''', (town_id, town_name_db, 'research', payload_str, 'pending', player_id))
|
''', (str(town_id), town_name_db, 'research', payload_str, now, now, str(player_id)))
|
||||||
log.warning(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:
|
else:
|
||||||
log.warning(f"[blueprint] Nothing to do for {town_name_db}")
|
log.warning(f"[blueprint] Nothing to do for {town_name_db}")
|
||||||
|
|||||||
Reference in New Issue
Block a user