From 3eacea5827af757ddeb5a6feee21114d4ed67997 Mon Sep 17 00:00:00 2001 From: Anton Tarasenko Date: Wed, 16 Nov 2022 04:02:05 +0700 Subject: [PATCH] Add methods for addding and removing users --- sources/Users/UserAPI.uc | 32 ++++++- sources/Users/Users_Feature.uc | 165 ++++++++++++++++++++++++++++++++- 2 files changed, 192 insertions(+), 5 deletions(-) diff --git a/sources/Users/UserAPI.uc b/sources/Users/UserAPI.uc index 97a53b7..8d39a66 100644 --- a/sources/Users/UserAPI.uc +++ b/sources/Users/UserAPI.uc @@ -230,6 +230,34 @@ public final function User FetchByKey(int userKey) return result; } +/** + * Returns names of all available groups that users can belong to. + * + * In case this feature is configured to load user groups from a database + * (either local or remote), the returned value is a locally cached one. + * This helps us avoid having to query database each time we want to check + * something about user groups, but it also means we might have an outdated + * information. + * + * @return Array with names of all available groups. + * All array elements are guaranteed to be not-`none`, unique and in + * lower case. + */ +public final function array GetAvailableGroups() +{ + local Users_Feature usersFeature; + local array result; + + usersFeature = Users_Feature(class'Users_Feature'.static + .GetEnabledInstance()); + if (usersFeature == none) { + return result; + } + result = usersFeature.GetAvailableGroups(); + usersFeature.FreeSelf(); + return result; +} + /** * Returns names of all groups available for the user with a SteamID given by * `steamID`. @@ -250,7 +278,7 @@ public final function User FetchByKey(int userKey) * If passed SteamID is `none` or data wasn't yet loaded - returns empty * array. */ -public final /*unreal*/ function array GetGroupsForSteamID( +public final function array GetGroupsForSteamID( BaseText steamID) { local Users_Feature usersFeature; @@ -382,7 +410,7 @@ public final function array GetGroupsForUser(User user) * correspond to unique players. * If data wasn't yet loaded - returns empty array. */ -public final function array GetGroupMembers(Text groupName) +public final function array GetGroupMembers(BaseText groupName) { local Users_Feature usersFeature; local array result; diff --git a/sources/Users/Users_Feature.uc b/sources/Users/Users_Feature.uc index f73cb8e..6da050c 100644 --- a/sources/Users/Users_Feature.uc +++ b/sources/Users/Users_Feature.uc @@ -156,6 +156,166 @@ private final function SaveLocalData() _.memory.Free(activeConfigName); } +/** + * Returns names of all available groups that users can belong to. + * + * In case this feature is configured to load user groups from a database + * (either local or remote), the returned value is a locally cached one. + * This helps us avoid having to query database each time we want to check + * something about user groups, but it also means we might have an outdated + * information. + * + * @return Array with names of all available groups. + * All array elements are guaranteed to be not-`none`, unique and in + * lower case. + */ +public final function array GetAvailableGroups() +{ + local int i; + local array result; + + for (i = 0; i < loadedUserGroups.length; i += 1) { + result[i] = loadedUserGroups[i].Copy(); + } + return result; +} + +public final function bool AddSteamIDToGroup( + BaseText steamID, + BaseText groupName) +{ + local Text lowercaseGroupName; + local HashTable groupUsers; + + if (loadedGroupToUsersMap == none) return false; + if (groupName == none) return false; + + lowercaseGroupName = groupName.LowerCopy(); + groupUsers = loadedGroupToUsersMap.GetHashTable(lowercaseGroupName); + lowercaseGroupName.FreeSelf(); + if (groupUsers == none) { + return false; + } + groupUsers.SetItem(steamID, none); + groupUsers.FreeSelf(); + return true; +} + +public final /*unreal*/ function bool AddSteamIDToGroup_S( + string steamID, + string groupName) +{ + local bool result; + local MutableText idWrapper, groupWrapper; + + idWrapper = _.text.FromStringM(steamID); + groupWrapper = _.text.FromStringM(groupName); + result = AddSteamIDToGroup(idWrapper, groupWrapper); + idWrapper.FreeSelf(); + groupWrapper.FreeSelf(); + return result; +} + +public final function bool AddUserIDToGroup( + UserID id, + BaseText groupName) +{ + local bool result; + local Text steamID; + + if (groupName == none) return false; + if (id == none) return false; + steamID = id.GetSteamID64String(); + if (steamID == none) return false; + + result = AddSteamIDToGroup(steamID, groupName); + steamID.FreeSelf(); + return result; +} + +public final function bool AddUserToGroup(User user, BaseText groupName) +{ + local bool result; + local UserID id; + + if (groupName == none) return false; + if (user == none) return false; + + id = user.GetID(); + result = AddUserIDToGroup(id, groupName); + _.memory.Free(id); + return result; +} + +public final function bool RemoveSteamIDFromGroup( + BaseText steamID, + BaseText groupName) +{ + local bool hadUser; + local Text lowercaseGroupName; + local HashTable groupUsers; + + if (groupName == none) return false; + if (loadedGroupToUsersMap == none) return false; + + lowercaseGroupName = groupName.LowerCopy(); + groupUsers = loadedGroupToUsersMap.GetHashTable(lowercaseGroupName); + lowercaseGroupName.FreeSelf(); + if (groupUsers == none) { + return false; + } + hadUser = groupUsers.HasKey(steamID); + groupUsers.RemoveItem(steamID); + groupUsers.FreeSelf(); + return hadUser; +} + +public final /*unreal*/ function bool RemoveSteamIDFromGroup_S( + string steamID, + string groupName) +{ + local bool result; + local MutableText idWrapper, groupWrapper; + + idWrapper = _.text.FromStringM(steamID); + groupWrapper = _.text.FromStringM(groupName); + result = RemoveSteamIDFromGroup(idWrapper, groupWrapper); + idWrapper.FreeSelf(); + groupWrapper.FreeSelf(); + return result; +} + +public final function bool RemoveUserIDFromGroup( + UserID id, + BaseText groupName) +{ + local bool result; + local Text steamID; + + if (groupName == none) return false; + if (id == none) return false; + steamID = id.GetSteamID64String(); + if (steamID == none) return false; + + result = RemoveSteamIDFromGroup(steamID, groupName); + steamID.FreeSelf(); + return result; +} + +public final function bool RemoveUserFromGroup(User user, BaseText groupName) +{ + local bool result; + local UserID id; + + if (groupName == none) return false; + if (user == none) return false; + + id = user.GetID(); + result = RemoveUserIDFromGroup(id, groupName); + _.memory.Free(id); + return result; +} + /** * Returns names of all groups available for the user with a SteamID given by * `steamID`. @@ -176,8 +336,7 @@ private final function SaveLocalData() * If passed SteamID is `none` or data wasn't yet loaded - returns empty * array. */ -public final /*unreal*/ function array GetGroupsForSteamID( - BaseText steamID) +public final function array GetGroupsForSteamID(BaseText steamID) { local Text immutableSteamID; local array result; @@ -316,7 +475,7 @@ public final function array GetGroupsForUser(User user) * correspond to unique players. * If data wasn't yet loaded - returns empty array. */ -public final function array GetGroupMembers(Text groupName) +public final function array GetGroupMembers(BaseText groupName) { local int i; local Text lowerCaseGroupName;