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

すべてを統合する - フレンドを管理する - (Unity モジュール)

Last updated on December 13, 2024

Connect the UI to get blocked players

  1. Open BlockedPlayersMenuHandler_Starter.cs and declare the module wrappers by adding the following code:

    private ManagingFriendsWrapper_Starter managingFriendsWrapper;

    private FriendsEssentialsWrapper_Starter friendsEssentialsWrapper;
  2. Replace the existing LoadBlockedPlayers() implementation with the code below to call the GetBlockedPlayers() function from the ManagingFriendsWrapper_Starter class.

    private void LoadBlockedPlayers()
    {
    CurrentView = BlockedFriendsView.Loading;

    managingFriendsWrapper.GetBlockedPlayers(OnLoadBlockedPlayersCompleted);
    }
  3. Replace the OnLoadBlockedPlayersCompleted callback function with the code below to handle the result.

    private void OnLoadBlockedPlayersCompleted(Result<BlockedList> result)
    {
    if (result.IsError)
    {
    CurrentView = BlockedFriendsView.LoadFailed;

    return;
    }

    BlockedData[] blockedData = result.Value.data;

    if (blockedData.Length <= 0)
    {
    CurrentView = BlockedFriendsView.Default;

    return;
    }

    IEnumerable<string> blockedPlayerIds = blockedData.Select(player => player.blockedUserId);

    GetBulkUserInfo(blockedPlayerIds.ToArray());
    }
  4. Create a function called GetBulkUserInfo() to get the user information of the blocked players.

    private void GetBulkUserInfo(params string[] userIds)
    {
    friendsEssentialsWrapper.GetBulkUserInfo(userIds, OnGetBulkUserInfoCompleted);
    }
  5. Create a callback function called OnGetBulkUserInfoCompleted() to handle the result.

    private void OnGetBulkUserInfoCompleted(Result<ListBulkUserInfoResponse> result)
    {
    if (result.IsError)
    {
    return;
    }

    CurrentView = BlockedFriendsView.LoadSuccess;

    ClearBlockedPlayers();

    PopulateBlockedPlayers(result.Value.data);
    }
  6. Create a function called PopulateBlockedPlayers() to populate the blocked players list.

    private void PopulateBlockedPlayers(params BaseUserInfo[] userInfo)
    {
    foreach (BaseUserInfo baseUserInfo in userInfo)
    {
    CreatePlayerEntry(baseUserInfo.userId, baseUserInfo.displayName);
    }
    }
  7. Create a function called CreatePlayerEntry() to create a player entry for each blocked player. If the player does not have a display name, the function will use the first five characters of the user ID as the display name.

    private void CreatePlayerEntry(string userId, string displayName)
    {
    GameObject playerEntry = Instantiate(playerEntryPrefab, resultContentPanel);
    playerEntry.name = userId;

    if (string.IsNullOrEmpty(displayName))
    {
    string truncatedUserId = userId[..5];
    displayName = $"Player-{truncatedUserId}";
    }

    BlockedPlayerEntryHandler playerEntryHandler = playerEntry.GetComponent<BlockedPlayerEntryHandler>();
    playerEntryHandler.UserId = userId;
    playerEntryHandler.FriendName.text = displayName;

    blockedPlayers.Add(userId, playerEntry);
    }
  8. Update the OnEnable() function to call the LoadBlockedPlayers() function and define the module wrappers. Replace the existing implementation with the code below.

    private void OnEnable()
    {
    if (managingFriendsWrapper == null)
    {
    managingFriendsWrapper = TutorialModuleManager.Instance.GetModuleClass<ManagingFriendsWrapper_Starter>();
    }

    if (friendsEssentialsWrapper == null)
    {
    friendsEssentialsWrapper = TutorialModuleManager.Instance.GetModuleClass<FriendsEssentialsWrapper_Starter>();
    }

    if (managingFriendsWrapper != null && friendsEssentialsWrapper != null)
    {
    LoadBlockedPlayers();
    }
    }

Connect the UI to display avatars of blocked players

  1. Open BlockedPlayerEntryHandler.cs and create a function called RetrieveUserAvatar() to get the user avatar

    private void RetrieveUserAvatar(string userId)
    {
    friendsEssentialsWrapper.GetUserAvatar(userId, result => OnGetAvatarCompleted(userId, result));
    }
  2. Create a callback function called OnGetAvatarCompleted() to handle the result.

    private void OnGetAvatarCompleted(string userId, Result<Texture2D> result)
    {
    if (result.IsError)
    {
    return;
    }

    if (!blockedPlayers.TryGetValue(userId, out GameObject playerEntry))
    {
    return;
    }

    if (!playerEntry.TryGetComponent(out BlockedPlayerEntryHandler playerEntryHandler))
    {
    return;
    }

    Rect rect = new Rect(0f, 0f, result.Value.width, result.Value.height);
    playerEntryHandler.FriendImage.sprite = Sprite.Create(result.Value, rect, Vector2.zero);
    }
  3. Update the CreatePlayerEntry() function to call the RetrieveUserAvatar() function. Add the highlighted code below.

    private void CreatePlayerEntry(string userId, string displayName)
    {
    //...
    blockedPlayers.Add(userId, playerEntry);

    RetrieveUserAvatar(userId);
    }

