Beispiel #1
0
        public static Dictionary <int, List <int> > FindDbDups(List <DSReplay> newreplays)
        {
            Dictionary <int, List <int> > CompareMe = new Dictionary <int, List <int> >();

            using (var context = new DSReplayContext(Program._opt))
            {
                //var replays = context.DSReplays.Include(p => p.DSPlayer);
                //newreplays = replays.ToList();

                int i = 0;
                foreach (DSReplay rep in newreplays.ToArray())
                {
                    i++;
                    List <string> repRaces = new List <string>(rep.DSPlayer.OrderBy(o => o.REALPOS).Select(s => s.RACE));

                    var compreps = context.DSReplays.Where(x => x.GAMETIME > rep.GAMETIME.AddDays(-1)
                                                           //var compreps = replays.Where(x => x.GAMETIME > rep.GAMETIME.AddDays(-2)
                                                           && x.GAMETIME <rep.GAMETIME.AddDays(1) &&
                                                                          x.DURATION> rep.DURATION - TimeSpan.FromMinutes(1).TotalSeconds &&
                                                           x.DURATION < rep.DURATION + TimeSpan.FromMinutes(1).TotalSeconds &&
                                                           x.ID != rep.ID);

                    var rreps = from r in compreps.Include(p => p.DSPlayer)
                                //var rreps = from r in compreps
                                from p in r.DSPlayer
                                select new
                    {
                        r.ID,
                        p.REALPOS,
                        p.RACE
                    };

                    if (compreps.Any())
                    {
                        HashSet <int> ids = rreps.Select(s => s.ID).ToHashSet();
                        foreach (int id in ids)
                        {
                            List <string> compRaces = rreps.Where(x => x.ID == id).OrderBy(o => o.REALPOS).Select(s => s.RACE).ToList();
                            if (repRaces.SequenceEqual(compRaces))
                            {
                                if (!CompareMe.ContainsKey(rep.ID))
                                {
                                    CompareMe[rep.ID] = new List <int>();
                                }
                                CompareMe[rep.ID].Add(id);
                            }
                        }
                    }


                    if (i % 100 == 0)
                    {
                        Program.logger.LogInformation($"{i}: comparereps => {CompareMe.Count}; searching: {compreps.Count()}");
                    }
                    //if (i > 4000)
                    //    return CompareMe;
                }
                return(CompareMe);
            }
        }
Beispiel #2
0
        private static DSReplayContext CreateDbIfNotExists(IHost host)
        {
            DSReplayContext context = null;

            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;
                var config   = services.GetRequiredService <IConfiguration>().GetSection("Config");
                if (config != null && Status.isFirstRun == false)
                {
                    config.Bind(DSdata.Config);
                }

                try
                {
                    context = services.GetRequiredService <DSReplayContext>();
                    context.Database.EnsureCreated();
                    DSdata.Status.Count = context.DSReplays.Count();
                }
                catch (Exception ex)
                {
                    var logger = services.GetRequiredService <ILogger <Program> >();
                    logger.LogError(ex, "An error occurred creating the DB.");
                }
            }
            return(context);
        }
Beispiel #3
0
        public static int ScanNG()
        {
            List <DSReplay> DSReplays = new List <DSReplay>();

            foreach (var dir in Directory.GetFiles(DSdata.ServerConfig.SumDir1))
            {
                DSReplays.AddRange(DataService.ReadJson(dir));
            }

            int c = DSReplays.Count;

            DSReplays = DataService.FindDups(DSReplays);
            Console.WriteLine("New Replays found: " + c);
            Console.WriteLine("Insert Replays: " + DSReplays.Count);



            int i = 0;

            using (var context = new DSReplayContext(Program._opt))
            {
                foreach (DSReplay rep in DSReplays)
                {
                    i++;
                    DBService.SaveReplay(context, rep, true);
                    if (i % 100 == 0)
                    {
                        context.SaveChanges();
                    }
                }
                context.SaveChanges();
            }

            return(DSReplays.Count);
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            Init();
            int i = 0;

            DSdata.Init();
            List <DSReplay> replays = new List <DSReplay>();

            using (var context = new DSReplayContext(_opt))
            {
                context.Database.EnsureCreated();
                i = context.DSReplays.Count();
                //replays = context.DSReplays.Include(p => p.DSPlayer).ToList();
            }

            DateTime t = DateTime.Now;

            //int done = 0;
            int done = DbDupFind.ScanAdd();

            //int done = DbDupFind.ScanNG();

            if (done > 0)
            {
                Program.SendUpdateRequest();
            }

            DSdata.ServerConfig.LastRun = t;
            Program.SaveConfig();
        }
