Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions backend/core/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,19 @@ def api_account_remove(_q, body):
def api_account_import_maa(_q, body):
"""从账号绑定的 MAA 目录导入干员识别结果(R2/R3)

body: {"file": "<账号文件名>"}(可省略,按上次账号→列表首个解析)。
body: {"file": "<账号文件名>", "force": true}(file 可省略,按上次账号→
列表首个解析)。force 只给「账号页手动导入」用——用户在弹窗里点了确认,
才算同意用识别结果覆盖森空岛同步的练度;向导里顺手导入不带 force,
会拿到 skippedBy=token 而不动数据。
同步任务,无 taskId;结果摘要(条数/来源/警告)直接返回给前端。
"""
body = body or {}
file = _account_for(body.get('file'))
d = _maa_dir_for(file)
if not d:
raise ValueError('该账号没有绑定 MAA 目录,也没有设置全局默认')
return operbox.import_into(file, d)
force = bool(body.get('force'))
return operbox.import_into(file, d, force=force)


def api_account_create_from_maa(_q, body):
Expand Down
31 changes: 30 additions & 1 deletion backend/core/operbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,18 +202,47 @@ def _summary(fname, path, data, built, snap):
}


def import_into(file: str, maa_dir: str) -> dict:
def _token_synced(snap: dict) -> bool:
"""这份快照的练度是不是森空岛同步来的(真数据,不该被识别结果顶掉)

判据两道:sync.source 是森空岛的接口,或文件里存着 token(能同步就说明
这是 token 账号)。MAA 直接建号/导入的账号 source=maa-operbox,都不命中。
"""
src = str(((snap.get('sync') or {}).get('source')) or '')
if 'skland' in src or 'zonai' in src:
return True
return bool((snap.get('skd') or {}).get('data'))


def import_into(file: str, maa_dir: str, force: bool = False) -> dict:
"""把 MAA 干员识别结果写进账号文件,返回导入摘要。

done=false(识别未完成)整单拒绝。chars/stats/missing/sync 被识别结果
覆盖(皮肤/信赖按 charId 沿用旧值);account/maa/tags/note/skd/skins
原样保留(合并语义同 updater.merge_preserved),原子替换落盘。

force=False 时拒绝覆盖「森空岛同步来的练度」:本地识别没有技能/专精/
模组,用它顶掉真数据是纯降级,作业匹配会退回只比等级(B17 的口径)。
更要紧的是 OperBoxData.json 每个 MAA 目录只有一份(= 当时登录的那个号),
批量加号时挨个导入会把所有账号写成同一份练度(2026-11-15 反馈「加两个
账号显示同一个练度」)。账号页的手动导入是用户点确认才走的,传 force。
"""
path, data, tables, built = _read_operbox(maa_dir)
# 读旧快照 → 合并 → 写回整段圈进写锁:与后台同步/其他请求并发时
# 不丢更新(#47)。锁内只有内存合并,没有 IO,拿一会儿没关系。
with atomicio.lock():
snap = accounts.get_account(file)
if not force and _token_synced(snap):
return {
'file': os.path.basename(str(file)),
'path': path,
'skippedBy': 'token',
'reason': '该账号的练度已经是森空岛同步的结果,MAA 本地识别不含'
'技能/专精/模组,不覆盖;确需替换请在账号页手动导入',
'imported': 0,
'stats': snap.get('stats') or {},
'sync': snap.get('sync') or {},
}
_apply_chars(snap, built['chars'], tables, path, data)
accounts.save_account(file, snap)
return _summary(os.path.basename(str(file)), path, data, built, snap)
Expand Down
4 changes: 3 additions & 1 deletion docs/backend.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ main()
| POST | `/api/account/maa` | 把 `maa.profile` 等用户字段写回账号 JSON(同步不覆盖的那几个) |
| POST | `/api/account/remove` | 删除账号:直接删除账号 JSON,不备份不可恢复。账号列表卡片悬停 ✕ 触发,前端有二次确认 |
| POST | `/api/account/refresh` | 同步账号。带 `token` 同步单个(密文写进账号文件 `skd` 段);不带则按各账号文件 ∪ 旧 `tokens.json` 全部同步。返回 `taskId` |
| POST | `/api/account/import-maa` | 从账号绑定的 MAA 目录读干员识别结果(`done=false` 残缺名单拒绝),覆盖练度/统计/缺卡(账号信息与 `skd` 保留),同步任务直接返回摘要 |
| POST | `/api/account/import-maa` | 从账号绑定的 MAA 目录读干员识别结果(`done=false` 残缺名单拒绝),覆盖练度/统计/缺卡(账号信息与 `skd` 保留),同步任务直接返回摘要。**默认不动森空岛同步来的练度**:命中 `operbox._token_synced()` 时返回 `skippedBy=token`,只把 `force=true` 当作用户确认(账号页手动导入)才覆盖 |
| POST | `/api/account/create-from-maa` | 不用森空岛 token:用 `dir` 指定 MAA 目录的干员识别结果直接建本地账号(合成 uid、昵称取备注或目录名、无 `skd` 段、重名顺延 (2)(3)),返回建号摘要 |
| POST | `/api/search` | 搜作业 + 练度匹配(body:`q` / `file` / `pages` / `minRating` / `limit`) |
| GET | `/api/activities` | 近期活动列表。**预留,无界面入口**(前端从 bootstrap 拿活动数据) |
Expand Down Expand Up @@ -201,6 +201,8 @@ pywebview 窗口与浏览器直开都是同源,不受影响。
### 没有技能数据的账号只比等级

