private void CreateMappingsForLocalDateConverter()
        {
            var converter = new LocalDateConverter();

            CreateMap <LocalDate, DateTime>().ConvertUsing(converter);
            CreateMap <LocalDate?, DateTime?>().ConvertUsing(converter);
            CreateMap <DateTime, LocalDate>().ConvertUsing(converter);
            CreateMap <DateTime?, LocalDate?>().ConvertUsing(converter);
        }
        public static void Reads_value_from_ISO_string()
        {
            var rawValue = Encoding.UTF8.GetBytes("\"2021-02-03\"");
            var reader   = new Utf8JsonReader(rawValue.AsSpan());

            Assert.Equal(JsonTokenType.None, reader.TokenType);
            reader.Read();

            var result = new LocalDateConverter().Read(ref reader, typeof(LocalDate), null);

            Assert.Equal(3.February(2021), result);
        }
示例#3
0
        /// <summary>
        /// Get number achievements unlock by month for a game or not.
        /// </summary>
        /// <param name="GameID"></param>
        /// <returns></returns>
        public AchievementsGraphicsDataCount GetCountByDay(Guid?GameID = null, int limit = 11)
        {
            string[] GraphicsAchievementsLabels = new string[limit + 1];
            ChartValues <CustomerForSingle> SourceAchievementsSeries = new ChartValues <CustomerForSingle>();

            LocalDateConverter localDateConverter = new LocalDateConverter();

            // All achievements
            if (GameID == null)
            {
                for (int i = limit; i >= 0; i--)
                {
                    //GraphicsAchievementsLabels[(limit - i)] = DateTime.Now.AddDays(-i).ToString("yyyy-MM-dd");
                    GraphicsAchievementsLabels[(limit - i)] = (string)localDateConverter.Convert(DateTime.Now.AddDays(-i), null, null, null);
                    SourceAchievementsSeries.Add(new CustomerForSingle
                    {
                        //Name = DateTime.Now.AddDays(-i).ToString("yyyy-MM-dd"),
                        Name   = (string)localDateConverter.Convert(DateTime.Now.AddDays(-i), null, null, null),
                        Values = 0
                    });
                }

                try
                {
                    foreach (var item in Database.Items)
                    {
                        if (!item.Value.HaveAchivements || item.Value.IsDeleted == false)
                        {
                            continue;
                        }

                        List <Achievements> temp = item.Value.Items;
                        foreach (Achievements itemAchievements in temp)
                        {
                            if (itemAchievements.DateUnlocked != null && itemAchievements.DateUnlocked != default(DateTime))
                            {
                                //string tempDate = ((DateTime)itemAchievements.DateUnlocked).ToLocalTime().ToString("yyyy-MM-dd");
                                string tempDate = (string)localDateConverter.Convert(((DateTime)itemAchievements.DateUnlocked).ToLocalTime(), null, null, null);
                                int    index    = Array.IndexOf(GraphicsAchievementsLabels, tempDate);

                                if (index >= 0 && index < (limit + 1))
                                {
                                    SourceAchievementsSeries[index].Values += 1;
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Common.LogError(ex, "SuccessStory", $"Error in load GetCountByDay()");
                }
            }
            // Achievement for a game
            else
            {
                try
                {
                    List <Achievements> Achievements = GetOnlyCache((Guid)GameID).Items;

                    if (Achievements != null && Achievements.Count > 0)
                    {
                        Achievements.Sort((x, y) => ((DateTime)y.DateUnlocked).CompareTo((DateTime)x.DateUnlocked));
                        DateTime TempDateTime = DateTime.Now;

                        // Find last achievement date unlock
                        if (((DateTime)Achievements[0].DateUnlocked).ToLocalTime().ToString("yyyy-MM-dd") != "0001-01-01" && ((DateTime)Achievements[0].DateUnlocked).ToLocalTime().ToString("yyyy-MM-dd") != "1982-12-15")
                        {
                            TempDateTime = ((DateTime)Achievements[0].DateUnlocked).ToLocalTime();
                        }

                        for (int i = limit; i >= 0; i--)
                        {
                            //GraphicsAchievementsLabels[(limit - i)] = TempDateTime.AddDays(-i).ToString("yyyy-MM-dd");
                            GraphicsAchievementsLabels[(limit - i)] = (string)localDateConverter.Convert(TempDateTime.AddDays(-i), null, null, null);
                            SourceAchievementsSeries.Add(new CustomerForSingle
                            {
                                //Name = TempDateTime.AddDays(-i).ToString("yyyy-MM-dd"),
                                Name   = (string)localDateConverter.Convert((TempDateTime.AddDays(-i)), null, null, null),
                                Values = 0
                            });
                        }

                        for (int i = 0; i < Achievements.Count; i++)
                        {
                            //string tempDate = ((DateTime)Achievements[i].DateUnlocked).ToLocalTime().ToString("yyyy-MM-dd");
                            string tempDate = (string)localDateConverter.Convert(((DateTime)Achievements[i].DateUnlocked).ToLocalTime(), null, null, null);
                            int    index    = Array.IndexOf(GraphicsAchievementsLabels, tempDate);

                            if (index >= 0 && index < (limit + 1))
                            {
                                SourceAchievementsSeries[index].Values += 1;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Common.LogError(ex, "SuccessStory", $"Error in load GetCountByDay({GameID.ToString()})");
                }
            }

            return(new AchievementsGraphicsDataCount {
                Labels = GraphicsAchievementsLabels, Series = SourceAchievementsSeries
            });
        }
        public void GetActivityForGamesTimeGraphics(GameActivities gameActivities, int variateurTime = 0, int limit = 9)
        {
            Task.Run(() =>
            {
                try
                {
                    this.Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new ThreadStart(delegate
                    {
                        if (!gameActivities.HasData)
                        {
                            this.Visibility = Visibility.Collapsed;
                            return;
                        }
                        else
                        {
                            this.Visibility = Visibility.Visible;
                        }
                    }));


                    string[] listDate = new string[limit + 1];
                    ChartValues <CustomerForTime> series1 = new ChartValues <CustomerForTime>();
                    ChartValues <CustomerForTime> series2 = new ChartValues <CustomerForTime>();
                    ChartValues <CustomerForTime> series3 = new ChartValues <CustomerForTime>();
                    ChartValues <CustomerForTime> series4 = new ChartValues <CustomerForTime>();
                    ChartValues <CustomerForTime> series5 = new ChartValues <CustomerForTime>();

                    bool HasData2 = false;
                    bool HasData3 = false;
                    bool HasData4 = false;
                    bool HasData5 = false;

                    List <Activity> Activities = gameActivities.Items;

                    // Find last activity date
                    DateTime dateStart = new DateTime(1982, 12, 15, 0, 0, 0);
                    for (int iActivity = 0; iActivity < Activities.Count; iActivity++)
                    {
                        DateTime dateSession = Convert.ToDateTime(Activities[iActivity].DateSession).ToLocalTime();
                        if (dateSession > dateStart)
                        {
                            dateStart = dateSession;
                        }
                    }
                    dateStart = dateStart.AddDays(variateurTime);

                    // Periode data showned
                    for (int i = limit; i >= 0; i--)
                    {
                        listDate[(limit - i)]           = dateStart.AddDays(-i).ToString("yyyy-MM-dd");
                        CustomerForTime customerForTime = new CustomerForTime
                        {
                            Name   = dateStart.AddDays(-i).ToString("yyyy-MM-dd"),
                            Values = 0
                        };
                        series1.Add(customerForTime);
                        series2.Add(customerForTime);
                        series3.Add(customerForTime);
                        series4.Add(customerForTime);
                        series5.Add(customerForTime);
                    }

                    LocalDateConverter localDateConverter = new LocalDateConverter();

                    // Search data in periode
                    for (int iActivity = 0; iActivity < Activities.Count; iActivity++)
                    {
                        long elapsedSeconds = Activities[iActivity].ElapsedSeconds;
                        string dateSession  = Convert.ToDateTime(Activities[iActivity].DateSession).ToLocalTime().ToString("yyyy-MM-dd");

                        //for (int iDay = 0; iDay < 10; iDay++)
                        for (int iDay = limit; iDay >= 0; iDay--)
                        {
                            if (listDate[iDay] == dateSession)
                            {
                                string tempName = series1[iDay].Name;
                                try
                                {
                                    tempName = (string)localDateConverter.Convert(DateTime.ParseExact(series1[iDay].Name, "yyyy-MM-dd", null), null, null, null);
                                }
                                catch
                                {
                                }

                                if (PluginDatabase.PluginSettings.CumulPlaytimeSession)
                                {
                                    series1[iDay] = new CustomerForTime
                                    {
                                        Name   = tempName,
                                        Values = series1[iDay].Values + elapsedSeconds,
                                    };
                                    continue;
                                }
                                else
                                {
                                    if (series1[iDay].Values == 0)
                                    {
                                        series1[iDay] = new CustomerForTime
                                        {
                                            Name   = tempName,
                                            Values = elapsedSeconds,
                                        };
                                        continue;
                                    }

                                    if (series2[iDay].Values == 0)
                                    {
                                        HasData2      = true;
                                        series2[iDay] = new CustomerForTime
                                        {
                                            Name   = tempName,
                                            Values = elapsedSeconds,
                                        };
                                        continue;
                                    }

                                    if (series3[iDay].Values == 0)
                                    {
                                        HasData3      = true;
                                        series3[iDay] = new CustomerForTime
                                        {
                                            Name   = tempName,
                                            Values = elapsedSeconds,
                                        };
                                        continue;
                                    }

                                    if (series4[iDay].Values == 0)
                                    {
                                        HasData4      = true;
                                        series4[iDay] = new CustomerForTime
                                        {
                                            Name   = tempName,
                                            Values = elapsedSeconds,
                                        };
                                        continue;
                                    }

                                    if (series5[iDay].Values == 0)
                                    {
                                        HasData5      = true;
                                        series5[iDay] = new CustomerForTime
                                        {
                                            Name   = tempName,
                                            Values = elapsedSeconds,
                                        };
                                        continue;
                                    }
                                }
                            }
                        }
                    }


                    this.Dispatcher.BeginInvoke(DispatcherPriority.Background, new ThreadStart(delegate
                    {
                        // Set data in graphic.
                        SeriesCollection activityForGameSeries = new SeriesCollection();
                        activityForGameSeries.Add(new ColumnSeries {
                            Title = "1", Values = series1
                        });
                        if (HasData2)
                        {
                            activityForGameSeries.Add(new ColumnSeries {
                                Title = "2", Values = series2
                            });
                        }
                        if (HasData3)
                        {
                            activityForGameSeries.Add(new ColumnSeries {
                                Title = "3", Values = series3
                            });
                        }
                        if (HasData4)
                        {
                            activityForGameSeries.Add(new ColumnSeries {
                                Title = "4", Values = series4
                            });
                        }
                        if (HasData5)
                        {
                            activityForGameSeries.Add(new ColumnSeries {
                                Title = "5", Values = series5
                            });
                        }

                        for (int iDay = 0; iDay < listDate.Length; iDay++)
                        {
                            listDate[iDay] = Convert.ToDateTime(listDate[iDay]).ToString(Constants.DateUiFormat);
                        }
                        string[] activityForGameLabels = listDate;

                        //let create a mapper so LiveCharts know how to plot our CustomerViewModel class
                        var customerVmMapper = Mappers.Xy <CustomerForTime>()
                                               .X((value, index) => index)
                                               .Y(value => value.Values);


                        //lets save the mapper globally
                        Charting.For <CustomerForTime>(customerVmMapper);

                        LongToTimePlayedConverter converter = new LongToTimePlayedConverter();
                        Func <double, string> activityForGameLogFormatter = value => (string)converter.Convert((long)value, null, null, CultureInfo.CurrentCulture);
                        PART_ChartTimeActivityLabelsY.LabelFormatter      = activityForGameLogFormatter;

                        PART_ChartTimeActivity.DataTooltip     = new CustomerToolTipForTime();
                        PART_ChartTimeActivityLabelsY.MinValue = 0;
                        PART_ChartTimeActivity.Series          = activityForGameSeries;
                        PART_ChartTimeActivityLabelsX.Labels   = activityForGameLabels;
                    }));
                }
                catch (Exception ex)
                {
                    Common.LogError(ex, "GameActivity");
                }
            });
        }