Browse Source

Change add/removeuser subcommands for usergroups command

pull/8/head
Anton Tarasenko 2 years ago
parent
commit
fb062a361e
  1. 156
      sources/Users/ACommandUserGroups.uc
  2. 2
      sources/Users/UserDatabase.uc
  3. 1
      sources/Users/Users_Feature.uc

156
sources/Users/ACommandUserGroups.uc

@ -38,22 +38,30 @@ protected function BuildData(CommandDataBuilder builder)
.Describe(P("Removes a group")) .Describe(P("Removes a group"))
.ParamText(P("group_name")); .ParamText(P("group_name"));
builder.SubCommand(P("adduser")) 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")) .ParamText(P("group_name"))
.ParamTextList(P("user_names")); .ParamText(P("user_id"))
.OptionalParams()
.ParamText(P("annotation"));
builder.SubCommand(P("removeuser")) 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")) .ParamText(P("group_name"))
.ParamTextList(P("user_names")); .ParamText(P("user_name"));
} }
protected function Executed(CallData arguments, EPlayer instigator) protected function Executed(CallData arguments, EPlayer instigator)
{ {
local Text groupName; local Text groupName, userID, userName, annotation;
local ArrayList userNames;
groupName = arguments.parameters.GetText(P("group_name")); groupName = arguments.parameters.GetText(P("group_name"));
userNames = arguments.parameters.GetArrayList(P("user_names")); // 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()) { if (arguments.subCommandName.IsEmpty()) {
DisplayUserGroups(); DisplayUserGroups();
} }
@ -67,101 +75,163 @@ protected function Executed(CallData arguments, EPlayer instigator)
RemoveGroup(groupName); RemoveGroup(groupName);
} }
else if (arguments.subCommandName.Compare(P("adduser"), SCASE_SENSITIVE)) { 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)) else if (arguments.subCommandName.Compare(P("removeuser"), SCASE_SENSITIVE))
{ {
RemoveUser(groupName, userNames); RemoveUser(groupName, userName);
} }
_.memory.Free(groupName); _.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 bool userInGroup;
local Text nextTextID; local UserID id;
local UserID nextID;
if (groupName == none) return; if (groupName == none) return;
if (userNames == none) return; if (textUserID == none) return;
for (i = 0; i < userNames.GetLength(); i += 1) id = UserID(_.memory.Allocate(class'UserID'));
{ id.Initialize(textUserID);
nextTextID = userNames.GetText(i); if (_.users.IsUserIDInGroup(id, groupName))
if (nextTextID == none) {
continue;
}
nextID = UserID(_.memory.Allocate(class'UserID'));
nextID.Initialize(nextTextID);
if (_.users.IsUserIDInGroup(nextID, groupName))
{ {
userInGroup = true;
callerConsole callerConsole
.Write(P("User ")) .Write(P("User "))
.UseColorOnce(_.color.Gray) .UseColorOnce(_.color.Gray)
.Write(nextTextID) .Write(textUserID)
.UseColorOnce(_.color.TextFailure) .UseColorOnce(_.color.TextFailure)
.Write(P(" is already in the group ")) .Write(P(" is already in the group "))
.UseColorOnce(_.color.TextEmphasis) .UseColorOnce(_.color.TextEmphasis)
.Write(groupName) .Write(groupName)
.WriteLine(P("!")); .WriteLine(P("!"));
} }
else else if (_.users.AddUserIDToGroup(id, groupName))
{ {
_.users.AddUserIDToGroup(nextID, groupName); userInGroup = true;
callerConsole callerConsole
.Write(F("{$TextPositive Added} user ")) .Write(F("{$TextPositive Added} user "))
.UseColorOnce(_.color.Gray) .UseColorOnce(_.color.Gray)
.Write(nextTextID) .Write(textUserID)
.Write(P(" to the group ")) .Write(P(" to the group "))
.UseColorOnce(_.color.TextEmphasis) .UseColorOnce(_.color.TextEmphasis)
.Write(groupName) .Write(groupName)
.WriteLine(P("!")); .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 int i;
local Text nextTextID; local UserID idFromName, idToRemove;
local UserID nextID; local array<Users_Feature.AnnotatedUserID> annotatedUsers;
if (groupName == none) return; if (groupName == none) return;
if (userNames == 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); for (i = 0; i < annotatedUsers.length; i += 1)
if (nextTextID == none) { {
continue; if (idFromName.IsEqual(annotatedUsers[i].id))
{
idToRemove = annotatedUsers[i].id;
break;
}
}
}
_.memory.Free(idFromName);
if (idToRemove == none)
{
for (i = 0; i < annotatedUsers.length; i += 1)
{
if (userName.Compare(
annotatedUsers[i].annotation,
SCASE_INSENSITIVE))
{
idToRemove = annotatedUsers[i].id;
break;
} }
nextID = UserID(_.memory.Allocate(class'UserID')); }
nextID.Initialize(nextTextID); }
if (!_.users.IsUserIDInGroup(nextID, groupName)) if (idToRemove == none)
{ {
callerConsole callerConsole
.Write(P("User ")) .Write(P("User "))
.UseColorOnce(_.color.Gray) .UseColorOnce(_.color.Gray)
.Write(nextTextID) .Write(userName)
.UseColorOnce(_.color.TextFailure) .UseColorOnce(_.color.TextFailure)
.Write(P(" doesn't belong to the group ")) .Write(P(" doesn't belong to the group "))
.UseColorOnce(_.color.TextEmphasis) .UseColorOnce(_.color.TextEmphasis)
.Write(groupName) .Write(groupName)
.WriteLine(P("!")); .WriteLine(P("!"));
} }
else else if (_.users.RemoveUserIDFromGroup(idToRemove, groupName))
{ {
_.users.RemoveUserIDFromGroup(nextID, groupName);
callerConsole callerConsole
.Write(F("{$TextNegative Removed} user ")) .Write(F("{$TextNegative Removed} user "))
.UseColorOnce(_.color.Gray) .UseColorOnce(_.color.Gray)
.Write(nextTextID) .Write(userName)
.Write(P(" from the group ")) .Write(P(" from the group "))
.UseColorOnce(_.color.TextEmphasis) .UseColorOnce(_.color.TextEmphasis)
.Write(groupName) .Write(groupName)
.WriteLine(P("!")); .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);
} }
} }

2
sources/Users/UserDatabase.uc

@ -112,7 +112,7 @@ public final function User FetchUser(UserID userID)
local User newUser; local User newUser;
for (i = 0; i < sessionUsers.length; i += 1) for (i = 0; i < sessionUsers.length; i += 1)
{ {
if (sessionUsers[i].GetID().IsEqualTo(userID)) { if (sessionUsers[i].GetID().IsEqual(userID)) {
return sessionUsers[i]; return sessionUsers[i];
} }
} }

1
sources/Users/Users_Feature.uc

@ -1700,7 +1700,6 @@ public final function bool IsSteamIDInGroup(
result = nextGroupUsers.HasKey(steamID); result = nextGroupUsers.HasKey(steamID);
} }
_.memory.Free(nextGroupUsers); _.memory.Free(nextGroupUsers);
steamID.FreeSelf();
return result; return result;
} }

Loading…
Cancel
Save