Connect the UI to unblock a player

  1. Open BlockedPlayerEntryHandler.cs and create a function called UnblockPlayer() to unblock a player by their user ID

    private void UnblockPlayer(string userId)
    {
    MenuManager.Instance.PromptMenu.ShowPromptMenu(FriendsHelper.PromptConfirmTitle,
    FriendsHelper.UnblockPlayerConfirmationMessage,
    "Yes",
    confirmAction: () =>
    {
    MenuManager.Instance.PromptMenu.ShowLoadingPrompt(FriendsHelper.UnblockingPlayerMessage);

    managingFriendsWrapper.UnblockPlayer(userId, result => OnUnblockPlayerCompleted(userId, result));
    },
    "No", null);
    }
  2. Create a callback function called OnUnblockPlayerCompleted() to handle the result. The function will show a prompt menu to confirm the unblocking action.

    private void OnUnblockPlayerCompleted(string userId, Result<UnblockPlayerResponse> result)
    {
    if (result.IsError)
    {
    MenuManager.Instance.PromptMenu.ShowPromptMenu(FriendsHelper.PromptErrorTitle,
    result.Error.Message, "OK", null);
    return;
    }

    MenuManager.Instance.PromptMenu.ShowPromptMenu(FriendsHelper.PromptMessageTitle,
    FriendsHelper.UnblockPlayerCompletedMessage, "OK", null);

    LoadBlockedPlayers();
    }
  3. Update the CreatePlayerEntry() function to call the UnblockPlayer() function when the unblock button is clicked. Add the code below.

    private void CreatePlayerEntry(string userId, string displayName)
    {
    //...
    playerEntryHandler.FriendName.text = displayName;
    playerEntryHandler.UnblockButton.onClick.AddListener(() => UnblockPlayer(userId));

    blockedPlayers.Add(userId, playerEntry);
    //...
    }

Connect the UI to block and unfriend a player

  1. Open FriendDetailsMenuHandler_Starter.cs and declare the module wrappers by adding the following code:

    private ManagingFriendsWrapper_Starter managingFriendsWrapper;
  2. Update the OnEnable() function to define the module wrappers. Add the code below.

    private void OnEnable()
    {
    if (managingFriendsWrapper == null)
    {
    managingFriendsWrapper = TutorialModuleManager.Instance.GetModuleClass<ManagingFriendsWrapper_Starter>();
    }
    }
  3. Update the Unfriend() function to enable unfriending players by their user IDs. This function will show a prompt menu to confirm the unfriending action. Replace the existing implementation with the code below.

    private void Unfriend()
    {
    MenuManager.Instance.PromptMenu.ShowPromptMenu(
    FriendsHelper.PromptConfirmTitle,
    FriendsHelper.UnfriendConfirmationMessage,
    "Yes",
    confirmAction: () =>
    {
    MenuManager.Instance.PromptMenu.ShowLoadingPrompt(FriendsHelper.UnfriendingMessage);

    managingFriendsWrapper.Unfriend(UserId, OnUnfriendCompleted);
    },
    "No", null);
    }
  4. Update the OnUnfriendCompleted() function to handle the result. Replace the existing implementation with the code below.

    private void OnUnfriendCompleted(Result result)
    {
    if (result.IsError)
    {
    MenuManager.Instance.PromptMenu.ShowPromptMenu(FriendsHelper.PromptErrorTitle,
    result.Error.Message, "OK", null);
    return;
    }

    BytewarsLogger.Log($"Successfully unfriended player with user ID: {UserId}");

    MenuManager.Instance.PromptMenu.ShowPromptMenu(FriendsHelper.PromptMessageTitle,
    FriendsHelper.UnfriendCompletedMessage, "OK", null);

    MenuManager.Instance.OnBackPressed();
    }
  5. Update the BlockPlayer() to enable blocking players by their user IDs. This function will show a prompt menu to confirm the blocking action. Replace the existing implementation with the code below.

    private void BlockPlayer()
    {
    MenuManager.Instance.PromptMenu.ShowPromptMenu(
    FriendsHelper.PromptConfirmTitle,
    FriendsHelper.BlockPlayerConfirmationMessage,
    "Yes",
    confirmAction: () =>
    {
    MenuManager.Instance.PromptMenu.ShowLoadingPrompt(FriendsHelper.BlockingPlayerMessage);

    managingFriendsWrapper.BlockPlayer(UserId, OnBlockPlayerComplete);
    },
    "No", null);
    }
  6. Update the OnBlockPlayerCompleted() function to handle the result. Replace the existing implementation with the code below.

    private void OnBlockPlayerComplete(Result<BlockPlayerResponse> result)
    {
    if (result.IsError)
    {
    MenuManager.Instance.PromptMenu.ShowPromptMenu(FriendsHelper.PromptErrorTitle,
    result.Error.Message, "OK", null);
    return;
    }

    BytewarsLogger.Log($"Successfully blocked player with user ID: {UserId}");

    MenuManager.Instance.PromptMenu.ShowPromptMenu(FriendsHelper.PromptMessageTitle,
    FriendsHelper.BlockPlayerCompletedMessage, "OK", null);

    MenuManager.Instance.OnBackPressed();
    }

Resources