Advertisement
Guest User

Untitled

a guest
Jan 16th, 2021
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using PlayFab;
  4. using PlayFab.ClientModels;
  5. using System;
  6. using System.Linq;
  7. using UnityEngine;
  8. using TMPro;
  9. using UnityEngine.Networking;
  10. using UnityEngine.UI;
  11.  
  12. namespace KnoxGameStudios
  13. {
  14. public class PlayfabFriendController : MonoBehaviour
  15. {
  16. public static Action<List<FriendInfo>> OnFriendListUpdated = delegate { };
  17. private List<FriendInfo> friends;
  18.  
  19. #region Unity Methods
  20. private void Awake()
  21. {
  22. friends = new List<FriendInfo>();
  23. PhotonConnector.GetPhotonFriends += HandleGetFriends;
  24. UIAddFriend.OnAddFriend += HandleAddPlayfabFriend;
  25. UIFriend.OnRemoveFriend += HandleRemoveFriend;
  26. }
  27.  
  28. private void OnDestroy()
  29. {
  30. PhotonConnector.GetPhotonFriends -= HandleGetFriends;
  31. UIAddFriend.OnAddFriend -= HandleAddPlayfabFriend;
  32. UIFriend.OnRemoveFriend -= HandleRemoveFriend;
  33. }
  34. #endregion
  35.  
  36. #region Private Methods
  37. private void HandleAddPlayfabFriend(string name)
  38. {
  39. Debug.Log($"Playfab add friend request for {name}");
  40. var request = new AddFriendRequest { FriendTitleDisplayName = name };
  41. PlayFabClientAPI.AddFriend(request, OnFriendAddedSuccess, OnFailure);
  42. }
  43. private void HandleRemoveFriend(string name)
  44. {
  45. string id = friends.FirstOrDefault(f => f.TitleDisplayName == name).FriendPlayFabId;
  46. Debug.Log($"Playfab remove friend {name} with id {id}");
  47. var request = new RemoveFriendRequest { FriendPlayFabId = id };
  48. PlayFabClientAPI.RemoveFriend(request, OnFriendRemoveSuccess, OnFailure);
  49. }
  50.  
  51. private void HandleGetFriends()
  52. {
  53. GetPlayfabFriends();
  54. }
  55.  
  56. private void GetPlayfabFriends()
  57. {
  58. Debug.Log("Playfab get friend list request");
  59. var request = new GetFriendsListRequest { IncludeSteamFriends = false, IncludeFacebookFriends = false, XboxToken = null, ProfileConstraints = new PlayerProfileViewConstraints { ShowAvatarUrl = true }, };
  60. PlayFabClientAPI.GetFriendsList(request, OnFriendsListSuccess, OnFailure);
  61. }
  62. #endregion
  63.  
  64. #region Playfab Call backs
  65. private void OnFriendAddedSuccess(AddFriendResult result)
  66. {
  67. Debug.Log("Playfab add friend success getting updated friend list");
  68. GetPlayfabFriends();
  69. }
  70.  
  71.  
  72. private void OnFriendsListSuccess(GetFriendsListResult result)
  73. {
  74. Debug.Log($"Playfab get friend list success: {result.Friends.Count}");
  75. friends = result.Friends;
  76. OnFriendListUpdated?.Invoke(result.Friends);
  77. }
  78.  
  79. private void OnFriendRemoveSuccess(RemoveFriendResult result)
  80. {
  81. Debug.Log($"Playfab remove friend success");
  82. GetPlayfabFriends();
  83. }
  84.  
  85. private void OnFailure(PlayFabError error)
  86. {
  87. Debug.Log($"Playfab Friend Error occured: {error.GenerateErrorReport()}");
  88. }
  89.  
  90. #endregion
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement