feat(saves): localize battle and preset names, merge overview form and guard cache writes
This commit is contained in:
1 parent
e6e75d92c6
commit
7cb1011f0d
11 files changed
+276
-59
No files matched your search
@@ -108,6 +108,9 @@ export function OfficialSaveImport({ copyId, userId, readOnly, onError, onImport
|
||||
return Number.isNaN(parsed) ? iso : new Date(parsed).toLocaleString(i18n.language);
|
||||
};
|
||||
|
||||
const language = i18n.language.toLowerCase().startsWith('zh') ? 'zh' : 'en';
|
||||
const vehicleSeparator = language === 'zh' ? '、' : ', ';
|
||||
const vehicleLabel = (id: string): string => preview?.vehicleNames[id]?.[language] ?? id;
|
||||
const canImportSelection = options.importSettings || options.unlockVehicles;
|
||||
|
||||
return (
|
||||
@@ -190,8 +193,10 @@ export function OfficialSaveImport({ copyId, userId, readOnly, onError, onImport
|
||||
{preview && (
|
||||
<div className="maintenance-confirm">
|
||||
<p>{t('saves.official.summary', { files: preview.files.length, presets: preview.presets.length, vehicles: preview.vehicles.length })}</p>
|
||||
{preview.presets.map((preset, index) => <p key={index}>{preset.title} · {preset.vehicles.join(', ')}</p>)}
|
||||
{preview.omitted.length > 0 && <p>{t('saves.official.omitted', { names: preview.omitted.join(', ') })}</p>}
|
||||
{preview.presets.map((preset, index) => (
|
||||
<p key={index}>{preset.title} · {preset.vehicles.map(vehicleLabel).join(vehicleSeparator)}</p>
|
||||
))}
|
||||
{preview.omitted.length > 0 && <p>{t('saves.official.omitted', { names: preview.omitted.map(vehicleLabel).join(vehicleSeparator) })}</p>}
|
||||
</div>
|
||||
)}
|
||||
{imported && <p className="small" role="status">{t('saves.official.done')}</p>}
|
||||
|
||||
@@ -18,6 +18,10 @@ export function SaveAvatarField({ copyId, avatar, disabled, onError }: SaveAvata
|
||||
const [preview, setPreview] = useState<{ id: string; data: string } | null>(null);
|
||||
const canPreview = choices?.some(({ id }) => id === selected) ?? false;
|
||||
|
||||
useEffect(() => {
|
||||
setSelected(avatar);
|
||||
}, [avatar]);
|
||||
|
||||
useEffect(() => {
|
||||
let mounted = true;
|
||||
ipcInvoke('saves:avatars', { copyId })
|
||||
|
||||
@@ -90,45 +90,46 @@ export function SavesPage({ copy, copies, section, readOnly, onError }: SavesPag
|
||||
.catch(onError)
|
||||
.finally(() => setSaving(false));
|
||||
};
|
||||
const submitEdits = (event: FormEvent<HTMLFormElement>): void => {
|
||||
event.preventDefault();
|
||||
if (!userId) return;
|
||||
const data = new FormData(event.currentTarget);
|
||||
const values: CurrencyEdits = {};
|
||||
for (const name of CURRENCIES) {
|
||||
const value = String(data.get(name) ?? '').trim();
|
||||
if (value) values[name] = name === 'gjnCents' ? Math.round(Number(value) * 100) : Number(value);
|
||||
}
|
||||
if (Object.keys(values).length === 0) return;
|
||||
setSaving(true);
|
||||
setMessage('');
|
||||
ipcInvoke('saves:editCurrencies', { copyId: copy.id, userId, values })
|
||||
.then((updated) => {
|
||||
updateDetails(updated);
|
||||
setMessage(t('saves.saved'));
|
||||
})
|
||||
.catch(onError)
|
||||
.finally(() => setSaving(false));
|
||||
};
|
||||
|
||||
const submitAccount = (event: FormEvent<HTMLFormElement>): void => {
|
||||
const submitOverview = (event: FormEvent<HTMLFormElement>): void => {
|
||||
event.preventDefault();
|
||||
if (!details || !userId) return;
|
||||
const data = new FormData(event.currentTarget);
|
||||
|
||||
const nick = String(data.get('nick') ?? '').trim();
|
||||
const days = String(data.get('premiumDays') ?? '').trim();
|
||||
const avatar = String(data.get('avatar') ?? '');
|
||||
const level = String(data.get('level') ?? '').trim();
|
||||
const edits: AccountEdits = {};
|
||||
if (nick !== details.save.nick) edits.nick = nick;
|
||||
if (avatar && avatar !== details.save.avatar) edits.avatar = avatar;
|
||||
if (level !== '' && Number(level) !== details.save.level) edits.level = Number(level);
|
||||
if (days !== '' && Number(days) !== details.save.premiumDays) edits.premiumDays = Number(days);
|
||||
if (Object.keys(edits).length === 0) return;
|
||||
const accountEdits: AccountEdits = {};
|
||||
if (nick && nick !== details.save.nick) accountEdits.nick = nick;
|
||||
if (avatar && avatar !== details.save.avatar) accountEdits.avatar = avatar;
|
||||
if (level !== '' && Number(level) !== details.save.level) accountEdits.level = Number(level);
|
||||
if (days !== '' && Number(days) !== details.save.premiumDays) accountEdits.premiumDays = Number(days);
|
||||
|
||||
const currencyValues: CurrencyEdits = {};
|
||||
for (const name of CURRENCIES) {
|
||||
const value = String(data.get(name) ?? '').trim();
|
||||
if (value) currencyValues[name] = name === 'gjnCents' ? Math.round(Number(value) * 100) : Number(value);
|
||||
}
|
||||
|
||||
const hasAccount = Object.keys(accountEdits).length > 0;
|
||||
const hasCurrencies = Object.keys(currencyValues).length > 0;
|
||||
if (!hasAccount && !hasCurrencies) return;
|
||||
|
||||
setSaving(true);
|
||||
setMessage('');
|
||||
ipcInvoke('saves:editAccount', { copyId: copy.id, userId, edits })
|
||||
.then((updated) => { updateDetails(updated); setMessage(t('saves.saved')); })
|
||||
(async () => {
|
||||
let updated: SaveDetails | null = null;
|
||||
if (hasAccount) {
|
||||
updated = await ipcInvoke('saves:editAccount', { copyId: copy.id, userId, edits: accountEdits });
|
||||
}
|
||||
if (hasCurrencies) {
|
||||
updated = await ipcInvoke('saves:editCurrencies', { copyId: copy.id, userId, values: currencyValues });
|
||||
}
|
||||
if (updated) {
|
||||
updateDetails(updated);
|
||||
setMessage(t('saves.saved'));
|
||||
}
|
||||
})()
|
||||
.catch(onError)
|
||||
.finally(() => setSaving(false));
|
||||
};
|
||||
@@ -238,13 +239,29 @@ export function SavesPage({ copy, copies, section, readOnly, onError }: SavesPag
|
||||
|
||||
if (!details) return <Sheet title={title} aside={copy.name}><p className="lead">{t(userId ? 'saves.loading' : 'saves.empty')}</p></Sheet>;
|
||||
const { save } = details;
|
||||
const language = i18n.language.toLowerCase().startsWith('zh') ? 'zh' : 'en';
|
||||
const vehicleSeparator = language === 'zh' ? '、' : ', ';
|
||||
const overviewKey = JSON.stringify([
|
||||
copy.id,
|
||||
save.userId,
|
||||
save.nick,
|
||||
save.avatar,
|
||||
save.level,
|
||||
save.premiumDays,
|
||||
save.currencies?.silverLions,
|
||||
save.currencies?.goldenEagles,
|
||||
save.currencies?.gjnCents,
|
||||
save.currencies?.freeRp,
|
||||
]);
|
||||
return (
|
||||
<Sheet title={title} aside={save.nick}>
|
||||
{section === 'overview' && (
|
||||
<>
|
||||
{readOnly && <p className="small">{t('saves.readOnly')}</p>}
|
||||
<Row label={t('saves.login')}>{save.login}</Row>
|
||||
<form key={JSON.stringify([copy.id, save.userId, save.nick, save.avatar, save.level, save.premiumDays])} onSubmit={submitAccount}>
|
||||
<Row label={t('saves.vehicles')}>{format.format(save.vehicleCount)}</Row>
|
||||
<Row label={t('saves.battles')}>{format.format(save.battleCount)}</Row>
|
||||
<form key={overviewKey} onSubmit={submitOverview}>
|
||||
<Row label={t('saves.nick')}>
|
||||
<input className="input" aria-label={t('saves.nick')} name="nick" defaultValue={save.nick} disabled={readOnly || saving} required />
|
||||
</Row>
|
||||
@@ -259,11 +276,6 @@ export function SavesPage({ copy, copies, section, readOnly, onError }: SavesPag
|
||||
<input className="input" aria-label={t('saves.premiumDays')} type="number" min="0" step="1"
|
||||
name="premiumDays" defaultValue={save.premiumDays} disabled={readOnly || saving} />
|
||||
</Row>
|
||||
<button type="submit" className="button" disabled={readOnly || saving}>{t('saves.saveAccount')}</button>
|
||||
</form>
|
||||
<Row label={t('saves.vehicles')}>{format.format(save.vehicleCount)}</Row>
|
||||
<Row label={t('saves.battles')}>{format.format(save.battleCount)}</Row>
|
||||
<form key={save.userId} onSubmit={submitEdits}>
|
||||
{CURRENCIES.map((name) => (
|
||||
<Row key={name} label={t(`saves.${name}`)}>
|
||||
<span>{save.currencies ? format.format(name === 'gjnCents' ? save.currencies[name] / 100 : save.currencies[name]) : '—'}</span>
|
||||
@@ -271,19 +283,31 @@ export function SavesPage({ copy, copies, section, readOnly, onError }: SavesPag
|
||||
aria-label={t(`saves.${name}`)} disabled={readOnly || saving} />
|
||||
</Row>
|
||||
))}
|
||||
<button type="submit" className="button button-primary" disabled={readOnly || saving}>{t('saves.apply')}</button>
|
||||
{message && <p className="small" role="status">{message}</p>}
|
||||
<div className="save-overview-actions">
|
||||
<button type="submit" className="button button-primary" disabled={readOnly || saving}>{t('saves.apply')}</button>
|
||||
{message && <p className="small" role="status">{message}</p>}
|
||||
</div>
|
||||
</form>
|
||||
</>
|
||||
)}
|
||||
{section === 'battles' && (
|
||||
details.battles.length === 0 ? <p className="lead">{t('saves.noBattles')}</p> : details.battles.map((battle) => (
|
||||
<Row key={battle.id} label={battle.mission || battle.mode} hint={date(battle.time)}>
|
||||
<span>{battle.won === null ? '—' : t(battle.won ? 'saves.win' : 'saves.loss')}</span>
|
||||
<span>{t('saves.battleRewards', { sl: format.format(battle.silverLions), rp: format.format(battle.researchPoints) })}</span>
|
||||
<span>{battle.vehicles.join(', ')}</span>
|
||||
</Row>
|
||||
))
|
||||
details.battles.length === 0 ? <p className="lead">{t('saves.noBattles')}</p> : details.battles.map((battle) => {
|
||||
const missionText = battle.missionName[language] || battle.mission || battle.mode;
|
||||
const vehiclesText = battle.vehicleNames.map((entry) => entry[language]).join(vehicleSeparator);
|
||||
return (
|
||||
<Row key={battle.id} label={missionText} hint={date(battle.time)}>
|
||||
<div className="battle-row">
|
||||
<div className="battle-summary">
|
||||
<span className={battle.won === true ? 'ok' : battle.won === false ? 'warn' : undefined}>
|
||||
{battle.won === null ? '—' : t(battle.won ? 'saves.win' : 'saves.loss')}
|
||||
</span>
|
||||
<span>{t('saves.battleRewards', { sl: format.format(battle.silverLions), rp: format.format(battle.researchPoints) })}</span>
|
||||
</div>
|
||||
{vehiclesText && <div className="battle-vehicles">{vehiclesText}</div>}
|
||||
</div>
|
||||
</Row>
|
||||
);
|
||||
})
|
||||
)}
|
||||
{section === 'official' && (
|
||||
<OfficialSaveImport key={save.userId} copyId={copy.id} userId={save.userId} readOnly={readOnly} onError={onError} onImported={refreshSave} />
|
||||
|
||||
@@ -162,6 +162,11 @@
|
||||
border-top: 0;
|
||||
}
|
||||
|
||||
.row-label {
|
||||
min-width: 0;
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
|
||||
.row-label small {
|
||||
display: block;
|
||||
margin-top: 2px;
|
||||
|
||||
@@ -311,6 +311,38 @@
|
||||
margin-top: 16px;
|
||||
}
|
||||
|
||||
.save-overview-actions {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
margin-top: 14px;
|
||||
}
|
||||
|
||||
.save-overview-actions .small {
|
||||
margin-top: 0;
|
||||
}
|
||||
|
||||
.battle-row {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 4px;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.battle-summary {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.battle-vehicles {
|
||||
font-size: 12.5px;
|
||||
color: var(--text-dim);
|
||||
line-height: 1.45;
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
|
||||
/* ===== About ===== */
|
||||
.update-notes {
|
||||
white-space: pre-line;
|
||||
|
||||
@@ -4,10 +4,11 @@ import { CopyIdSchema } from './install.ts';
|
||||
export const LocalUserIdSchema = z.string().regex(/^\d+$/);
|
||||
|
||||
export const SaveKeysSchema = z.object({ copyId: CopyIdSchema, userId: LocalUserIdSchema });
|
||||
export const LocalizedNameSchema = z.object({ en: z.string(), zh: z.string() });
|
||||
export const SaveAvatarIdSchema = z.string().regex(/^cardicon_[a-z0-9_]+$/);
|
||||
export const SaveAvatarChoicesSchema = z.array(z.object({
|
||||
id: SaveAvatarIdSchema,
|
||||
name: z.object({ en: z.string(), zh: z.string() }),
|
||||
name: LocalizedNameSchema,
|
||||
}));
|
||||
export type SaveAvatarChoices = z.output<typeof SaveAvatarChoicesSchema>;
|
||||
|
||||
@@ -48,8 +49,10 @@ export const SaveDetailsSchema = z.object({
|
||||
save: SaveSummarySchema,
|
||||
maxLevel: z.number().int().positive().nullable(),
|
||||
battles: z.array(z.object({
|
||||
id: z.string(), time: z.number(), won: z.boolean().nullable(), mission: z.string(), mode: z.string(),
|
||||
vehicles: z.array(z.string()), silverLions: z.number(), researchPoints: z.number(),
|
||||
id: z.string(), time: z.number(), won: z.boolean().nullable(), mission: z.string(),
|
||||
missionName: LocalizedNameSchema, mode: z.string(),
|
||||
vehicles: z.array(z.string()), vehicleNames: z.array(LocalizedNameSchema),
|
||||
silverLions: z.number(), researchPoints: z.number(),
|
||||
})),
|
||||
owned: z.array(z.string()),
|
||||
research: z.array(z.object({ name: z.string(), points: z.number() })),
|
||||
@@ -87,6 +90,7 @@ export const OfficialPreviewSchema = z.object({
|
||||
files: z.array(z.object({ name: z.string(), version: z.number().int() })),
|
||||
presets: z.array(z.object({ country: z.string(), title: z.string(), vehicles: z.array(z.string()) })),
|
||||
vehicles: z.array(z.string()),
|
||||
vehicleNames: z.record(z.string(), LocalizedNameSchema),
|
||||
omitted: z.array(z.string()),
|
||||
});
|
||||
export const ResearchBranchSchema = z.object({ country: z.string().min(1), type: z.string().min(1) });
|
||||
|
||||
Reference in new issue
Block a user