auto trade and auto bandit

This commit is contained in:
2026-05-01 01:54:09 +03:00
parent 2a73e46a7b
commit 2921dff257
6 changed files with 630 additions and 7 deletions

View File

@@ -195,6 +195,17 @@ def get_pending_command():
'loot_option': farm_row['loot_option'] if farm_row else 1
}
# Bot settings (bootcamp + rural trade)
bot_row = c.execute(
'SELECT * FROM bot_settings WHERE player_id = ?', (str(player_id),)
).fetchone()
bot_settings = {
'bootcamp_enabled': bool(bot_row['bootcamp_enabled']) if bot_row else False,
'bootcamp_use_def': bool(bot_row['bootcamp_use_def']) if bot_row else False,
'rural_trade_enabled': bool(bot_row['rural_trade_enabled']) if bot_row else False,
'rural_trade_ratio': bot_row['rural_trade_ratio'] if bot_row else 3,
}
# Feature flags — look up this player's authorized features from their clan
member_row = c.execute(
'SELECT features FROM clan_members WHERE player_id = ?', (str(player_id),)
@@ -215,6 +226,7 @@ def get_pending_command():
'farm': farm_cmd,
'farm_upgrade': farm_upgrade_cmd,
'farm_settings': farm_settings,
'bot_settings': bot_settings,
'enabled_features': enabled_features,
'sync_requested': sync_req
})
@@ -367,6 +379,46 @@ def farm_status():
conn.close()
return jsonify(json.loads(row['value']) if row else {'warehouse_full': False})
# ------------------------------------------------------------------
# POST /api/bot-logs
# TM bot reports log entries for bootcamp / rural_trade loops.
# ------------------------------------------------------------------
@api.route('/api/bot-logs', methods=['POST'])
def api_bot_logs():
clan = _get_clan_from_request()
if not clan:
return make_response('Unauthorized', 403)
data = request.get_json(silent=True) or {}
player_id = str(data.get('player_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
conn = get_db()
conn.execute(
'INSERT INTO bot_logs (player_id, feature, message) VALUES (?, ?, ?)',
(player_id, feature, message)
)
# Keep only latest 50 per player/feature
conn.execute('''
DELETE FROM bot_logs
WHERE player_id = ? AND feature = ?
AND id NOT IN (
SELECT id FROM bot_logs
WHERE player_id = ? AND feature = ?
ORDER BY id DESC LIMIT 50
)
''', (player_id, feature, player_id, feature))
conn.commit()
conn.close()
return jsonify({'ok': True})
# ------------------------------------------------------------------
# GET /api/bot
# Serves the modular bot code concatenated into a single response

View File

@@ -441,3 +441,105 @@ def fail_stale_commands():
conn.commit()
conn.close()
return jsonify({'ok': True, 'failed': affected})
# ------------------------------------------------------------------
# GET /dashboard/bot-settings — fetch bootcamp + rural trade config
# POST /dashboard/bot-settings — save config
# ------------------------------------------------------------------
@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')
if not player_id:
return jsonify({'error': 'missing player_id'}), 400
conn = get_db()
c = conn.cursor()
if request.method == 'GET':
row = c.execute(
'SELECT * FROM bot_settings WHERE player_id = ?', (player_id,)
).fetchone()
conn.close()
if row:
return jsonify(dict(row))
return jsonify({
'player_id': player_id,
'bootcamp_enabled': 0,
'bootcamp_use_def': 0,
'rural_trade_enabled': 0,
'rural_trade_ratio': 3,
})
# POST — upsert
data = request.json or {}
c.execute('''
INSERT INTO bot_settings (player_id, bootcamp_enabled, bootcamp_use_def,
rural_trade_enabled, rural_trade_ratio, updated_at)
VALUES (?, ?, ?, ?, ?, ?)
ON CONFLICT(player_id) DO UPDATE SET
bootcamp_enabled = excluded.bootcamp_enabled,
bootcamp_use_def = excluded.bootcamp_use_def,
rural_trade_enabled = excluded.rural_trade_enabled,
rural_trade_ratio = excluded.rural_trade_ratio,
updated_at = excluded.updated_at
''', (
player_id,
int(bool(data.get('bootcamp_enabled', 0))),
int(bool(data.get('bootcamp_use_def', 0))),
int(bool(data.get('rural_trade_enabled', 0))),
int(data.get('rural_trade_ratio', 3)),
datetime.utcnow().isoformat()
))
conn.commit()
conn.close()
return jsonify({'ok': True})
# ------------------------------------------------------------------
# GET /dashboard/bot-logs?player_id=&feature= — last 50 log lines
# POST /dashboard/bot-logs — append + prune
# ------------------------------------------------------------------
@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')
if not player_id:
return jsonify({'error': 'missing player_id'}), 400
conn = get_db()
c = conn.cursor()
if request.method == 'GET':
feature = request.args.get('feature', '')
query = 'SELECT * FROM bot_logs WHERE player_id = ?'
params = [player_id]
if feature:
query += ' AND feature = ?'
params.append(feature)
query += ' ORDER BY id DESC LIMIT 50'
rows = c.execute(query, params).fetchall()
conn.close()
return jsonify([dict(r) for r in rows])
# POST — append entry and prune to last 50
data = request.json or {}
feature = data.get('feature', 'bootcamp')
message = data.get('message', '')
c.execute(
'INSERT INTO bot_logs (player_id, feature, message) VALUES (?, ?, ?)',
(player_id, feature, message)
)
# Prune: keep only the latest 50 per player/feature
c.execute('''
DELETE FROM bot_logs
WHERE player_id = ? AND feature = ?
AND id NOT IN (
SELECT id FROM bot_logs
WHERE player_id = ? AND feature = ?
ORDER BY id DESC LIMIT 50
)
''', (player_id, feature, player_id, feature))
conn.commit()
conn.close()
return jsonify({'ok': True})