diff --git a/backend/core/api.py b/backend/core/api.py index 1007408..06f5e69 100644 --- a/backend/core/api.py +++ b/backend/core/api.py @@ -360,6 +360,12 @@ def api_maa_open(q, body): return {'started': maa.launch(_maa_dir_for(q.get('file') or body.get('file')))} +def api_copilot_prepare(_q, body): + """加入作业集时的隐式预下载:把「下载 + 修正」挪到加入那一刻后台做, + 点「写入 MAA」时直接取现成的(没预到的现场兜底)""" + return {'queued': copilot.prepare((body or {}).get('ids') or [])} + + def api_maa_apply(_q, body): body = body or {} items = body.get('items') or [] @@ -530,6 +536,7 @@ def api_export(_q, body): '/api/maa/close': api_maa_close, '/api/maa/open': api_maa_open, '/api/maa/apply': api_maa_apply, + '/api/copilot/prepare': api_copilot_prepare, '/api/maa/clear': api_maa_clear, '/api/maa/tasklist': api_maa_tasklist, '/api/task': api_task, diff --git a/backend/core/copilot.py b/backend/core/copilot.py index b7f82f4..a4e875c 100644 --- a/backend/core/copilot.py +++ b/backend/core/copilot.py @@ -747,6 +747,81 @@ def download_content(copilot_id): return c +# ---------------------------------------------------------------- 加入即预下载 +# 「加入作业集」那一刻就把下载+修正做完(后台单线程排队,对作业站客气点), +# 点「写入 MAA」时直接取现成的,只剩关 MAA → 备份 → 写配置 → 重启。 +# 缓存只认 copilotId、进程内有效:作业集本身就是前端内存态,两边同生共死; +# 作业站后来改了作业也用加入那一刻的版本——本来就是按当时的评分挑的。 +_prepare_cv = threading.Condition() +_prepare_q = [] # 待预取的 cid(FIFO) +_prepared = {} # cid → (content, flags, sname, notes) +_preparing = {} # cid → 'queued' / 'working':fetch_prepared 等它出结果 +_failed = set() # 预取失败过的:写入时现场下载兜底;重新「加入」会再排队 + + +def prepare(ids): + """把作业排进后台预取队列(去重),返回本次新排队的条数""" + n = 0 + with _prepare_cv: + for i in ids: + try: + cid = int(i) + except (TypeError, ValueError): + continue + if cid in _prepared or cid in _preparing: + continue + _failed.discard(cid) # 重新加入算一次重试机会 + _preparing[cid] = 'queued' + _prepare_q.append(cid) + n += 1 + if n: + _prepare_cv.notify_all() + return n + + +def _prepare_fetch(cid): + """下载 + 修正 + 难度标志:预取和写入兜底共用这一段""" + content = download_content(cid) + content, notes = normalize_for_maa(content) + return content, difficulty_flags(content), content.get('stage_name') or '', notes + + +def _prepare_worker(): + while True: + with _prepare_cv: + while not _prepare_q: + _prepare_cv.wait() + cid = _prepare_q.pop(0) + _preparing[cid] = 'working' + try: + row = _prepare_fetch(cid) + ok = True + except Exception: + ok = False + with _prepare_cv: + if ok: + _prepared[cid] = row + else: + _failed.add(cid) + _preparing.pop(cid, None) + _prepare_cv.notify_all() # 叫醒等着取货的写入线程 + + +threading.Thread(target=_prepare_worker, daemon=True, name='copilot-prepare').start() + + +def fetch_prepared(cid): + """写入 MAA 线程用:优先拿预取结果(还在排队/在途就等它做完),没预取过 + 或预取失败的现场下载——永远有结果,最坏退回没有预取时的老行为""" + with _prepare_cv: + while cid in _preparing: + _prepare_cv.wait() + row = _prepared.get(cid) + if row is not None: + return row + (True,) + return _prepare_fetch(cid) + (False,) + + def detail_opers(content, owned_map=None): """详情弹窗用的干员列表:没写练度的按星级补默认要求(带 implied 标记)。 diff --git a/backend/core/maa.py b/backend/core/maa.py index cec90da..7eff32b 100644 --- a/backend/core/maa.py +++ b/backend/core/maa.py @@ -288,6 +288,23 @@ def apply_plan(items, profile=None, mode='replace', auto_start=True, tid=None, m if not profile or profile not in configs: raise ValueError('配置「%s」不存在,可选:%s' % (profile, ', '.join(configs))) + # 取齐所有作业再关 MAA(#53 的原则推进一步):加入作业集时大多已预下载好 + # (copilot.prepare),还在途的等它做完,没预到的现场下载。放在关进程之前, + # 下载卡住或失败时用户的 MAA 不白关;MAA 关闭到重启之间只剩写文件写配置。 + fetched = {} # copilotId → (content, flags, stage_name),普通/突袭两行共用一份不重复取 + for idx, it in enumerate(items, 1): + cid = int(it['copilotId']) + if cid in fetched: + continue + content, flags, sname, notes, pre = copilot.fetch_prepared(cid) + log('[%d/%d] %s #%s %s' % (idx, len(items), + '取预下载的作业' if pre else '下载作业', cid, it.get('title') or '')) + for n in notes: + log(' 修正:%s' % n) + fetched[cid] = (content, flags, sname) + if fetched: + log('作业已就绪 %d 份' % len(fetched)) + want = os.path.normcase(os.path.normpath(d)) rows = list_running() mine = next((r for r in rows if _dir_of(r) == want), None) @@ -330,29 +347,23 @@ def apply_plan(items, profile=None, mode='replace', auto_start=True, tid=None, m old = list(cop.get('TaskList') or []) if mode == 'append' else [] new_entries, files = [], [] - # copilotId → (文件名, flags, stage_name),同一份作业被普通/突袭两行各加一次时不重复下载 - cache = {} + # copilotId → (文件名, flags, stage_name):同一份作业被普通/突袭两行各加一次时只落一次盘 + fname_cache = {} for idx, it in enumerate(items, 1): cid = int(it['copilotId']) stage_code = it.get('stageCode') or str(cid) - if cid in cache: - fname, flags, sname = cache[cid] - log('[%d/%d] 复用已下载的作业 #%s %s' % (idx, len(items), cid, it.get('title') or '')) + if cid in fname_cache: + fname, flags, sname = fname_cache[cid] + log('[%d/%d] 复用已就绪的作业 #%s %s' % (idx, len(items), cid, it.get('title') or '')) else: - log('[%d/%d] 下载作业 #%s %s' % (idx, len(items), cid, it.get('title') or '')) - content = copilot.download_content(cid) - content, notes = copilot.normalize_for_maa(content) - for n in notes: - log(' 修正:%s' % n) - flags = copilot.difficulty_flags(content) - sname = content.get('stage_name') or '' + content, flags, sname = fetched[cid] fname = '%s_%s.json' % (_safe(stage_code), cid) os.makedirs(_copilot_dir(d), exist_ok=True) fpath = os.path.join(_copilot_dir(d), fname) with io.open(fpath, 'w', encoding='utf-8') as f: json.dump(content, f, ensure_ascii=False, indent=2) - cache[cid] = (fname, flags, sname) + fname_cache[cid] = (fname, flags, sname) if fname not in files: files.append(fname) diff --git a/frontend/app.js b/frontend/app.js index 3c51c8b..4a5e166 100644 --- a/frontend/app.js +++ b/frontend/app.js @@ -1053,33 +1053,54 @@ function renderParadoxPanel(root, d, close) { } /* ------------------------------------------------------------ 搜索 */ -/* 输入停约 0.5s 自动搜索:每次键入重置计时,清空输入不搜也不弹提示 */ +/* 输入停约 0.5s 自动搜索:每次键入重置计时,清空输入不搜也不弹提示。 + 搜索落地后顺手在后台预载下一关(见 preloadNext),点「下一关」时秒开。 */ let searchTimer = 0; let searchAbort = null; // 在途搜索的 AbortController;配合 searchKey 给 doSearch 去重 let searchKey = ''; // 在途搜索的「账号|关键词」 +let preloaded = null; // 下一关的后台预载 { key, ac, promise }:只存数据,不碰 UI /* 立刻作废还在飞行的搜索:中止请求(活动检索后端要跑十几秒,等它跑完再搜新的没意义)。 - 被中止的一方走 doSearch catch 的 aborted 分支安静退场,不碰 UI。 */ + 被中止的一方走 doSearch catch 的 aborted 分支安静退场,不碰 UI。预载一并作废。 */ function stopSearch() { if (searchAbort) { const a = searchAbort; searchAbort = null; searchKey = ''; a.abort(); } + if (preloaded) { const p = preloaded; preloaded = null; p.ac.abort(); } } +/* 键入即停在途搜索;预载先缓一口气——防抖落地后 doSearch 按新关键词决定认领它 + (键入的正是预载代号)还是作废它(stopSearch),不白白重发一遍 */ function queueAutoSearch() { clearTimeout(searchTimer); - stopSearch(); // 键入即停旧搜索:不等它完成,新一轮防抖接管 + if (searchAbort) { const a = searchAbort; searchAbort = null; searchKey = ''; a.abort(); } searchTimer = setTimeout(() => { if ($('#search-input').value.trim()) doSearch(); - // 旧搜索被作废后输入又清空了:骨架屏不能一直转下去,落回可解释的空态 - else if ($('#results').dataset.mode === 'loading') { - const box = $('#results'); - box.dataset.mode = ''; - box.innerHTML = '
输入关卡或活动代号,停顿后自动搜索
输入关卡或活动代号,停顿后自动搜索