< Summary

Class:TeleportPromptHUDController
Assembly:TeleportPromptHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/TeleportPromptHUD/TeleportPromptHUDController.cs
Covered lines:39
Uncovered lines:67
Coverable lines:106
Total lines:263
Line coverage:36.7% (39 of 106)
Covered branches:0
Total branches:0
Covered methods:7
Total methods:17
Method coverage:41.1% (7 of 17)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
TeleportPromptHUDController(...)0%110100%
Dispose()0%220100%
SetVisibility(...)0%2.652045.45%
RequestJSONTeleport(...)0%3.653058.33%
ClosePanel()0%2100%
ReceivedRequestTeleportData(...)0%12300%
SetCoordinates(...)0%20400%
SetCoordinatesAsync()0%1321100%
CloseView()0%2100%
ChangeVisibility(...)0%2100%
RequestCoordinatesTeleport(...)0%2100%
SetSceneEvent(...)0%56700%
OnTeleportPressed()0%42600%
GetCoordinates()0%42600%
TeleportData()0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/TeleportPromptHUD/TeleportPromptHUDController.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using DCL;
 3using UnityEngine;
 4using System;
 5using DCL.Helpers;
 6using DCL.Map;
 7using DCL.Tasks;
 8using RPC.Context;
 9using System.Threading;
 10
 11public class TeleportPromptHUDController : IHUD
 12{
 13    private const string TELEPORT_COMMAND_MAGIC = "magic";
 14    private const string TELEPORT_COMMAND_CROWD = "crowd";
 15
 16    private const string EVENT_STRING_LIVE = "LIVE";
 17    private const string EVENT_STRING_TODAY = "Today @ {0:HH:mm}";
 18    private const int INITIAL_ANIMATION_DELAY = 500;
 19
 2820    internal TeleportPromptHUDView view { get; private set; }
 21
 22    private readonly RestrictedActionsContext restrictedActionsServiceContext;
 23    private readonly ITeleportController teleportController;
 24    private readonly DataStore dataStore;
 25    private readonly IMinimapApiBridge minimapApiBridge;
 26    private bool isVisible;
 27    private TeleportData teleportData;
 228    private CancellationTokenSource cancellationToken = new ();
 29    private EventData currentEvent;
 30
 231    public TeleportPromptHUDController(DataStore dataStore,
 32        IMinimapApiBridge minimapApiBridge,
 33        RestrictedActionsContext restrictedActionsContext,
 34        ITeleportController teleportController)
 35    {
 236        this.dataStore = dataStore;
 237        this.minimapApiBridge = minimapApiBridge;
 38
 239        view = UnityEngine.Object.Instantiate(Resources.Load<GameObject>("TeleportPromptHUD")).GetComponent<TeleportProm
 240        view.name = "_TeleportPromptHUD";
 241        view.OnCloseEvent += ClosePanel;
 242        view.OnTeleportEvent += OnTeleportPressed;
 43
 244        dataStore.HUDs.gotoPanelVisible.OnChange += ChangeVisibility;
 245        dataStore.HUDs.gotoPanelCoordinates.OnChange += SetCoordinates;
 246        dataStore.world.requestTeleportData.OnChange += ReceivedRequestTeleportData;
 47
 248        restrictedActionsServiceContext = restrictedActionsContext;
 249        this.teleportController = teleportController;
 250        restrictedActionsServiceContext.TeleportToPrompt += RequestCoordinatesTeleport;
 251    }
 52
 53    public void Dispose()
 54    {
 255        view.OnCloseEvent -= CloseView;
 256        view.OnTeleportEvent -= OnTeleportPressed;
 57
 258        if (view)
 59        {
 260            view.OnCloseEvent -= ClosePanel;
 261            view.OnTeleportEvent -= OnTeleportPressed;
 262            UnityEngine.Object.Destroy(view.gameObject);
 63        }
 64
 265        dataStore.HUDs.gotoPanelVisible.OnChange -= ChangeVisibility;
 266        dataStore.HUDs.gotoPanelCoordinates.OnChange -= SetCoordinates;
 267        restrictedActionsServiceContext.TeleportToPrompt -= RequestCoordinatesTeleport;
 268    }
 69
 70    public void SetVisibility(bool visible)
 71    {
 172        if (visible)
 73        {
 174            view.SetInAnimation();
 175            AudioScriptableObjects.fadeIn.Play(true);
 76        }
 77        else
 78        {
 079            AudioScriptableObjects.dialogClose.Play(true);
 080            currentEvent = null;
 081            teleportData = null;
 082            view.SetOutAnimation();
 083            view.Reset();
 084            AudioScriptableObjects.dialogClose.Play(true);
 85        }
 186        isVisible = visible;
 187    }
 88
 89    public void RequestJSONTeleport(string teleportDataJson)
 90    {
 191        Utils.UnlockCursor();
 92
 193        teleportData = Utils.SafeFromJson<TeleportData>(teleportDataJson);
 194        currentEvent = teleportData.sceneEvent;
 195        SetVisibility(true);
 96
 197        switch (teleportData.destination)
 98        {
 99            case TELEPORT_COMMAND_MAGIC:
 1100                view.ShowTeleportToMagic();
 1101                break;
 102            case TELEPORT_COMMAND_CROWD:
 0103                view.ShowTeleportToCrowd();
 0104                break;
 105            default:
 0106                cancellationToken = cancellationToken.SafeRestart();
 0107                SetCoordinatesAsync(CoordinateUtils.ParseCoordinatesString(teleportData.destination), null, null, cancel
 108                break;
 109        }
 0110    }
 111
 112    private void ClosePanel()
 113    {
 0114        SetVisibility(false);
 0115    }
 116
 117    private void ReceivedRequestTeleportData(string current, string previous)
 118    {
 0119        if (string.IsNullOrEmpty(current) || isVisible)
 0120            return;
 121
 0122        RequestJSONTeleport(current);
 0123    }
 124
 125    private void SetCoordinates((ParcelCoordinates coordinates, string realm, Action onAcceptedCallback) current,
 126        (ParcelCoordinates coordinates, string realm, Action onAcceptedCallback) previous)
 127    {
 0128        if (current == previous) return;
 129
 0130        view.Reset();
 0131        cancellationToken = cancellationToken.SafeRestart();
 0132        SetCoordinatesAsync(current.coordinates, current.realm, current.onAcceptedCallback, cancellationToken.Token).For
 0133    }
 134
 135    private async UniTaskVoid SetCoordinatesAsync(ParcelCoordinates coordinates, string realm,
 136        Action onAcceptedCallback, CancellationToken cancellationToken)
 137    {
 138        try
 139        {
 0140            await minimapApiBridge.GetScenesInformationAroundParcel(new Vector2Int(coordinates.x, coordinates.y), 2, can
 0141            MinimapMetadata.MinimapSceneInfo sceneInfo = MinimapMetadata.GetMetadata().GetSceneInfo(coordinates.x, coord
 142
 0143            view.ShowTeleportToCoords(coordinates.ToString(), sceneInfo?.name, sceneInfo?.owner, sceneInfo?.previewImage
 144
 0145            teleportData = new TeleportData
 146            {
 147                coordinates = coordinates,
 148                sceneData = sceneInfo,
 149                sceneEvent = currentEvent,
 150                realm = realm,
 151                onAcceptedCallback = onAcceptedCallback,
 152            };
 153
 0154            SetSceneEvent(teleportData.sceneEvent);
 155
 0156            await UniTask.Delay(INITIAL_ANIMATION_DELAY, cancellationToken: cancellationToken);
 157
 0158            view.SetLoadingCompleted();
 0159        }
 0160        catch (OperationCanceledException)
 161        {
 0162            teleportData = new TeleportData
 163            {
 164                destination = coordinates.ToString(),
 165                sceneData = null,
 166                sceneEvent = null,
 167            };
 0168        }
 0169        catch (Exception e) { Debug.LogException(e); }
 0170    }
 171
 172    private void CloseView() =>
 0173        SetVisibility(false);
 174
 175    private void ChangeVisibility(bool current, bool previous) =>
 0176        SetVisibility(current);
 177
 178    private bool RequestCoordinatesTeleport(int xCoordinate, int yCoordinate)
 179    {
 0180        Utils.UnlockCursor();
 181
 0182        ParcelCoordinates coordinates = new ParcelCoordinates(xCoordinate, yCoordinate);
 0183        teleportData = new TeleportData() { coordinates = coordinates };
 184
 0185        SetVisibility(true);
 0186        cancellationToken = cancellationToken.SafeRestart();
 0187        SetCoordinatesAsync(coordinates, null, null, cancellationToken.Token).Forget();
 0188        return true;
 189    }
 190
 191    private void SetSceneEvent(EventData eventData)
 192    {
 0193        if (eventData == null) return;
 194
 0195        DateTime dateNow = DateTime.Now;
 196        DateTime eventStart;
 197        DateTime eventEnd;
 198
 0199        if (DateTime.TryParse(eventData.start_at, out eventStart) && DateTime.TryParse(eventData.finish_at, out eventEnd
 200        {
 0201            bool startsToday = eventStart.Date == dateNow.Date;
 0202            bool isNow = eventStart <= dateNow && dateNow <= eventEnd;
 203
 0204            if (isNow || startsToday)
 205            {
 0206                string eventStatus = EVENT_STRING_LIVE;
 207
 0208                if (!isNow && startsToday)
 0209                    eventStatus = string.Format(EVENT_STRING_TODAY, eventStart);
 210
 0211                view.SetEventInfo(eventData.name, eventStatus, eventData.total_attendees);
 212            }
 213        }
 0214    }
 215
 216    private void OnTeleportPressed()
 217    {
 0218        switch (teleportData.destination)
 219        {
 220            case TELEPORT_COMMAND_CROWD:
 0221                teleportController.GoToCrowd();
 0222                break;
 223            case TELEPORT_COMMAND_MAGIC:
 0224                teleportController.GoToMagic();
 0225                break;
 226            default:
 0227                if (!string.IsNullOrEmpty(teleportData.realm))
 0228                    teleportController.JumpIn(teleportData.GetCoordinates().x, teleportData.GetCoordinates().y, teleport
 229                else
 0230                    teleportController.Teleport(teleportData.GetCoordinates().x, teleportData.GetCoordinates().y);
 231                break;
 232        }
 233
 0234        teleportData?.onAcceptedCallback?.Invoke();
 235
 0236        CloseView();
 0237    }
 238
 239    [Serializable]
 240    internal class TeleportData
 241    {
 242        public Vector2Int? coordinates;
 243
 244        public Vector2Int GetCoordinates() =>
 0245            (coordinates ??= new Vector2Int(int.TryParse(destination.Split(',')[0], out int x) ? x : 0,
 246                int.TryParse(destination.Split(',')[1], out int y) ? y : 0));
 247
 1248        public string destination = "";
 249        public MinimapMetadata.MinimapSceneInfo sceneData;
 250        public EventData sceneEvent;
 251        public string realm;
 252        public Action onAcceptedCallback;
 253    }
 254
 255    [Serializable]
 256    internal class EventData
 257    {
 258        public string name;
 259        public int total_attendees;
 260        public string start_at;
 261        public string finish_at;
 262    }
 263}