fix ?
This commit is contained in:
@@ -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})
|
||||
|
||||
@@ -84,9 +84,12 @@ def player_farm(player_id, world_id):
|
||||
@dashboard.route('/dashboard/farm-settings', methods=['GET'])
|
||||
def get_farm_settings():
|
||||
player_id = request.args.get('player_id')
|
||||
world_id = request.args.get('world_id', '')
|
||||
player_key = f"{player_id}_{world_id}" if world_id else player_id
|
||||
|
||||
conn = get_db()
|
||||
row = conn.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()
|
||||
conn.close()
|
||||
if row:
|
||||
@@ -99,6 +102,9 @@ def set_farm_settings():
|
||||
if not data or 'player_id' not in data:
|
||||
return jsonify({'error': 'missing player_id'}), 400
|
||||
player_id = data['player_id']
|
||||
world_id = data.get('world_id', '')
|
||||
player_key = f"{player_id}_{world_id}" if world_id else player_id
|
||||
|
||||
enabled = 1 if data.get('enabled') else 0
|
||||
loot_option = int(data.get('loot_option', 1))
|
||||
conn = get_db()
|
||||
@@ -109,7 +115,7 @@ def set_farm_settings():
|
||||
enabled = excluded.enabled,
|
||||
loot_option = excluded.loot_option,
|
||||
updated_at = excluded.updated_at
|
||||
''', (player_id, enabled, loot_option, datetime.utcnow().isoformat()))
|
||||
''', (player_key, enabled, loot_option, datetime.utcnow().isoformat()))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
return jsonify({'ok': True})
|
||||
@@ -521,15 +527,18 @@ def fail_stale_commands():
|
||||
@dashboard.route('/dashboard/bot-settings', methods=['GET', 'POST'])
|
||||
def bot_settings():
|
||||
player_id = request.args.get('player_id') or (request.json or {}).get('player_id')
|
||||
world_id = request.args.get('world_id') or (request.json or {}).get('world_id', '')
|
||||
if not player_id:
|
||||
return jsonify({'error': 'missing player_id'}), 400
|
||||
|
||||
player_key = f"{player_id}_{world_id}" if world_id else player_id
|
||||
|
||||
conn = get_db()
|
||||
c = conn.cursor()
|
||||
|
||||
if request.method == 'GET':
|
||||
row = c.execute(
|
||||
'SELECT * FROM bot_settings WHERE player_id = ?', (player_id,)
|
||||
'SELECT * FROM bot_settings WHERE player_id = ?', (player_key,)
|
||||
).fetchone()
|
||||
conn.close()
|
||||
if row:
|
||||
@@ -555,7 +564,7 @@ def bot_settings():
|
||||
rural_trade_ratio = excluded.rural_trade_ratio,
|
||||
updated_at = excluded.updated_at
|
||||
''', (
|
||||
player_id,
|
||||
player_key,
|
||||
int(bool(data.get('bootcamp_enabled', 0))),
|
||||
int(bool(data.get('bootcamp_use_def', 0))),
|
||||
int(bool(data.get('rural_trade_enabled', 0))),
|
||||
@@ -574,8 +583,11 @@ def bot_settings():
|
||||
@dashboard.route('/dashboard/bot-logs', methods=['GET', 'POST'])
|
||||
def bot_logs():
|
||||
player_id = request.args.get('player_id') or (request.json or {}).get('player_id')
|
||||
world_id = request.args.get('world_id') or (request.json or {}).get('world_id', '')
|
||||
if not player_id:
|
||||
return jsonify({'error': 'missing player_id'}), 400
|
||||
|
||||
player_key = f"{player_id}_{world_id}" if world_id else player_id
|
||||
|
||||
conn = get_db()
|
||||
c = conn.cursor()
|
||||
@@ -583,7 +595,7 @@ def bot_logs():
|
||||
if request.method == 'GET':
|
||||
feature = request.args.get('feature', '')
|
||||
query = 'SELECT * FROM bot_logs WHERE player_id = ?'
|
||||
params = [player_id]
|
||||
params = [player_key]
|
||||
if feature:
|
||||
query += ' AND feature = ?'
|
||||
params.append(feature)
|
||||
@@ -598,7 +610,7 @@ def bot_logs():
|
||||
message = data.get('message', '')
|
||||
c.execute(
|
||||
'INSERT INTO bot_logs (player_id, feature, message) VALUES (?, ?, ?)',
|
||||
(player_id, feature, message)
|
||||
(player_key, feature, message)
|
||||
)
|
||||
# Prune: keep only the latest 50 per player/feature
|
||||
c.execute('''
|
||||
@@ -609,7 +621,7 @@ def 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})
|
||||
@@ -623,9 +635,12 @@ def bot_logs():
|
||||
@dashboard.route('/dashboard/bootcamp-attack-now', methods=['POST'])
|
||||
def bootcamp_attack_now():
|
||||
player_id = (request.json or {}).get('player_id')
|
||||
world_id = (request.json or {}).get('world_id', '')
|
||||
if not player_id:
|
||||
return jsonify({'error': 'missing player_id'}), 400
|
||||
key = f'bootcamp_attack_now_{player_id}'
|
||||
|
||||
player_key = f"{player_id}_{world_id}" if world_id else player_id
|
||||
key = f'bootcamp_attack_now_{player_key}'
|
||||
conn = get_db()
|
||||
conn.execute('''
|
||||
INSERT INTO kv_store (key, value, updated_at) VALUES (?, '1', ?)
|
||||
|
||||
Reference in New Issue
Block a user