Fix MCP cancellation, mirror run state, and battle boundaries
This commit is contained in:
+37
-21
@@ -1,16 +1,18 @@
|
|||||||
"""Bounded P/Enter batches through MCP; stop when the battle UI disappears."""
|
"""Bounded P/Enter batches through MCP; stop when the battle UI disappears."""
|
||||||
import argparse
|
import argparse
|
||||||
import json
|
import json
|
||||||
|
import math
|
||||||
|
import os
|
||||||
import time
|
import time
|
||||||
from mcp_session import Session
|
from pathlib import Path
|
||||||
|
from mcp_session import Session, SessionCancelled, SessionTimeout
|
||||||
|
from game_scenes import battle_is_terminal, battle_is_visible
|
||||||
|
|
||||||
|
STOP_FILE = Path(os.environ.get('LC_JEV_HOME', str(Path.home() / 'lc_mcp' / 'jev'))) / 'JEV_STOP'
|
||||||
|
|
||||||
|
|
||||||
def is_terminal(state):
|
def is_terminal(state):
|
||||||
texts = [item['text'].replace(' ', '') for item in state.get('items', [])]
|
return battle_is_terminal(state)
|
||||||
return any(text.upper() in ('VICTORY', 'DEFEAT') or
|
|
||||||
any(marker in text for marker in ('正在探索', '获得E.G.O饰品', '选择主题卡包',
|
|
||||||
'开始战斗', '选择一位罪人', '选项事件'))
|
|
||||||
for text in texts)
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
@@ -18,24 +20,38 @@ def main():
|
|||||||
parser.add_argument('--seconds', type=float, default=40)
|
parser.add_argument('--seconds', type=float, default=40)
|
||||||
parser.add_argument('--battle-confirmed', action='store_true')
|
parser.add_argument('--battle-confirmed', action='store_true')
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
if not math.isfinite(args.seconds) or args.seconds <= 0:
|
||||||
|
parser.error('--seconds must be finite and positive')
|
||||||
deadline = time.monotonic() + min(50, max(1, args.seconds))
|
deadline = time.monotonic() + min(50, max(1, args.seconds))
|
||||||
pairs = 0
|
pairs = 0
|
||||||
with Session() as client:
|
state = {}
|
||||||
state = client.call('get_state')
|
def cancelled():
|
||||||
texts = ' '.join(item['text'] for item in state.get('items', []))
|
return STOP_FILE.exists() or time.monotonic() >= deadline
|
||||||
active = args.battle_confirmed or any(marker in texts.upper() for marker in ('WAVE', 'TURN', 'DURANTE', '胜率', '伤害'))
|
try:
|
||||||
while time.monotonic() < deadline:
|
if cancelled():
|
||||||
# 战斗特写会隐藏 HUD;只有明确的结算/地图/选项信号才退出。
|
raise SessionCancelled('Battle batch stopped before connecting')
|
||||||
if not active or is_terminal(state):
|
with Session(cancelled=cancelled) as client:
|
||||||
break
|
|
||||||
# 短批次,批次末只检查界面是否仍为战斗,不分析拼点。
|
|
||||||
for _ in range(2):
|
|
||||||
client.call('keypress', {'key': 'p'})
|
|
||||||
client.call('keypress', {'key': 'enter'})
|
|
||||||
pairs += 1
|
|
||||||
time.sleep(1.5)
|
|
||||||
state = client.call('get_state')
|
state = client.call('get_state')
|
||||||
print(json.dumps({'pairs': pairs, 'state': state}, ensure_ascii=False))
|
active = args.battle_confirmed or battle_is_visible(state)
|
||||||
|
while not cancelled():
|
||||||
|
# Cinematics hide the HUD; explicit scene boundaries end combat.
|
||||||
|
if not state.get('ok') or not active or is_terminal(state):
|
||||||
|
break
|
||||||
|
for _ in range(2):
|
||||||
|
for key in ('p', 'enter'):
|
||||||
|
if cancelled():
|
||||||
|
raise SessionCancelled('Battle batch stopped between keys')
|
||||||
|
client.call('keypress', {'key': key})
|
||||||
|
pairs += 1
|
||||||
|
time.sleep(min(1.5, max(0, deadline - time.monotonic())))
|
||||||
|
if cancelled():
|
||||||
|
break
|
||||||
|
state = client.call('get_state')
|
||||||
|
result = {'pairs': pairs, 'state': state, 'stopped': cancelled()}
|
||||||
|
except (SessionCancelled, SessionTimeout) as error:
|
||||||
|
result = {'pairs': pairs, 'state': state, 'stopped': True,
|
||||||
|
'error': str(error), 'outcome_unknown': error.outcome_unknown}
|
||||||
|
print(json.dumps(result, ensure_ascii=False))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
Reference in New Issue
Block a user