| | 1 | | using DCL; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using DCL.Interface; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.Events; |
| | 6 | | using UnityEngine.EventSystems; |
| | 7 | |
|
| | 8 | | public class WorldChatWindowHUDController : IHUD |
| | 9 | | { |
| | 10 | | internal const string PLAYER_PREFS_LAST_READ_WORLD_CHAT_MESSAGES = "LastReadWorldChatMessages"; |
| | 11 | |
|
| | 12 | | private ChatHUDController chatHudController; |
| | 13 | | public WorldChatWindowHUDView view; |
| | 14 | |
|
| | 15 | | private IChatController chatController; |
| | 16 | | private IMouseCatcher mouseCatcher; |
| | 17 | |
|
| 10 | 18 | | internal bool resetInputFieldOnSubmit = true; |
| | 19 | | private int invalidSubmitLastFrame = 0; |
| 4 | 20 | | UserProfile ownProfile => UserProfile.GetOwnUserProfile(); |
| 10 | 21 | | public string lastPrivateMessageReceivedSender = string.Empty; |
| | 22 | |
|
| | 23 | | public event UnityAction<string> OnPressPrivateMessage; |
| | 24 | | public event System.Action OnOpen; |
| | 25 | |
|
| | 26 | | public void Initialize(IChatController chatController, IMouseCatcher mouseCatcher) |
| | 27 | | { |
| 10 | 28 | | view = WorldChatWindowHUDView.Create(); |
| 10 | 29 | | view.controller = this; |
| | 30 | |
|
| 10 | 31 | | chatHudController = new ChatHUDController(DataStore.i, ProfanityFilterSharedInstances.regexFilter); |
| 10 | 32 | | chatHudController.Initialize(view.chatHudView); |
| 10 | 33 | | chatHudController.OnPressPrivateMessage -= ChatHUDController_OnPressPrivateMessage; |
| 10 | 34 | | chatHudController.OnPressPrivateMessage += ChatHUDController_OnPressPrivateMessage; |
| 10 | 35 | | LoadLatestReadWorldChatMessagesStatus(); |
| | 36 | |
|
| 10 | 37 | | view.OnSendMessage += SendChatMessage; |
| | 38 | |
|
| 10 | 39 | | this.chatController = chatController; |
| 10 | 40 | | this.mouseCatcher = mouseCatcher; |
| | 41 | |
|
| 10 | 42 | | if (chatController != null) |
| | 43 | | { |
| 8 | 44 | | chatController.OnAddMessage -= OnAddMessage; |
| 8 | 45 | | chatController.OnAddMessage += OnAddMessage; |
| | 46 | | } |
| | 47 | |
|
| 10 | 48 | | if (mouseCatcher != null) |
| | 49 | | { |
| 8 | 50 | | mouseCatcher.OnMouseLock += view.ActivatePreview; |
| | 51 | | } |
| 10 | 52 | | } |
| | 53 | |
|
| 0 | 54 | | void ChatHUDController_OnPressPrivateMessage(string friendUserId) { OnPressPrivateMessage?.Invoke(friendUserId); } |
| | 55 | |
|
| | 56 | | public void Dispose() |
| | 57 | | { |
| 10 | 58 | | if (chatController != null) |
| 8 | 59 | | chatController.OnAddMessage -= OnAddMessage; |
| | 60 | |
|
| 10 | 61 | | if (chatHudController != null) |
| 10 | 62 | | chatHudController.OnPressPrivateMessage -= ChatHUDController_OnPressPrivateMessage; |
| | 63 | |
|
| 10 | 64 | | if (mouseCatcher != null) |
| 8 | 65 | | mouseCatcher.OnMouseLock -= view.ActivatePreview; |
| | 66 | |
|
| 10 | 67 | | view.OnSendMessage -= SendChatMessage; |
| | 68 | |
|
| 10 | 69 | | if (view != null) |
| 10 | 70 | | Object.Destroy(view.gameObject); |
| 10 | 71 | | } |
| | 72 | |
|
| | 73 | | bool IsOldPrivateMessage(ChatMessage message) |
| | 74 | | { |
| 5 | 75 | | if (message.messageType != ChatMessage.Type.PRIVATE) |
| 1 | 76 | | return false; |
| | 77 | |
|
| 4 | 78 | | double timestampAsSeconds = message.timestamp / 1000.0f; |
| | 79 | |
|
| 4 | 80 | | if (timestampAsSeconds < chatController.initTime) |
| 0 | 81 | | return true; |
| | 82 | |
|
| 4 | 83 | | return false; |
| | 84 | | } |
| | 85 | |
|
| | 86 | | void OnAddMessage(ChatMessage message) |
| | 87 | | { |
| 5 | 88 | | if (IsOldPrivateMessage(message)) |
| 0 | 89 | | return; |
| | 90 | |
|
| 5 | 91 | | view.chatHudView.controller.AddChatMessage(ChatHUDController.ChatMessageToChatEntry(message), view.isInPreview); |
| | 92 | |
|
| 5 | 93 | | if (message.messageType == ChatMessage.Type.PRIVATE && message.recipient == ownProfile.userId) |
| 3 | 94 | | lastPrivateMessageReceivedSender = UserProfileController.userProfilesCatalog.Get(message.sender).userName; |
| 5 | 95 | | } |
| | 96 | |
|
| | 97 | | //NOTE(Brian): Send chat responsibilities must be on the chatHud containing window like this one, this way we ensure |
| | 98 | | // it can be reused by the private messaging windows down the road. |
| | 99 | | public void SendChatMessage(ChatMessage message) |
| | 100 | | { |
| 3 | 101 | | bool isValidMessage = !string.IsNullOrEmpty(message.body) && !string.IsNullOrWhiteSpace(message.body); |
| 3 | 102 | | bool isPrivateMessage = message.messageType == ChatMessage.Type.PRIVATE; |
| | 103 | |
|
| 3 | 104 | | if (!isValidMessage) |
| | 105 | | { |
| 0 | 106 | | view.chatHudView.ResetInputField(); |
| 0 | 107 | | EventSystem.current.SetSelectedGameObject(null); |
| | 108 | |
|
| 0 | 109 | | if (!isPrivateMessage && !view.isInPreview) |
| | 110 | | { |
| 0 | 111 | | view.ActivatePreview(); |
| 0 | 112 | | SceneReferences.i.mouseCatcher.LockCursor(); |
| 0 | 113 | | invalidSubmitLastFrame = Time.frameCount; |
| | 114 | | } |
| | 115 | |
|
| 0 | 116 | | return; |
| | 117 | | } |
| | 118 | |
|
| 3 | 119 | | if (resetInputFieldOnSubmit) |
| | 120 | | { |
| 0 | 121 | | view.chatHudView.ResetInputField(); |
| 0 | 122 | | view.chatHudView.FocusInputField(); |
| | 123 | | } |
| | 124 | |
|
| 3 | 125 | | if (isPrivateMessage) |
| | 126 | | { |
| 2 | 127 | | message.body = $"/w {message.recipient} {message.body}"; |
| | 128 | | } |
| | 129 | |
|
| 3 | 130 | | WebInterface.SendChatMessage(message); |
| 3 | 131 | | } |
| | 132 | |
|
| 32 | 133 | | public void SetVisibility(bool visible) { view.gameObject.SetActive(visible); } |
| | 134 | |
|
| | 135 | | public bool OnPressReturn() |
| | 136 | | { |
| 3 | 137 | | OnOpen?.Invoke(); |
| | 138 | |
|
| 3 | 139 | | if (EventSystem.current != null && |
| | 140 | | EventSystem.current.currentSelectedGameObject != null && |
| | 141 | | EventSystem.current.currentSelectedGameObject.GetComponent<TMPro.TMP_InputField>() != null) |
| 0 | 142 | | return false; |
| | 143 | |
|
| 3 | 144 | | if ((Time.frameCount - invalidSubmitLastFrame) < 2) |
| 0 | 145 | | return false; |
| | 146 | |
|
| 3 | 147 | | ForceFocus(); |
| 3 | 148 | | return true; |
| | 149 | | } |
| | 150 | |
|
| | 151 | | public void ForceFocus(string setInputText = null) |
| | 152 | | { |
| 3 | 153 | | SetVisibility(true); |
| 3 | 154 | | view.chatHudView.FocusInputField(); |
| 3 | 155 | | view.DeactivatePreview(); |
| 3 | 156 | | SceneReferences.i?.mouseCatcher.UnlockCursor(); |
| | 157 | |
|
| 3 | 158 | | if (!string.IsNullOrEmpty(setInputText)) |
| | 159 | | { |
| 0 | 160 | | view.chatHudView.inputField.text = setInputText; |
| 0 | 161 | | view.chatHudView.inputField.caretPosition = setInputText.Length; |
| | 162 | | } |
| 3 | 163 | | } |
| | 164 | |
|
| | 165 | | public void MarkWorldChatMessagesAsRead(long? timestamp = null) |
| | 166 | | { |
| 3 | 167 | | long timeMark = System.DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); |
| 3 | 168 | | if (timestamp != null && timestamp.Value > timeMark) |
| 0 | 169 | | timeMark = timestamp.Value; |
| | 170 | |
|
| 3 | 171 | | CommonScriptableObjects.lastReadWorldChatMessages.Set(timeMark); |
| 3 | 172 | | SaveLatestReadWorldChatMessagesStatus(); |
| 3 | 173 | | } |
| | 174 | |
|
| | 175 | | private void SaveLatestReadWorldChatMessagesStatus() |
| | 176 | | { |
| 3 | 177 | | PlayerPrefsUtils.SetString(PLAYER_PREFS_LAST_READ_WORLD_CHAT_MESSAGES, CommonScriptableObjects.lastReadWorldChat |
| 3 | 178 | | PlayerPrefsUtils.Save(); |
| 3 | 179 | | } |
| | 180 | |
|
| | 181 | | private void LoadLatestReadWorldChatMessagesStatus() |
| | 182 | | { |
| 10 | 183 | | CommonScriptableObjects.lastReadWorldChatMessages.Set(0); |
| 10 | 184 | | string storedLastReadWorldChatMessagesString = PlayerPrefsUtils.GetString(PLAYER_PREFS_LAST_READ_WORLD_CHAT_MESS |
| 10 | 185 | | CommonScriptableObjects.lastReadWorldChatMessages.Set(System.Convert.ToInt64(string.IsNullOrEmpty(storedLastRead |
| 10 | 186 | | } |
| | 187 | | } |