Beispiel #5
0
 public static void Delete(HashSet <int> DeleteMe)
 {
     using (var context = new DSReplayContext(Program._opt))
     {
         int i = 0;
         foreach (int id in DeleteMe)
         {
             DBService.DeleteRep(context, id, true);
             i++;
             if (i % 100 == 0)
             {
                 context.SaveChanges();
             }
         }
         context.SaveChanges();
     }
 }
Beispiel #6
0
 public BulkInsert(DSReplayContext context, DSoptions options, DBService db)
 {
     _context = context;
     _options = options;
     _db      = db;
 }
Beispiel #7
0
        public async Task ProduceFromOldDb(sc2dsstatsContext context, DSReplayContext oldcontext, List <string> playerNames, CancellationToken token)
        {
            if (Producing)
            {
                return;
            }
            Producing   = true;
            PlayerNames = playerNames;
            int skip = 0;
            int take = 250;

            Threads       = 1;
            producerCount = 1;
            decodeCount   = 0;
            FailedReplays = new ConcurrentBag <string>();
            StartTime     = DateTime.UtcNow;

            insertService.WriteStart();
            notifySource = new CancellationTokenSource();
            _            = Notify(notifySource.Token);
            insertService.ReplaysInserted += InsertService_ReplaysInserted;

            try
            {
                var oldReplays = await oldcontext.DSReplays
                                 .Include(i => i.Middle)
                                 .Include(i => i.DSPlayer)
                                 .ThenInclude(j => j.Breakpoints)
                                 .AsNoTracking()
                                 .OrderByDescending(o => o.GAMETIME)
                                 .AsSplitQuery()
                                 .Take(take)
                                 .ToListAsync();

                while (oldReplays.Any() && !token.IsCancellationRequested)
                {
                    var json       = JsonSerializer.Serialize(oldReplays);
                    var newReplays = JsonSerializer.Deserialize <List <DsReplayDto> >(json, new JsonSerializerOptions()
                    {
                        PropertyNameCaseInsensitive = true
                    });

                    newReplays.ForEach(f => InsertReplay(f, PlayerNames));

                    await Task.Delay(5000); // TODO

                    skip        += take;
                    decodeCount += take;
                    oldReplays   = await oldcontext.DSReplays
                                   .Include(i => i.Middle)
                                   .Include(i => i.DSPlayer)
                                   .ThenInclude(j => j.Breakpoints)
                                   .AsNoTracking()
                                   .OrderByDescending(o => o.GAMETIME)
                                   .AsSplitQuery()
                                   .Skip(skip)
                                   .Take(take)
                                   .ToListAsync();
                }
            }
            catch (OperationCanceledException) { }
            catch (Exception ex)
            {
                logger.LogError($"failed importing old db: {ex.Message}");
            }
            finally
            {
                PrducerFinished();
            }
        }
