Edit research branches in local saves

This commit is contained in:
N0VAN0M4LY committed 2026-11-29 16:16:42 +08:00
1 parent 3e2acc5a17
commit 3e08e9314e
14 files changed
+291 -15

No files matched your search

+4 -1
View File
@@ -13,7 +13,7 @@ import { createCopyDraft, listCopies, readInstallRecord, renameCopy, setCurrentC
import { readInstalledLab } from '../services/launch/lab-handoff.ts';
import { getLaunchState, openServerStatus, startLaunch, stopLaunch } from '../services/launch/launch-controller.ts';
import { resourcePaths } from '../services/resources/resource-paths.ts';
import { createSave, exportSave, getSaveDetails, getSaveList, importSelectedBackups, listSaveBackups, pickOfficialBackups, pickSaveArchive, previewSelectedBackups, removeSave, restoreSaveBackup, saveWriteInProgress, selectSave, setSaveCurrencies, takeSaveBackup, transferSave } from '../services/saves/manage-saves.ts';
import { changeSaveResearch, createSave, exportSave, getSaveDetails, getSaveList, getSaveResearch, importSelectedBackups, listSaveBackups, pickOfficialBackups, pickSaveArchive, previewSelectedBackups, removeSave, restoreSaveBackup, saveWriteInProgress, selectSave, setSaveCurrencies, takeSaveBackup, transferSave } from '../services/saves/manage-saves.ts';
import { broadcast, handle } from './register.ts';
/** Registers the handler of every contract channel. One line per channel; the work is in services/. */
@@ -124,6 +124,9 @@ export function registerIpcHandlers(): void {
handle('saves:importOfficial', ({ copyId, userId, paths, options }) =>
importSelectedBackups(copyId, userId, paths, options));
handle('saves:research', ({ copyId, userId, branch }) => getSaveResearch(copyId, userId, branch));
handle('saves:editResearch', ({ copyId, userId, branch, action }) => changeSaveResearch(copyId, userId, branch, action));
handle('launch:getState', () => getLaunchState());
handle('launch:getLabInfo', () => readInstalledLab().catch(() => null));
handle('launch:start', () => {
@@ -3,7 +3,7 @@ import { app, dialog, utilityProcess, type BrowserWindow } from 'electron';
import { compareVersions, isInSteamLibrary } from '@wto/patcher';
import { AppError } from '@shared/errors.ts';
import type { InstallRecord } from '@shared/schemas/install.ts';
import type { CurrencyEdits, OfficialImportOptions, OfficialPreview, SaveBackup, SaveDetails, SaveList, SaveRegistration, SaveTransfer } from '@shared/schemas/saves.ts';
import type { CurrencyEdits, OfficialImportOptions, OfficialPreview, SaveBackup, SaveDetails, SaveList, SaveRegistration, SaveTransfer, ResearchBranch, ResearchOperation, SaveResearch } from '@shared/schemas/saves.ts';
import type { SaveReply, SaveResult, SaveTask } from '../../workers/save-protocol.ts';
import { maintenanceInProgress } from '../copies/copy-maintenance.ts';
import { canonicalCopyPath, copyPathContains, listCopies, updateCopyRecord } from '../install/install-record.ts';
@@ -198,6 +198,18 @@ async function verifyBackupLocations(copy: InstallRecord, paths: readonly string
}));
}
export async function getSaveResearch(copyId: string, userId: string, branch: ResearchBranch | undefined): Promise<SaveResearch> {
const copy = await requireCopy(copyId);
return await run({ operation: 'readResearch', ...location(copy), userId, ...(branch ? { branch } : {}) }) as SaveResearch;
}
export async function changeSaveResearch(copyId: string, userId: string, branch: ResearchBranch, action: ResearchOperation): Promise<SaveResearch> {
return write(async () => {
const copy = await requireCopy(copyId);
return await run({ operation: 'editResearch', ...location(copy), userId, branch, action }) as SaveResearch;
});
}
async function validateProfile(archive: SaveArchive, copy: InstallRecord): Promise<void> {
const profile = archive.files['profile.blk'];
if (!profile) return;
@@ -1,5 +1,5 @@
import type { AppErrorCode } from '@shared/errors.ts';
import type { CurrencyEdits, OfficialImportOptions, OfficialPreview, SaveDetails, SaveList } from '@shared/schemas/saves.ts';
import type { CurrencyEdits, OfficialImportOptions, OfficialPreview, ResearchBranch, ResearchOperation, SaveDetails, SaveList, SaveResearch } from '@shared/schemas/saves.ts';
interface Location {
readonly gameFolder: string;
@@ -12,9 +12,11 @@ export type SaveTask = Location & (
| { readonly operation: 'edit'; readonly userId: string; readonly values: CurrencyEdits }
| { readonly operation: 'previewOfficial'; readonly paths: readonly string[]; readonly includeUsedVehicles: boolean }
| { readonly operation: 'importOfficial'; readonly paths: readonly string[]; readonly userId: string; readonly options: OfficialImportOptions }
| { readonly operation: 'readResearch'; readonly userId: string; readonly branch?: ResearchBranch }
| { readonly operation: 'editResearch'; readonly userId: string; readonly branch: ResearchBranch; readonly action: ResearchOperation }
| { readonly operation: 'remove'; readonly userId: string }
| { readonly operation: 'filter'; readonly profile: Uint8Array; readonly filter: boolean }
| { readonly operation: 'create'; readonly login: string; readonly password: string; readonly nick: string }
);
export type SaveResult = SaveList['saves'] | SaveDetails | OfficialPreview | { userId: string } | Uint8Array | null;
export type SaveResult = SaveList['saves'] | SaveDetails | OfficialPreview | SaveResearch | { userId: string } | Uint8Array | null;
export type SaveReply = { ok: true; result: SaveResult } | { ok: false; error: AppErrorCode };
@@ -1,5 +1,5 @@
import type { MessageEvent } from 'electron';
import { AccountStore, editSaveCurrencies, importOfficialBackup, listSaveSummaries, prepareTransferredProfile, previewOfficialBackup, readSaveDetails } from '@wto/server';
import { AccountStore, editSaveCurrencies, editSaveResearch, importOfficialBackup, listSaveSummaries, prepareTransferredProfile, previewOfficialBackup, readSaveDetails, readSaveResearch } from '@wto/server';
import type { SaveTask, SaveReply } from './save-protocol.ts';
function execute(task: SaveTask): SaveReply {
@@ -17,6 +17,14 @@ function execute(task: SaveTask): SaveReply {
return { ok: true, result: previewOfficialBackup(task, task.paths, task.includeUsedVehicles) };
case 'importOfficial':
return { ok: true, result: importOfficialBackup(task, task.userId, task.paths, task.options) };
case 'readResearch': {
const result = readSaveResearch(task, task.userId, task.branch);
return result ? { ok: true, result } : { ok: false, error: 'save_not_found' };
}
case 'editResearch': {
const result = editSaveResearch(task, task.userId, task.branch, task.action);
return result ? { ok: true, result } : { ok: false, error: 'save_operation_failed' };
}
case 'remove':
return new AccountStore(task.dataFolder).remove(task.userId)
? { ok: true, result: null } : { ok: false, error: 'save_not_found' };
@@ -261,6 +261,38 @@
"official": "Import official backup",
"backup": "Backup and restore"
},
"tree": {
"branch": "Country and vehicle type",
"country": "Country",
"type": "Vehicle type",
"actions": "Bulk actions",
"research": "Research all",
"purchase": "Grant regular vehicles",
"equipment": "Unlock modifications",
"empty": "This copy has no eligible research branches.",
"rank": "Rank {{rank}}",
"owned": "Owned",
"points": "{{earned}} / {{needed}} RP",
"countries": {
"country_usa": "United States",
"country_germany": "Germany",
"country_ussr": "USSR",
"country_britain": "Great Britain",
"country_japan": "Japan",
"country_china": "China",
"country_italy": "Italy",
"country_france": "France",
"country_sweden": "Sweden",
"country_israel": "Israel"
},
"types": {
"Aircraft": "Aircraft",
"Tank": "Ground vehicles",
"Ship": "Bluewater fleet",
"Boat": "Coastal fleet",
"Helicopter": "Helicopters"
}
},
"official": {
"files": "Backup files",
"fileHint": "Select copies of global.blk, machine.blk or storage.blk.",
@@ -261,6 +261,38 @@
"official": "导入正版备份",
"backup": "备份与还原"
},
"tree": {
"branch": "国家与兵种",
"country": "国家",
"type": "兵种",
"actions": "批量操作",
"research": "全部研发",
"purchase": "获得常规载具",
"equipment": "解锁改装件",
"empty": "此副本没有可编辑的研发分支。",
"rank": "等级 {{rank}}",
"owned": "已拥有",
"points": "{{earned}} / {{needed}} 研发点",
"countries": {
"country_usa": "美国",
"country_germany": "德国",
"country_ussr": "苏联",
"country_britain": "英国",
"country_japan": "日本",
"country_china": "中国",
"country_italy": "意大利",
"country_france": "法国",
"country_sweden": "瑞典",
"country_israel": "以色列"
},
"types": {
"Aircraft": "空军",
"Tank": "陆军",
"Ship": "远洋舰队",
"Boat": "近岸舰队",
"Helicopter": "直升机"
}
},
"official": {
"files": "备份文件",
"fileHint": "请选择复制备份的 global.blk、machine.blk 或 storage.blk。",
@@ -0,0 +1,91 @@
import { useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import type { ResearchBranch, ResearchOperation, SaveResearch as ResearchData } from '@shared/schemas/saves.ts';
import { Row } from '../../components/Row.tsx';
import { ipcInvoke } from '../../ipc.ts';
interface Props {
readonly copyId: string;
readonly userId: string;
readonly readOnly: boolean;
readonly onError: (error: unknown) => void;
}
const ACTIONS = ['research', 'purchase', 'equipment'] as const;
export function SaveResearch({ copyId, userId, readOnly, onError }: Props) {
const { t, i18n } = useTranslation();
const [research, setResearch] = useState<ResearchData | null>(null);
const [busy, setBusy] = useState(false);
const [done, setDone] = useState(false);
const keys = { copyId, userId };
useEffect(() => {
let mounted = true;
ipcInvoke('saves:research', { copyId, userId }).then((result) => {
if (mounted) setResearch(result);
}).catch(onError);
return () => { mounted = false; };
}, [copyId, userId, onError]);
function choose(branch: ResearchBranch | undefined): void {
if (!branch) return;
setBusy(true);
setDone(false);
ipcInvoke('saves:research', { ...keys, branch }).then(setResearch).catch(onError).finally(() => setBusy(false));
}
function run(action: ResearchOperation): void {
if (!research?.selected) return;
setBusy(true);
setDone(false);
ipcInvoke('saves:editResearch', { ...keys, branch: research.selected, action }).then((result) => {
setResearch(result);
setDone(true);
}).catch(onError).finally(() => setBusy(false));
}
if (!research) return <p className="lead">{t('saves.loading')}</p>;
const selected = research.selected;
if (!selected) return <p className="lead">{t('saves.tree.empty')}</p>;
const countries = [...new Set(research.branches.map(({ country }) => country))];
const types = research.branches.filter(({ country }) => country === selected.country);
const ranks = [...new Set(research.vehicles.map(({ rank }) => rank))];
const number = new Intl.NumberFormat(i18n.language);
return (
<>
<Row label={t('saves.tree.branch')}>
<select aria-label={t('saves.tree.country')} className="select" disabled={busy} value={selected.country} onChange={(event) => {
const country = event.target.value;
const branches = research.branches.filter((branch) => branch.country === country);
choose(branches.find(({ type }) => type === selected.type) ?? branches[0]);
}}>
{countries.map((country) => <option value={country} key={country}>{t(`saves.tree.countries.${country}`, country)}</option>)}
</select>
<select aria-label={t('saves.tree.type')} className="select" disabled={busy} value={selected.type} onChange={(event) => choose({ country: selected.country, type: event.target.value })}>
{types.map(({ type }) => <option value={type} key={type}>{t(`saves.tree.types.${type}`, type)}</option>)}
</select>
</Row>
<Row label={t('saves.tree.actions')}>
{ACTIONS.map((action) => <button type="button" className="button" key={action} disabled={busy || readOnly} onClick={() => run(action)}>
{t(`saves.tree.${action}`)}
</button>)}
</Row>
{readOnly && <p className="small">{t('saves.readOnly')}</p>}
{done && <p className="small" role="status">{t('saves.saved')}</p>}
{ranks.map((rank) => (
<section key={rank} className="save-research-group">
<h3 className="section-title">{t('saves.tree.rank', { rank })}</h3>
<div className="copy-list">
{research.vehicles.filter((vehicle) => vehicle.rank === rank).map((vehicle) => (
<article key={vehicle.name} className="copy-list-card">
<strong>{vehicle.name}</strong>
<span>{vehicle.owned ? t('saves.tree.owned') : t('saves.tree.points', { earned: number.format(vehicle.points), needed: number.format(vehicle.needed) })}</span>
</article>
))}
</div>
</section>
))}
</>
);
}
@@ -7,6 +7,7 @@ import { Sheet } from '../../components/Sheet.tsx';
import { ipcInvoke } from '../../ipc.ts';
import type { SaveSection } from '../../shell/navigation.ts';
import { OfficialSaveImport } from './OfficialSaveImport.tsx';
import { SaveResearch } from './SaveResearch.tsx';
import { SaveBackups } from './SaveBackups.tsx';
interface SavesPageProps {
@@ -239,12 +240,7 @@ export function SavesPage({ copy, copies, section, readOnly, onError }: SavesPag
)}
{section === 'official' && <OfficialSaveImport key={save.userId} copyId={copy.id} userId={save.userId} readOnly={readOnly} onError={onError} />}
{section === 'backup' && <SaveBackups key={save.userId} copyId={copy.id} userId={save.userId} readOnly={readOnly} onError={onError} onRestored={updateDetails} />}
{section === 'vehicles' && (
<>
<Row label={t('saves.owned')}>{details.owned.length ? details.owned.join(', ') : '—'}</Row>
<Row label={t('saves.research')}>{details.research.length ? details.research.map(({ name, points }) => `${name} · ${format.format(points)}`).join(', ') : '—'}</Row>
</>
)}
{section === 'vehicles' && <SaveResearch key={save.userId} copyId={copy.id} userId={save.userId} readOnly={readOnly} onError={onError} />}
</Sheet>
);
}
@@ -307,6 +307,10 @@
margin-top: 12px;
}
.save-research-group {
margin-top: 16px;
}
/* ===== About ===== */
.about-head {
display: flex;
+9 -1
View File
@@ -15,7 +15,7 @@ import {
} from '../schemas/install.ts';
import { LabInfoSchema, LaunchStateSchema } from '../schemas/launch.ts';
import { MaintenanceInfoSchema, MaintenanceProgressSchema, MaintenanceRequestSchema } from '../schemas/maintenance.ts';
import { OfficialImportOptionsSchema, OfficialPreviewSchema, SaveOfficialRequestSchema, CurrencyEditsSchema, SaveBackupNameSchema, SaveBackupSchema, SaveDetailsSchema, SaveKeysSchema, SaveListSchema, SaveRegistrationSchema, SaveTransferSchema } from '../schemas/saves.ts';
import { ResearchBranchSchema, ResearchOperationSchema, SaveResearchSchema, OfficialImportOptionsSchema, OfficialPreviewSchema, SaveOfficialRequestSchema, CurrencyEditsSchema, SaveBackupNameSchema, SaveBackupSchema, SaveDetailsSchema, SaveKeysSchema, SaveListSchema, SaveRegistrationSchema, SaveTransferSchema } from '../schemas/saves.ts';
import { SettingsPatchSchema, SettingsSchema } from '../schemas/settings.ts';
/**
@@ -188,6 +188,14 @@ export const ipcContract = {
input: SaveOfficialRequestSchema.extend({ options: OfficialImportOptionsSchema }),
output: OfficialPreviewSchema,
},
'saves:research': {
input: SaveKeysSchema.extend({ branch: ResearchBranchSchema.optional() }),
output: SaveResearchSchema,
},
'saves:editResearch': {
input: SaveKeysSchema.extend({ branch: ResearchBranchSchema, action: ResearchOperationSchema }),
output: SaveResearchSchema,
},
'launch:getState': {
input: z.void(),
output: LaunchStateSchema,
+13
View File
@@ -64,6 +64,19 @@ export const OfficialPreviewSchema = z.object({
vehicles: z.array(z.string()),
omitted: z.array(z.string()),
});
export const ResearchBranchSchema = z.object({ country: z.string().min(1), type: z.string().min(1) });
export const ResearchOperationSchema = z.enum(['research', 'purchase', 'equipment']);
export const SaveResearchSchema = z.object({
branches: z.array(ResearchBranchSchema),
selected: ResearchBranchSchema.nullable(),
vehicles: z.array(z.object({
name: z.string(), rank: z.number(), owned: z.boolean(), points: z.number(), needed: z.number(),
})),
});
export type ResearchBranch = z.output<typeof ResearchBranchSchema>;
export type ResearchOperation = z.output<typeof ResearchOperationSchema>;
export type SaveResearch = z.output<typeof SaveResearchSchema>;
export type OfficialPreview = z.output<typeof OfficialPreviewSchema>;
export type OfficialImportOptions = z.output<typeof OfficialImportOptionsSchema>;