@ -1,6 +1,6 @@
/**
/**
* Command for displaying help information about registered Acedia's commands.
* Command for displaying help information about registered Acedia's commands.
* Copyright 2022 Anton Tarasenko
* Copyright 2022-2023 Anton Tarasenko
*------------------------------------------------------------------------------
*------------------------------------------------------------------------------
* This file is part of Acedia.
* This file is part of Acedia.
*
*
@ -29,8 +29,12 @@ protected function BuildData(CommandDataBuilder builder)
@ "groups. Changes made by it will always affect current session,"
@ "groups. Changes made by it will always affect current session,"
@ "but might fail to be saved in case user groups are stored in"
@ "but might fail to be saved in case user groups are stored in"
@ "a database that is either corrupted or in read-only mode."));
@ "a database that is either corrupted or in read-only mode."));
builder.SubCommand(P("show"))
builder.SubCommand(P("list"))
.Describe(P("Shows all groups along with users that belong to them."));
.Describe(P("Lists specified groups along with users that belong to"
@ "them. If no groups were specified at all - lists all available"
@ "groups."))
.OptionalParams()
.ParamTextList(P("groups"));
builder.SubCommand(P("add"))
builder.SubCommand(P("add"))
.Describe(P("Adds a new group"))
.Describe(P("Adds a new group"))
.ParamText(P("group_name"));
.ParamText(P("group_name"));
@ -50,11 +54,31 @@ protected function BuildData(CommandDataBuilder builder)
@ "user's id or annotation, with id taking priority."))
@ "user's id or annotation, with id taking priority."))
.ParamText(P("group_name"))
.ParamText(P("group_name"))
.ParamText(P("user_name"));
.ParamText(P("user_name"));
builder.SubCommand(P("addplayer"))
.Describe(P("Adds new user to the group, specified by the player"
@ "selector. Can add several players at once."
@ "Allows to also optionally specify annotation"
@ "(human-readable name) that can be thought of as"
@ "a {$TextEmphasis comment}. If annotation isn't specified"
@ "current nickname will be used as one."))
.ParamText(P("group_name"))
.ParamPlayers(P("player_selector"))
.OptionalParams()
.ParamText(P("annotation"));
builder.SubCommand(P("removeplayer"))
.Describe(P("Removes user from the group, specified by player selector."
@ "Can remove several players at once."))
.ParamText(P("group_name"))
.ParamPlayers(P("player_selector"));
builder.Option(P("force"))
.Describe(P("Allows to force usage of invalid user IDs."));
}
}
protected function Executed(CallData arguments, EPlayer instigator)
protected function Executed(CallData arguments, EPlayer instigator)
{
{
local bool forceOption;
local Text groupName, userID, userName, annotation;
local Text groupName, userID, userName, annotation;
local ArrayList players, groups;
groupName = arguments.parameters.GetText(P("group_name"));
groupName = arguments.parameters.GetText(P("group_name"));
// For parameters named `user_id`, can only be ID
// For parameters named `user_id`, can only be ID
@ -62,11 +86,14 @@ protected function Executed(CallData arguments, EPlayer instigator)
// For parameters named `user_id`, can be either ID or annotation
// For parameters named `user_id`, can be either ID or annotation
userName = arguments.parameters.GetText(P("user_name"));
userName = arguments.parameters.GetText(P("user_name"));
annotation = arguments.parameters.GetText(P("annotation"));
annotation = arguments.parameters.GetText(P("annotation"));
// An array of players that can be specified for some commands
players = arguments.parameters.GetArrayList(P("player_selector"));
groups = arguments.parameters.GetArrayList(P("groups"));
if (arguments.subCommandName.IsEmpty()) {
if (arguments.subCommandName.IsEmpty()) {
DisplayUserGroups();
DisplayUserGroups();
}
}
else if (arguments.subCommandName.Compare(P("show "), SCASE_SENSITIVE)) {
else if (arguments.subCommandName.Compare(P("list "), SCASE_SENSITIVE)) {
DisplayUserGroupsWithUsers();
DisplayUserGroupsWithUsers(groups );
}
}
else if (arguments.subCommandName.Compare(P("add"), SCASE_SENSITIVE)) {
else if (arguments.subCommandName.Compare(P("add"), SCASE_SENSITIVE)) {
AddGroup(groupName);
AddGroup(groupName);
@ -75,78 +102,124 @@ 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, userID, annotation);
AddOrAnnotate User(groupName, userID, annotation, forceOp tion);
}
}
else if (arguments.subCommandName.Compare(P("removeuser"), SCASE_SENSITIVE))
else if (arguments.subCommandName.Compare(P("removeuser"), SCASE_SENSITIVE))
{
{
RemoveUser(groupName, userName);
RemoveUser(groupName, userName);
}
}
else if (arguments.subCommandName.Compare(P("addplayer"), SCASE_SENSITIVE)) {
AddOrAnnotatePlayers(groupName, players, annotation);
}
else if (arguments.subCommandName
.Compare(P("removeplayer"), SCASE_SENSITIVE))
{
RemovePlayers(groupName, players);
}
_.memory.Free(groupName);
_.memory.Free(groupName);
_.memory.Free(userID);
_.memory.Free(userID);
_.memory.Free(userName);
_.memory.Free(userName);
_.memory.Free(annotation);
_.memory.Free(annotation);
_.memory.Free(players);
_.memory.Free(groups);
}
}
private function AddUser(
private function bool ValidateGroupExistence(BaseText groupName)
BaseText groupName,
BaseText textUserID,
BaseText annotation)
{
{
local bool userInGroup;
if (_.users.IsGroupExisting(groupName)) {
local UserID id;
return true;
}
callerConsole
.Write(P("Group "))
.UseColorOnce(_.color.TextEmphasis)
.Write(groupName)
.UseColorOnce(_.color.TextFailure)
.Write(P(" doesn't exists"))
.WriteLine(P("!"));
return false;
}
if (groupName == none) return;
private function bool ValidateUserID(BaseText textUserID)
if (textUserID == none) return;
{
local int i;
id = UserID(_.memory.Allocate(class'UserID'));
if (textUserID == none) {
id.Initialize(textUserID);
return false;
if (_.users.IsUserIDInGroup(id, groupName))
}
if (textUserID.IsEmpty())
{
callerConsole.WriteLine(F("Valid User ID"
@ "{$TextFailure shouldn't be empty},"
@ "use {$TextEmphasis --force} flag if you want to enforce"
@ "using it."));
return false;
}
for (i = 0; i < textUserID.GetLength(); i += 1)
{
if (!_.text.IsDigit(textUserID.GetCharacter(i)))
{
callerConsole.WriteLine(F("Valid User ID"
@ "{$TextFailure should consist only of digits},"
@ "use {$TextEmphasis --force} flag if you want"
@ "to enforce using it."));
return false;
}
}
return true;
}
private function bool TryAddingUserID(
BaseText groupName,
UserID userID,
BaseText userSpecifiedID)
{
if (_.users.IsUserIDInGroup(userID, groupName))
{
{
userInGroup = true;
callerConsole
callerConsole
.Write(P("User "))
.Write(P("User id specified as "))
.UseColorOnce(_.color.Gray)
.UseColorOnce(_.color.Gray)
.Write(textUserID)
.Write(userSpecified ID)
.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 if (_.users.AddUserIDToGroup(id, groupName))
else if (_.users.AddUserIDToGroup(userID , groupName))
{
{
userInGroup = true;
callerConsole
callerConsole
.Write(F("{$TextPositive Added} user "))
.Write(F("{$TextPositive Added} user id specified as "))
.UseColorOnce(_.color.Gray)
.UseColorOnce(_.color.Gray)
.Write(textUserID)
.Write(userSpecified ID)
.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 {
else
// One of the reasons - NO GROUP
{
callerConsole
callerConsole
.UseColorOnce(_.color.TextFailure)
.UseColorOnce(_.color.TextFailure)
.Write(P("Failed (for unknown reason)"))
.Write(P("Failed (for unknown reason)"))
.Write(P(" to add user "))
.Write(P(" to add user id "))
.UseColorOnce(_.color.Gray)
.UseColorOnce(_.color.Gray).Write(userSpecifiedID)
.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("!"));
return false;
}
}
if (!userInGroup || annotation == none) {
return true;
return;
}
}
_.users.SetAnnotationForUserID(groupName, id, annotation);
_.memory.Free(id);
private function DisplayAnnotation(
BaseText userSpecifiedName,
BaseText groupName,
BaseText annotation)
{
callerConsole
callerConsole
.Write(P("Annotation for user "))
.Write(P("Annotation for user id specified as "))
.UseColorOnce(_.color.Gray)
.UseColorOnce(_.color.Gray)
.Write(textUserID)
.Write(userSpecifiedName )
.UseColorOnce(_.color.TextPositive)
.UseColorOnce(_.color.TextPositive)
.Write(P(" in the group "))
.Write(P(" in the group "))
.UseColorOnce(_.color.TextEmphasis)
.UseColorOnce(_.color.TextEmphasis)
@ -156,44 +229,160 @@ private function AddUser(
.WriteLine(annotation);
.WriteLine(annotation);
}
}
private function RemoveUser(BaseText groupName, BaseText userName)
private function AddOrAnnotateUser(
BaseText groupName,
BaseText textUserID,
BaseText annotation,
bool forceOption)
{
local UserID id;
if (groupName == none) return;
if (textUserID == none) return;
if (!ValidateGroupExistence(groupName)) return;
if (!forceOption && !ValidateUserID(textUserID)) return;
id = UserID(_.memory.Allocate(class'UserID'));
id.Initialize(textUserID);
if (!TryAddingUserID(groupName, id, textUserID) || annotation == none)
{
_.memory.Free(id);
return;
}
_.users.SetAnnotationForUserID(groupName, id, annotation);
_.memory.Free(id);
DisplayAnnotation(textUserID, groupName, annotation);
}
private function AddOrAnnotatePlayers(
BaseText groupName,
ArrayList players,
BaseText annotation)
{
{
local int i;
local int i;
local UserID idFromName, idToRemove;
local BaseText playerName, nextAnnotation;
local array<Users_Feature.AnnotatedUserID> annotatedUsers;
local EPlayer nextPlayer;
local UserID nextID;
if (groupName == none) return;
if (groupName == none) return;
if (userName == none) return;
if (players == none) return;
if (!ValidateGroupExistence(groupName)) return;
idFromName = UserID(_.memory.Allocate(class'UserID'));
for (i = 0; i < players.GetLength(); i += 1)
idFromName.Initialize(userName);
annotatedUsers = _.users.GetAnnotatedGroupMembers(groupName);
if (idFromName.IsInitialized())
{
{
for (i = 0; i < annotatedUsers.length; i += 1)
nextPlayer = EPlayer(players.GetItem(i));
if (nextPlayer == none) {
continue;
}
playerName = nextPlayer.GetName();
nextID = nextPlayer.GetUserID();
if (TryAddingUserID(groupName, nextID, playerName))
{
if (annotation == none) {
nextAnnotation = playerName;
}
else {
nextAnnotation = annotation;
}
_.users.SetAnnotationForUserID(groupName, nextID, nextAnnotation);
DisplayAnnotation(playerName, groupName, nextAnnotation);
_.memory.Free(nextID);
nextAnnotation = none;
}
_.memory.Free(nextPlayer);
_.memory.Free(playerName);
_.memory.Free(nextID);
nextPlayer = none;
playerName = none;
nextID = none;
}
}
private function TryRemovingUserID(
BaseText groupName,
UserID idToRemove,
BaseText userSpecifiedName)
{
{
if (idFromName.IsEqual(annotatedUsers[i].id))
local Text idAsText;
idAsText = idToRemove.GetUniqueID();
if (_.users.RemoveUserIDFromGroup(idToRemove, groupName))
{
{
idToRemove = annotatedUsers[i].id;
callerConsole
break;
.Write(F("{$TextNegative Removed} user "))
.UseColorOnce(_.color.Gray)
.Write(userSpecifiedName)
.Write(P(" (with id "))
.UseColorOnce(_.color.Gray)
.Write(idAsText)
.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 with id "))
.UseColorOnce(_.color.Gray)
.Write(idAsText)
.Write(P(" from the group "))
.UseColorOnce(_.color.TextEmphasis)
.Write(groupName)
.WriteLine(P("."));
}
}
_.memory.Free(idAsText);
}
}
_.memory.Free(idFromName);
if (idToRemove == none)
private function bool RemoveUsersByAnnotation(
BaseText groupName,
BaseText userName)
{
{
local int i;
local bool removedUser;
local array<Users_Feature.AnnotatedUserID> annotatedUsers;
annotatedUsers = _.users.GetAnnotatedGroupMembers(groupName);
for (i = 0; i < annotatedUsers.length; i += 1)
for (i = 0; i < annotatedUsers.length; i += 1)
{
{
if (userName.Compare(
if (userName.Compare(annotatedUsers[i].annotation, SCASE_INSENSITIVE))
annotatedUsers[i].annotation,
SCASE_INSENSITIVE))
{
{
idToRemove = annotatedUsers[i].id;
TryRemovingUserID(groupName, annotatedUsers[i].id, userName);
break;
removedUser = true ;
}
}
}
}
for (i = 0; i < annotatedUsers.length; i += 1)
{
_.memory.Free(annotatedUsers[i].id);
_.memory.Free(annotatedUsers[i].annotation);
}
}
if (idToRemove == none)
return removedUser;
}
private function RemoveUser(BaseText groupName, BaseText userName)
{
local bool matchedUserName;
local UserID idFromName;
if (groupName == none) return;
if (userName == none) return;
if (!ValidateGroupExistence(groupName)) return;
idFromName = UserID(_.memory.Allocate(class'UserID'));
idFromName.Initialize(userName);
if ( idFromName.IsInitialized()
&& _.users.IsUserIDInGroup(idFromName, groupName))
{
TryRemovingUserID(groupName, idFromName, userName);
matchedUserName = true;
}
else {
matchedUserName = RemoveUsersByAnnotation(groupName, userName);
}
_.memory.Free(idFromName);
if (!matchedUserName)
{
{
callerConsole
callerConsole
.Write(P("User "))
.Write(P("User "))
@ -205,33 +394,47 @@ private function RemoveUser(BaseText groupName, BaseText userName)
.Write(groupName)
.Write(groupName)
.WriteLine(P("!"));
.WriteLine(P("!"));
}
}
else if (_.users.RemoveUserIDFromGroup(idToRemove, groupName))
}
private function RemovePlayers(BaseText groupName, ArrayList players)
{
local int i;
local Text playerName;
local EPlayer nextPlayer;
local UserID nextID;
if (groupName == none) return;
if (players == none) return;
if (!ValidateGroupExistence(groupName)) return;
for (i = 0; i < players.GetLength(); i += 1)
{
nextPlayer = EPlayer(players.GetItem(i));
if (nextPlayer == none) {
continue;
}
playerName = nextPlayer.GetName();
nextID = nextPlayer.GetUserID();
if (!_.users.IsUserIDInGroup(nextID, groupName))
{
{
callerConsole
callerConsole
.Write(F("{$TextNegative Removed} user "))
.Write(P("Play er "))
.UseColorOnce(_.color.Gray)
.UseColorOnce(_.color.Gray)
.Write(userName)
.Write(play erName)
.Write(P(" from the group "))
.Write(F(" {$TextFailure doesn't belong} to the group "))
.UseColorOnce(_.color.TextEmphasis)
.UseColorOnce(_.color.TextEmphasis)
.Write(groupName)
.Write(groupName)
.WriteLine(P("!"));
.WriteLine(P("!"));
}
}
else {
else {
callerConsole
TryRemovingUserID(groupName, nextID, playerName);
.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(nextPlayer);
{
_.memory.Free(playerName);
_.memory.Free(annotatedUsers[i].id);
_.memory.Free(nextID);
_.memory.Free(annotatedUsers[i].annotation);
nextPlayer = none;
playerName = none;
nextID = none;
}
}
}
}
@ -346,9 +549,36 @@ private function bool ValidateUsersFeature()
return false;
return false;
}
}
private function DisplayUserGroupsWithUsers()
private function bool IsGroupSpecified(
ArrayList specifiedGroups,
BaseText groupToCheck)
{
{
local int i;
local int i;
local int length;
local Text nextGroup;
if (groupToCheck == none) return false;
if (specifiedGroups == none) return true;
length = groupToCheck.GetLength();
if (length <= 0) return true;
for (i = 0; i < length; i += 1)
{
nextGroup = specifiedGroups.GetText(i);
if (groupToCheck.Compare(nextGroup, SCASE_INSENSITIVE))
{
nextGroup.FreeSelf();
return true;
}
_.memory.Free(nextGroup);
}
return false;
}
private function DisplayUserGroupsWithUsers(ArrayList specifiedGroups)
{
local int i;
local bool displayedGroup;
local array<Text> availableGroups;
local array<Text> availableGroups;
if (!ValidateUsersFeature()) {
if (!ValidateUsersFeature()) {
@ -363,6 +593,9 @@ private function DisplayUserGroupsWithUsers()
}
}
for (i = 0; i < availableGroups.length; i += 1)
for (i = 0; i < availableGroups.length; i += 1)
{
{
if (IsGroupSpecified(specifiedGroups, availableGroups[i]))
{
displayedGroup = true;
callerConsole
callerConsole
.Write(P("User group "))
.Write(P("User group "))
.UseColorOnce(_.color.TextEmphasis)
.UseColorOnce(_.color.TextEmphasis)
@ -370,8 +603,12 @@ private function DisplayUserGroupsWithUsers()
.WriteLine(P(":"));
.WriteLine(P(":"));
DisplayUsersFor(availableGroups[i]);
DisplayUsersFor(availableGroups[i]);
}
}
}
callerConsole.Flush();
callerConsole.Flush();
_.memory.FreeMany(availableGroups);
_.memory.FreeMany(availableGroups);
if (!displayedGroup && specifiedGroups != none) {
callerConsole.WriteLine(F("{$TextFailure No valid groups} specified!"));
}
}
}
private function DisplayUsersFor(Text groupName)
private function DisplayUsersFor(Text groupName)