This commit is contained in:
2026-05-02 01:40:21 +03:00
parent 4272edf432
commit 6157ae1034
4 changed files with 84 additions and 45 deletions

View File

@@ -117,13 +117,28 @@ def receive_state():
# ------------------------------------------------------------------
def _fetch_pending_of_type(c, cmd_type, player_id, world_id):
"""Fetch a single oldest pending command of a given type (recruit, market, etc.)."""
row = c.execute('''
SELECT c.* FROM commands c
JOIN town_state ts ON c.town_id = ts.town_id
WHERE c.status = 'pending' AND c.type = ? AND c.player_id = ? AND ts.world_id = ?
ORDER BY c.updated_at ASC, c.id ASC
LIMIT 1
''', (cmd_type, player_id, world_id)).fetchone()
# We use LEFT JOIN because global commands (like farm_loot) use a pseudo town_id like "0_gr121"
# which does not exist in town_state.
global_town_id = f"0_{world_id}" if world_id else "0"
if world_id:
row = c.execute('''
SELECT c.* FROM commands c
LEFT JOIN town_state ts ON c.town_id = ts.town_id
WHERE c.status = 'pending' AND c.type = ? AND c.player_id = ?
AND (ts.world_id = ? OR c.town_id = ?)
ORDER BY c.updated_at ASC, c.id ASC
LIMIT 1
''', (cmd_type, player_id, world_id, global_town_id)).fetchone()
else:
row = c.execute('''
SELECT * FROM commands
WHERE status = 'pending' AND type = ? AND player_id = ?
ORDER BY updated_at ASC, id ASC
LIMIT 1
''', (cmd_type, player_id)).fetchone()
if not row:
return None
c.execute('''
@@ -242,9 +257,12 @@ def get_pending_command():
research_cmd = _fetch_pending_of_type(c, 'research', player_id, world_id)
sync_req = _check_and_reset_sync(c, player_id)
# Determine player_key for world-specific settings if world_id is provided
player_key = f"{player_id}_{world_id}" if world_id else player_id
# Farm settings
farm_row = c.execute(
'SELECT enabled, loot_option FROM farm_settings WHERE player_id = ?', (player_id,)
'SELECT enabled, loot_option FROM farm_settings WHERE player_id = ?', (player_key,)
).fetchone()
farm_settings = {
'enabled': bool(farm_row['enabled']) if farm_row else False,
@@ -253,7 +271,7 @@ def get_pending_command():
# Bot settings (bootcamp + rural trade)
bot_row = c.execute(
'SELECT * FROM bot_settings WHERE player_id = ?', (str(player_id),)
'SELECT * FROM bot_settings WHERE player_id = ?', (player_key,)
).fetchone()
bot_settings = {
'bootcamp_enabled': bool(bot_row['bootcamp_enabled']) if bot_row else False,
@@ -263,7 +281,7 @@ def get_pending_command():
}
# One-shot manual attack flag
attack_now_key = f'bootcamp_attack_now_{player_id}'
attack_now_key = f'bootcamp_attack_now_{player_key}'
flag_row = c.execute('SELECT value FROM kv_store WHERE key = ?', (attack_now_key,)).fetchone()
if flag_row and flag_row['value'] == '1':
bot_settings['attack_now'] = True
@@ -457,16 +475,19 @@ def api_bot_logs():
data = request.get_json(silent=True) or {}
player_id = str(data.get('player_id', ''))
world_id = str(data.get('world_id', ''))
feature = data.get('feature', '')
message = data.get('message', '')
if not player_id or not feature or not message:
return jsonify({'error': 'missing fields'}), 400
player_key = f"{player_id}_{world_id}" if world_id else player_id
conn = get_db()
conn.execute(
'INSERT INTO bot_logs (player_id, feature, message) VALUES (?, ?, ?)',
(player_id, feature, message)
(player_key, feature, message)
)
# Keep only latest 50 per player/feature
conn.execute('''
@@ -477,7 +498,7 @@ def api_bot_logs():
WHERE player_id = ? AND feature = ?
ORDER BY id DESC LIMIT 50
)
''', (player_id, feature, player_id, feature))
''', (player_key, feature, player_key, feature))
conn.commit()
conn.close()
return jsonify({'ok': True})