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

+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);
}