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