Пример #1
0
        public static async Task <TimelineResponse> GetDuration(sc2dsstatsContext context, DsRequest request)
        {
            var replays = ReplayFilter.Filter(context, request);

            replays = replays.Where(x => x.Gametime >= request.StartTime);
            if (request.EndTime != DateTime.Today)
            {
                replays = replays.Where(x => x.Gametime <= request.EndTime);
            }

            var results = request.Player switch
            {
                true => from r in replays
                from p in r.Dsplayers
                where p.Race == (byte)DSData.GetCommander(request.Interest) && p.isPlayer
                select new
                {
                    r.Id,
                    r.Duration,
                    p.Win
                },
                false => from r in replays
                from p in r.Dsplayers
                where p.Race == (byte)DSData.GetCommander(request.Interest)
                select new
                {
                    r.Id,
                    r.Duration,
                    p.Win
                }
            };
            var lresults = await results.ToListAsync();

            TimelineResponse response = new TimelineResponse()
            {
                Interest    = request.Interest,
                Count       = lresults.Select(s => s.Id).Distinct().Count(),
                AvgDuration = (int)(lresults.Sum(s => s.Duration) / lresults.Count),
                Items       = new List <DsResponseItem>()
            };

            if (lresults.Any())
            {
                for (int i = 0; i < DSData.durations.Length; i++)
                {
                    int startd = 0;
                    if (i > 0)
                    {
                        Match m = d_rx.Match(DSData.durations[i]);
                        if (m.Success)
                        {
                            startd = int.Parse(m.Value);
                        }
                    }
                    int endd = startd + 3;
                    if (i == 0)
                    {
                        endd = 8;
                    }
                    if (i == DSData.durations.Length - 1)
                    {
                        endd = 200;
                    }
                    var ilresults = lresults.Where(x => x.Duration > startd * 60 && x.Duration < endd * 60).ToList();


                    response.Items.Add(new DsResponseItem()
                    {
                        Label = $"{DSData.durations[i]} min ({ilresults.Count})",
                        Count = ilresults.Count,
                        Wins  = ilresults.Where(x => x.Win == true).Count()
                    });
                }
            }
            var sma = new SimpleMovingAverage(4);

            response.SmaData = new List <double>();
            response.SmaData = TimelineService.GetNiceLineData(response.Items.Select(s => s.Count == 0 ? 0 : ((double)s.Wins * 100.0 / (double)s.Count)), 4);

            return(response);
        }
    }
Пример #2
0
        public static async Task <TimelineResponse> GetTimelineFromTimeResults(sc2dsstatsContext context, TimelineRequest request)
        {
            if (request.Step < 3)
            {
                request.Step = 3;
            }

            if (request.smaK < 3)
            {
                request.smaK = 3;
            }



            DateTime requestTime = request.StartTime;

            List <string> timeRequestStrings = new List <string>();

            while (requestTime < request.EndTime)
            {
                timeRequestStrings.Add(requestTime.ToString("yyyyMM"));
                requestTime = requestTime.AddMonths(1);
            }

            List <DsTimeResult> timeResults = await context.DsTimeResults
                                              .AsNoTracking()
                                              .Where(f =>
                                                     f.Cmdr == request.Interest &&
                                                     f.Opp == String.Empty &&
                                                     timeRequestStrings.Contains(f.Timespan) &&
                                                     f.Player == request.Player
                                                     )
                                              .ToListAsync();

            int tcount = timeResults.Sum(s => s.Count);
            TimelineResponse response = new TimelineResponse()
            {
                Interest    = request.Interest,
                Count       = tcount / 6,
                Versus      = request.Versus,
                AvgDuration = (int)(timeResults.Sum(s => s.Duration) / tcount),
                Items       = new List <DsResponseItem>()
            };

            requestTime = request.StartTime;
            while (requestTime < request.EndTime)
            {
                var timeResult = timeResults.SingleOrDefault(f => f.Timespan == requestTime.ToString("yyyyMM"));

                if (timeResult == null || timeResult.Count == 0)
                {
                    response.Items.Add(new TimelineResponseItem()
                    {
                        Label = $"{requestTime.ToString("yyyy-MM")} (0)",
                        Count = 0,
                        Wins  = 0
                    });
                }
                else
                {
                    response.Items.Add(new TimelineResponseItem()
                    {
                        Label = $"{requestTime.ToString("yyyy-MM")} ({timeResult.Count})",
                        Count = timeResult.Count,
                        Wins  = timeResult.Wins
                    });
                }
                requestTime = requestTime.AddMonths(1);
            }

            if (response.Items.Any() && response.Items.Last().Count < 10)
            {
                response.Items.RemoveAt(response.Items.Count - 1);
            }

            var sma = new SimpleMovingAverage(request.smaK);

            response.SmaData = new List <double>();
            response.SmaData = GetNiceLineData(response.Items.Select(s => s.Count == 0 ? 0 : ((double)s.Wins * 100.0 / (double)s.Count)), request.smaK);

            return(response);
        }