Exemplo n.º 1
0
        private static TimeRecorderInfo InitializeTimeRecorder()
        {
            Debug.Log("Inicializando time recorder");

            DateTime dateTime = DateTime.Now;

            timeRecorderInfo = new TimeRecorderInfo()
            {
                years             = new List <YearInfo>(),
                totalRecordedTime = 0
            };

            // Setup current year
            var year = new YearInfo()
            {
                year   = dateTime.Year,
                months = new List <MonthInfo>()
            };

            // Setup current month
            var month = new MonthInfo()
            {
                month = dateTime.Month,
                dates = new List <DateInfo>()
            };

            // Setup current day
            var day = new DateInfo()
            {
                date          = dateTime.Day,
                timeInSeconds = 0,
            };

            // Setup time recorder object
            month.dates.Add(day);
            year.months.Add(month);
            timeRecorderInfo.years.Add(year);

            // Save the registry
            string timeRecorderJson = JsonUtility.ToJson(timeRecorderInfo);

            PlayerPrefs.SetString(TimeRecorderExtras.TIME_RECORDER_REGISTRY, timeRecorderJson);
            PlayerPrefs.Save();

            Debug.Log("Time recorder initialized");

            return(timeRecorderInfo);
        }
Exemplo n.º 2
0
        private static bool SaveTimeRecorded(bool countdownCompleted)
        {
            string timeRecorderJson = PlayerPrefs.GetString(TimeRecorderExtras.TIME_RECORDER_REGISTRY, "");

            // parse stored json data
            try{
                // Is a new time recorder?
                if (string.IsNullOrEmpty(timeRecorderJson))
                {
                    timeRecorderInfo = InitializeTimeRecorder();
                }
                else
                {
                    timeRecorderInfo = JsonUtility.FromJson <TimeRecorderInfo>(timeRecorderJson);
                }
            } catch (Exception e) {
                Debug.LogError("Any error ocurred trying to parse TimeRecorder JSON, a json file backup will be generated & data will be refreshed: " + e);

                PlayerPrefs.DeleteKey(TimeRecorderExtras.TIME_RECORDER_REGISTRY);
                PlayerPrefs.Save();

                // Save local backgup
                string fileName   = string.Format(TimeRecorderExtras.CORRUPTED_JSON_BACKUP, DateTime.Now.Ticks);
                string path       = Path.Combine(Application.dataPath, fileName);
                Task   saveBackup = WriteTextAsync(path, timeRecorderJson);

                // Save Data into the next iteration
                return(false);
            }

            #region Validate timeRecorderInfo object
            var currentTime = DateTime.Now;

            // Save time
            if (timeRecorderInfo.years == null)
            {
                timeRecorderInfo.years = new List <YearInfo>();
            }

            var yearInfoIdx = timeRecorderInfo.years.FindIndex(y => y.year == currentTime.Year);
            var yearInfo    = new YearInfo();

            if (yearInfoIdx == -1)
            {
                // Create & setup year
                yearInfo.year   = currentTime.Year;
                yearInfo.months = new List <MonthInfo>();
                timeRecorderInfo.years.Add(yearInfo);
            }
            else
            {
                // Find year
                yearInfo = timeRecorderInfo.years[yearInfoIdx];
            }

            // Initialize year months
            if (yearInfo.months == null)
            {
                yearInfo.months = new List <MonthInfo>();
            }

            var monthInfoIdx = yearInfo.months.FindIndex(m => m.month == currentTime.Month);
            var monthInfo    = new MonthInfo();

            if (monthInfoIdx == -1)
            {
                // Create & setup month
                monthInfo.month = currentTime.Month;
                monthInfo.dates = new List <DateInfo>();
                yearInfo.months.Add(monthInfo);
            }
            else
            {
                // Find moth
                monthInfo = yearInfo.months[monthInfoIdx];
            }

            // Initialize month dates
            if (monthInfo.dates == null)
            {
                monthInfo.dates = new List <DateInfo>();
            }

            var dateInfoIdx = monthInfo.dates.FindIndex(m => m.date == currentTime.Day);
            var dateInfo    = new DateInfo();

            if (dateInfoIdx == -1)
            {
                // Create & setup day
                dateInfo.date          = currentTime.Day;
                dateInfo.timeInSeconds = 0;
                dateInfoIdx            = monthInfo.dates.Count;
                monthInfo.dates.Add(dateInfo);
            }
            else
            {
                // Find day
                dateInfo = monthInfo.dates[dateInfoIdx];
            }
            #endregion

            int secondsToAdd = saveOnMinutes * 60;

            if (countdownCompleted)
            {
                // Add time reference for current date
                dateInfo.timeInSeconds      += secondsToAdd;
                monthInfo.dates[dateInfoIdx] = dateInfo;
            }
            else
            {
                if (EditorApplication.timeSinceStartup < saveOnMinutes * 60)
                {
                    // Editor recently oppened so i should save EditorApplication.timeSinceStartup
                    secondsToAdd = (int)EditorApplication.timeSinceStartup;
                }
                else
                {
                    // Save time elapsed from the last save time
                    DateTime lastSave = nextSaveTime.AddMinutes(-saveOnMinutes);

                    var diferencia = DateTime.Now - lastSave;
                    secondsToAdd = diferencia.Seconds;
                }
                Debug.Log("No se completó pero aún así guardo");
            }


            timeRecorderInfo.totalRecordedTime += secondsToAdd;

            // Save the registry
            timeRecorderJson = JsonUtility.ToJson(timeRecorderInfo);

            PlayerPrefs.SetString(TimeRecorderExtras.TIME_RECORDER_REGISTRY, timeRecorderJson);
            PlayerPrefs.Save();

            if (TimeRecorderWindow.Instance)
            {
                TimeRecorderWindow.Instance.RepaintWindow();
            }

            Debug.Log("Your develop time has been tracked");
            return(true);
        }
