From fb062a361e3db795dc0618c10a68a15ee3b42807 Mon Sep 17 00:00:00 2001 From: Anton Tarasenko Date: Tue, 13 Dec 2022 04:39:34 +0700 Subject: [PATCH] Change add/removeuser subcommands for usergroups command --- sources/Users/ACommandUserGroups.uc | 230 ++++++++++++++++++---------- sources/Users/UserDatabase.uc | 2 +- sources/Users/Users_Feature.uc | 1 - 3 files changed, 151 insertions(+), 82 deletions(-) diff --git a/sources/Users/ACommandUserGroups.uc b/sources/Users/ACommandUserGroups.uc index 77dcc0d..54990d2 100644 --- a/sources/Users/ACommandUserGroups.uc +++ b/sources/Users/ACommandUserGroups.uc @@ -38,22 +38,30 @@ protected function BuildData(CommandDataBuilder builder) .Describe(P("Removes a group")) .ParamText(P("group_name")); builder.SubCommand(P("adduser")) - .Describe(P("Adds new user to the group")) + .Describe(P("Adds new user to the group. Allows to also optionally" + @ "specify annotation (human-readable name) that can be thought of" + @ "as a {$TextEmphasis comment}.")) .ParamText(P("group_name")) - .ParamTextList(P("user_names")); + .ParamText(P("user_id")) + .OptionalParams() + .ParamText(P("annotation")); builder.SubCommand(P("removeuser")) - .Describe(P("Removes user from the group")) + .Describe(P("Removes user from the group. User can be specified by both" + @ "user's id or annotation, with id taking priority.")) .ParamText(P("group_name")) - .ParamTextList(P("user_names")); + .ParamText(P("user_name")); } protected function Executed(CallData arguments, EPlayer instigator) { - local Text groupName; - local ArrayList userNames; + local Text groupName, userID, userName, annotation; - groupName = arguments.parameters.GetText(P("group_name")); - userNames = arguments.parameters.GetArrayList(P("user_names")); + groupName = arguments.parameters.GetText(P("group_name")); + // For parameters named `user_id`, can only be ID + userID = arguments.parameters.GetText(P("user_id")); + // For parameters named `user_id`, can be either ID or annotation + userName = arguments.parameters.GetText(P("user_name")); + annotation = arguments.parameters.GetText(P("annotation")); if (arguments.subCommandName.IsEmpty()) { DisplayUserGroups(); } @@ -67,102 +75,164 @@ protected function Executed(CallData arguments, EPlayer instigator) RemoveGroup(groupName); } else if (arguments.subCommandName.Compare(P("adduser"), SCASE_SENSITIVE)) { - AddUser(groupName, userNames); + AddUser(groupName, userID, annotation); } else if (arguments.subCommandName.Compare(P("removeuser"), SCASE_SENSITIVE)) { - RemoveUser(groupName, userNames); + RemoveUser(groupName, userName); } _.memory.Free(groupName); - _.memory.Free(userNames); + _.memory.Free(userID); + _.memory.Free(userName); + _.memory.Free(annotation); } -private function AddUser(BaseText groupName, ArrayList userNames) +private function AddUser( + BaseText groupName, + BaseText textUserID, + BaseText annotation) { - local int i; - local Text nextTextID; - local UserID nextID; + local bool userInGroup; + local UserID id; - if (groupName == none) return; - if (userNames == none) return; + if (groupName == none) return; + if (textUserID == none) return; - for (i = 0; i < userNames.GetLength(); i += 1) + id = UserID(_.memory.Allocate(class'UserID')); + id.Initialize(textUserID); + if (_.users.IsUserIDInGroup(id, groupName)) { - nextTextID = userNames.GetText(i); - if (nextTextID == none) { - continue; - } - nextID = UserID(_.memory.Allocate(class'UserID')); - nextID.Initialize(nextTextID); - if (_.users.IsUserIDInGroup(nextID, groupName)) - { - callerConsole - .Write(P("User ")) - .UseColorOnce(_.color.Gray) - .Write(nextTextID) - .UseColorOnce(_.color.TextFailure) - .Write(P(" is already in the group ")) - .UseColorOnce(_.color.TextEmphasis) - .Write(groupName) - .WriteLine(P("!")); - } - else - { - _.users.AddUserIDToGroup(nextID, groupName); - callerConsole - .Write(F("{$TextPositive Added} user ")) - .UseColorOnce(_.color.Gray) - .Write(nextTextID) - .Write(P(" to the group ")) - .UseColorOnce(_.color.TextEmphasis) - .Write(groupName) - .WriteLine(P("!")); - } + userInGroup = true; + callerConsole + .Write(P("User ")) + .UseColorOnce(_.color.Gray) + .Write(textUserID) + .UseColorOnce(_.color.TextFailure) + .Write(P(" is already in the group ")) + .UseColorOnce(_.color.TextEmphasis) + .Write(groupName) + .WriteLine(P("!")); + } + else if (_.users.AddUserIDToGroup(id, groupName)) + { + userInGroup = true; + callerConsole + .Write(F("{$TextPositive Added} user ")) + .UseColorOnce(_.color.Gray) + .Write(textUserID) + .Write(P(" to the group ")) + .UseColorOnce(_.color.TextEmphasis) + .Write(groupName) + .WriteLine(P("!")); } + else { + // One of the reasons - NO GROUP + callerConsole + .UseColorOnce(_.color.TextFailure) + .Write(P("Failed (for unknown reason)")) + .Write(P(" to add user ")) + .UseColorOnce(_.color.Gray) + .Write(textUserID) + .Write(P(" to the group ")) + .UseColorOnce(_.color.TextEmphasis) + .Write(groupName) + .WriteLine(P("!")); + } + if (!userInGroup || annotation == none) { + return; + } + _.users.SetAnnotationForUserID(groupName, id, annotation); + _.memory.Free(id); + callerConsole + .Write(P("Annotation for user ")) + .UseColorOnce(_.color.Gray) + .Write(textUserID) + .UseColorOnce(_.color.TextPositive) + .Write(P(" in the group ")) + .UseColorOnce(_.color.TextEmphasis) + .Write(groupName) + .Write(P(" is set to ")) + .UseColorOnce(_.color.TextNeutral) + .WriteLine(annotation); } -private function RemoveUser(BaseText groupName, ArrayList userNames) +private function RemoveUser(BaseText groupName, BaseText userName) { local int i; - local Text nextTextID; - local UserID nextID; + local UserID idFromName, idToRemove; + local array annotatedUsers; - if (groupName == none) return; - if (userNames == none) return; + if (groupName == none) return; + if (userName == none) return; - for (i = 0; i < userNames.GetLength(); i += 1) + idFromName = UserID(_.memory.Allocate(class'UserID')); + idFromName.Initialize(userName); + annotatedUsers = _.users.GetAnnotatedGroupMembers(groupName); + if (idFromName.IsInitialized()) { - nextTextID = userNames.GetText(i); - if (nextTextID == none) { - continue; - } - nextID = UserID(_.memory.Allocate(class'UserID')); - nextID.Initialize(nextTextID); - if (!_.users.IsUserIDInGroup(nextID, groupName)) + for (i = 0; i < annotatedUsers.length; i += 1) { - callerConsole - .Write(P("User ")) - .UseColorOnce(_.color.Gray) - .Write(nextTextID) - .UseColorOnce(_.color.TextFailure) - .Write(P(" doesn't belong to the group ")) - .UseColorOnce(_.color.TextEmphasis) - .Write(groupName) - .WriteLine(P("!")); + if (idFromName.IsEqual(annotatedUsers[i].id)) + { + idToRemove = annotatedUsers[i].id; + break; + } } - else + } + _.memory.Free(idFromName); + if (idToRemove == none) + { + for (i = 0; i < annotatedUsers.length; i += 1) { - _.users.RemoveUserIDFromGroup(nextID, groupName); - callerConsole - .Write(F("{$TextNegative Removed} user ")) - .UseColorOnce(_.color.Gray) - .Write(nextTextID) - .Write(P(" from the group ")) - .UseColorOnce(_.color.TextEmphasis) - .Write(groupName) - .WriteLine(P("!")); + if (userName.Compare( + annotatedUsers[i].annotation, + SCASE_INSENSITIVE)) + { + idToRemove = annotatedUsers[i].id; + break; + } } } + if (idToRemove == none) + { + callerConsole + .Write(P("User ")) + .UseColorOnce(_.color.Gray) + .Write(userName) + .UseColorOnce(_.color.TextFailure) + .Write(P(" doesn't belong to the group ")) + .UseColorOnce(_.color.TextEmphasis) + .Write(groupName) + .WriteLine(P("!")); + } + else if (_.users.RemoveUserIDFromGroup(idToRemove, groupName)) + { + callerConsole + .Write(F("{$TextNegative Removed} user ")) + .UseColorOnce(_.color.Gray) + .Write(userName) + .Write(P(" from the group ")) + .UseColorOnce(_.color.TextEmphasis) + .Write(groupName) + .WriteLine(P("!")); + } + else { + callerConsole + .UseColorOnce(_.color.TextFailure) + .Write(P("Failed (for unknown reason)")) + .Write(P("to remove user ")) + .UseColorOnce(_.color.Gray) + .Write(userName) + .Write(P(" from the group ")) + .UseColorOnce(_.color.TextEmphasis) + .Write(groupName) + .WriteLine(P(".")); + } + for (i = 0; i < annotatedUsers.length; i += 1) + { + _.memory.Free(annotatedUsers[i].id); + _.memory.Free(annotatedUsers[i].annotation); + } } private function AddGroup(BaseText groupName) diff --git a/sources/Users/UserDatabase.uc b/sources/Users/UserDatabase.uc index 6990f34..348a21e 100644 --- a/sources/Users/UserDatabase.uc +++ b/sources/Users/UserDatabase.uc @@ -112,7 +112,7 @@ public final function User FetchUser(UserID userID) local User newUser; for (i = 0; i < sessionUsers.length; i += 1) { - if (sessionUsers[i].GetID().IsEqualTo(userID)) { + if (sessionUsers[i].GetID().IsEqual(userID)) { return sessionUsers[i]; } } diff --git a/sources/Users/Users_Feature.uc b/sources/Users/Users_Feature.uc index 29f22df..27fe3fd 100644 --- a/sources/Users/Users_Feature.uc +++ b/sources/Users/Users_Feature.uc @@ -1700,7 +1700,6 @@ public final function bool IsSteamIDInGroup( result = nextGroupUsers.HasKey(steamID); } _.memory.Free(nextGroupUsers); - steamID.FreeSelf(); return result; }