メインコンテンツまでスキップ

Implement player blocking

Last updated on October 23, 2024

Overview

The AccelByte Gaming Services (AGS) Friends service allows you to enable your players to manage their interactions with other players in your games, include the ability to block players from interacting with them.

This article provides an overview of player blocking and walks you through how to implement player blocking into your game.

What player blocking does

Blocking allows players to restrict other players from interacting with them. When one player blocks another, both players are prevented from:

  • Adding each other as a friend. If the players are already friends, they will be unfriended.
  • Inviting each other to a party session. Note: Requires AGS Session version 3.13.11 or later.
  • Meeting each other in matches. Note: Requires AGS Matchmaking version 2.15.3 or later.
  • Being automatically placed in the same party. Note: They can still be in the same party if another player invites both of them.
  • Inviting each other to join groups.
  • Seeing each other's player profiles in group members lists.
  • Being matched together through matchmaking. Note: This can be adjusted in the matchmaking ruleset.

The lobby block can also be configured to be used by the chat service block behavior. When configured, the chat service will use the lobby block data instead of its own block feature. The block feature (user interaction) applies for personal chat and session chat to restrict:

  • Personal chat: Cannot send message to the block user and vice versa.
  • Session chat: A blocked player cannot be in one session. However, if the user blocks another user while on the same party, then the impacted users can still send the message, but they cannot see each other's message.
備考

To configure the lobby block to be used by the chat service, please contact the AccelByte team via the customer portal or community forum.

Implement player blocking

Using the AGS Game SDK, the player block features are available via WebSocket or HTTP REST endpoints. This article provides a sample implementation using both ways.

Block a player

Use the following code to block a player:

FOnlineSubsystemAccelByte* OnlineSubsystem = static_cast<FOnlineSubsystemAccelByte*>(Online::GetSubsystem(GetWorld()));
FOnlineIdentityAccelBytePtr IdentityInterface = StaticCastSharedPtr<FOnlineIdentityAccelByte>(OnlineSubsystem->GetIdentityInterface());
FOnlineFriendsAccelBytePtr FriendsInterface = StaticCastSharedPtr<FOnlineFriendsAccelByte>(OnlineSubsystem->GetFriendsInterface());

const int32 BlockerLocalUserNum = 0;
const FUniqueNetIdPtr BlockerUserId = IdentityInterface->GetUniquePlayerId(BlockerLocalUserNum);
const FUniqueNetIdPtr BlockedUserId;

auto OnBlockedPlayerCompleteDelegate = FriendsInterface->AddOnBlockedPlayerCompleteDelegate_Handle(BlockerLocalUserNum, FOnBlockedPlayerCompleteDelegate::CreateLambda([BlockerLocalUserNum](int32 LocalUserNum, bool bWasSuccessful, const FUniqueNetId& UniqueID, const FString& ListName, const FString& ErrorStr)
{
if (LocalUserNum == BlockerLocalUserNum && bWasSuccessful)
{
// Do something
}
}));

FriendsInterface->BlockPlayer(BlockerLocalUserNum, *BlockedUserId);

Listen for player blocking events

The game needs to know if a player has been blocked by another player to avoid matching blocked players together and other similar tasks. Blocking events can be tracked by subscribing to the event as shown below. The event will be raised on the blocked player side and pass data containing both the blocking player's and the blocked player's user IDs.

const int32 LocalUserNum = 0;
FApiClientPtr ApiClient = IdentityInterface->GetApiClient(LocalUserNum);

ApiClient->Lobby.SetBlockPlayerNotifDelegate(AccelByte::Api::Lobby::FBlockPlayerNotif::CreateLambda([](const FAccelByteModelsBlockPlayerNotif & Result) {
// Do something if BlockPlayerNotifDelegate succeeds
}));

Unblock a player

Use the following code to unblock a player:

const int32 UnblockerLocalUserNum = 0;
const FUniqueNetIdPtr UnblockerUserId = IdentityInterface->GetUniquePlayerId(UnblockerLocalUserNum);
const FUniqueNetIdPtr UnblockedUserId;

auto OnUnblockedPlayerCompleteDelegate = FriendsInterface->AddOnUnblockedPlayerCompleteDelegate_Handle(UnblockerLocalUserNum, FOnBlockedPlayerCompleteDelegate::CreateLambda([UnblockerLocalUserNum](int32 LocalUserNum, bool bWasSuccessful, const FUniqueNetId& UniqueID, const FString& ListName, const FString& ErrorStr)
{
if (LocalUserNum == UnblockerLocalUserNum && bWasSuccessful)
{
// Do something
}
}));

FriendsInterface->UnblockPlayer(UnblockerLocalUserNum, *UnblockedUserId);

Listen for player unblocked events

This event will be raised on the unblocked player's side and pass data containing both the actor (player who unblocked) and target (the unblocked player) user IDs.

