This commit is contained in:
2026-05-02 01:11:42 +03:00
parent 0f54ef9191
commit f22b92ae89

View File

@@ -5,6 +5,9 @@ from datetime import datetime, timedelta
import os import os
from flask import make_response from flask import make_response
from blueprint_engine import evaluate_blueprints from blueprint_engine import evaluate_blueprints
import logging
_log = logging.getLogger(__name__)
api = Blueprint('api', __name__) api = Blueprint('api', __name__)
@@ -148,23 +151,39 @@ def _fetch_pending_builds_all_towns(c, player_id, world_id):
has reported its result (which was causing commands to pile up in EXECUTING). has reported its result (which was causing commands to pile up in EXECUTING).
""" """
# Towns that currently have a build already in-flight — don't touch those. # Towns that currently have a build already in-flight — don't touch those.
executing_rows = c.execute(''' if world_id:
SELECT DISTINCT c.town_id FROM commands c executing_rows = c.execute('''
JOIN town_state ts ON c.town_id = ts.town_id SELECT DISTINCT c.town_id FROM commands c
WHERE c.status = 'executing' AND c.type = 'build' AND c.player_id = ? AND ts.world_id = ? JOIN town_state ts ON c.town_id = ts.town_id
''', (player_id, world_id)).fetchall() WHERE c.status = 'executing' AND c.type = 'build' AND c.player_id = ? AND ts.world_id = ?
''', (player_id, world_id)).fetchall()
else:
executing_rows = c.execute('''
SELECT DISTINCT town_id FROM commands
WHERE status = 'executing' AND type = 'build' AND player_id = ?
''', (player_id,)).fetchall()
busy_towns = {r['town_id'] for r in executing_rows} busy_towns = {r['town_id'] for r in executing_rows}
# Get every town that has at least one pending build, ordered by # Get every town that has at least one pending build, ordered by
# which town has been waiting longest (MIN updated_at across its commands). # which town has been waiting longest (MIN updated_at across its commands).
town_rows = c.execute(''' if world_id:
SELECT c.town_id town_rows = c.execute('''
FROM commands c SELECT c.town_id
JOIN town_state ts ON c.town_id = ts.town_id FROM commands c
WHERE c.status = 'pending' AND c.type = 'build' AND c.player_id = ? AND ts.world_id = ? JOIN town_state ts ON c.town_id = ts.town_id
GROUP BY c.town_id WHERE c.status = 'pending' AND c.type = 'build' AND c.player_id = ? AND ts.world_id = ?
ORDER BY MIN(c.updated_at) ASC GROUP BY c.town_id
''', (player_id, world_id)).fetchall() ORDER BY MIN(c.updated_at) ASC
''', (player_id, world_id)).fetchall()
else:
town_rows = c.execute('''
SELECT town_id
FROM commands
WHERE status = 'pending' AND type = 'build' AND player_id = ?
GROUP BY town_id
ORDER BY MIN(updated_at) ASC
''', (player_id,)).fetchall()
_log.warning(f"[poll] build towns found: {[r['town_id'] for r in town_rows]}, busy: {busy_towns}")
results = [] results = []
now = datetime.utcnow().isoformat() now = datetime.utcnow().isoformat()
@@ -200,6 +219,7 @@ def _fetch_pending_builds_all_towns(c, player_id, world_id):
def get_pending_command(): def get_pending_command():
player_id = request.args.get('player_id') player_id = request.args.get('player_id')
world_id = request.args.get('world_id') world_id = request.args.get('world_id')
_log.warning(f"[poll] player_id={player_id!r} world_id={world_id!r}")
if not player_id: if not player_id:
return jsonify({'error': 'no player_id provided'}), 400 return jsonify({'error': 'no player_id provided'}), 400