From 08be8654e4c16cf5355b7a9fdc6ad06cc61eb751 Mon Sep 17 00:00:00 2001 From: Dev Date: Mon, 24 Apr 2023 12:47:19 +0100 Subject: [PATCH] Refactor isNicknameTaken() --- project/src/helpers/ItemHelper.ts | 2 +- project/src/helpers/ProfileHelper.ts | 30 +++++++++++++++++++++++----- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/project/src/helpers/ItemHelper.ts b/project/src/helpers/ItemHelper.ts index 20801b5d..0c809f3f 100644 --- a/project/src/helpers/ItemHelper.ts +++ b/project/src/helpers/ItemHelper.ts @@ -339,7 +339,7 @@ class ItemHelper } /** - * Recursive function that looks at every item from parameter and gets their childrens Ids + * Recursive function that looks at every item from parameter and gets their childrens Ids + includes parent item in results * @param items * @param itemID * @returns an array of strings diff --git a/project/src/helpers/ProfileHelper.ts b/project/src/helpers/ProfileHelper.ts index d922fbb3..073696d6 100644 --- a/project/src/helpers/ProfileHelper.ts +++ b/project/src/helpers/ProfileHelper.ts @@ -124,19 +124,24 @@ export class ProfileHelper return output; } - public isNicknameTaken(info: IValidateNicknameRequestData, sessionID: string): boolean + /** + * Check if a nickname is used by another profile loaded by the server + * @param nicknameRequest + * @param sessionID Session id + * @returns True if already used + */ + public isNicknameTaken(nicknameRequest: IValidateNicknameRequestData, sessionID: string): boolean { for (const id in this.saveServer.getProfiles()) { const profile = this.saveServer.getProfile(id); - - // rome-ignore lint/complexity/useSimplifiedLogicExpression: - if (!("characters" in profile) || !("pmc" in profile.characters) || !("Info" in profile.characters.pmc)) + if (!this.profileHasInfoProperty(profile)) { continue; } - if (profile.info.id !== sessionID && profile.characters.pmc.Info.LowerNickname === info.nickname.toLowerCase()) + if (!this.sessionIdMatchesProfileId(profile.info.id, sessionID) + && this.nicknameMatches(profile.characters.pmc.Info.LowerNickname, nicknameRequest.nickname)) { return true; } @@ -145,6 +150,21 @@ export class ProfileHelper return false; } + protected profileHasInfoProperty(profile: IAkiProfile): boolean + { + return !!(profile?.characters?.pmc?.Info); + } + + protected nicknameMatches(profileName: string, nicknameRequest: string): boolean + { + return profileName.toLowerCase() === nicknameRequest.toLowerCase(); + } + + protected sessionIdMatchesProfileId(profileId: string, sessionId: string): boolean + { + return profileId === sessionId; + } + /** * Add experience to a PMC inside the players profile * @param sessionID Session id