| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using DCL.Interface; |
| | 5 | | using SocialFeaturesAnalytics; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.Linq; |
| | 8 | | using UnityEngine; |
| | 9 | | using UnityEngine.Assertions; |
| | 10 | |
|
| | 11 | | public class PlayerInfoCardHUDController : IHUD |
| | 12 | | { |
| | 13 | | internal readonly PlayerInfoCardHUDView view; |
| | 14 | | internal readonly StringVariable currentPlayerId; |
| | 15 | | internal UserProfile currentUserProfile; |
| | 16 | |
|
| | 17 | | private UserProfile viewingUserProfile; |
| 62 | 18 | | private UserProfile ownUserProfile => userProfileBridge.GetOwn(); |
| | 19 | |
|
| | 20 | | private readonly IFriendsController friendsController; |
| | 21 | | private readonly InputAction_Trigger toggleFriendsTrigger; |
| | 22 | | private readonly InputAction_Trigger closeWindowTrigger; |
| | 23 | | private readonly InputAction_Trigger toggleWorldChatTrigger; |
| | 24 | | private readonly IUserProfileBridge userProfileBridge; |
| | 25 | | private readonly IWearableCatalogBridge wearableCatalogBridge; |
| | 26 | | private readonly IProfanityFilter profanityFilter; |
| | 27 | | private readonly DataStore dataStore; |
| | 28 | | private readonly BooleanVariable playerInfoCardVisibleState; |
| 20 | 29 | | private readonly List<string> loadedWearables = new List<string>(); |
| | 30 | | private readonly ISocialAnalytics socialAnalytics; |
| | 31 | | private double passportOpenStartTime = 0; |
| | 32 | |
|
| 20 | 33 | | public PlayerInfoCardHUDController(IFriendsController friendsController, |
| | 34 | | StringVariable currentPlayerIdData, |
| | 35 | | IUserProfileBridge userProfileBridge, |
| | 36 | | IWearableCatalogBridge wearableCatalogBridge, |
| | 37 | | ISocialAnalytics socialAnalytics, |
| | 38 | | IProfanityFilter profanityFilter, |
| | 39 | | DataStore dataStore, |
| | 40 | | BooleanVariable playerInfoCardVisibleState) |
| | 41 | | { |
| 20 | 42 | | this.friendsController = friendsController; |
| 20 | 43 | | view = PlayerInfoCardHUDView.CreateView(); |
| 20 | 44 | | view.Initialize(() => OnCloseButtonPressed(), |
| | 45 | | ReportPlayer, BlockPlayer, UnblockPlayer, |
| | 46 | | AddPlayerAsFriend, CancelInvitation, AcceptFriendRequest, RejectFriendRequest); |
| 20 | 47 | | currentPlayerId = currentPlayerIdData; |
| 20 | 48 | | this.userProfileBridge = userProfileBridge; |
| 20 | 49 | | this.wearableCatalogBridge = wearableCatalogBridge; |
| 20 | 50 | | this.socialAnalytics = socialAnalytics; |
| 20 | 51 | | this.profanityFilter = profanityFilter; |
| 20 | 52 | | this.dataStore = dataStore; |
| 20 | 53 | | this.playerInfoCardVisibleState = playerInfoCardVisibleState; |
| 20 | 54 | | currentPlayerId.OnChange += OnCurrentPlayerIdChanged; |
| 20 | 55 | | OnCurrentPlayerIdChanged(currentPlayerId, null); |
| | 56 | |
|
| 20 | 57 | | toggleFriendsTrigger = Resources.Load<InputAction_Trigger>("ToggleFriends"); |
| 20 | 58 | | toggleFriendsTrigger.OnTriggered -= OnCloseButtonPressed; |
| 20 | 59 | | toggleFriendsTrigger.OnTriggered += OnCloseButtonPressed; |
| | 60 | |
|
| 20 | 61 | | closeWindowTrigger = Resources.Load<InputAction_Trigger>("CloseWindow"); |
| 20 | 62 | | closeWindowTrigger.OnTriggered -= OnCloseButtonPressed; |
| 20 | 63 | | closeWindowTrigger.OnTriggered += OnCloseButtonPressed; |
| | 64 | |
|
| 20 | 65 | | toggleWorldChatTrigger = Resources.Load<InputAction_Trigger>("ToggleWorldChat"); |
| 20 | 66 | | toggleWorldChatTrigger.OnTriggered -= OnCloseButtonPressed; |
| 20 | 67 | | toggleWorldChatTrigger.OnTriggered += OnCloseButtonPressed; |
| | 68 | |
|
| 20 | 69 | | friendsController.OnUpdateFriendship -= OnFriendStatusUpdated; |
| 20 | 70 | | friendsController.OnUpdateFriendship += OnFriendStatusUpdated; |
| 20 | 71 | | } |
| | 72 | |
|
| | 73 | | public void CloseCard() |
| | 74 | | { |
| 0 | 75 | | currentPlayerId.Set(null); |
| 0 | 76 | | } |
| | 77 | |
|
| | 78 | | private void OnCloseButtonPressed(DCLAction_Trigger action = DCLAction_Trigger.CloseWindow) |
| | 79 | | { |
| 0 | 80 | | CloseCard(); |
| 0 | 81 | | } |
| | 82 | |
|
| | 83 | | private void AddPlayerAsFriend() |
| | 84 | | { |
| 0 | 85 | | UserProfile currentUserProfile = userProfileBridge.Get(currentPlayerId); |
| | 86 | |
|
| | 87 | | // Add fake action to avoid waiting for kernel |
| 0 | 88 | | userProfileBridge.AddUserProfileToCatalog(new UserProfileModel |
| | 89 | | { |
| | 90 | | userId = currentPlayerId, |
| | 91 | | name = currentUserProfile != null ? currentUserProfile.userName : currentPlayerId |
| | 92 | | }); |
| | 93 | |
|
| 0 | 94 | | friendsController.RequestFriendship(currentPlayerId); |
| 0 | 95 | | socialAnalytics.SendFriendRequestSent(ownUserProfile.userId, currentPlayerId, 0, PlayerActionSource.Passport); |
| 0 | 96 | | } |
| | 97 | |
|
| | 98 | | private void CancelInvitation() |
| | 99 | | { |
| 0 | 100 | | friendsController.CancelRequest(currentPlayerId); |
| 0 | 101 | | socialAnalytics.SendFriendRequestCancelled(ownUserProfile.userId, currentPlayerId, PlayerActionSource.Passport); |
| 0 | 102 | | } |
| | 103 | |
|
| | 104 | | private void AcceptFriendRequest() |
| | 105 | | { |
| 0 | 106 | | friendsController.AcceptFriendship(currentPlayerId); |
| 0 | 107 | | socialAnalytics.SendFriendRequestApproved(ownUserProfile.userId, currentPlayerId, PlayerActionSource.Passport); |
| 0 | 108 | | } |
| | 109 | |
|
| | 110 | | private void RejectFriendRequest() |
| | 111 | | { |
| 0 | 112 | | friendsController.RejectFriendship(currentPlayerId); |
| 0 | 113 | | socialAnalytics.SendFriendRequestRejected(ownUserProfile.userId, currentPlayerId, PlayerActionSource.Passport); |
| 0 | 114 | | } |
| | 115 | |
|
| | 116 | | private void OnCurrentPlayerIdChanged(string current, string previous) |
| | 117 | | { |
| 45 | 118 | | if (currentUserProfile != null) |
| 4 | 119 | | currentUserProfile.OnUpdate -= SetUserProfile; |
| | 120 | |
|
| 45 | 121 | | currentUserProfile = string.IsNullOrEmpty(current) |
| | 122 | | ? null |
| | 123 | | : userProfileBridge.Get(current); |
| | 124 | |
|
| 45 | 125 | | if (currentUserProfile == null) |
| | 126 | | { |
| 23 | 127 | | if (playerInfoCardVisibleState.Get()) |
| 3 | 128 | | socialAnalytics.SendPassportClose(Time.realtimeSinceStartup - passportOpenStartTime); |
| | 129 | |
|
| 23 | 130 | | view.SetCardActive(false); |
| 23 | 131 | | wearableCatalogBridge.RemoveWearablesInUse(loadedWearables); |
| 23 | 132 | | loadedWearables.Clear(); |
| 23 | 133 | | } |
| | 134 | | else |
| | 135 | | { |
| 22 | 136 | | currentUserProfile.OnUpdate += SetUserProfile; |
| | 137 | |
|
| 22 | 138 | | TaskUtils.Run(async () => |
| | 139 | | { |
| 22 | 140 | | await AsyncSetUserProfile(currentUserProfile); |
| 22 | 141 | | view.SetCardActive(true); |
| 22 | 142 | | socialAnalytics.SendPassportOpen(); |
| 22 | 143 | | }) |
| | 144 | | .Forget(); |
| | 145 | |
|
| 22 | 146 | | passportOpenStartTime = Time.realtimeSinceStartup; |
| | 147 | | } |
| 22 | 148 | | } |
| | 149 | |
|
| | 150 | | private void SetUserProfile(UserProfile userProfile) |
| | 151 | | { |
| 9 | 152 | | Assert.IsTrue(userProfile != null, "userProfile can't be null"); |
| | 153 | |
|
| 18 | 154 | | TaskUtils.Run(async () => await AsyncSetUserProfile(userProfile)).Forget(); |
| 9 | 155 | | } |
| | 156 | | private async UniTask AsyncSetUserProfile(UserProfile userProfile) |
| | 157 | | { |
| 31 | 158 | | string filterName = await FilterName(userProfile); |
| 31 | 159 | | string filterDescription = await FilterDescription(userProfile); |
| 31 | 160 | | await UniTask.SwitchToMainThread(); |
| | 161 | |
|
| 31 | 162 | | view.SetName(filterName); |
| 31 | 163 | | view.SetDescription(filterDescription); |
| 31 | 164 | | view.ClearCollectibles(); |
| 31 | 165 | | view.SetIsBlocked(IsBlocked(userProfile.userId)); |
| 31 | 166 | | LoadAndShowWearables(userProfile); |
| 31 | 167 | | UpdateFriendshipInteraction(); |
| | 168 | |
|
| 31 | 169 | | if (viewingUserProfile != null) |
| 13 | 170 | | viewingUserProfile.snapshotObserver.RemoveListener(view.SetFaceSnapshot); |
| | 171 | |
|
| 31 | 172 | | userProfile.snapshotObserver.AddListener(view.SetFaceSnapshot); |
| 31 | 173 | | viewingUserProfile = userProfile; |
| 31 | 174 | | } |
| | 175 | |
|
| | 176 | | public void SetVisibility(bool visible) |
| | 177 | | { |
| 1 | 178 | | view.SetVisibility(visible); |
| | 179 | |
|
| 1 | 180 | | if (viewingUserProfile != null) |
| 0 | 181 | | viewingUserProfile.snapshotObserver.RemoveListener(view.SetFaceSnapshot); |
| | 182 | |
|
| 1 | 183 | | if (visible) |
| | 184 | | { |
| 1 | 185 | | if (viewingUserProfile != null) |
| 0 | 186 | | viewingUserProfile.snapshotObserver.AddListener(view.SetFaceSnapshot); |
| | 187 | | } |
| 1 | 188 | | } |
| | 189 | |
|
| | 190 | | private void BlockPlayer() |
| | 191 | | { |
| 0 | 192 | | if (ownUserProfile.IsBlocked(currentUserProfile.userId)) return; |
| 0 | 193 | | ownUserProfile.Block(currentUserProfile.userId); |
| 0 | 194 | | view.SetIsBlocked(true); |
| 0 | 195 | | WebInterface.SendBlockPlayer(currentUserProfile.userId); |
| 0 | 196 | | socialAnalytics.SendPlayerBlocked(friendsController.IsFriend(currentUserProfile.userId), PlayerActionSource.Pass |
| 0 | 197 | | } |
| | 198 | |
|
| | 199 | | private void UnblockPlayer() |
| | 200 | | { |
| 0 | 201 | | if (!ownUserProfile.IsBlocked(currentUserProfile.userId)) return; |
| 0 | 202 | | ownUserProfile.Unblock(currentUserProfile.userId); |
| 0 | 203 | | view.SetIsBlocked(false); |
| 0 | 204 | | WebInterface.SendUnblockPlayer(currentUserProfile.userId); |
| 0 | 205 | | socialAnalytics.SendPlayerUnblocked(friendsController.IsFriend(currentUserProfile.userId), PlayerActionSource.Pa |
| 0 | 206 | | } |
| | 207 | |
|
| | 208 | | private void ReportPlayer() |
| | 209 | | { |
| 0 | 210 | | WebInterface.SendReportPlayer(currentPlayerId, currentUserProfile?.name); |
| 0 | 211 | | socialAnalytics.SendPlayerReport(PlayerReportIssueType.None, 0, PlayerActionSource.Passport); |
| 0 | 212 | | } |
| | 213 | |
|
| | 214 | | public void Dispose() |
| | 215 | | { |
| 20 | 216 | | if (currentUserProfile != null) |
| 18 | 217 | | currentUserProfile.OnUpdate -= SetUserProfile; |
| | 218 | |
|
| 20 | 219 | | if (currentPlayerId != null) |
| 20 | 220 | | currentPlayerId.OnChange -= OnCurrentPlayerIdChanged; |
| | 221 | |
|
| 20 | 222 | | if (closeWindowTrigger != null) |
| 20 | 223 | | closeWindowTrigger.OnTriggered -= OnCloseButtonPressed; |
| | 224 | |
|
| 20 | 225 | | if (closeWindowTrigger != null) |
| 20 | 226 | | closeWindowTrigger.OnTriggered -= OnCloseButtonPressed; |
| | 227 | |
|
| 20 | 228 | | if (toggleWorldChatTrigger != null) |
| 20 | 229 | | toggleWorldChatTrigger.OnTriggered -= OnCloseButtonPressed; |
| | 230 | |
|
| 20 | 231 | | if (toggleFriendsTrigger != null) |
| 20 | 232 | | toggleFriendsTrigger.OnTriggered -= OnCloseButtonPressed; |
| | 233 | |
|
| 20 | 234 | | if (viewingUserProfile != null) |
| 18 | 235 | | viewingUserProfile.snapshotObserver.RemoveListener(view.SetFaceSnapshot); |
| | 236 | |
|
| 20 | 237 | | if (view != null) |
| 20 | 238 | | Object.Destroy(view.gameObject); |
| 20 | 239 | | } |
| | 240 | |
|
| | 241 | | private void OnFriendStatusUpdated(string userId, FriendshipAction action) |
| | 242 | | { |
| 0 | 243 | | if (currentUserProfile == null) |
| 0 | 244 | | return; |
| | 245 | |
|
| 0 | 246 | | UpdateFriendshipInteraction(); |
| 0 | 247 | | } |
| | 248 | |
|
| | 249 | | private void UpdateFriendshipInteraction() |
| | 250 | | { |
| 31 | 251 | | if (currentUserProfile == null) |
| | 252 | | { |
| 0 | 253 | | view.HideFriendshipInteraction(); |
| 0 | 254 | | return; |
| | 255 | | } |
| | 256 | |
|
| 31 | 257 | | view.UpdateFriendshipInteraction(CanBeFriends(), |
| | 258 | | friendsController.GetUserStatus(currentUserProfile.userId)); |
| 31 | 259 | | } |
| | 260 | |
|
| | 261 | | private bool CanBeFriends() |
| | 262 | | { |
| 31 | 263 | | return friendsController != null && friendsController.IsInitialized && currentUserProfile.hasConnectedWeb3; |
| | 264 | | } |
| | 265 | |
|
| | 266 | | private void LoadAndShowWearables(UserProfile userProfile) |
| | 267 | | { |
| 31 | 268 | | wearableCatalogBridge.RequestOwnedWearables(userProfile.userId) |
| | 269 | | .Then(wearables => |
| | 270 | | { |
| 186 | 271 | | var wearableIds = wearables.Select(x => x.id).ToArray(); |
| 31 | 272 | | userProfile.SetInventory(wearableIds); |
| 31 | 273 | | loadedWearables.AddRange(wearableIds); |
| 31 | 274 | | var containedWearables = wearables |
| | 275 | | // this makes any sense? |
| 155 | 276 | | .Where(wearable => wearableCatalogBridge.IsValidWearable(wearable.id)); |
| 31 | 277 | | view.SetWearables(containedWearables); |
| 31 | 278 | | }) |
| | 279 | | .Catch(Debug.LogError); |
| 31 | 280 | | } |
| | 281 | |
|
| | 282 | | private bool IsBlocked(string userId) |
| | 283 | | { |
| 31 | 284 | | return ownUserProfile != null && ownUserProfile.IsBlocked(userId); |
| | 285 | | } |
| | 286 | |
|
| | 287 | | private async UniTask<string> FilterName(UserProfile userProfile) |
| | 288 | | { |
| 31 | 289 | | return IsProfanityFilteringEnabled() |
| | 290 | | ? await profanityFilter.Filter(userProfile.userName) |
| | 291 | | : userProfile.userName; |
| 31 | 292 | | } |
| | 293 | |
|
| | 294 | | private async UniTask<string> FilterDescription(UserProfile userProfile) |
| | 295 | | { |
| 31 | 296 | | return IsProfanityFilteringEnabled() |
| | 297 | | ? await profanityFilter.Filter(userProfile.description) |
| | 298 | | : userProfile.description; |
| 31 | 299 | | } |
| | 300 | |
|
| | 301 | | private bool IsProfanityFilteringEnabled() |
| | 302 | | { |
| 62 | 303 | | return dataStore.settings.profanityChatFilteringEnabled.Get(); |
| | 304 | | } |
| | 305 | | } |