"""Seal ordinary two-slot entry, carrying and actual MemoryCard restart as one scope.

Later targeted progression/actor consumers run independently and cannot change
this normal campaign scope's result. Retain genuine AP history for continuation.
"""
import asyncio
import json
from pathlib import Path
import pickle
from test_gameplay_real_server import make_parser,run,run_acceptance
from test_real_server import sha


async def accept(session,driver):
    games=await run_acceptance(session,driver)
    for slot in list(session.clients):await session.stop_client(slot)
    for game in games.values():await asyncio.to_thread(game.shutdown)
    save=session.output/'real-server.apsave-input'
    save.write_bytes(pickle.dumps(session.ctx.get_save(),protocol=pickle.HIGHEST_PROTOCOL))
    receipt={'format':'pikmin2-gameplay-continuation-v1','server_save':save.name,'server_save_sha256':sha(save),
        'native_sha256':sha(session.args.native),'client_sha256':sha(session.args.client),
        'native_runtime_sha256':{p.name:sha(p) for p in session.args.native.parent.glob('*.dll')},
        'patches':{str(s):sha(p) for s,(p,_) in session.patches.items()},
        'runs':{str(s):{k:str(v) if isinstance(v,Path) else v for k,v in row.items()} for s,row in session.runs.items()},
        'gates':session.gates}
    (session.output/'continuation.json').write_text(json.dumps(receipt,indent=2)+'\n',encoding='utf8')
    session.event('actual-normal-campaign-and-real-ap-history-checkpoint',receipt=receipt)


if __name__=='__main__':
    asyncio.run(run(make_parser(__doc__).parse_args(),acceptance=accept,
        scope_gates=('normal_entry','carrying','card_save_load_restart'),acceptance_kind='normal_gameplay',boundary=__doc__))