Exemplo n.º 3
0
        public (DateInfo dayInfo, MonthInfo monthInfo, YearInfo yearInfo) VerifyByDatetime(DateTime dateTime)
        {
            // Save time
            if (years == null)
            {
                years = new List <YearInfo>();
            }

            var yearInfoIdx = years.FindIndex(y => y.year == dateTime.Year);
            var yearInfo    = new YearInfo();

            if (yearInfoIdx == -1)
            {
                // Create & setup year
                yearInfo.year   = dateTime.Year;
                yearInfo.months = new List <MonthInfo>();
                years.Add(yearInfo);
            }
            else
            {
                // Find year
                yearInfo = years[yearInfoIdx];
            }

            // Initialize year months
            if (yearInfo.months == null)
            {
                yearInfo.months = new List <MonthInfo>();
            }

            var monthInfoIdx = yearInfo.months.FindIndex(m => m.month == dateTime.Month);
            var monthInfo    = new MonthInfo();

            if (monthInfoIdx == -1)
            {
                // Create & setup month
                monthInfo.month = dateTime.Month;
                monthInfo.dates = new List <DateInfo>();
                yearInfo.months.Add(monthInfo);
            }
            else
            {
                // Find moth
                monthInfo = yearInfo.months[monthInfoIdx];
            }

            // Initialize month dates
            if (monthInfo.dates == null)
            {
                monthInfo.dates = new List <DateInfo>();
            }

            var dateInfoIdx = monthInfo.dates.FindIndex(m => m.date == dateTime.Day);
            var dateInfo    = new DateInfo();

            if (dateInfoIdx == -1)
            {
                // Create & setup day
                dateInfo.date          = dateTime.Day;
                dateInfo.timeInSeconds = 0;
                monthInfo.dates.Add(dateInfo);
            }
            else
            {
                // Find day
                dateInfo = monthInfo.dates[dateInfoIdx];
            }

            return(dateInfo, monthInfo, yearInfo);
        }