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

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

Last updated on December 13, 2024

Connect the UI to get incoming requests

  1. Open FriendRequestMenuHandler_Starter.cs and declare the FriendsEssentialsWrapper_Starter class as a private variable.

    private FriendsEssentialsWrapper_Starter friendsEssentialsWrapper;
  2. Navigate to the OnEnable() function and add the code below to retrieve the friendsEssentialsWrapper:

    private void OnEnable()
    {
    if (friendsEssentialsWrapper == null)
    {
    friendsEssentialsWrapper = TutorialModuleManager.Instance.GetModuleClass<FriendsEssentialsWrapper_Starter>();
    }
    }
  3. Replace the LoadIncomingFriendRequests() function with the following code to call the LoadIncomingFriendRequests() function from the wrapper class and pass OnLoadIncomingFriendRequestsCompleted as the callback function to handle the result.

    private void LoadIncomingFriendRequests()
    {
    CurrentView = FriendRequestsView.Loading;

    friendsEssentialsWrapper.LoadIncomingFriendRequests(OnLoadIncomingFriendRequestsCompleted);
    }
  4. Replace the OnLoadIncomingFriendRequestsCompleted callback function to show the UI if the result failed or empty, or retrieve user information from the friend ID list from the result.

    private void OnLoadIncomingFriendRequestsCompleted(Result<Friends> result)
    {
    if (result.IsError)
    {
    CurrentView = FriendRequestsView.LoadFailed;
    return;
    }

    if (result.Value.friendsId.Length <= 0)
    {
    CurrentView = FriendRequestsView.Default;
    return;
    }

    GetBulkUserInfo(result.Value);
    }
  5. Create a function called GetBulkUserInfo() as a main function to get the user information from the list of friend IDs.

    private void GetBulkUserInfo(Friends friends)
    {
    friendsEssentialsWrapper.GetBulkUserInfo(friends.friendsId, OnGetBulkUserInfoCompleted);
    }
  6. Create a callback function called OnGetBulkUserInfoCompleted() to display the user information retrieved from GetBulkUserInfo function.

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

    ClearFriendRequestList();
    CurrentView = FriendRequestsView.LoadSuccess;

    PopulateFriendRequestList(result.Value.data);
    }
  7. Create a function called PopulateFriendRequestList() to populate the friend request list.

    private void PopulateFriendRequestList(params BaseUserInfo[] userInfo)
    {
    foreach (BaseUserInfo baseUserInfo in userInfo)
    {
    CreateFriendEntry(baseUserInfo.userId, baseUserInfo.displayName);
    }
    }
  8. Create a function called CreateFriendEntry() to create a friend entry for each friend request.

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

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

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

    friendRequests.Add(userId, playerEntry);
    }
  9. Update the OnEnable() function to load the friend request list when the UI is enabled. Add the code below.

    private void OnEnable()
    {
    //...
    if (friendsEssentialsWrapper != null)
    {
    LoadIncomingFriendRequests();
    }
    }