Beispiel #8
0
        public static DataResult GetDurations(DSoptions _options, DSReplayContext _context, IJSRuntime _jsRuntime, object lockobject)
        {
            if (String.IsNullOrEmpty(_options.Interest))
            {
                return(new DataResult());
            }

            var replays = DBReplayFilter.Filter(_options, _context, false);

            var result = (_options.Player, _options.Dataset.Any()) switch
            {
                (true, false) => from r in replays
                from p in r.DSPlayer
                where p.RACE == _options.Interest && p.NAME.Length == 64
                select new
                {
                    r.ID,
                    r.DURATION,
                    p.WIN
                },
                (true, true) => from r in replays
                from p in r.DSPlayer
                where p.RACE == _options.Interest && _options.Dataset.Contains(p.NAME)
                select new
                {
                    r.ID,
                    r.DURATION,
                    p.WIN
                },
                _ => from r in replays
                from p in r.DSPlayer
                where p.RACE == _options.Interest
                select new
                {
                    r.ID,
                    r.DURATION,
                    p.WIN
                }
            };
            var lresult = result.ToList();

            if (!lresult.Any())
            {
                return(new DataResult());
            }

            CultureInfo           provider = CultureInfo.InvariantCulture;
            DataResult            dresult  = new DataResult();
            List <double>         data     = new List <double>();
            List <double>         xdata    = new List <double>();
            List <double>         ydata    = new List <double>();
            List <string>         labels   = new List <string>();
            Func <double, double> f        = null;

            int count = lresult.Count;

            dresult.CmdrInfo.Wins      = lresult.Where(x => x.WIN == true).Count();
            dresult.CmdrInfo.Games     = count;
            dresult.CmdrInfo.AWinrate  = Math.Round((double)dresult.CmdrInfo.Wins * 100 / dresult.CmdrInfo.Games, 2);
            dresult.CmdrInfo.ADuration = TimeSpan.FromDays(3);
            dresult.CmdrInfo.GameIDs   = lresult.Select(s => s.ID).ToHashSet();


            for (int i = 0; i < DSdata.s_durations.Length; i++)
            {
                double games  = 1;
                double wins   = 0;
                int    startd = 0;

                if (i > 0)
                {
                    Match m = d_rx.Match(DSdata.s_durations[i]);
                    if (m.Success)
                    {
                        startd = int.Parse(m.Value);
                    }
                }

                int endd = startd + 3;
                if (i == 0)
                {
                    endd = 8;
                }
                if (i == DSdata.s_durations.Length - 1)
                {
                    endd = 200;
                }

                var ilresult = lresult.Where(x => x.DURATION > startd * 60 && x.DURATION < endd * 60);
                games = ilresult.Count();
                wins  = ilresult.Where(x => x.WIN == true).Count();

                data.Add(Math.Round(wins * 100 / games, 2));
                labels.Add(DSdata.s_durations[i] + " min (" + games + ")");
                xdata.Add(i);
                ydata.Add(data.Last());
            }

            if (xdata.Any() && xdata.Count > 1)
            {
                int order = 3;
                if (xdata.Count <= 3)
                {
                    if (xdata.Count < 3)
                    {
                        order = 1;
                    }
                    else
                    {
                        order = xdata.Count - 2;
                    }
                }
                f = Fit.PolynomialFunc(xdata.ToArray(), ydata.ToArray(), order);
            }

            dresult.Labels       = labels;
            dresult.Dataset.data = data;
            dresult.fTimeline    = f;
            dresult.fStartTime   = DateTime.MinValue;

            ChartService.SetColor(dresult.Dataset, _options.Chart.type, _options.Interest);
            dresult.Dataset.fill             = false;
            dresult.Dataset.pointRadius      = 2;
            dresult.Dataset.pointHoverRadius = 10;
            dresult.Dataset.showLine         = false;
            for (int i = 0; i < dresult.Labels.Count; i++)
            {
                ChartJSInterop.AddData(_jsRuntime,
                                       dresult.Labels[i],
                                       dresult.Dataset.data[i],
                                       dresult.Dataset.backgroundColor.Last(),
                                       null,
                                       lockobject
                                       ).GetAwaiter();
            }
            if (dresult.fTimeline != null)
            {
                ChartJSdataset dataset = new ChartJSdataset();
                dataset.label       = _options.Interest + "_line";
                dataset.borderWidth = 3;
                dataset.pointRadius = 1;
                ChartService.SetColor(dataset, _options.Chart.type, _options.Interest);
                dataset.backgroundColor.Clear();
                _options.Chart.data.datasets.Add(dataset);
                ChartJSInterop.AddDataset(_jsRuntime, JsonConvert.SerializeObject(dataset, Formatting.Indented), lockobject).GetAwaiter();

                for (int i = 0; i < dresult.Labels.Count; i++)
                {
                    double fdata = dresult.fTimeline(i);

                    ChartJSInterop.AddData(_jsRuntime,
                                           dresult.Labels[i],
                                           fdata,
                                           dresult.Dataset.backgroundColor.Last(),
                                           null,
                                           lockobject
                                           ).GetAwaiter();
                }
            }
            return(dresult);
        }
        public static int UpdateFromOldDb(sc2dsstatsContext context, DSReplayContext oldcontext, InsertService insertService, bool fullCopy = false)
        {
            throw new NotImplementedException();
            //lock (lockobject)
            //{
            //    if (isRunning)
            //    {
            //        return 0;
            //    }
            //    isRunning = true;
            //}
            //var restPlayer = context.DSRestPlayers.FirstOrDefault(f => f.Name == "olddb");
            //if (restPlayer == null)
            //{
            //    restPlayer = new db.DSRestPlayer()
            //    {
            //        Name = "olddb",
            //        LastUpload = new DateTime(2021, 11, 09, 00, 00, 00)
            //    };
            //    context.DSRestPlayers.Add(restPlayer);
            //}

            //if (fullCopy)
            //{
            //    restPlayer = new db.DSRestPlayer()
            //    {
            //        LastUpload = DateTime.MinValue
            //    };
            //}

            //int skip = 0;
            //int take = 1000;

            //var oldReplays = oldcontext.DSReplays
            //    .Include(i => i.Middle)
            //    .Include(i => i.DSPlayer)
            //        .ThenInclude(j => j.Breakpoints)
            //    .AsNoTracking()
            //    .Where(x => x.Upload > restPlayer.LastUpload)
            //    .OrderBy(o => o.ID)
            //    .AsSplitQuery()
            //    .Take(take)
            //    .ToList();

            //if (!oldReplays.Any())
            //{
            //    isRunning = false;
            //    return 0;
            //}
            //DateTime latestReplay = DateTime.MinValue;

            //while (oldReplays.Any())
            //{
            //    if (!fullCopy)
            //    {
            //        var l = oldReplays.OrderByDescending(o => o.Upload).First().Upload;
            //        if (l > latestReplay)
            //            latestReplay = l;
            //    }
            //    var json = JsonSerializer.Serialize(oldReplays);
            //    var newReplays = JsonSerializer.Deserialize<List<DsReplayDto>>(json, new JsonSerializerOptions() { PropertyNameCaseInsensitive = true });
            //    newReplays.SelectMany(s => s.DSPlayer).Where(x => x.NAME.Length == 64).ToList().ForEach(f => f.isPlayer = true);
            //    Console.WriteLine($"Inserting {newReplays.Count} replays from olddb");
            //    EventWaitHandle ewh = new EventWaitHandle(false, EventResetMode.ManualReset);
            //    insertService.InsertReplays(newReplays, "olddb", ewh, !fullCopy);
            //    ewh.WaitOne();
            //    skip += take;
            //    oldReplays = oldcontext.DSReplays
            //    .Include(i => i.Middle)
            //    .Include(i => i.DSPlayer)
            //        .ThenInclude(j => j.Breakpoints)
            //    .AsNoTracking()
            //    .Where(x => x.Upload > restPlayer.LastUpload)
            //    .OrderBy(o => o.ID)
            //    .AsSplitQuery()
            //    .Skip(skip)
            //    .Take(take)
            //    .ToList();
            //}
            //if (!fullCopy)
            //{
            //    restPlayer.LastUpload = latestReplay;
            //    context.SaveChanges();
            //}
            //isRunning = false;
            //return insertService.NewReplaysCount;
        }
