< Summary

Class:DCL.ServerTime.WorldTimer
Assembly:TimeController
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Rendering/ProceduralSkybox/World Timer/WorldTimer.cs
Covered lines:0
Uncovered lines:21
Coverable lines:21
Total lines:78
Line coverage:0% (0 of 21)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
WorldTimer()0%2100%
GetTimeFromServer()0%2100%
WebRequestCompleted(...)0%12300%
UpdateTimeWithServerTime(...)0%12300%
GetCurrentTime()0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Rendering/ProceduralSkybox/World Timer/WorldTimer.cs

#LineLine coverage
 1using System.Collections;
 2using System.Collections.Generic;
 3using UnityEngine;
 4using System;
 5using UnityEngine.Networking;
 6
 7namespace DCL.ServerTime
 8{
 9    public class WorldTimer
 10    {
 11        public delegate void OnTimeUpdated(DateTime serverTime);
 12        public event OnTimeUpdated OnTimeChanged;
 13
 14        public float serverHitFrequency;
 015        public string serverURL = "https://peer.decentraland.org/lambdas/health";
 16
 17        private bool initialized = false;
 018        private DateTime lastTimeFromServer = DateTime.Now.ToUniversalTime();
 019        private DateTime lastTimeFromSystem = DateTime.Now;
 20        private TimeSpan timeOffset;
 21        private bool stopTimer = false;
 22        UnityWebRequest webRequest;
 23
 024        public WorldTimer() { GetTimeFromServer(); }
 25
 26        void GetTimeFromServer()
 27        {
 028            webRequest = UnityWebRequest.Get(serverURL);
 029            UnityWebRequestAsyncOperation temp = webRequest.SendWebRequest();
 030            temp.completed += WebRequestCompleted;
 031        }
 32
 33        private void WebRequestCompleted(AsyncOperation obj)
 34        {
 035            switch (webRequest.result)
 36            {
 37                case UnityWebRequest.Result.InProgress:
 38                    break;
 39                case UnityWebRequest.Result.Success:
 040                    string responseHeaderTime = webRequest.GetResponseHeader("date");
 041                    UpdateTimeWithServerTime(responseHeaderTime);
 042                    initialized = true;
 43                    break;
 44                case UnityWebRequest.Result.ConnectionError:
 45                    break;
 46                case UnityWebRequest.Result.ProtocolError:
 47                    break;
 48                case UnityWebRequest.Result.DataProcessingError:
 49                    break;
 50                default:
 51                    break;
 52            }
 53
 054            webRequest.Dispose();
 055        }
 56
 57        void UpdateTimeWithServerTime(string datetime)
 58        {
 59            DateTime serverTime;
 060            if (DateTime.TryParse(datetime, out serverTime))
 61            {
 62                // Update last server time
 063                lastTimeFromServer = serverTime.ToUniversalTime();
 64                // Update current time from the system
 065                lastTimeFromSystem = DateTime.Now;
 66
 67                // Fire Event
 068                OnTimeChanged?.Invoke(lastTimeFromServer);
 69            }
 070        }
 71
 72        public DateTime GetCurrentTime()
 73        {
 074            TimeSpan systemTimeOffset = DateTime.Now - lastTimeFromSystem;
 075            return lastTimeFromServer.ToUniversalTime().Add(systemTimeOffset);
 76        }
 77    }
 78}