Connect the UI to get outgoing requests

  1. Open SentFriendRequestMenuHandler_Starter.cs and declare the FriendsEssentialsWrapper_Starter class as a private variable.

    private FriendsEssentialsWrapper_Starter friendsEssentialsWrapper;
  2. Navigate to the OnEnable() function and add the code below to retrieve the friendsEssentialsWrapper:

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

  3. Replace the LoadOutgoingFriendRequests() with the code below to call the LoadOutgoingFriendRequests() function from the wrapper class and pass OnLoadOutgoingFriendRequestsCompleted as the callback function to handle the result.

    private void LoadOutgoingFriendRequests()
    {
    CurrentView = SentFriendRequestsView.Loading;

    friendsEssentialsWrapper.LoadOutgoingFriendRequests(OnLoadOutgoingRequestsCompleted);
    }
  4. Create a callback function called OnLoadOutgoingRequestsCompleted(). This will handle the UI if the LoadOutgoingFriendRequests failed and retrieve user information based on the retrieved friend IDs if it succeed.

    private void OnLoadOutgoingRequestsCompleted(Result<Friends> result)
    {
    if (result.IsError)
    {
    CurrentView = SentFriendRequestsView.LoadFailed;
    return;
    }

    if (result.Value.friendsId.Length <= 0)
    {
    CurrentView = SentFriendRequestsView.Default;
    return;
    }

    GetBulkUserInfo(result.Value);
    }
  5. Create a function called GetBulkUserInfo(). This will be the main function that retrieves the user information from the list of friend IDs.

    private void GetBulkUserInfo(Friends friends)
    {
    friendsEssentialsWrapper.GetBulkUserInfo(friends.friendsId, OnGetBulkUserInfoCompleted);
    }
  6. Create a callback function called OnGetBulkUserInfoCompleted() to display the user information retrieved from GetBulkUserInfo function.

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

    ClearFriendRequestList();
    CurrentView = SentFriendRequestsView.LoadSuccess;

    PopulateFriendRequestList(result.Value.data);
    }
  7. Create a function called PopulateFriendRequestList() to populate the sent friend request list.

    private void PopulateFriendRequestList(params BaseUserInfo[] userInfo)
    {
    foreach (BaseUserInfo baseUserInfo in userInfo)
    {
    CreateFriendEntry(baseUserInfo.userId, baseUserInfo.displayName);
    }
    }
  8. Create a function called CreateFriendEntry() to create a sent friend entry for each friend request.

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

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

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

    friendRequests.Add(userId, playerEntry);
    }
  9. Update the OnEnable() function to load the sent friend request list when the UI is enabled. Add the code below:

    private void OnEnable()
    {
    //...
    if (friendsEssentialsWrapper != null)
    {
    LoadOutgoingFriendRequests();
    }
    }

Connect the UI to display avatars of friend requests

  1. In both FriendRequestMenuHandler_Starter.cs and SentFriendRequestMenuHandler_Starter.cs, create a function called RetrieveUserAvatar() to get the user avatar and a pass OnGetAvatarCompleted as the result handler.

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

    private void OnGetAvatarCompleted(Result<Texture2D> result, string userId)
    {
    if (result.IsError)
    {
    BytewarsLogger.LogWarning($"Unable to get avatar for user Id: {userId}, " +
    $"Error Code: {result.Error.Code}, " +
    $"Error Message: {result.Error.Message}");
    return;
    }

    if (result.Value == null || !friendRequests.TryGetValue(userId, out GameObject friendEntry))
    {
    return;
    }

    Image friendImage = friendEntry.GetComponent<SentFriendRequestsEntryHandler>().FriendImage;
    Rect imageRect = new Rect(0f, 0f, result.Value.width, result.Value.height);
    friendImage.sprite = Sprite.Create(result.Value, imageRect, Vector2.zero);
    }
  3. Update the CreateFriendEntry() function in both files to call the RetrieveUserAvatar() function.

    private void CreateFriendEntry(string userId, string displayName)
    {
    //...
    friendRequests.Add(userId, playerEntry);

    RetrieveUserAvatar(userId);
    }