const int32 LocalUserNum = 0;
FApiClientPtr ApiClient = IdentityInterface->GetApiClient(LocalUserNum);

ApiClient->Lobby.SetUnblockPlayerNotifDelegate(AccelByte::Api::Lobby::FUnblockPlayerNotif::CreateLambda([](const FAccelByteModelsUnblockPlayerNotif & Result) {
// Do something if UnblockPlayerNotifDelegate succeeds
}));

Retrieve the list of blocked players

Use the following code to retrieve a list of blocked players for the current user (the caller). This has a callback that returns an array of data containing the user ID of each blocked player.

const int32 UnblockerLocalUserNum = 0;
const FUniqueNetIdPtr BlockerUserId = IdentityInterface->GetUniquePlayerId(BlockerLocalUserNum);

auto OnQueryBlockedPlayersCompleteDelegate = FriendsInterface->AddOnQueryBlockedPlayersCompleteDelegate_Handle(FOnQueryBlockedPlayersCompleteDelegate::CreateLambda([](const FUniqueNetId& UserId, bool bWasSuccessful, const FString& Error)
{
// Do something
}));
FriendsInterface->QueryBlockedPlayers(*BlockerUserId);

TArray<TSharedRef<FOnlineBlockedPlayer>> BlockedPlayers;
FriendsInterface->GetBlockedPlayers(*BlockerUserId, BlockedPlayers);

Retrieve the list of blockers

Use the following code to retrieve a list of players that have blocked a player. This has a callback that returns an array of data that contains the blocked player's user ID.

const int32 LocalUserNum = 0;
FApiClientPtr ApiClient = IdentityInterface->GetApiClient(LocalUserNum);

ApiClient->Lobby.GetListOfBlockers(THandler < FAccelByteModelsListBlockerResponse > ::CreateLambda([](const FAccelByteModelsListBlockerResponse & Result) {
// Do something if GetListOfBlockers succeeds
}), FErrorHandler::CreateLambda([](int32 ErrorCode,
const FString & ErrorMessage) {
// Do something if GetListOfBlockers fails
UE_LOG(LogTemp, Log, TEXT("Error GetListOfBlockers, Error Code: %d Error Message: %s"), ErrorCode, * ErrorMessage);
}));

Configure matchmaking behavior

注記

This feature requires AGS Matchmaking version 2.15.3 or later.

By default, AGS Matchmaking will never match players who have blocked each other. However, using the match ruleset, you can control the matchmaking behavior. You can find a sample ruleset below.

ruleset
{
"alliance": {
"min_number": 2,
"max_number": 2,
"player_min_number": 5,
"player_max_number": 5
},
"blocked_player_option": "blockedPlayerCannotMatch" // Default value
}

You can control the matchmaking behavior for blocked players using blocked_player_option:

  • blockedPlayerCannotMatch: the default matchmaking behavior. The blocked players cannot be matched into the same game or session.
  • blockedPlayerCanMatchOnDifferentTeam: using this value, blocked players can match with each other to be placed on different teams.
  • blockedPlayerCanMatch: Using this value, blocked players can match with each other normally.

Synchronize blocklist data with native platforms

注記

This feature requires AGS Lobby version 3.35.1 or later.

AGS supports synchronization blocked players with a native platform (currently only available for PlayStation Network). This process will add the blocklist data from the native platform to the AGS database automatically. One common use case is so the game client can synchronize the blocklist data with the native platform after logging in.

Parameter NameTypeDescription
PlatformIdFStringThe ID of the platform you want to synchronize the friend list into. Valid values are:steam, ps5, ps4.
PsnEnvFStringThe selected PlayStation (PSN) environment to synchronize friends with. Valid values are:sp-int, prod-qa, np

Use the following code to implement blocked player synchronization:

const int32 LocalUserNum = 0;

auto OnSyncThirdPartyBlockListCompleteDelegate = FriendsInterface->AddOnSyncThirdPartyBlockListCompleteDelegate_Handle(LocalUserNum, FOnSyncThirdPartyBlockListCompleteDelegate::CreateLambda([](int32 InLocalUserNum, const FOnlineError& ErrorInfo, const TArray<FAccelByteModelsSyncThirdPartyBlockListResponse>& Response)
{
// Do something
}));

FAccelByteModelsSyncThirdPartyBlockListInfo PlatformInfo;
PlatformInfo.PlatformId = FAccelByteUtilities::GetPlatformString(EAccelBytePlatformType::PS4);
PlatformInfo.PsnEnv = "np";

FAccelByteModelsSyncThirdPartyBlockListRequest BlockSyncRequest;
BlockSyncRequest.BlockListSyncDetails.Add(PlatformInfo);

FriendsInterface->SyncThirdPartyPlatformBlockList(LocalUserNum, BlockSyncRequest);

Endpoint Reference

Sync PSN Blocked Playershttps://docs.accelbyte.io/api-explorer/lobby