Beispiel #10
0
        public static HashSet <int> CheckDups(Dictionary <int, List <int> > CompareMe)
        {
            HashSet <int> DeleteMe = new HashSet <int>();
            int           i        = 0;
            int           c        = CompareMe.Count;

            using (var context = new DSReplayContext(Program._opt))
            {
                foreach (int id in CompareMe.Keys)
                {
                    i++;
                    if (i % 100 == 0)
                    {
                        Program.logger.LogInformation($"{i}/{c}");
                    }

                    if (DeleteMe.Contains(id))
                    {
                        continue;
                    }



                    DSReplay replay = context.DSReplays.First(s => s.ID == id);

                    foreach (int cid in CompareMe[id])
                    {
                        if (DeleteMe.Contains(cid))
                        {
                            continue;
                        }
                        DSReplay crep          = context.DSReplays.First(s => s.ID == cid);
                        int      isDupPossible = 0;

                        if (replay.REPLAY == crep.REPLAY)
                        {
                            if (new Version(replay.VERSION) >= new Version(crep.VERSION))
                            {
                                DeleteMe.Add(crep.ID);
                            }
                            else
                            {
                                DeleteMe.Add(replay.ID);
                            }
                            continue;
                        }

                        if (replay.VERSION == crep.VERSION)
                        {
                            if (replay.MAXKILLSUM == crep.MAXKILLSUM)
                            {
                                isDupPossible++;
                            }
                            if (replay.MAXLEAVER == crep.MAXLEAVER)
                            {
                                isDupPossible++;
                            }
                            if (replay.MININCOME == crep.MININCOME)
                            {
                                isDupPossible++;
                            }
                            if (replay.MINARMY == crep.MINARMY)
                            {
                                isDupPossible++;
                            }
                        }
                        else
                        {
                            int kdiff = Math.Abs(replay.MAXKILLSUM - crep.MAXKILLSUM);
                            //int adiff = Math.Abs(rep.MINARMY - crep.MINARMY);
                            int mdiff = Math.Abs(replay.MINKILLSUM - crep.MINKILLSUM);
                            int ddiff = Math.Abs(replay.DURATION - crep.DURATION);

                            if (ddiff == 0 || (kdiff < 1000 && mdiff < 1000))
                            {
                                isDupPossible = 4;
                            }
                        }

                        if (isDupPossible > 2)
                        {
                            replay = context.DSReplays
                                     .Include(p => p.DSPlayer)
                                     .Single(s => s.ID == id);
                            crep = context.DSReplays
                                   .Include(p => p.DSPlayer)
                                   .Single(s => s.ID == cid);

                            if (new Version(replay.VERSION) > new Version(crep.VERSION))
                            {
                                foreach (var ent in crep.DSPlayer.Select(s => new { s.NAME, s.REALPOS }))
                                {
                                    try
                                    {
                                        if (ent.NAME.Length == 64 && replay.DSPlayer.Single(x => x.REALPOS == ent.REALPOS).NAME.Length < 64)
                                        {
                                            replay.DSPlayer.Single(x => x.REALPOS == ent.REALPOS).NAME = ent.NAME;
                                        }
                                    }
                                    catch (Exception e)
                                    {
                                        Program.logger.LogInformation($"No RealPos Player found {e.Message}: {replay.ID}");
                                    }
                                }
                                DeleteMe.Add(crep.ID);
                            }
                            else
                            {
                                foreach (var ent in replay.DSPlayer.Select(s => new { s.NAME, s.REALPOS }))
                                {
                                    try
                                    {
                                        if (ent.NAME.Length == 64 && crep.DSPlayer.Single(x => x.REALPOS == ent.REALPOS).NAME.Length < 64)
                                        {
                                            crep.DSPlayer.Single(x => x.REALPOS == ent.REALPOS).NAME = ent.NAME;
                                        }
                                    }
                                    catch (Exception e)
                                    {
                                        Program.logger.LogInformation($"No RealPos Player found {e.Message}: {crep.ID}");
                                    }
                                }
                                DeleteMe.Add(replay.ID);
                            }
                            context.SaveChanges();
                        }
                    }
                }
            }
            return(DeleteMe);
        }