Connect the UI to accept, reject, and cancel friend requests

  1. In FriendRequestMenuHandler_Starter.cs, create a function called AcceptFriendInvitation() to accept an incoming friend request.

    private void AcceptFriendInvitation(string userId)
    {
    MenuManager.Instance.PromptMenu.ShowLoadingPrompt(FriendsHelper.AcceptingFriendRequestMessage);

    friendsEssentialsWrapper.AcceptFriend(userId, OnAcceptInvitationCompleted);
    }
  2. Create a callback function called OnAcceptInvitationCompleted() to handle the result. It will show a confirmation that the request was completed and update the friend request list.

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

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

    LoadIncomingFriendRequests();
    }
  3. Create a function called DeclineFriendInvitation() to reject an incoming friend request.

    private void DeclineFriendInvitation(string userId)
    {
    MenuManager.Instance.PromptMenu.ShowLoadingPrompt(FriendsHelper.RejectingFriendRequestMessage);

    friendsEssentialsWrapper.DeclineFriend(userId, OnDeclineInvitationCompleted);
    }
  4. Create a callback function called OnDeclineInvitationCompleted() to handle the result. It will show a confirmation that the request was completed and update the friend request list.

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

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

    LoadIncomingFriendRequests();
    }
  5. Add the code below to CreateFriendEntry() to connect the accept and reject buttons to the AcceptFriendInvitation() and DeclineFriendInvitation() functions.

    private void CreateFriendEntry(string userId, string displayName)
    {
    //...
    playerEntryHandler.UserId = userId;
    playerEntryHandler.FriendName.text = displayName;
    playerEntryHandler.AcceptButton.onClick.AddListener(() => AcceptFriendInvitation(userId));
    playerEntryHandler.RejectButton.onClick.AddListener(() => DeclineFriendInvitation(userId));

    friendRequests.Add(userId, playerEntry);
    //...
    }
  6. Open SentFriendRequestMenuHandler_Starter.cs, create a function called CancelFriendRequest() to cancel a friend request.

    private void CancelFriendRequest(string userId)
    {
    MenuManager.Instance.PromptMenu.ShowLoadingPrompt(FriendsHelper.CancelingFriendRequestMessage);

    friendsEssentialsWrapper.CancelFriendRequests(userId, OnCancelFriendRequestCompleted);
    }
  7. Create a callback function called OnCancelFriendRequestCompleted() to handle the result. It will show a confirmation that the request was completed and update the sent friend request list.

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

    MenuManager.Instance.PromptMenu.ShowPromptMenu("Message", FriendsHelper.FriendRequestCanceledMessage, "OK", null);
    LoadOutgoingFriendRequests();
    }
  8. Add the code below to CreateFriendEntry() to connect the cancel button to the CancelFriendRequest() function.

    private void CreateFriendEntry(string userId, string displayName)
    {
    //...
    playerEntryHandler.UserId = userId;
    playerEntryHandler.FriendName.text = displayName;
    playerEntryHandler.CancelButton.onClick.AddListener(() => CancelFriendRequest(userId));

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

Connect the UI to listen to friend request events

  1. Open FriendRequestMenuHandler_Starter.cs, modify the Awake() function to listen to the OnIncomingRequest and OnRequestCanceled events. Add the code below:

    private void Awake()
    {
    CurrentView = FriendRequestsView.Default;

    backButton.onClick.AddListener(MenuManager.Instance.OnBackPressed);

    FriendsEssentialsWrapper.OnIncomingRequest += OnFriendRequestUpdated;
    FriendsEssentialsWrapper.OnRequestCanceled += OnFriendRequestUpdated;
    }
  2. Create a function called OnIncomingFriendRequest() to handle the incoming friend request event. It will update the friend request list.

    private void OnFriendRequestUpdated(string userId)
    {
    if (!gameObject.activeSelf)
    {
    return;
    }

    LoadIncomingFriendRequests();
    }
  3. Open SentFriendRequestMenuHandler_Starter.cs and modify the Awake() function to listen to the OnRequestRejected and OnRequestAccepted events. Add the code below:

    private void Awake()
    {
    CurrentView = SentFriendRequestsView.Default;

    backButton.onClick.AddListener(MenuManager.Instance.OnBackPressed);

    FriendsEssentialsWrapper.OnRequestRejected += OnFriendRequestUpdated;
    FriendsEssentialsWrapper.OnRequestAccepted += OnFriendRequestUpdated;
    }
  4. Create a function called OnFriendRequestUpdated() to handle the friend request updated event. It will update the sent friend request list.

    private void OnFriendRequestUpdated(string userId)
    {
    if (!gameObject.activeSelf)
    {
    return;
    }

    LoadOutgoingFriendRequests();
    }

Resources