| | 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); |
| | 95 | |
|
| 0 | 96 | | WebInterface.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage |
| | 97 | | { |
| | 98 | | userId = currentPlayerId, action = FriendshipAction.REQUESTED_TO |
| | 99 | | }); |
| | 100 | |
|
| 0 | 101 | | socialAnalytics.SendFriendRequestSent(ownUserProfile.userId, currentPlayerId, 0, PlayerActionSource.Passport); |
| 0 | 102 | | } |
| | 103 | |
|
| | 104 | | private void CancelInvitation() |
| | 105 | | { |
| | 106 | | // Add fake action to avoid waiting for kernel |
| 0 | 107 | | friendsController.CancelRequest(currentPlayerId); |
| | 108 | |
|
| 0 | 109 | | WebInterface.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage() |
| | 110 | | { |
| | 111 | | userId = currentPlayerId, action = FriendshipAction.CANCELLED |
| | 112 | | }); |
| | 113 | |
|
| 0 | 114 | | socialAnalytics.SendFriendRequestCancelled(ownUserProfile.userId, currentPlayerId, PlayerActionSource.Passport); |
| 0 | 115 | | } |
| | 116 | |
|
| | 117 | | private void AcceptFriendRequest() |
| | 118 | | { |
| | 119 | | // Add fake action to avoid waiting for kernel |
| 0 | 120 | | friendsController.AcceptFriendship(currentPlayerId); |
| | 121 | |
|
| 0 | 122 | | WebInterface.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage() |
| | 123 | | { |
| | 124 | | userId = currentPlayerId, action = FriendshipAction.APPROVED |
| | 125 | | }); |
| | 126 | |
|
| 0 | 127 | | socialAnalytics.SendFriendRequestApproved(ownUserProfile.userId, currentPlayerId, PlayerActionSource.Passport); |
| 0 | 128 | | } |
| | 129 | |
|
| | 130 | | private void RejectFriendRequest() |
| | 131 | | { |
| | 132 | | // Add fake action to avoid waiting for kernel |
| 0 | 133 | | friendsController.RejectFriendship(currentPlayerId); |
| | 134 | |
|
| 0 | 135 | | WebInterface.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage() |
| | 136 | | { |
| | 137 | | userId = currentPlayerId, action = FriendshipAction.REJECTED |
| | 138 | | }); |
| | 139 | |
|
| 0 | 140 | | socialAnalytics.SendFriendRequestRejected(ownUserProfile.userId, currentPlayerId, PlayerActionSource.Passport); |
| 0 | 141 | | } |
| | 142 | |
|
| | 143 | | private void OnCurrentPlayerIdChanged(string current, string previous) |
| | 144 | | { |
| 45 | 145 | | if (currentUserProfile != null) |
| 4 | 146 | | currentUserProfile.OnUpdate -= SetUserProfile; |
| | 147 | |
|
| 45 | 148 | | currentUserProfile = string.IsNullOrEmpty(current) |
| | 149 | | ? null |
| | 150 | | : userProfileBridge.Get(current); |
| | 151 | |
|
| 45 | 152 | | if (currentUserProfile == null) |
| | 153 | | { |
| 23 | 154 | | if (playerInfoCardVisibleState.Get()) |
| 3 | 155 | | socialAnalytics.SendPassportClose(Time.realtimeSinceStartup - passportOpenStartTime); |
| | 156 | |
|
| 23 | 157 | | view.SetCardActive(false); |
| 23 | 158 | | wearableCatalogBridge.RemoveWearablesInUse(loadedWearables); |
| 23 | 159 | | loadedWearables.Clear(); |
| 23 | 160 | | } |
| | 161 | | else |
| | 162 | | { |
| 22 | 163 | | currentUserProfile.OnUpdate += SetUserProfile; |
| | 164 | |
|
| 22 | 165 | | TaskUtils.Run(async () => |
| | 166 | | { |
| 22 | 167 | | await AsyncSetUserProfile(currentUserProfile); |
| 22 | 168 | | view.SetCardActive(true); |
| 22 | 169 | | socialAnalytics.SendPassportOpen(); |
| 22 | 170 | | }) |
| | 171 | | .Forget(); |
| | 172 | |
|
| 22 | 173 | | passportOpenStartTime = Time.realtimeSinceStartup; |
| | 174 | | } |
| 22 | 175 | | } |
| | 176 | |
|
| | 177 | | private void SetUserProfile(UserProfile userProfile) |
| | 178 | | { |
| 9 | 179 | | Assert.IsTrue(userProfile != null, "userProfile can't be null"); |
| | 180 | |
|
| 18 | 181 | | TaskUtils.Run(async () => await AsyncSetUserProfile(userProfile)).Forget(); |
| 9 | 182 | | } |
| | 183 | | private async UniTask AsyncSetUserProfile(UserProfile userProfile) |
| | 184 | | { |
| 31 | 185 | | string filterName = await FilterName(userProfile); |
| 31 | 186 | | string filterDescription = await FilterDescription(userProfile); |
| 31 | 187 | | await UniTask.SwitchToMainThread(); |
| | 188 | |
|
| 31 | 189 | | view.SetName(filterName); |
| 31 | 190 | | view.SetDescription(filterDescription); |
| 31 | 191 | | view.ClearCollectibles(); |
| 31 | 192 | | view.SetIsBlocked(IsBlocked(userProfile.userId)); |
| 31 | 193 | | LoadAndShowWearables(userProfile); |
| 31 | 194 | | UpdateFriendshipInteraction(); |
| | 195 | |
|
| 31 | 196 | | if (viewingUserProfile != null) |
| 13 | 197 | | viewingUserProfile.snapshotObserver.RemoveListener(view.SetFaceSnapshot); |
| | 198 | |
|
| 31 | 199 | | userProfile.snapshotObserver.AddListener(view.SetFaceSnapshot); |
| 31 | 200 | | viewingUserProfile = userProfile; |
| 31 | 201 | | } |
| | 202 | |
|
| | 203 | | public void SetVisibility(bool visible) |
| | 204 | | { |
| 1 | 205 | | view.SetVisibility(visible); |
| | 206 | |
|
| 1 | 207 | | if (viewingUserProfile != null) |
| 0 | 208 | | viewingUserProfile.snapshotObserver.RemoveListener(view.SetFaceSnapshot); |
| | 209 | |
|
| 1 | 210 | | if (visible) |
| | 211 | | { |
| 1 | 212 | | if (viewingUserProfile != null) |
| 0 | 213 | | viewingUserProfile.snapshotObserver.AddListener(view.SetFaceSnapshot); |
| | 214 | | } |
| 1 | 215 | | } |
| | 216 | |
|
| | 217 | | private void BlockPlayer() |
| | 218 | | { |
| 0 | 219 | | if (ownUserProfile.IsBlocked(currentUserProfile.userId)) return; |
| 0 | 220 | | ownUserProfile.Block(currentUserProfile.userId); |
| 0 | 221 | | view.SetIsBlocked(true); |
| 0 | 222 | | WebInterface.SendBlockPlayer(currentUserProfile.userId); |
| 0 | 223 | | socialAnalytics.SendPlayerBlocked(friendsController.IsFriend(currentUserProfile.userId), PlayerActionSource.Pass |
| 0 | 224 | | } |
| | 225 | |
|
| | 226 | | private void UnblockPlayer() |
| | 227 | | { |
| 0 | 228 | | if (!ownUserProfile.IsBlocked(currentUserProfile.userId)) return; |
| 0 | 229 | | ownUserProfile.Unblock(currentUserProfile.userId); |
| 0 | 230 | | view.SetIsBlocked(false); |
| 0 | 231 | | WebInterface.SendUnblockPlayer(currentUserProfile.userId); |
| 0 | 232 | | socialAnalytics.SendPlayerUnblocked(friendsController.IsFriend(currentUserProfile.userId), PlayerActionSource.Pa |
| 0 | 233 | | } |
| | 234 | |
|
| | 235 | | private void ReportPlayer() |
| | 236 | | { |
| 0 | 237 | | WebInterface.SendReportPlayer(currentPlayerId, currentUserProfile?.name); |
| 0 | 238 | | socialAnalytics.SendPlayerReport(PlayerReportIssueType.None, 0, PlayerActionSource.Passport); |
| 0 | 239 | | } |
| | 240 | |
|
| | 241 | | public void Dispose() |
| | 242 | | { |
| 20 | 243 | | if (currentUserProfile != null) |
| 18 | 244 | | currentUserProfile.OnUpdate -= SetUserProfile; |
| | 245 | |
|
| 20 | 246 | | if (currentPlayerId != null) |
| 20 | 247 | | currentPlayerId.OnChange -= OnCurrentPlayerIdChanged; |
| | 248 | |
|
| 20 | 249 | | if (closeWindowTrigger != null) |
| 20 | 250 | | closeWindowTrigger.OnTriggered -= OnCloseButtonPressed; |
| | 251 | |
|
| 20 | 252 | | if (closeWindowTrigger != null) |
| 20 | 253 | | closeWindowTrigger.OnTriggered -= OnCloseButtonPressed; |
| | 254 | |
|
| 20 | 255 | | if (toggleWorldChatTrigger != null) |
| 20 | 256 | | toggleWorldChatTrigger.OnTriggered -= OnCloseButtonPressed; |
| | 257 | |
|
| 20 | 258 | | if (toggleFriendsTrigger != null) |
| 20 | 259 | | toggleFriendsTrigger.OnTriggered -= OnCloseButtonPressed; |
| | 260 | |
|
| 20 | 261 | | if (viewingUserProfile != null) |
| 18 | 262 | | viewingUserProfile.snapshotObserver.RemoveListener(view.SetFaceSnapshot); |
| | 263 | |
|
| 20 | 264 | | if (view != null) |
| 20 | 265 | | Object.Destroy(view.gameObject); |
| 20 | 266 | | } |
| | 267 | |
|
| | 268 | | private void OnFriendStatusUpdated(string userId, FriendshipAction action) |
| | 269 | | { |
| 0 | 270 | | if (currentUserProfile == null) |
| 0 | 271 | | return; |
| | 272 | |
|
| 0 | 273 | | UpdateFriendshipInteraction(); |
| 0 | 274 | | } |
| | 275 | |
|
| | 276 | | private void UpdateFriendshipInteraction() |
| | 277 | | { |
| 31 | 278 | | if (currentUserProfile == null) |
| | 279 | | { |
| 0 | 280 | | view.HideFriendshipInteraction(); |
| 0 | 281 | | return; |
| | 282 | | } |
| | 283 | |
|
| 31 | 284 | | view.UpdateFriendshipInteraction(CanBeFriends(), |
| | 285 | | friendsController.GetUserStatus(currentUserProfile.userId)); |
| 31 | 286 | | } |
| | 287 | |
|
| | 288 | | private bool CanBeFriends() |
| | 289 | | { |
| 31 | 290 | | return friendsController != null && friendsController.isInitialized && currentUserProfile.hasConnectedWeb3; |
| | 291 | | } |
| | 292 | |
|
| | 293 | | private void LoadAndShowWearables(UserProfile userProfile) |
| | 294 | | { |
| 31 | 295 | | wearableCatalogBridge.RequestOwnedWearables(userProfile.userId) |
| | 296 | | .Then(wearables => |
| | 297 | | { |
| 186 | 298 | | var wearableIds = wearables.Select(x => x.id).ToArray(); |
| 31 | 299 | | userProfile.SetInventory(wearableIds); |
| 31 | 300 | | loadedWearables.AddRange(wearableIds); |
| 31 | 301 | | var containedWearables = wearables |
| | 302 | | // this makes any sense? |
| 155 | 303 | | .Where(wearable => wearableCatalogBridge.IsValidWearable(wearable.id)); |
| 31 | 304 | | view.SetWearables(containedWearables); |
| 31 | 305 | | }) |
| | 306 | | .Catch(Debug.LogError); |
| 31 | 307 | | } |
| | 308 | |
|
| | 309 | | private bool IsBlocked(string userId) |
| | 310 | | { |
| 31 | 311 | | return ownUserProfile != null && ownUserProfile.IsBlocked(userId); |
| | 312 | | } |
| | 313 | |
|
| | 314 | | private async UniTask<string> FilterName(UserProfile userProfile) |
| | 315 | | { |
| 31 | 316 | | return IsProfanityFilteringEnabled() |
| | 317 | | ? await profanityFilter.Filter(userProfile.userName) |
| | 318 | | : userProfile.userName; |
| 31 | 319 | | } |
| | 320 | |
|
| | 321 | | private async UniTask<string> FilterDescription(UserProfile userProfile) |
| | 322 | | { |
| 31 | 323 | | return IsProfanityFilteringEnabled() |
| | 324 | | ? await profanityFilter.Filter(userProfile.description) |
| | 325 | | : userProfile.description; |
| 31 | 326 | | } |
| | 327 | |
|
| | 328 | | private bool IsProfanityFilteringEnabled() |
| | 329 | | { |
| 62 | 330 | | return dataStore.settings.profanityChatFilteringEnabled.Get(); |
| | 331 | | } |
| | 332 | | } |