59 lines
2.3 KiB
Python
59 lines
2.3 KiB
Python
"""Bounded P/Enter batches through MCP; stop when the battle UI disappears."""
|
|
import argparse
|
|
import json
|
|
import math
|
|
import os
|
|
import time
|
|
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):
|
|
return battle_is_terminal(state)
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('--seconds', type=float, default=40)
|
|
parser.add_argument('--battle-confirmed', action='store_true')
|
|
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))
|
|
pairs = 0
|
|
state = {}
|
|
def cancelled():
|
|
return STOP_FILE.exists() or time.monotonic() >= deadline
|
|
try:
|
|
if cancelled():
|
|
raise SessionCancelled('Battle batch stopped before connecting')
|
|
with Session(cancelled=cancelled) as client:
|
|
state = client.call('get_state')
|
|
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__':
|
|
main()
|