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:
1 parent
1d3af395c2
commit
d20b7f4249
13 files changed
+127
-19
No files matched your search
@@ -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)}`);
|
||||
|
||||
Reference in new issue
Block a user