Browse Source

Change add/removeuser subcommands for usergroups command

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

230
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,102 +75,164 @@ 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);
if (_.users.IsUserIDInGroup(id, groupName))
{ {
nextTextID = userNames.GetText(i); userInGroup = true;
if (nextTextID == none) { callerConsole
continue; .Write(P("User "))
} .UseColorOnce(_.color.Gray)
nextID = UserID(_.memory.Allocate(class'UserID')); .Write(textUserID)
nextID.Initialize(nextTextID); .UseColorOnce(_.color.TextFailure)
if (_.users.IsUserIDInGroup(nextID, groupName)) .Write(P(" is already in the group "))
{ .UseColorOnce(_.color.TextEmphasis)
callerConsole .Write(groupName)
.Write(P("User ")) .WriteLine(P("!"));
.UseColorOnce(_.color.Gray) }
.Write(nextTextID) else if (_.users.AddUserIDToGroup(id, groupName))
.UseColorOnce(_.color.TextFailure) {
.Write(P(" is already in the group ")) userInGroup = true;
.UseColorOnce(_.color.TextEmphasis) callerConsole
.Write(groupName) .Write(F("{$TextPositive Added} user "))
.WriteLine(P("!")); .UseColorOnce(_.color.Gray)
} .Write(textUserID)
else .Write(P(" to the group "))
{ .UseColorOnce(_.color.TextEmphasis)
_.users.AddUserIDToGroup(nextID, groupName); .Write(groupName)
callerConsole .WriteLine(P("!"));
.Write(F("{$TextPositive Added} user "))
.UseColorOnce(_.color.Gray)
.Write(nextTextID)
.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 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;
}
nextID = UserID(_.memory.Allocate(class'UserID'));
nextID.Initialize(nextTextID);
if (!_.users.IsUserIDInGroup(nextID, groupName))
{ {
callerConsole if (idFromName.IsEqual(annotatedUsers[i].id))
.Write(P("User ")) {
.UseColorOnce(_.color.Gray) idToRemove = annotatedUsers[i].id;
.Write(nextTextID) break;
.UseColorOnce(_.color.TextFailure) }
.Write(P(" doesn't belong to the group "))
.UseColorOnce(_.color.TextEmphasis)
.Write(groupName)
.WriteLine(P("!"));
} }
else }
_.memory.Free(idFromName);
if (idToRemove == none)
{
for (i = 0; i < annotatedUsers.length; i += 1)
{ {
_.users.RemoveUserIDFromGroup(nextID, groupName); if (userName.Compare(
callerConsole annotatedUsers[i].annotation,
.Write(F("{$TextNegative Removed} user ")) SCASE_INSENSITIVE))
.UseColorOnce(_.color.Gray) {
.Write(nextTextID) idToRemove = annotatedUsers[i].id;
.Write(P(" from the group ")) break;
.UseColorOnce(_.color.TextEmphasis) }
.Write(groupName)
.WriteLine(P("!"));
} }
} }
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) private function AddGroup(BaseText groupName)

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