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