Refactor isNicknameTaken()

This commit is contained in:
Dev 2023-04-24 12:47:19 +01:00
parent 2c3e61b413
commit 08be8654e4
2 changed files with 26 additions and 6 deletions

View File

@ -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

View File

@ -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: <explanation>
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