| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Threading; |
| | 5 | | using UnityEngine; |
| | 6 | | using Object = UnityEngine.Object; |
| | 7 | | using Random = UnityEngine.Random; |
| | 8 | |
|
| | 9 | | namespace 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 | |
|
| 0 | 28 | | public HintRequestService(List<IHintRequestSource> hintRequestSources, ISceneController sceneController, IHintTe |
| | 29 | | { |
| 0 | 30 | | this.hintRequestSources = hintRequestSources; |
| 0 | 31 | | hintsDictionary = new Dictionary<SourceTag, List<Hint>>(); |
| 0 | 32 | | selectedHints = new Dictionary<Hint, Texture2D>(); |
| 0 | 33 | | orderedSourceTags = new[] { SourceTag.Scene, SourceTag.Event, SourceTag.Dcl }; |
| 0 | 34 | | this.textureRequestHandler = textureRequestHandler; |
| 0 | 35 | | this.sceneController = sceneController; |
| 0 | 36 | | } |
| | 37 | |
|
| | 38 | | public void Dispose() |
| | 39 | | { |
| 0 | 40 | | foreach (List<Hint> hintList in hintsDictionary.Values) { DisposeHints(hintList); } |
| | 41 | |
|
| 0 | 42 | | hintsDictionary.Clear(); |
| | 43 | |
|
| 0 | 44 | | foreach (Texture2D texture in selectedHints.Values) |
| | 45 | | { |
| 0 | 46 | | if (texture != null) { Object.Destroy(texture); } |
| | 47 | | } |
| | 48 | |
|
| 0 | 49 | | selectedHints.Clear(); |
| 0 | 50 | | } |
| | 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 | | { |
| 0 | 63 | | if (ctx.IsCancellationRequested) { return selectedHints; } |
| | 64 | |
|
| 0 | 65 | | List<Hint> hints = await GetHintsAsync(ctx); |
| | 66 | |
|
| 0 | 67 | | hints = SelectOptimalHints(hints, totalHints); |
| | 68 | |
|
| 0 | 69 | | await DownloadTextures(hints, ctx); |
| 0 | 70 | | } |
| 0 | 71 | | catch (Exception ex) { Debug.LogWarning(ex); } |
| | 72 | |
|
| 0 | 73 | | return selectedHints; |
| 0 | 74 | | } |
| | 75 | |
|
| | 76 | | private async UniTask<List<Hint>> GetHintsAsync(CancellationToken ctx) |
| | 77 | | { |
| 0 | 78 | | var hints = new List<Hint>(); |
| | 79 | |
|
| 0 | 80 | | foreach (IHintRequestSource source in hintRequestSources) |
| | 81 | | { |
| 0 | 82 | | if (!ctx.IsCancellationRequested) |
| | 83 | | { |
| | 84 | | try |
| | 85 | | { |
| 0 | 86 | | List<Hint> sourceHints = await source.GetHintsAsync(ctx); |
| | 87 | |
|
| 0 | 88 | | if (sourceHints != null) |
| 0 | 89 | | hints.AddRange(sourceHints); |
| | 90 | |
|
| 0 | 91 | | foreach (Hint hint in sourceHints) |
| | 92 | | { |
| 0 | 93 | | if (!hintsDictionary.TryGetValue(hint.SourceTag, out List<Hint> hintList)) |
| | 94 | | { |
| 0 | 95 | | hintList = new List<Hint>(); |
| 0 | 96 | | hintsDictionary[hint.SourceTag] = hintList; |
| | 97 | | } |
| | 98 | |
|
| 0 | 99 | | hintList.Add(hint); |
| | 100 | | } |
| 0 | 101 | | } |
| | 102 | | catch (Exception ex) |
| | 103 | | { |
| 0 | 104 | | Debug.LogWarning(ex); |
| 0 | 105 | | throw; |
| | 106 | | } |
| | 107 | | } |
| | 108 | | } |
| | 109 | |
|
| 0 | 110 | | return hints; |
| 0 | 111 | | } |
| | 112 | |
|
| | 113 | | private List<Hint> SelectOptimalHints(List<Hint> hints, int totalHints) |
| | 114 | | { |
| 0 | 115 | | var optimalHints = new List<Hint>(); |
| 0 | 116 | | var allAvailableHints = new List<Hint>(hints); |
| | 117 | |
|
| 0 | 118 | | foreach (SourceTag sourceTag in orderedSourceTags) |
| | 119 | | { |
| 0 | 120 | | if (hintsDictionary.TryGetValue(sourceTag, out List<Hint> sourceHints)) |
| | 121 | | { |
| 0 | 122 | | while (sourceHints.Count > 0 && optimalHints.Count < totalHints) |
| | 123 | | { |
| 0 | 124 | | Hint selectedHint = GetRandomHint(sourceHints); |
| 0 | 125 | | allAvailableHints.Remove(selectedHint); |
| 0 | 126 | | optimalHints.Add(selectedHint); |
| | 127 | | } |
| | 128 | | } |
| | 129 | | } |
| | 130 | |
|
| | 131 | | // Random hint selection to fill the remaining slots |
| 0 | 132 | | while (allAvailableHints.Count > 0 && optimalHints.Count < totalHints) |
| | 133 | | { |
| 0 | 134 | | Hint selectedHint = GetRandomHint(allAvailableHints); |
| 0 | 135 | | optimalHints.Add(selectedHint); |
| | 136 | | } |
| | 137 | |
|
| 0 | 138 | | return optimalHints; |
| | 139 | | } |
| | 140 | |
|
| | 141 | | private Hint GetRandomHint(List<Hint> hints) |
| | 142 | | { |
| 0 | 143 | | int randomIndex = Random.Range(0, hints.Count); |
| 0 | 144 | | Hint randomHint = hints[randomIndex]; |
| 0 | 145 | | hints.RemoveAt(randomIndex); |
| 0 | 146 | | return randomHint; |
| | 147 | | } |
| | 148 | |
|
| | 149 | | private async UniTask DownloadTextures(List<Hint> finalHints, CancellationToken ctx) |
| | 150 | | { |
| 0 | 151 | | foreach (Hint hint in finalHints) |
| | 152 | | { |
| 0 | 153 | | if (ctx.IsCancellationRequested) { break; } |
| | 154 | |
|
| 0 | 155 | | try { selectedHints.Add(hint, await textureRequestHandler.DownloadTexture(hint.TextureUrl, ctx)); } |
| 0 | 156 | | catch (Exception ex) { Debug.LogWarning(ex); } |
| | 157 | | } |
| 0 | 158 | | } |
| | 159 | |
|
| | 160 | | internal void AddToHintsDictionary(SourceTag keyTag, List<Hint> valueHints) |
| | 161 | | { |
| 0 | 162 | | hintsDictionary.Add(keyTag, valueHints); |
| 0 | 163 | | } |
| | 164 | |
|
| | 165 | | internal void AddToSelectedHints(Hint keyHint, Texture2D valueTexture) |
| | 166 | | { |
| 0 | 167 | | selectedHints.Add(keyHint, valueTexture); |
| 0 | 168 | | } |
| | 169 | |
|
| | 170 | | private void DisposeHints(List<Hint> hints) |
| | 171 | | { |
| 0 | 172 | | foreach (KeyValuePair<Hint, Texture2D> hintKvp in selectedHints) |
| | 173 | | { |
| 0 | 174 | | if (hintKvp.Value != null) { Object.Destroy(hintKvp.Value); } |
| | 175 | | } |
| 0 | 176 | | } |
| | 177 | | } |
| | 178 | | } |