enchance farming/fix
This commit is contained in:
@@ -256,15 +256,29 @@ def sync_request():
|
||||
@api.route('/api/commands/<int:cmd_id>/result', methods=['POST'])
|
||||
def command_result(cmd_id):
|
||||
data = request.get_json(silent=True) or {}
|
||||
status = data.get('status', 'done') # 'done' | 'failed'
|
||||
status = data.get('status', 'done') # 'done' | 'failed' | 'pending' (requeue)
|
||||
msg = data.get('message', '')
|
||||
now = datetime.utcnow().isoformat()
|
||||
|
||||
conn = get_db()
|
||||
# Look up type + player_id for post-update hooks
|
||||
cmd = conn.execute(
|
||||
'SELECT type, player_id FROM commands WHERE id = ?', (cmd_id,)
|
||||
).fetchone()
|
||||
|
||||
conn.execute('''
|
||||
UPDATE commands
|
||||
SET status = ?, result_msg = ?, updated_at = ?
|
||||
WHERE id = ?
|
||||
''', (status, msg, datetime.utcnow().isoformat(), cmd_id))
|
||||
''', (status, msg, now, cmd_id))
|
||||
|
||||
# When an explicit farm_loot command succeeds, record the timestamp
|
||||
if cmd and cmd['type'] == 'farm_loot' and status == 'done' and cmd['player_id']:
|
||||
conn.execute('''
|
||||
INSERT INTO kv_store (key, value, updated_at) VALUES (?, ?, ?)
|
||||
ON CONFLICT(key) DO UPDATE SET value=excluded.value, updated_at=excluded.updated_at
|
||||
''', (f'last_farmed_{cmd["player_id"]}', now, now))
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
return jsonify({'ok': True})
|
||||
@@ -334,10 +348,17 @@ def farm_status():
|
||||
conn = get_db()
|
||||
if request.method == 'POST':
|
||||
data = request.get_json(silent=True) or {}
|
||||
now = datetime.utcnow().isoformat()
|
||||
conn.execute('''
|
||||
INSERT INTO kv_store (key, value, updated_at) VALUES (?, ?, ?)
|
||||
ON CONFLICT(key) DO UPDATE SET value=excluded.value, updated_at=excluded.updated_at
|
||||
''', (kv_key, json.dumps(data), datetime.utcnow().isoformat()))
|
||||
''', (kv_key, json.dumps(data), now))
|
||||
# Auto-farm reports warehouse_full=false when it successfully looted something
|
||||
if not data.get('warehouse_full', True):
|
||||
conn.execute('''
|
||||
INSERT INTO kv_store (key, value, updated_at) VALUES (?, ?, ?)
|
||||
ON CONFLICT(key) DO UPDATE SET value=excluded.value, updated_at=excluded.updated_at
|
||||
''', (f'last_farmed_{player_id}', now, now))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
return jsonify({'ok': True})
|
||||
|
||||
@@ -126,6 +126,12 @@ def get_farm_data():
|
||||
rows = conn.execute(
|
||||
'SELECT town_id, town_name, data FROM town_state WHERE player_id = ?', (player_id,)
|
||||
).fetchall()
|
||||
|
||||
# Also fetch when the bot last farmed
|
||||
lf_row = conn.execute(
|
||||
"SELECT value FROM kv_store WHERE key = ?", (f'last_farmed_{player_id}',)
|
||||
).fetchone()
|
||||
last_farmed_at = lf_row['value'] if lf_row else None
|
||||
conn.close()
|
||||
|
||||
now_ts = int(datetime.utcnow().timestamp())
|
||||
@@ -142,7 +148,7 @@ def get_farm_data():
|
||||
'ready_farms': len(ready),
|
||||
'next_ready_at': min((f['lootable_at'] for f in farm_data if f.get('lootable_at', 0) > now_ts and f.get('relation_status', 0) == 1), default=None)
|
||||
})
|
||||
return jsonify(farms_summary)
|
||||
return jsonify({'towns': farms_summary, 'last_farmed_at': last_farmed_at})
|
||||
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user