Beispiel #11
0
        public static List <DSReplay> InsertReplays()
        {
            List <DSReplay> NewDbReps = new List <DSReplay>();
            List <DSReplay> Dups      = new List <DSReplay>();

            using (var context = new DSReplayContext(Program._opt))
            {
                var Replays = context.DSReplays.Include(p => p.DSPlayer);
                foreach (dsreplay replay in sReplays.ToArray())
                {
                    //DSReplay crep = context.DSReplays
                    //    .Include(p => p.DSPlayer)
                    //    .FirstOrDefault(s => s.HASH == replay.HASH);



                    DSReplay crep = Replays.FirstOrDefault(s => s.HASH == replay.HASH);
                    if (crep != null)
                    {
                        if (new Version(replay.VERSION) > new Version(crep.VERSION))
                        {
                            foreach (var ent in crep.DSPlayer.Select(s => new { s.NAME, s.REALPOS }))
                            {
                                try
                                {
                                    if (ent.NAME.Length == 64 && replay.PLAYERS.Single(x => x.REALPOS == ent.REALPOS).NAME.Length < 64)
                                    {
                                        replay.PLAYERS.Single(x => x.REALPOS == ent.REALPOS).NAME = ent.NAME;
                                    }
                                }
                                catch
                                {
                                    Console.WriteLine("???");
                                }
                            }
                            DBService.DeleteRep(context, crep.ID);
                        }
                        else
                        {
                            int i = 0;
                            foreach (var ent in replay.PLAYERS.Select(s => new { s.NAME, s.REALPOS }))
                            {
                                try
                                {
                                    if (ent.NAME.Length == 64 && crep.DSPlayer.Single(x => x.REALPOS == ent.REALPOS).NAME.Length < 64)
                                    {
                                        i++;
                                        crep.DSPlayer.Single(x => x.REALPOS == ent.REALPOS).NAME = ent.NAME;
                                    }
                                }
                                catch
                                {
                                    Console.WriteLine(":(");
                                }
                            }
                            if (i > 0)
                            {
                                context.SaveChanges();
                            }
                            sReplays.Remove(replay);
                        }
                    }
                }

                int j = 0;
                foreach (dsreplay rep in sReplays)
                {
                    DSReplay newdbrep = Map.Rep(rep);
                    newdbrep.REPLAYPATH = "";
                    DBService.SaveReplay(context, newdbrep, true);
                    NewDbReps.Add(newdbrep);
                    j++;
                    if (j % 100 == 0)
                    {
                        context.SaveChanges();
                        Console.WriteLine($"{j}/{sReplays.Count}");
                    }
                }
                context.SaveChanges();
            }

            return(NewDbReps);
        }
