feat(saves): edit local save nicknames and premium days

Let the launcher update a save's account nickname and replace or clear its remaining premium time. Reuse registration validation and preserve the existing login and password hash.
This commit is contained in:
N0VAN0M4LY committed 2026-11-29 17:41:46 +08:00
1 parent 1d3af395c2
commit d20b7f4249
13 files changed
+127 -19

No files matched your search

+21 -3
View File
@@ -61,12 +61,12 @@ export class AccountStore {
register(login: string, password: string, nick: string): RegistrationResult {
login = login.trim();
nick = nick.trim();
const displayName = normalizedNickname(nick);
if (!login || /[\s\p{Cc}]/u.test(login)) return { kind: 'invalid-login' };
if (!password) return { kind: 'invalid-password' };
if (!nick || [...nick].length > NICK_MAX_LENGTH || /\p{Cc}/u.test(nick)) return { kind: 'invalid-nick' };
if (displayName === undefined) return { kind: 'invalid-nick' };
if (this.#find(login)) return { kind: 'already-registered' };
return { kind: 'registered', account: this.#create(login, password, nick) };
return { kind: 'registered', account: this.#create(login, password, displayName) };
}
#create(login: string, password: string, nick: string): Account {
@@ -78,6 +78,19 @@ export class AccountStore {
return account;
}
rename(userId: string, nick: string): 'updated' | 'invalid-nick' | 'not-found' {
const displayName = normalizedNickname(nick);
if (displayName === undefined) return 'invalid-nick';
const prior = this.byUserId(userId);
if (!prior) return 'not-found';
if (prior.nick === displayName) return 'updated';
const accounts = this.#file.accounts.map((account) => account.userId === userId ? { ...account, nick: displayName } : account);
const file = { ...this.#file, accounts };
writeFileAtomic(this.#path, JSON.stringify(file, null, 2));
this.#file = file;
return 'updated';
}
remove(userId: string): boolean {
const accounts = this.#file.accounts.filter((account) => account.userId !== userId);
if (accounts.length === this.#file.accounts.length) return false;
@@ -115,6 +128,11 @@ export class AccountStore {
}
}
export function normalizedNickname(nick: string): string | undefined {
const value = nick.trim();
return value && [...value].length <= NICK_MAX_LENGTH && !/\p{Cc}/u.test(value) ? value : undefined;
}
function readAccountFile(path: string): AccountFile {
const file = JSON.parse(readFileSync(path, 'utf8')) as AccountFile;
if (file.formatVersion !== 1) throw new Error(`${path}: unsupported account file version ${String(file.formatVersion)}`);
+1 -1
View File
@@ -7,7 +7,7 @@ export { AccountStore, type RegistrationResult } from './accounts/account-store.
export { REGISTRATION_PATH } from './accounts/registration-service.ts';
export { HEALTH_PATH } from './transport/http-listener.ts';
export { editSaveResearch, readSaveResearch } from './saves/save-research.ts';
export { editSaveCurrencies, listSaveSummaries, readSaveDetails } from './saves/save-editor.ts';
export { editSaveAccount, editSaveCurrencies, listSaveSummaries, readSaveDetails } from './saves/save-editor.ts';
export { importOfficialBackup, previewOfficialBackup } from './saves/import/official-backup.ts';
export { prepareTransferredProfile } from './saves/transferred-profile.ts';
export { readLauncherTuning, writeLauncherTuning, type LauncherTuningPatch } from './tuning/launcher-tuning.ts';
@@ -45,6 +45,15 @@ export function extendPremiumAccount(profile: BlkBlock, days: number, nowFiletim
setParam(entry, 'expiredTime', { type: 'int64', value: base + BigInt(days) * TICKS_PER_DAY });
}
export function setPremiumDays(profile: BlkBlock, days: number, nowFiletime: bigint): void {
if (!Number.isSafeInteger(days) || days < 0) throw new RangeError('Premium days must be a whole non-negative number.');
const expires = nowFiletime + BigInt(days) * TICKS_PER_DAY;
if (expires >= (1n << 63n)) throw new RangeError('Premium expiration exceeds the saved format.');
const entry = ensureBlock(ensureBlock(profile, ENTITLEMENTS_INFO), PREMIUM_ACCOUNT_ENTITLEMENT);
setParam(entry, 'count', { type: 'int', value: days > 0 ? Math.max(1, intField(entry, 'count')) : 0 });
setParam(entry, 'expiredTime', { type: 'int64', value: expires });
}
/** Remaining whole days (rounded up) of Premium Account, or 0 when inactive. */
export function remainingPremiumDays(profile: BlkBlock, nowFiletime: bigint): number {
const entry = findBlock(findBlock(profile, ENTITLEMENTS_INFO) ?? EMPTY_BLK, PREMIUM_ACCOUNT_ENTITLEMENT);
+29 -8
View File
@@ -1,13 +1,13 @@
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { decodeBlk, findBlock, findParam, type BlkBlock } from '@wto/blk';
import { AccountStore, type Account } from '../accounts/account-store.ts';
import { AccountStore, normalizedNickname, type Account } from '../accounts/account-store.ts';
import { ClanStore } from '../clans/clan-store.ts';
import { DatasetCache } from '../game-data/datasets/dataset-cache.ts';
import { GameFiles } from '../game-data/game-files.ts';
import { intField, stringField } from '../profile/blk-fields.ts';
import { balanceOf, setGjnCents, setGoldenEagles, setSilverLions } from '../profile/profile-currency.ts';
import { remainingPremiumDays } from '../profile/profile-entitlements.ts';
import { remainingPremiumDays, setPremiumDays } from '../profile/profile-entitlements.ts';
import { ownedVehicles } from '../profile/profile-hangar.ts';
import { freeResearchPoints, setFreeResearchPoints } from '../profile/profile-research-points.ts';
import { EULT_SESSION_RESULT } from '../profile/profile-trophies-log.ts';
@@ -87,15 +87,27 @@ export function readSaveDetails(dataFolder: string, userId: string) {
return { save: summary(account, profile), battles: profile ? battleEntries(profile) : [], owned, research };
}
export interface AccountEdits {
readonly nick?: string | undefined;
readonly premiumDays?: number | undefined;
}
export function editSaveAccount(location: SaveLocation, userId: string, edits: AccountEdits): 'updated' | 'invalid-nick' | 'not-found' {
const accounts = new AccountStore(location.dataFolder);
if (!accounts.byUserId(userId)) return 'not-found';
const nick = edits.nick === undefined ? undefined : normalizedNickname(edits.nick);
if (edits.nick !== undefined && nick === undefined) return 'invalid-nick';
const days = edits.premiumDays;
if (days !== undefined) {
updateProfile(location, accounts, userId, (profile) => setPremiumDays(profile, days, serverTimeNow()));
}
return nick === undefined ? 'updated' : accounts.rename(userId, nick);
}
export function editSaveCurrencies(location: SaveLocation, userId: string, values: CurrencyEdits): boolean {
const accounts = new AccountStore(location.dataFolder);
if (!accounts.byUserId(userId)) return false;
const tuning = loadTuning(location.dataFolder, () => {});
const gameFiles = new GameFiles(location.gameFolder);
const datasets = new DatasetCache(location.dataFolder, gameFiles, () => {});
const clans = new ClanStore(location.dataFolder, tuning.clans);
const { profiles } = setUpProfiles(location.dataFolder, gameFiles, datasets, tuning, { accounts, clans });
profiles.update(userId, (profile) => {
updateProfile(location, accounts, userId, (profile) => {
if (values.silverLions !== undefined) setSilverLions(profile, values.silverLions);
if (values.goldenEagles !== undefined) setGoldenEagles(profile, values.goldenEagles);
if (values.gjnCents !== undefined) setGjnCents(profile, values.gjnCents);
@@ -103,3 +115,12 @@ export function editSaveCurrencies(location: SaveLocation, userId: string, value
});
return true;
}
function updateProfile(location: SaveLocation, accounts: AccountStore, userId: string, change: (profile: BlkBlock) => void): void {
const tuning = loadTuning(location.dataFolder, () => {});
const gameFiles = new GameFiles(location.gameFolder);
const datasets = new DatasetCache(location.dataFolder, gameFiles, () => {});
const clans = new ClanStore(location.dataFolder, tuning.clans);
const { profiles } = setUpProfiles(location.dataFolder, gameFiles, datasets, tuning, { accounts, clans });
profiles.update(userId, change);
}