< Summary

Class:DCL.Huds.QuestsPanel.QuestsPanelHUDView
Assembly:QuestsPanelHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/QuestsPanelHUD/QuestsPanelHUDView.cs
Covered lines:90
Uncovered lines:14
Coverable lines:104
Total lines:225
Line coverage:86.5% (90 of 104)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
QuestsPanelHUDView()0%110100%
QuestsPanelHUDView()0%110100%
Create()0%110100%
Awake()0%110100%
RequestAddOrUpdateQuest(...)0%220100%
AddOrUpdateQuest(...)0%5.145082.35%
RemoveQuest(...)0%3.013091.67%
ClearQuests()0%2.062075%
ShowQuestPopup(...)0%220100%
Update()0%6.846071.43%
OrderQuests()0%12120100%
SetVisibility(...)0%220100%
Dispose()0%220100%
OnDestroy()0%110100%
CloseQuestsPanel()0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/QuestsPanelHUD/QuestsPanelHUDView.cs

#LineLine coverage
 1using System;
 2using DCL.Helpers;
 3using System.Collections.Generic;
 4using System.Linq;
 5using UnityEngine;
 6using UnityEngine.UI;
 7
 8namespace DCL.Huds.QuestsPanel
 9{
 10    public interface IQuestsPanelHUDView
 11    {
 12        void RequestAddOrUpdateQuest(string questId);
 13        void RemoveQuest(string questId);
 14        void ClearQuests();
 15        void SetVisibility(bool active);
 16        bool isVisible { get; }
 17        void Dispose();
 18    }
 19
 20    public class QuestsPanelHUDView : MonoBehaviour, IQuestsPanelHUDView
 21    {
 122        internal static int ENTRIES_PER_FRAME { get; set; } = 5;
 23        private const string VIEW_PATH = "QuestsPanelHUD";
 24
 25        [SerializeField] internal RectTransform availableQuestsContainer;
 26        [SerializeField] internal RectTransform completedQuestsContainer;
 27        [SerializeField] private GameObject questsContainerSeparators;
 28        [SerializeField] private GameObject questPrefab;
 29        [SerializeField] private GameObject noQuestsTitle;
 30        [SerializeField] internal QuestsPanelPopup questPopup;
 31        [SerializeField] private Button closeButton;
 32        [SerializeField] private Button backgroundButton;
 33        [SerializeField] private DynamicScrollSensitivity dynamicScrollSensitivity;
 34
 035        private static BaseDictionary<string, QuestModel> quests => DataStore.i.Quests.quests;
 36
 2137        private string currentQuestInPopup = "";
 2138        internal readonly Dictionary<string, QuestsPanelEntry> questEntries =  new Dictionary<string, QuestsPanelEntry>(
 39        private bool orderQuestsRequested = false;
 40        private bool layoutRebuildRequested = false;
 2141        internal readonly List<string> questsToBeAdded = new List<string>();
 42        private bool isDestroyed = false;
 43
 44        internal static QuestsPanelHUDView Create()
 45        {
 146            var view = Instantiate(Resources.Load<GameObject>(VIEW_PATH)).GetComponent<QuestsPanelHUDView>();
 47#if UNITY_EDITOR
 148            view.gameObject.name = "_QuestsPanelHUDView";
 49#endif
 150            return view;
 51        }
 52
 53        public void Awake()
 54        {
 1955            questPopup.gameObject.SetActive(false);
 1956            noQuestsTitle.SetActive(false);
 1957            closeButton.onClick.AddListener(CloseQuestsPanel);
 1958            backgroundButton.onClick.AddListener(CloseQuestsPanel);
 1959        }
 60
 61        public void RequestAddOrUpdateQuest(string questId)
 62        {
 463            if (questsToBeAdded.Contains(questId))
 64            {
 165                AddOrUpdateQuest(questId);
 166                return;
 67            }
 68
 369            questsToBeAdded.Add(questId);
 370        }
 71
 72        internal void AddOrUpdateQuest(string questId)
 73        {
 1874            if (!quests.TryGetValue(questId, out QuestModel quest))
 75            {
 176                Debug.LogError($"Couldn't find quest with ID {questId} in DataStore");
 177                return;
 78            }
 79
 80            //Quest has no available tasks, we remove it.
 1781            if (!quest.hasAvailableTasks)
 82            {
 083                RemoveQuest(questId);
 084                return;
 85            }
 86
 1787            if (!questEntries.TryGetValue(questId, out QuestsPanelEntry questEntry))
 88            {
 1689                questEntry = Instantiate(questPrefab).GetComponent<QuestsPanelEntry>();
 1690                questEntry.OnReadMoreClicked += ShowQuestPopup;
 1691                questEntries.Add(questId, questEntry);
 92            }
 93
 1794            questEntry.transform.localScale = Vector3.one;
 1795            questEntry.Populate(quest);
 1796            if (currentQuestInPopup == questId)
 097                questPopup.Populate(quest);
 98
 1799            orderQuestsRequested = true;
 17100            layoutRebuildRequested = true;
 17101        }
 102
 103        public void RemoveQuest(string questId)
 104        {
 2105            questsToBeAdded.Remove(questId);
 106
 2107            if (!questEntries.TryGetValue(questId, out QuestsPanelEntry questEntry))
 1108                return;
 1109            questEntries.Remove(questId);
 1110            questEntry.Unparent();
 1111            questEntry.SelfDestroy();
 112
 1113            if (currentQuestInPopup == questId)
 0114                questPopup.Close();
 115
 1116            questsContainerSeparators.SetActive(completedQuestsContainer.childCount > 0);
 1117            noQuestsTitle.SetActive(availableQuestsContainer.childCount == 0 );
 1118            layoutRebuildRequested = true;
 1119        }
 120
 121        public void ClearQuests()
 122        {
 1123            questPopup.Close();
 2124            foreach (QuestsPanelEntry questEntry in questEntries.Values)
 125            {
 0126                questEntry.transform.SetParent(null);
 0127                Destroy(questEntry.gameObject);
 128            }
 129
 1130            questEntries.Clear();
 1131            questsToBeAdded.Clear();
 1132            questsContainerSeparators.SetActive(completedQuestsContainer.childCount > 0);
 1133            noQuestsTitle.SetActive(availableQuestsContainer.childCount == 0 );
 1134            layoutRebuildRequested = true;
 1135        }
 136
 137        internal void ShowQuestPopup(string questId)
 138        {
 8139            if (!quests.TryGetValue(questId, out QuestModel quest))
 140            {
 1141                Debug.Log($"Couldnt find quest with id {questId}");
 1142                return;
 143            }
 144
 7145            questPopup.SetPositionToReadMoreButton(questEntries[questId].readMorePosition);
 146
 7147            currentQuestInPopup = questId;
 7148            questPopup.Populate(quest);
 7149            questPopup.Show();
 7150        }
 151
 152        internal void Update()
 153        {
 4154            if (layoutRebuildRequested)
 155            {
 2156                layoutRebuildRequested = false;
 2157                Utils.ForceRebuildLayoutImmediate(GetComponent<RectTransform>());
 2158                dynamicScrollSensitivity?.RecalculateSensitivity();
 159            }
 160
 4161            if (orderQuestsRequested)
 162            {
 1163                orderQuestsRequested = false;
 1164                OrderQuests();
 165            }
 166
 8167            for (int i = 0; i < ENTRIES_PER_FRAME && questsToBeAdded.Count > 0; i++)
 168            {
 0169                string questId = questsToBeAdded.First();
 0170                questsToBeAdded.RemoveAt(0);
 0171                AddOrUpdateQuest(questId);
 172            }
 4173        }
 174
 175        private void OrderQuests()
 176        {
 9177            var questModels = questEntries.Keys.Select(x => quests.Get(x));
 178
 14179            string[] availableIdsSorted = questModels.Where(x => !x.isCompleted).OrderBy(x => x.assignmentTime).ThenBy(x
 8180            for (int i = 0; i < availableIdsSorted.Length; i++)
 181            {
 3182                questEntries[availableIdsSorted[i]].transform.SetParent(availableQuestsContainer);
 3183                questEntries[availableIdsSorted[i]].transform.localScale = Vector3.one;
 3184                questEntries[availableIdsSorted[i]].transform.SetSiblingIndex(i);
 185            }
 186
 8187            string[] completedQuestsSorted = questModels.Where(x => x.isCompleted).OrderBy(x => x.completionTime).ThenBy
 4188            for (int i = 0; i < completedQuestsSorted.Length; i++)
 189            {
 1190                questEntries[completedQuestsSorted[i]].transform.SetParent(completedQuestsContainer);
 1191                questEntries[completedQuestsSorted[i]].transform.localScale = Vector3.one;
 1192                questEntries[completedQuestsSorted[i]].transform.SetSiblingIndex(i);
 193            }
 194
 1195            questsContainerSeparators.SetActive(completedQuestsContainer.childCount > 0);
 1196            noQuestsTitle.SetActive(availableQuestsContainer.childCount == 0 );
 1197        }
 198
 199        public void SetVisibility(bool active)
 200        {
 2201            gameObject.SetActive(active);
 202
 2203            if (active)
 1204                AudioScriptableObjects.dialogOpen.Play();
 205            else
 1206                AudioScriptableObjects.dialogClose.Play();
 1207        }
 208
 0209        public bool isVisible => gameObject.activeSelf;
 210
 211        public void Dispose()
 212        {
 1213            if (!isDestroyed)
 1214                Destroy(gameObject);
 1215        }
 216
 38217        private void OnDestroy() { isDestroyed = true; }
 218
 219        private void CloseQuestsPanel()
 220        {
 0221            QuestsUIAnalytics.SendQuestLogVisibiltyChanged(false, "quest_log_close_button");
 0222            DataStore.i.HUDs.questsPanelVisible.Set(false);
 0223        }
 224    }
 225}