Beispiel #12
0
        public static List <DSReplay> InsertDSReplays(List <DSReplay> replays)
        {
            List <DSReplay> NewDbReps = new List <DSReplay>();
            List <DSReplay> Dups      = new List <DSReplay>();

            using (var context = new DSReplayContext(Program._opt))
            {
                var Replays = context.DSReplays.Include(p => p.DSPlayer);
                foreach (DSReplay replay in replays.ToArray())
                {
                    if (replay.MINARMY == 0 && replay.MINKILLSUM == 0 && replay.MAXKILLSUM == 0 && replay.DURATION < 120)
                    {
                        replays.Remove(replay);
                        continue;
                    }
                    //DSReplay crep = context.DSReplays
                    //    .Include(p => p.DSPlayer)
                    //    .FirstOrDefault(s => s.HASH == replay.HASH);
                    DSReplay crep = Replays.FirstOrDefault(s => s.HASH == replay.HASH);
                    if (crep != null)
                    {
                        if (new Version(replay.VERSION) > new Version(crep.VERSION))
                        {
                            foreach (var ent in crep.DSPlayer.Select(s => new { s.NAME, s.REALPOS }))
                            {
                                try
                                {
                                    if (ent.NAME.Length == 64 && replay.DSPlayer.Single(x => x.REALPOS == ent.REALPOS).NAME.Length < 64)
                                    {
                                        replay.DSPlayer.Single(x => x.REALPOS == ent.REALPOS).NAME = ent.NAME;
                                    }
                                }
                                catch (Exception e)
                                {
                                    Program.logger.LogInformation($"Error in finding RealPos {e.Message}");
                                }
                            }
                            DBService.DeleteRep(context, crep.ID);
                        }
                        else
                        {
                            int i = 0;
                            foreach (var ent in replay.DSPlayer.Select(s => new { s.NAME, s.REALPOS }))
                            {
                                try
                                {
                                    if (ent.NAME.Length == 64 && crep.DSPlayer.Single(x => x.REALPOS == ent.REALPOS).NAME.Length < 64)
                                    {
                                        i++;
                                        crep.DSPlayer.Single(x => x.REALPOS == ent.REALPOS).NAME = ent.NAME;
                                    }
                                }
                                catch (Exception e)
                                {
                                    Program.logger.LogInformation($"Error in finding RealPos {e.Message}");
                                }
                            }
                            if (i > 0)
                            {
                                context.SaveChanges();
                            }
                            replays.Remove(replay);
                        }
                    }
                    else
                    {
                        //Console.WriteLine("no dup");
                    }
                }

                int j = 0;
                foreach (DSReplay newdbrep in replays)
                {
                    newdbrep.REPLAYPATH = "";
                    DBService.SaveReplay(context, newdbrep, true);
                    NewDbReps.Add(newdbrep);
                    j++;
                    if (j % 100 == 0)
                    {
                        context.SaveChanges();
                        Program.logger.LogInformation($"{j}/{replays.Count}");
                    }
                }
                context.SaveChanges();
            }

            return(NewDbReps);
        }
Beispiel #13
0
        public void Init(DSReplayContext _context, List <string> names)
        {
            if (!names.Any())
            {
                return;
            }
            var reps = from p in _context.DSPlayers
                       where names.Contains(p.NAME)
                       select new
            {
                p.REALPOS,
                p.RACE,
                p.OPPRACE,
                p.PDURATION,
                p.WIN
            };

            int total     = 0;
            int totalwins = 0;
            int tduration = 0;

            foreach (string cmdr in DSdata.s_races_cmdr)
            {
                var        cmdrreps = reps.Where(x => x.RACE == cmdr);
                PlayerStat stat     = new PlayerStat();
                int        duration = 0;
                if (cmdrreps.Any())
                {
                    stat.Race            = cmdr;
                    stat.Count           = cmdrreps.Count();
                    stat.Wins            = cmdrreps.Where(x => x.WIN == true).Count();
                    stat.Winrate         = MathF.Round((float)stat.Wins * 100 / (float)stat.Count, 2);
                    duration             = cmdrreps.Sum(s => s.PDURATION);
                    stat.AvgGameDuration = TimeSpan.FromSeconds(duration / stat.Count);
                    Stats.Add(stat);


                    foreach (string oppcmdr in DSdata.s_races_cmdr)
                    {
                        var vsreps = cmdrreps.Where(o => o.OPPRACE == oppcmdr);
                        if (vsreps.Count() > 1)
                        {
                            PlayerStat vsstat = new PlayerStat();
                            vsstat.Count   = vsreps.Count();
                            vsstat.Race    = cmdr;
                            vsstat.OppRace = oppcmdr;
                            vsstat.Wins    = vsreps.Where(x => x.WIN == true).Count();
                            vsstat.Winrate = MathF.Round((float)vsstat.Wins * 100 / (float)vsstat.Count, 2);
                            int vduration = vsreps.Sum(s => s.PDURATION);
                            vsstat.AvgGameDuration = TimeSpan.FromSeconds(vduration / vsstat.Count);
                            stat.Vs.Add(stat);

                            if (WorstStat.Race == String.Empty)
                            {
                                WorstStat = vsstat;
                            }
                            else if (vsstat.Winrate < WorstStat.Winrate)
                            {
                                WorstStat = vsstat;
                            }

                            if (BestStat.Race == String.Empty)
                            {
                                BestStat = vsstat;
                            }
                            else if (vsstat.Winrate > BestStat.Winrate)
                            {
                                BestStat = vsstat;
                            }
                        }
                    }
                }


                var        oppcmdrreps = reps.Where(x => x.OPPRACE == cmdr);
                PlayerStat oppstat     = new PlayerStat();
                if (oppcmdrreps.Any())
                {
                    oppstat.Race    = cmdr;
                    oppstat.Count   = oppcmdrreps.Count();
                    oppstat.Wins    = oppcmdrreps.Where(x => x.WIN == true).Count();
                    oppstat.Winrate = MathF.Round((float)oppstat.Wins * 100 / (float)oppstat.Count, 2);
                    int oppduration = oppcmdrreps.Sum(s => s.PDURATION);
                    oppstat.AvgGameDuration = TimeSpan.FromSeconds(oppduration / oppstat.Count);
                    OppStats.Add(oppstat);
                }

                total     += stat.Count;
                totalwins += stat.Wins;
                tduration += duration;
            }

            TStats.Race  = "Total";
            TStats.Count = total;
            if (total > 0)
            {
                TStats.Winrate         = MathF.Round((float)totalwins * 100 / total, 2);
                TStats.AvgGameDuration = TimeSpan.FromSeconds(tduration / total);
            }

            for (int i = 1; i <= 6; i++)
            {
                var        preps = reps.Where(x => x.REALPOS == i);
                PlayerStat stat  = new PlayerStat();
                if (preps.Any())
                {
                    stat.Race    = String.Empty;
                    stat.Pos     = i;
                    stat.Count   = preps.Count();
                    stat.Wins    = preps.Where(x => x.WIN == true).Count();
                    stat.Winrate = MathF.Round((float)stat.Wins * 100 / (float)stat.Count, 2);
                    int pduration = preps.Sum(s => s.PDURATION);
                    stat.AvgGameDuration = TimeSpan.FromSeconds(pduration / stat.Count);
                    PosStats.Add(stat);
                }
            }
        }