MAA 干员识别(`operbox.py`)拿不到技能等级、专精、模组,导入后的账号这三项是空的。
也正因为它是降级数据,识别结果只在账号**没有练度**时兜底,不覆盖森空岛同步来的练度
(详见上表 `/api/account/import-maa` 的 `force` 语义)。
如果照实判「不满足」,这类账号搜出来的作业会被全部判成 `train` 再被过滤掉,等于不可用。
所以 `build_owned()` 给每个干员标 `skillKnown`(`skills` 数组是否为空),`req_met()` 遇到
`skillKnown=False` 时**只比精英化与等级,技能与模组一律当已拥有**:
Expand Down
2 changes: 1 addition & 1 deletion docs/frontend.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ api('/api/xxx', body) // 统一包成 {ok, data|error}
| `toggleBasket()` | 窄屏抽屉开关(≤1180px),控制 `.basket.show`、遮罩与浮动按钮 |
| `renderResult()` | 单关卡卡片 / 活动关卡列表两套版式 |
| `renderBasket()` | 右侧作业集(含拖动排序) |
| `openAccountWizard(mode)` | 账号向导共用组件(首次引导 `onboard` / 添加账号 `add`):第 1 页选 MAA 根目录,第 2 页二选一——森空岛 token(一行一个批量导入,成功后自动从 MAA 导入练度)或「直接从选定的 MAA 导入」(不用 token,调 `POST /api/account/create-from-maa` 建本地账号) |
| `openAccountWizard(mode)` | 账号向导共用组件(首次引导 `onboard` / 添加账号 `add`):第 1 页选 MAA 根目录,第 2 页二选一——森空岛 token(一行一个批量导入)或「直接从选定的 MAA 导入」(不用 token,调 `POST /api/account/create-from-maa` 建本地账号)。token 那条路**只在「只加了一个号且它同步后没有练度」时**才用 MAA 识别结果兜底:`OperBoxData.json` 每个 MAA 目录只有一份(当时登录的那个号),批量加号挨个导入会把所有账号写成同一个练度,而 token 同步来的练度含技能/专精/模组,被本地识别顶掉是纯降级 |
| `openOnboarding()` / `openAddAccount()` | 上面的薄包装(引导多一个「稍后设置」,成功后把目录存为全局默认) |
| `openSettings()` | 设置弹窗 |
| `pollTask(tid, root, sel)` | 任务日志轮询(同步账号、写入 MAA、同步抽卡、签到共用)。日志容器默认 `#task-log`,签到面板两块日志各有 id,用 `sel` 指过去 |
Expand Down
53 changes: 35 additions & 18 deletions frontend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ async function importFromMaa() {
pr.operboxCount != null ? pr.operboxCount + ' 名干员' : '内容待更新时解析'
}${pr.operboxSyncTime ? ' · ' + esc(fmtTime(pr.operboxSyncTime)) + ' 识别' : ''}</code></div>
</div>
<div class="onb-hint bad">! 本地识别不含技能、专精、模组数据。更新后作业匹配只比精英化与等级,技能模组一律按已拥有处理,可能推给你实际打不了的作业——抄之前请在 MAA 里确认。</div>
<div class="onb-hint bad">! 会覆盖这个账号当前的练度(包括森空岛同步来的)。本地识别不含技能、专精、模组数据,更新后作业匹配只比精英化与等级,技能模组一律按已拥有处理,可能推给你实际打不了的作业——抄之前请在 MAA 里确认。</div>
<div class="modal-foot">
<button class="btn-plain" data-close>取消</button>
<button class="btn-solid" id="btn-import-go">开始更新</button>
Expand All @@ -577,7 +577,8 @@ async function importFromMaa() {
btn.disabled = true;
btn.textContent = '更新中…';
try {
const r = await api('/api/account/import-maa', { file: S.current });
// force:用户在这个弹窗里点了确认,才允许覆盖森空岛同步的练度
const r = await api('/api/account/import-maa', { file: S.current, force: true });
const st = r.stats || {};
const warns = (r.warnings || []).map(w => `<div class="onb-hint bad">! ${esc(w)}</div>`).join('');
close();
Expand Down Expand Up @@ -2327,29 +2328,45 @@ function openAccountWizard(mode) {
for (const file of added) {
if (file) await api('/api/account/maa', { file, patch: { dir } }).catch(() => { });
}
// 新账号顺手导入 MAA 里的干员识别结果(不依赖 token;失败不影响账号添加)
if (operboxReady) {
for (const file of added) {
if (!file) continue;
taskLogLine(logBox, `从 MAA 导入干员信息(${file})…`);
try {
const r = await api('/api/account/import-maa', { file });
importedTotal += r.imported || 0;
taskLogLine(logBox, `✓ 已导入 ${r.imported || 0} 名干员${r.skipped ? `(跳过 ${r.skipped} 名)` : ''},来源 ${r.source === 'yituliu' ? '一图流' : 'MAA 本地识别'}`);
} catch (e) {
taskLogLine(logBox, `✗ ${file} 干员信息导入失败:${e.message}`, true);
}
}
} else {
taskLogLine(logBox, '! 该 MAA 还没有干员识别数据(data/OperBoxData.json),可先在 MAA 小工具里识别一次,之后从森空岛页手动导入', true);
}
}
const acc = await api('/api/accounts');
S.accounts = acc.accounts;
renderAccounts();
renderMaaState();
const st = await api('/api/settings', {});
S.settings = st.settings;

// 用 MAA 识别结果兜底练度,只在「只加了一个号、且它同步后没有练度」时做。
// OperBoxData.json 一个 MAA 目录只有一份,就是当时登录在 MAA 里的那个号:
// 批量加号时挨个导入,会把所有账号写成同一份练度(2026-11-15 反馈「添加
// 两个账号显示同一个练度」);而 token 同步来的练度含技能/专精/模组,
// 被本地识别顶掉是纯降级。需要覆盖时走账号页的手动导入(那里会确认)。
if (dir && operboxReady) {
const ok = added.filter(Boolean);
const one = ok.length === 1 ? ok[0] : '';
const hasChars = one
&& (((S.accounts.find(a => a.file === one) || {}).stats || {}).charCount || 0) > 0;
if (one && !hasChars) {
taskLogLine(logBox, `从 MAA 导入干员信息(${one})…`);
try {
const r = await api('/api/account/import-maa', { file: one });
if (r.skippedBy === 'token') {
taskLogLine(logBox, `· 跳过 MAA 识别结果:${r.reason || '练度已是森空岛同步的结果'}`);
} else {
importedTotal += r.imported || 0;
taskLogLine(logBox, `✓ 已导入 ${r.imported || 0} 名干员${r.skipped ? `(跳过 ${r.skipped} 名)` : ''},来源 ${r.source === 'yituliu' ? '一图流' : 'MAA 本地识别'}`);
}
} catch (e) {
taskLogLine(logBox, `✗ ${one} 干员信息导入失败:${e.message}`, true);
}
} else if (ok.length > 1) {
taskLogLine(logBox, '! 一次添加了多个账号,未从 MAA 导入练度——MAA 只有一份识别结果(当时登录的那个号),套到多个账号上会显示同一个练度', true);
} else {
taskLogLine(logBox, '· 练度已由森空岛同步,未用 MAA 识别结果覆盖(本地识别不含技能/专精/模组;确需替换可在账号页手动导入)');
}
} else if (dir) {
taskLogLine(logBox, '! 该 MAA 还没有干员识别数据(data/OperBoxData.json),可先在 MAA 小工具里识别一次,之后从森空岛页手动导入', true);
}
}
if (failed.length) {
toast(`成功 ${added.length} 个,失败 ${failed.length} 个,详见日志`, 'err', 6000);
Expand Down
25 changes: 25 additions & 0 deletions tools/smoketest/smoke_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,31 @@ def write_operbox(done):
and c['skinCount'] == 1, 'MAA 没有的皮肤/信赖该沿用旧值,而不是清零'
assert snap['skins'] == [{'id': 's1'}] and snap['stats']['skinCount'] == 1

# 森空岛同步来的练度不许被识别结果顶掉:识别结果每个 MAA 目录只有一份
# (当时登录的那个号),批量加号时挨个导入会让所有账号练度一样(2026-11-15)
tok_snap = {'account': {'uid': '2', 'nickName': '乙'},
'chars': [dict(old_char, charId=six[0], level=80)],
'stats': {'charCount': 1},
'sync': {'source': 'zonai.skland.com/api/v1/game/player/info'}}
with open(os.path.join(cfg, '乙.json'), 'w', encoding='utf-8') as f:
json.dump(tok_snap, f, ensure_ascii=False)
r = operbox.import_into('乙.json', maa)
assert r.get('skippedBy') == 'token' and r.get('imported') == 0, \
'token 同步的练度该被拦下,实际:%r' % r
assert r.get('reason'), '跳过要给界面一句人话原因'
after = json.load(open(os.path.join(cfg, '乙.json'), encoding='utf-8'))
assert after['chars'] == tok_snap['chars'], '拦下时不能动现有练度'
# 只存了 token、没写 sync.source 的老账号同样受保护(有 token 就是能同步的号)
with open(os.path.join(cfg, '丙.json'), 'w', encoding='utf-8') as f:
json.dump({'account': {'uid': '3'}, 'chars': [dict(old_char, level=70)],
'stats': {}, 'skd': {'data': 'x'}}, f, ensure_ascii=False)
assert operbox.import_into('丙.json', maa).get('skippedBy') == 'token'
# 账号页手动导入(用户确认过)带 force,必须照旧能覆盖
r = operbox.import_into('乙.json', maa, force=True)
assert r['imported'] == 1 and 'skippedBy' not in r, 'force 必须放行:%r' % r
after = json.load(open(os.path.join(cfg, '乙.json'), encoding='utf-8'))
assert after['chars'][0]['charId'] == six[0] and after['sync']['source'] == 'maa-operbox'


# ---------------------------------------------------------------- B15 从 MAA 直接建号
def check_create_from_maa(tmp):
Expand Down
Loading