< Summary

Class:WorldChatWindowHUDController
Assembly:WorldChatWindowHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/WorldChatWindowHUD/WorldChatWindowHUDController.cs
Covered lines:71
Uncovered lines:17
Coverable lines:88
Total lines:187
Line coverage:80.6% (71 of 88)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
WorldChatWindowHUDController()0%110100%
Initialize(...)0%330100%
ChatHUDController_OnPressPrivateMessage(...)0%6200%
Dispose()0%550100%
IsOldPrivateMessage(...)0%3.043083.33%
OnAddMessage(...)0%4.074083.33%
SendChatMessage(...)0%14.277047.06%
SetVisibility(...)0%110100%
OnPressReturn()0%6.846071.43%
ForceFocus(...)0%3.143075%
MarkWorldChatMessagesAsRead(...)0%3.043083.33%
SaveLatestReadWorldChatMessagesStatus()0%110100%
LoadLatestReadWorldChatMessagesStatus()0%220100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/WorldChatWindowHUD/WorldChatWindowHUDController.cs

#LineLine coverage
 1using DCL;
 2using DCL.Helpers;
 3using DCL.Interface;
 4using UnityEngine;
 5using UnityEngine.Events;
 6using UnityEngine.EventSystems;
 7
 8public 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
 1018    internal bool resetInputFieldOnSubmit = true;
 19    private int invalidSubmitLastFrame = 0;
 420    UserProfile ownProfile => UserProfile.GetOwnUserProfile();
 1021    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    {
 1028        view = WorldChatWindowHUDView.Create();
 1029        view.controller = this;
 30
 1031        chatHudController = new ChatHUDController(DataStore.i, ProfanityFilterSharedInstances.regexFilter);
 1032        chatHudController.Initialize(view.chatHudView);
 1033        chatHudController.OnPressPrivateMessage -= ChatHUDController_OnPressPrivateMessage;
 1034        chatHudController.OnPressPrivateMessage += ChatHUDController_OnPressPrivateMessage;
 1035        LoadLatestReadWorldChatMessagesStatus();
 36
 1037        view.OnSendMessage += SendChatMessage;
 38
 1039        this.chatController = chatController;
 1040        this.mouseCatcher = mouseCatcher;
 41
 1042        if (chatController != null)
 43        {
 844            chatController.OnAddMessage -= OnAddMessage;
 845            chatController.OnAddMessage += OnAddMessage;
 46        }
 47
 1048        if (mouseCatcher != null)
 49        {
 850            mouseCatcher.OnMouseLock += view.ActivatePreview;
 51        }
 1052    }
 53
 054    void ChatHUDController_OnPressPrivateMessage(string friendUserId) { OnPressPrivateMessage?.Invoke(friendUserId); }
 55
 56    public void Dispose()
 57    {
 1058        if (chatController != null)
 859            chatController.OnAddMessage -= OnAddMessage;
 60
 1061        if (chatHudController != null)
 1062            chatHudController.OnPressPrivateMessage -= ChatHUDController_OnPressPrivateMessage;
 63
 1064        if (mouseCatcher != null)
 865            mouseCatcher.OnMouseLock -= view.ActivatePreview;
 66
 1067        view.OnSendMessage -= SendChatMessage;
 68
 1069        if (view != null)
 1070            Object.Destroy(view.gameObject);
 1071    }
 72
 73    bool IsOldPrivateMessage(ChatMessage message)
 74    {
 575        if (message.messageType != ChatMessage.Type.PRIVATE)
 176            return false;
 77
 478        double timestampAsSeconds = message.timestamp / 1000.0f;
 79
 480        if (timestampAsSeconds < chatController.initTime)
 081            return true;
 82
 483        return false;
 84    }
 85
 86    void OnAddMessage(ChatMessage message)
 87    {
 588        if (IsOldPrivateMessage(message))
 089            return;
 90
 591        view.chatHudView.controller.AddChatMessage(ChatHUDController.ChatMessageToChatEntry(message), view.isInPreview);
 92
 593        if (message.messageType == ChatMessage.Type.PRIVATE && message.recipient == ownProfile.userId)
 394            lastPrivateMessageReceivedSender = UserProfileController.userProfilesCatalog.Get(message.sender).userName;
 595    }
 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    {
 3101        bool isValidMessage = !string.IsNullOrEmpty(message.body) && !string.IsNullOrWhiteSpace(message.body);
 3102        bool isPrivateMessage = message.messageType == ChatMessage.Type.PRIVATE;
 103
 3104        if (!isValidMessage)
 105        {
 0106            view.chatHudView.ResetInputField();
 0107            EventSystem.current.SetSelectedGameObject(null);
 108
 0109            if (!isPrivateMessage && !view.isInPreview)
 110            {
 0111                view.ActivatePreview();
 0112                SceneReferences.i.mouseCatcher.LockCursor();
 0113                invalidSubmitLastFrame = Time.frameCount;
 114            }
 115
 0116            return;
 117        }
 118
 3119        if (resetInputFieldOnSubmit)
 120        {
 0121            view.chatHudView.ResetInputField();
 0122            view.chatHudView.FocusInputField();
 123        }
 124
 3125        if (isPrivateMessage)
 126        {
 2127            message.body = $"/w {message.recipient} {message.body}";
 128        }
 129
 3130        WebInterface.SendChatMessage(message);
 3131    }
 132
 32133    public void SetVisibility(bool visible) { view.gameObject.SetActive(visible); }
 134
 135    public bool OnPressReturn()
 136    {
 3137        OnOpen?.Invoke();
 138
 3139        if (EventSystem.current != null &&
 140            EventSystem.current.currentSelectedGameObject != null &&
 141            EventSystem.current.currentSelectedGameObject.GetComponent<TMPro.TMP_InputField>() != null)
 0142            return false;
 143
 3144        if ((Time.frameCount - invalidSubmitLastFrame) < 2)
 0145            return false;
 146
 3147        ForceFocus();
 3148        return true;
 149    }
 150
 151    public void ForceFocus(string setInputText = null)
 152    {
 3153        SetVisibility(true);
 3154        view.chatHudView.FocusInputField();
 3155        view.DeactivatePreview();
 3156        SceneReferences.i?.mouseCatcher.UnlockCursor();
 157
 3158        if (!string.IsNullOrEmpty(setInputText))
 159        {
 0160            view.chatHudView.inputField.text = setInputText;
 0161            view.chatHudView.inputField.caretPosition = setInputText.Length;
 162        }
 3163    }
 164
 165    public void MarkWorldChatMessagesAsRead(long? timestamp = null)
 166    {
 3167        long timeMark = System.DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
 3168        if (timestamp != null && timestamp.Value > timeMark)
 0169            timeMark = timestamp.Value;
 170
 3171        CommonScriptableObjects.lastReadWorldChatMessages.Set(timeMark);
 3172        SaveLatestReadWorldChatMessagesStatus();
 3173    }
 174
 175    private void SaveLatestReadWorldChatMessagesStatus()
 176    {
 3177        PlayerPrefsUtils.SetString(PLAYER_PREFS_LAST_READ_WORLD_CHAT_MESSAGES, CommonScriptableObjects.lastReadWorldChat
 3178        PlayerPrefsUtils.Save();
 3179    }
 180
 181    private void LoadLatestReadWorldChatMessagesStatus()
 182    {
 10183        CommonScriptableObjects.lastReadWorldChatMessages.Set(0);
 10184        string storedLastReadWorldChatMessagesString = PlayerPrefsUtils.GetString(PLAYER_PREFS_LAST_READ_WORLD_CHAT_MESS
 10185        CommonScriptableObjects.lastReadWorldChatMessages.Set(System.Convert.ToInt64(string.IsNullOrEmpty(storedLastRead
 10186    }
 187}