< Summary

Class:DCL.Controllers.LoadingScreenV2.HintRequestService
Assembly:DCL.LoadingScreenV2
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/LoadingScreenV2/HintServiceScripts/HintRequestService.cs
Covered lines:0
Uncovered lines:66
Coverable lines:66
Total lines:178
Line coverage:0% (0 of 66)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:10
Method coverage:0% (0 of 10)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
HintRequestService(...)0%2100%
Dispose()0%20400%
RequestHintsFromSources()0%42600%
GetHintsAsync()0%1561200%
SelectOptimalHints(...)0%56700%
GetRandomHint(...)0%2100%
DownloadTextures()0%72800%
AddToHintsDictionary(...)0%2100%
AddToSelectedHints(...)0%2100%
DisposeHints(...)0%12300%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/LoadingScreenV2/HintServiceScripts/HintRequestService.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using System;
 3using System.Collections.Generic;
 4using System.Threading;
 5using UnityEngine;
 6using Object = UnityEngine.Object;
 7using Random = UnityEngine.Random;
 8
 9namespace DCL.Controllers.LoadingScreenV2
 10{
 11    /// <summary>
 12    ///     The HintRequestService class is a central manager for retrieving loading screen hints from different sources
 13    ///     and downloading corresponding textures. The class prioritizes hints based on source tag order.
 14    ///     If every IHintRequestSource fails, the service should return an empty list of Hints.
 15    ///     If the texture download fails for any hint, we show the hint with no texture.
 16    /// </summary>
 17    public class HintRequestService : IDisposable
 18    {
 19        private readonly List<IHintRequestSource> hintRequestSources;
 20        private readonly Dictionary<SourceTag, List<Hint>> hintsDictionary;
 21        private readonly SourceTag[] orderedSourceTags;
 22
 23        private readonly ISceneController sceneController;
 24        private readonly IHintTextureRequestHandler textureRequestHandler;
 25
 26        private readonly Dictionary<Hint, Texture2D> selectedHints;
 27
 028        public HintRequestService(List<IHintRequestSource> hintRequestSources, ISceneController sceneController, IHintTe
 29        {
 030            this.hintRequestSources = hintRequestSources;
 031            hintsDictionary = new Dictionary<SourceTag, List<Hint>>();
 032            selectedHints = new Dictionary<Hint, Texture2D>();
 033            orderedSourceTags = new[] { SourceTag.Scene, SourceTag.Event, SourceTag.Dcl };
 034            this.textureRequestHandler = textureRequestHandler;
 035            this.sceneController = sceneController;
 036        }
 37
 38        public void Dispose()
 39        {
 040            foreach (List<Hint> hintList in hintsDictionary.Values) { DisposeHints(hintList); }
 41
 042            hintsDictionary.Clear();
 43
 044            foreach (Texture2D texture in selectedHints.Values)
 45            {
 046                if (texture != null) { Object.Destroy(texture); }
 47            }
 48
 049            selectedHints.Clear();
 050        }
 51
 52        /// <summary>
 53        /// This method will request hints from the hint sources, and return the hints as a dictionary of textures.
 54        /// The totalHints parameter is the number of hints to return.
 55        /// </summary>
 56        /// <param name="ctx"></param>
 57        /// <param name="totalHints"></param>
 58        /// <returns>hints</returns>
 59        public async UniTask<Dictionary<Hint, Texture2D>> RequestHintsFromSources(CancellationToken ctx, int totalHints)
 60        {
 61            try
 62            {
 063                if (ctx.IsCancellationRequested) { return selectedHints; }
 64
 065                List<Hint> hints = await GetHintsAsync(ctx);
 66
 067                hints = SelectOptimalHints(hints, totalHints);
 68
 069                await DownloadTextures(hints, ctx);
 070            }
 071            catch (Exception ex) { Debug.LogWarning(ex); }
 72
 073            return selectedHints;
 074        }
 75
 76        private async UniTask<List<Hint>> GetHintsAsync(CancellationToken ctx)
 77        {
 078            var hints = new List<Hint>();
 79
 080            foreach (IHintRequestSource source in hintRequestSources)
 81            {
 082                if (!ctx.IsCancellationRequested)
 83                {
 84                    try
 85                    {
 086                        List<Hint> sourceHints = await source.GetHintsAsync(ctx);
 87
 088                        if (sourceHints != null)
 089                            hints.AddRange(sourceHints);
 90
 091                        foreach (Hint hint in sourceHints)
 92                        {
 093                            if (!hintsDictionary.TryGetValue(hint.SourceTag, out List<Hint> hintList))
 94                            {
 095                                hintList = new List<Hint>();
 096                                hintsDictionary[hint.SourceTag] = hintList;
 97                            }
 98
 099                            hintList.Add(hint);
 100                        }
 0101                    }
 102                    catch (Exception ex)
 103                    {
 0104                        Debug.LogWarning(ex);
 0105                        throw;
 106                    }
 107                }
 108            }
 109
 0110            return hints;
 0111        }
 112
 113        private List<Hint> SelectOptimalHints(List<Hint> hints, int totalHints)
 114        {
 0115            var optimalHints = new List<Hint>();
 0116            var allAvailableHints = new List<Hint>(hints);
 117
 0118            foreach (SourceTag sourceTag in orderedSourceTags)
 119            {
 0120                if (hintsDictionary.TryGetValue(sourceTag, out List<Hint> sourceHints))
 121                {
 0122                    while (sourceHints.Count > 0 && optimalHints.Count < totalHints)
 123                    {
 0124                        Hint selectedHint = GetRandomHint(sourceHints);
 0125                        allAvailableHints.Remove(selectedHint);
 0126                        optimalHints.Add(selectedHint);
 127                    }
 128                }
 129            }
 130
 131            // Random hint selection to fill the remaining slots
 0132            while (allAvailableHints.Count > 0 && optimalHints.Count < totalHints)
 133            {
 0134                Hint selectedHint = GetRandomHint(allAvailableHints);
 0135                optimalHints.Add(selectedHint);
 136            }
 137
 0138            return optimalHints;
 139        }
 140
 141        private Hint GetRandomHint(List<Hint> hints)
 142        {
 0143            int randomIndex = Random.Range(0, hints.Count);
 0144            Hint randomHint = hints[randomIndex];
 0145            hints.RemoveAt(randomIndex);
 0146            return randomHint;
 147        }
 148
 149        private async UniTask DownloadTextures(List<Hint> finalHints, CancellationToken ctx)
 150        {
 0151            foreach (Hint hint in finalHints)
 152            {
 0153                if (ctx.IsCancellationRequested) { break; }
 154
 0155                try { selectedHints.Add(hint, await textureRequestHandler.DownloadTexture(hint.TextureUrl, ctx)); }
 0156                catch (Exception ex) { Debug.LogWarning(ex); }
 157            }
 0158        }
 159
 160        internal void AddToHintsDictionary(SourceTag keyTag, List<Hint> valueHints)
 161        {
 0162            hintsDictionary.Add(keyTag, valueHints);
 0163        }
 164
 165        internal void AddToSelectedHints(Hint keyHint, Texture2D valueTexture)
 166        {
 0167            selectedHints.Add(keyHint, valueTexture);
 0168        }
 169
 170        private void DisposeHints(List<Hint> hints)
 171        {
 0172            foreach (KeyValuePair<Hint, Texture2D> hintKvp in selectedHints)
 173            {
 0174                if (hintKvp.Value != null) { Object.Destroy(hintKvp.Value); }
 175            }
 0176        }
 177    }
 178}