| | 1 | | using DCL.Interface; |
| | 2 | | using UnityEngine; |
| | 3 | |
|
| | 4 | | public class PlayerInfoCardHUDController : IHUD |
| | 5 | | { |
| | 6 | | internal const string CURRENT_PLAYER_ID = "CurrentPlayerInfoCardId"; |
| | 7 | |
|
| | 8 | | internal PlayerInfoCardHUDView view; |
| | 9 | | internal StringVariable currentPlayerId; |
| | 10 | | internal UserProfile currentUserProfile; |
| 0 | 11 | | private UserProfile ownUserProfile => UserProfile.GetOwnUserProfile(); |
| | 12 | |
|
| | 13 | | private InputAction_Trigger toggleFriendsTrigger; |
| | 14 | | private InputAction_Trigger closeWindowTrigger; |
| | 15 | | private InputAction_Trigger toggleWorldChatTrigger; |
| | 16 | |
|
| 4 | 17 | | public PlayerInfoCardHUDController() |
| | 18 | | { |
| 4 | 19 | | view = PlayerInfoCardHUDView.CreateView(); |
| 4 | 20 | | view.Initialize(() => { OnCloseButtonPressed(); } |
| | 21 | | , ReportPlayer, BlockPlayer, UnblockPlayer, |
| | 22 | | AddPlayerAsFriend, CancelInvitation, AcceptFriendRequest, RejectFriendRequest); |
| 4 | 23 | | currentPlayerId = Resources.Load<StringVariable>(CURRENT_PLAYER_ID); |
| 4 | 24 | | currentPlayerId.OnChange += OnCurrentPlayerIdChanged; |
| 4 | 25 | | OnCurrentPlayerIdChanged(currentPlayerId, null); |
| | 26 | |
|
| 4 | 27 | | toggleFriendsTrigger = Resources.Load<InputAction_Trigger>("ToggleFriends"); |
| 4 | 28 | | toggleFriendsTrigger.OnTriggered -= OnCloseButtonPressed; |
| 4 | 29 | | toggleFriendsTrigger.OnTriggered += OnCloseButtonPressed; |
| | 30 | |
|
| 4 | 31 | | closeWindowTrigger = Resources.Load<InputAction_Trigger>("CloseWindow"); |
| 4 | 32 | | closeWindowTrigger.OnTriggered -= OnCloseButtonPressed; |
| 4 | 33 | | closeWindowTrigger.OnTriggered += OnCloseButtonPressed; |
| | 34 | |
|
| 4 | 35 | | toggleWorldChatTrigger = Resources.Load<InputAction_Trigger>("ToggleWorldChat"); |
| 4 | 36 | | toggleWorldChatTrigger.OnTriggered -= OnCloseButtonPressed; |
| 4 | 37 | | toggleWorldChatTrigger.OnTriggered += OnCloseButtonPressed; |
| 4 | 38 | | } |
| | 39 | |
|
| 2 | 40 | | public void CloseCard() { currentPlayerId.Set(null); } |
| | 41 | |
|
| 0 | 42 | | private void OnCloseButtonPressed(DCLAction_Trigger action = DCLAction_Trigger.CloseWindow) { CloseCard(); } |
| | 43 | |
|
| | 44 | | private void AddPlayerAsFriend() |
| | 45 | | { |
| | 46 | | // Add fake action to avoid waiting for kernel |
| 0 | 47 | | UserProfileController.i.AddUserProfileToCatalog(new UserProfileModel() |
| | 48 | | { |
| | 49 | | userId = currentPlayerId, |
| | 50 | | name = currentPlayerId |
| | 51 | | }); |
| | 52 | |
|
| 0 | 53 | | FriendsController.i.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage() |
| | 54 | | { |
| | 55 | | userId = currentPlayerId, |
| | 56 | | action = FriendshipAction.REQUESTED_TO |
| | 57 | | }); |
| 0 | 58 | | WebInterface.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage() |
| | 59 | | { |
| | 60 | | userId = currentPlayerId, action = FriendshipAction.REQUESTED_TO |
| | 61 | | }); |
| 0 | 62 | | } |
| | 63 | |
|
| | 64 | | private void CancelInvitation() |
| | 65 | | { |
| | 66 | | // Add fake action to avoid waiting for kernel |
| 0 | 67 | | FriendsController.i.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage() |
| | 68 | | { |
| | 69 | | userId = currentPlayerId, |
| | 70 | | action = FriendshipAction.CANCELLED |
| | 71 | | }); |
| | 72 | |
|
| 0 | 73 | | WebInterface.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage() |
| | 74 | | { |
| | 75 | | userId = currentPlayerId, action = FriendshipAction.CANCELLED |
| | 76 | | }); |
| 0 | 77 | | } |
| | 78 | |
|
| | 79 | | private void AcceptFriendRequest() |
| | 80 | | { |
| | 81 | | // Add fake action to avoid waiting for kernel |
| 0 | 82 | | FriendsController.i.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage() |
| | 83 | | { |
| | 84 | | userId = currentPlayerId, |
| | 85 | | action = FriendshipAction.APPROVED |
| | 86 | | }); |
| 0 | 87 | | WebInterface.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage() |
| | 88 | | { |
| | 89 | | userId = currentPlayerId, action = FriendshipAction.APPROVED |
| | 90 | | }); |
| 0 | 91 | | } |
| | 92 | |
|
| | 93 | | private void RejectFriendRequest() |
| | 94 | | { |
| | 95 | | // Add fake action to avoid waiting for kernel |
| 0 | 96 | | FriendsController.i.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage() |
| | 97 | | { |
| | 98 | | userId = currentPlayerId, |
| | 99 | | action = FriendshipAction.REJECTED |
| | 100 | | }); |
| 0 | 101 | | WebInterface.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage() |
| | 102 | | { |
| | 103 | | userId = currentPlayerId, action = FriendshipAction.REJECTED |
| | 104 | | }); |
| 0 | 105 | | } |
| | 106 | |
|
| | 107 | | internal void OnCurrentPlayerIdChanged(string current, string previous) |
| | 108 | | { |
| 6 | 109 | | if (currentUserProfile != null) |
| 1 | 110 | | currentUserProfile.OnUpdate -= SetUserProfile; |
| | 111 | |
|
| 6 | 112 | | currentUserProfile = string.IsNullOrEmpty(currentPlayerId) |
| | 113 | | ? null |
| | 114 | | : UserProfileController.userProfilesCatalog.Get(currentPlayerId); |
| | 115 | |
|
| 6 | 116 | | if (currentUserProfile == null) |
| | 117 | | { |
| 4 | 118 | | view.SetCardActive(false); |
| 4 | 119 | | } |
| | 120 | | else |
| | 121 | | { |
| 2 | 122 | | currentUserProfile.OnUpdate += SetUserProfile; |
| 2 | 123 | | SetUserProfile(currentUserProfile); |
| 2 | 124 | | view.SetCardActive(true); |
| | 125 | | } |
| 2 | 126 | | } |
| | 127 | |
|
| 4 | 128 | | private void SetUserProfile(UserProfile userProfile) { view.SetUserProfile(userProfile); } |
| | 129 | |
|
| 2 | 130 | | public void SetVisibility(bool visible) { view.SetVisibility(visible); } |
| | 131 | |
|
| | 132 | | private void BlockPlayer() |
| | 133 | | { |
| 0 | 134 | | if (ownUserProfile.blocked.Contains(currentUserProfile.userId)) |
| 0 | 135 | | return; |
| | 136 | |
|
| 0 | 137 | | ownUserProfile.blocked.Add(currentUserProfile.userId); |
| | 138 | |
|
| 0 | 139 | | view.SetIsBlocked(true); |
| | 140 | |
|
| 0 | 141 | | WebInterface.SendBlockPlayer(currentUserProfile.userId); |
| 0 | 142 | | } |
| | 143 | |
|
| | 144 | | private void UnblockPlayer() |
| | 145 | | { |
| 0 | 146 | | if (!ownUserProfile.blocked.Contains(currentUserProfile.userId)) |
| 0 | 147 | | return; |
| | 148 | |
|
| 0 | 149 | | ownUserProfile.blocked.Remove(currentUserProfile.userId); |
| | 150 | |
|
| 0 | 151 | | view.SetIsBlocked(false); |
| | 152 | |
|
| 0 | 153 | | WebInterface.SendUnblockPlayer(currentUserProfile.userId); |
| 0 | 154 | | } |
| | 155 | |
|
| 0 | 156 | | private void ReportPlayer() { WebInterface.SendReportPlayer(currentPlayerId); } |
| | 157 | |
|
| | 158 | | public void Dispose() |
| | 159 | | { |
| 4 | 160 | | if (currentUserProfile != null) |
| 1 | 161 | | currentUserProfile.OnUpdate -= SetUserProfile; |
| | 162 | |
|
| 4 | 163 | | if (currentPlayerId != null) |
| 4 | 164 | | currentPlayerId.OnChange -= OnCurrentPlayerIdChanged; |
| | 165 | |
|
| 4 | 166 | | if (closeWindowTrigger != null) |
| 4 | 167 | | closeWindowTrigger.OnTriggered -= OnCloseButtonPressed; |
| | 168 | |
|
| 4 | 169 | | if (closeWindowTrigger != null) |
| 4 | 170 | | closeWindowTrigger.OnTriggered -= OnCloseButtonPressed; |
| | 171 | |
|
| 4 | 172 | | if (toggleWorldChatTrigger != null) |
| 4 | 173 | | toggleWorldChatTrigger.OnTriggered -= OnCloseButtonPressed; |
| | 174 | |
|
| 4 | 175 | | if (view != null) |
| 4 | 176 | | UnityEngine.Object.Destroy(view.gameObject); |
| 4 | 177 | | } |
| | 178 | | } |