Beispiel #14
0
 public ChartService(IJSRuntime jsRuntime, DSReplayContext context, DBService db)
 {
     _jsRuntime = jsRuntime;
     _context   = context;
     _db        = db;
 }
Beispiel #15
0
        public static async Task GetBuild(DSoptions _options, DSReplayContext _context, object dblock, ILogger _logger)
        {
            if (_options.Vs == "ALL")
            {
                _options.Vs = String.Empty;
            }
            string Hash = _options.GenHash();

            if (!_options.Decoding && BuildCache.ContainsKey(Hash))
            {
                _options.buildResult = BuildCache[Hash];
                return;
            }

            bool doWait = false;

            lock (Computing)
            {
                if (!Computing.Contains(Hash))
                {
                    Computing.Add(Hash);
                }
                else
                {
                    doWait = true;
                }
            }

            if (doWait)
            {
                while (Computing.Contains(Hash))
                {
                    await Task.Delay(500);
                }
                if (BuildCache.ContainsKey(Hash))
                {
                    _options.buildResult = BuildCache[Hash];
                }
                return;
            }

            BuildResult bresult = new BuildResult();

            foreach (var ent in _options.Gamemodes.Keys.ToArray())
            {
                _options.Gamemodes[ent] = false;
            }

            if (_options.Interest == "Terran" || _options.Interest == "Zerg" || _options.Interest == "Protoss")
            {
                _options.Gamemodes["GameModeStandard"] = true;
            }
            else
            {
                _options.Gamemodes["GameModeCommanders"]       = true;
                _options.Gamemodes["GameModeCommandersHeroic"] = true;
            }

            lock (dblock)
            {
                bool mengskfilter = _options.MengskPreviewFilter;
                _options.MengskPreviewFilter = false;
                var replays = DBReplayFilter.Filter(_options, _context);
                _options.MengskPreviewFilter = mengskfilter;

                bresult.TotalGames = replays.Count();

                var presult = (String.IsNullOrEmpty(_options.Vs) switch
                {
                    true => from r in replays
                    from t1 in r.DSPlayer
                    where t1.RACE == _options.Interest
                    where _options.Dataset.Contains(t1.NAME)
                    from u1 in t1.Breakpoints
                    where u1.Breakpoint == _options.Breakpoint
                    select new
                    {
                        r.ID,
                        r.DURATION,
                        r.GAMETIME,
                        t1.WIN,
                        u1.dsUnitsString,
                        u1.Upgrades,
                        u1.Gas
                    },
                    false => from r in replays
                    from t1 in r.DSPlayer
                    where t1.RACE == _options.Interest && t1.OPPRACE == _options.Vs
                    where _options.Dataset.Contains(t1.NAME)
                    from u1 in t1.Breakpoints
                    where u1.Breakpoint == _options.Breakpoint
                    select new
                    {
                        r.ID,
                        r.DURATION,
                        r.GAMETIME,
                        t1.WIN,
                        u1.dsUnitsString,
                        u1.Upgrades,
                        u1.Gas
                    }
                });
Beispiel #16
0
        public static DataResult GetTimeLine(DSoptions _options, DSReplayContext _context, IJSRuntime _jsRuntime, object lockobject)
        {
            if (String.IsNullOrEmpty(_options.Interest))
            {
                return(new DataResult());
            }

            var replays = DBReplayFilter.Filter(_options, _context, false);

            var result = (_options.Player, _options.Dataset.Any()) switch
            {
                (true, false) => from r in replays
                from p in r.DSPlayer
                where p.RACE == _options.Interest && p.NAME.Length == 64
                select new
                {
                    r.ID,
                    r.GAMETIME,
                    p.WIN
                },
                (true, true) => from r in replays
                from p in r.DSPlayer
                where p.RACE == _options.Interest && _options.Dataset.Contains(p.NAME)
                select new
                {
                    r.ID,
                    r.GAMETIME,
                    p.WIN
                },
                _ => from r in replays
                from p in r.DSPlayer
                where p.RACE == _options.Interest
                select new
                {
                    r.ID,
                    r.GAMETIME,
                    p.WIN
                }
            };
            var lresult = result.OrderBy(o => o.GAMETIME).ToList();

            if (!lresult.Any())
            {
                return(new DataResult());
            }

            CultureInfo           provider = CultureInfo.InvariantCulture;
            DataResult            dresult  = new DataResult();
            List <double>         data     = new List <double>();
            List <double>         xdata    = new List <double>();
            List <double>         ydata    = new List <double>();
            List <string>         labels   = new List <string>();
            Func <double, double> f        = null;

            int count = lresult.Count;
            int step  = 50;
            int cstep = count / step;

            while (cstep < 10)
            {
                step -= 1;
                cstep = count / step;
                if (step <= 1)
                {
                    break;
                }
            }
            dresult.CmdrInfo.Wins      = lresult.Where(x => x.WIN == true).Count();
            dresult.CmdrInfo.Games     = count;
            dresult.CmdrInfo.AWinrate  = Math.Round((double)dresult.CmdrInfo.Wins * 100 / dresult.CmdrInfo.Games, 2);
            dresult.CmdrInfo.ADuration = TimeSpan.FromDays(cstep);
            dresult.CmdrInfo.GameIDs   = lresult.Select(s => s.ID).ToHashSet();


            for (int i = 0; i < step; i++)
            {
                var sreps = lresult.Skip(i * cstep).Take(cstep);
                if (!sreps.Any())
                {
                    continue;
                }
                data.Add(Math.Round((double)sreps.Where(x => x.WIN == true).Count() * 100 / (double)sreps.Count(), 2));
                labels.Add(sreps.Last().GAMETIME.ToString("yyyy-MM-dd"));
                xdata.Add(i);
                ydata.Add(data.Last());
            }

            if (xdata.Any() && xdata.Count > 1)
            {
                int order = 6;
                if (xdata.Count <= 6)
                {
                    if (xdata.Count < 3)
                    {
                        order = 1;
                    }
                    else
                    {
                        order = xdata.Count - 2;
                    }
                }
                f = Fit.PolynomialFunc(xdata.ToArray(), ydata.ToArray(), order);
            }

            dresult.Labels       = labels;
            dresult.Dataset.data = data;
            dresult.fTimeline    = f;
            dresult.fStartTime   = lresult.First().GAMETIME;

            ChartService.SetColor(dresult.Dataset, _options.Chart.type, _options.Interest);
            dresult.Dataset.fill             = false;
            dresult.Dataset.pointRadius      = 2;
            dresult.Dataset.pointHoverRadius = 10;
            dresult.Dataset.showLine         = false;
            for (int i = 0; i < dresult.Labels.Count; i++)
            {
                ChartJSInterop.AddData(_jsRuntime,
                                       dresult.Labels[i],
                                       dresult.Dataset.data[i],
                                       dresult.Dataset.backgroundColor.Last(),
                                       null,
                                       lockobject
                                       ).GetAwaiter();
            }
            if (dresult.fTimeline != null)
            {
                string   fstart  = dresult.Labels.First().Substring(0, 10);
                DateTime ffstart = DateTime.ParseExact(fstart, "yyyy-MM-dd", provider);

                ChartJSdataset dataset = new ChartJSdataset();
                dataset.label       = _options.Interest + "_line";
                dataset.borderWidth = 3;
                dataset.pointRadius = 1;
                ChartService.SetColor(dataset, _options.Chart.type, _options.Interest);
                dataset.backgroundColor.Clear();
                _options.Chart.data.datasets.Add(dataset);
                ChartJSInterop.AddDataset(_jsRuntime, JsonConvert.SerializeObject(dataset, Formatting.Indented), lockobject).GetAwaiter();

                for (int i = 0; i < dresult.Labels.Count; i++)
                {
                    double fdata = dresult.fTimeline(i);

                    ChartJSInterop.AddData(_jsRuntime,
                                           dresult.Labels[i],
                                           fdata,
                                           dresult.Dataset.backgroundColor.Last(),
                                           null,
                                           lockobject
                                           ).GetAwaiter();
                }
            }
            return(dresult);
        }