multi account large update
This commit is contained in:
@@ -65,13 +65,13 @@ def receive_state():
|
||||
# Returns one 'build' AND one 'recruit' command independently,
|
||||
# so both queues are served in parallel without blocking each other.
|
||||
# ------------------------------------------------------------------
|
||||
def _fetch_pending_of_type(c, cmd_type):
|
||||
def _fetch_pending_of_type(c, cmd_type, player_id):
|
||||
row = c.execute('''
|
||||
SELECT * FROM commands
|
||||
WHERE status = 'pending' AND type = ?
|
||||
WHERE status = 'pending' AND type = ? AND player_id = ?
|
||||
ORDER BY id ASC
|
||||
LIMIT 1
|
||||
''', (cmd_type,)).fetchone()
|
||||
''', (cmd_type, player_id)).fetchone()
|
||||
if not row:
|
||||
return None
|
||||
c.execute('''
|
||||
@@ -88,12 +88,16 @@ def _fetch_pending_of_type(c, cmd_type):
|
||||
|
||||
@api.route('/api/commands/pending', methods=['GET'])
|
||||
def get_pending_command():
|
||||
player_id = request.args.get('player_id')
|
||||
if not player_id:
|
||||
return jsonify({'error': 'no player_id provided'}), 400
|
||||
|
||||
conn = get_db()
|
||||
c = conn.cursor()
|
||||
|
||||
build_cmd = _fetch_pending_of_type(c, 'build')
|
||||
recruit_cmd = _fetch_pending_of_type(c, 'recruit')
|
||||
market_cmd = _fetch_pending_of_type(c, 'market_offer')
|
||||
build_cmd = _fetch_pending_of_type(c, 'build', player_id)
|
||||
recruit_cmd = _fetch_pending_of_type(c, 'recruit', player_id)
|
||||
market_cmd = _fetch_pending_of_type(c, 'market_offer', player_id)
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
@@ -133,16 +137,22 @@ def command_result(cmd_id):
|
||||
# ------------------------------------------------------------------
|
||||
@api.route('/api/captcha/alert', methods=['POST'])
|
||||
def captcha_alert():
|
||||
player_id = request.args.get('player_id')
|
||||
if not player_id:
|
||||
return jsonify({'error': 'no player_id provided'}), 400
|
||||
|
||||
data = request.get_json(silent=True) or {}
|
||||
detected = bool(data.get('detected', False))
|
||||
kv_key = f'captcha_active_{player_id}'
|
||||
|
||||
conn = get_db()
|
||||
conn.execute('''
|
||||
INSERT INTO kv_store (key, value, updated_at)
|
||||
VALUES ('captcha_active', ?, ?)
|
||||
VALUES (?, ?, ?)
|
||||
ON CONFLICT(key) DO UPDATE SET
|
||||
value = excluded.value,
|
||||
updated_at = excluded.updated_at
|
||||
''', ('1' if detected else '0', datetime.utcnow().isoformat()))
|
||||
''', (kv_key, '1' if detected else '0', datetime.utcnow().isoformat()))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
return jsonify({'ok': True})
|
||||
|
||||
@@ -12,7 +12,14 @@ dashboard = Blueprint('dashboard', __name__)
|
||||
# ------------------------------------------------------------------
|
||||
@dashboard.route('/')
|
||||
def index():
|
||||
return render_template('dashboard.html')
|
||||
conn = get_db()
|
||||
players = conn.execute('SELECT DISTINCT player, player_id FROM town_state WHERE player IS NOT NULL ORDER BY player ASC').fetchall()
|
||||
conn.close()
|
||||
return render_template('index.html', players=players)
|
||||
|
||||
@dashboard.route('/player/<player_id>')
|
||||
def player_dashboard(player_id):
|
||||
return render_template('dashboard.html', player_id=player_id)
|
||||
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
@@ -21,13 +28,15 @@ def index():
|
||||
# ------------------------------------------------------------------
|
||||
@dashboard.route('/dashboard/towns', methods=['GET'])
|
||||
def get_towns():
|
||||
player_id = request.args.get('player_id')
|
||||
conn = get_db()
|
||||
rows = conn.execute('''
|
||||
SELECT town_id, town_name, player, player_id, alliance_id,
|
||||
world_id, x, y, sea, data, updated_at
|
||||
FROM town_state
|
||||
WHERE player_id = ?
|
||||
ORDER BY town_name ASC
|
||||
''').fetchall()
|
||||
''', (player_id, )).fetchall()
|
||||
conn.close()
|
||||
|
||||
towns = []
|
||||
@@ -74,10 +83,11 @@ def get_towns():
|
||||
# ------------------------------------------------------------------
|
||||
@dashboard.route('/dashboard/client-status', methods=['GET'])
|
||||
def client_status():
|
||||
player_id = request.args.get('player_id')
|
||||
conn = get_db()
|
||||
row = conn.execute('''
|
||||
SELECT MAX(updated_at) AS last_seen FROM town_state
|
||||
''').fetchone()
|
||||
SELECT MAX(updated_at) AS last_seen FROM town_state WHERE player_id = ?
|
||||
''', (player_id, )).fetchone()
|
||||
conn.close()
|
||||
|
||||
last_seen = row['last_seen'] if row else None
|
||||
@@ -100,9 +110,10 @@ def client_status():
|
||||
# ------------------------------------------------------------------
|
||||
@dashboard.route('/dashboard/captcha-status', methods=['GET'])
|
||||
def captcha_status():
|
||||
player_id = request.args.get('player_id')
|
||||
conn = get_db()
|
||||
row = conn.execute(
|
||||
"SELECT value FROM kv_store WHERE key = 'captcha_active'"
|
||||
"SELECT value FROM kv_store WHERE key = ?", (f'captcha_active_{player_id}', )
|
||||
).fetchone()
|
||||
conn.close()
|
||||
active = bool(row and row['value'] == '1')
|
||||
@@ -115,13 +126,15 @@ def captcha_status():
|
||||
# ------------------------------------------------------------------
|
||||
@dashboard.route('/dashboard/commands', methods=['GET'])
|
||||
def get_commands():
|
||||
player_id = request.args.get('player_id')
|
||||
conn = get_db()
|
||||
rows = conn.execute('''
|
||||
SELECT id, town_id, town_name, type, payload, status, result_msg, created_at, updated_at
|
||||
FROM commands
|
||||
WHERE player_id = ?
|
||||
ORDER BY id DESC
|
||||
LIMIT 50
|
||||
''').fetchall()
|
||||
''', (player_id, )).fetchall()
|
||||
conn.close()
|
||||
|
||||
return jsonify([dict(r) for r in rows])
|
||||
@@ -138,7 +151,7 @@ def create_command():
|
||||
if not data:
|
||||
return jsonify({'error': 'no data'}), 400
|
||||
|
||||
required = ['town_id', 'type', 'payload']
|
||||
required = ['town_id', 'type', 'payload', 'player_id']
|
||||
for field in required:
|
||||
if field not in data:
|
||||
return jsonify({'error': f'missing field: {field}'}), 400
|
||||
@@ -149,7 +162,7 @@ def create_command():
|
||||
|
||||
# Reject if the Tampermonkey client is offline (no state push in last 150 s)
|
||||
conn = get_db()
|
||||
row = conn.execute('SELECT MAX(updated_at) AS last_seen FROM town_state').fetchone()
|
||||
row = conn.execute('SELECT MAX(updated_at) AS last_seen FROM town_state WHERE player_id = ?', (data['player_id'], )).fetchone()
|
||||
last_seen = row['last_seen'] if row else None
|
||||
client_online = False
|
||||
if last_seen:
|
||||
@@ -165,15 +178,16 @@ def create_command():
|
||||
|
||||
c = conn.cursor()
|
||||
c.execute('''
|
||||
INSERT INTO commands (town_id, town_name, type, payload, status, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, 'pending', ?, ?)
|
||||
INSERT INTO commands (town_id, town_name, type, payload, status, created_at, updated_at, player_id)
|
||||
VALUES (?, ?, ?, ?, 'pending', ?, ?, ?)
|
||||
''', (
|
||||
str(data['town_id']),
|
||||
data.get('town_name', ''),
|
||||
cmd_type,
|
||||
json.dumps(data['payload']),
|
||||
datetime.utcnow().isoformat(),
|
||||
datetime.utcnow().isoformat()
|
||||
datetime.utcnow().isoformat(),
|
||||
str(data['player_id'])
|
||||
))
|
||||
cmd_id = c.lastrowid
|
||||
conn.commit()
|
||||
@@ -202,6 +216,7 @@ def cancel_command(cmd_id):
|
||||
# ------------------------------------------------------------------
|
||||
@dashboard.route('/dashboard/commands/fail-stale', methods=['POST'])
|
||||
def fail_stale_commands():
|
||||
player_id = request.args.get('player_id')
|
||||
conn = get_db()
|
||||
c = conn.cursor()
|
||||
c.execute('''
|
||||
@@ -209,8 +224,8 @@ def fail_stale_commands():
|
||||
SET status = 'failed',
|
||||
result_msg = 'Client went offline',
|
||||
updated_at = ?
|
||||
WHERE status IN ('pending', 'executing')
|
||||
''', (datetime.utcnow().isoformat(),))
|
||||
WHERE status IN ('pending', 'executing') AND player_id = ?
|
||||
''', (datetime.utcnow().isoformat(), player_id))
|
||||
affected = c.rowcount
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
Reference in New Issue
Block a user