Ejemplo n.º 1
0
        protected override void Seed(HGV.Nullifier.Data.DataContext context)
        {
            //  This method will be called after migrating to the latest version.
            //  You can use the DbSet<T>.AddOrUpdate() helper extension method to avoid creating duplicate seed data.

            var client = new HGV.Basilius.MetaClient();
            var heroes = client.GetHeroes()
                         .Select(_ => new Hero()
            {
                id   = _.Id,
                key  = _.Key,
                name = _.Name
            })
                         .ToArray();

            var abilities = client.GetSkills()
                            .Where(_ => _.Id != HGV.Basilius.Ability.GENERIC)
                            .Select(_ => new Ability()
            {
                id      = _.Id,
                key     = _.Key,
                name    = _.Name,
                hero_id = _.HeroId
            }).ToArray();

            var timezones = new Dictionary <int, int>()
            {
                { 0, 0 },   // UNKNOWN
                { 1, -5 },  // US WEST
                { 2, -8 },  // US EAST
                { 3, +2 },  // EUROPE
                { 5, +8 },  // SINGAPORE
                { 6, +4 },  // DUBAI
                { 7, +10 }, // AUSTRALIA
                { 8, +2 },  // STOCKHOLM
                { 9, +2 },  // AUSTRIA
                { 10, -3 }, // BRAZIL
                { 11, +2 }, // SOUTHAFRICA
                { 12, +8 }, // PW TELECOM SHANGHAI
                { 13, +8 }, // PW UNICOM
                { 14, -3 }, // CHILE
                { 15, -5 }, // PERU
                { 16, +5 }, // INDIA
                { 17, +8 }, // PW TELECOM GUANGDONG
                { 18, +8 }, // PW TELECOM ZHEJIANG
                { 19, +9 }, // JAPAN
                { 20, +8 }, // PW TELECOM WUHAN
                { 25, +8 }, // PW UNICOM TIANJIN
            };
            var regions = client.GetRegions()
                          .Select(_ => new { id = _.Key, name = _.Value })
                          .Join(timezones, _ => _.id, _ => _.Key, (lhs, rhs) => new Region()
            {
                id       = lhs.id,
                name     = lhs.name,
                timezone = rhs.Value
            }).ToArray();

            context.Heroes.AddOrUpdate(_ => _.id, heroes);
            context.Abilities.AddOrUpdate(_ => _.id, abilities);
            context.Regions.AddOrUpdate(_ => _.id, regions);
        }
Ejemplo n.º 2
0
        static void OnNewGameState(GameState gs)
        {
            try
            {
                bool DEBUGING = true;

                if (gs.Map.GameState == DOTA_GameState.Undefined)
                {
                    return;
                }

                if (gs.Previously.Map.GameState == GS_WAITING && gs.Map.GameState == GS_DRAFTING)
                {
                    var client   = new HGV.Basilius.MetaClient();
                    var heroKeys = client.GetADHeroes().ToDictionary(_ => _.Key, _ => _.Id);

                    Console.WriteLine("Drafting");
                    Console.WriteLine("Hero {0}:{1}", gs.Hero.ID, gs.Hero.Name);

                    Thread.Sleep(4000); // Wait for ability draft screen to load.

                    var captureImage = ScreenCapture.CaptureApplication();

                    if (DEBUGING == true)
                    {
                        captureImage.Save(".//output//capture.png");
                    }

                    Bitmap overlay;
                    Bitmap source;
                    {
                        // create filter
                        var colorFiltering = new ColorFiltering();
                        // set channels' ranges to keep
                        colorFiltering.Red   = new IntRange(50, 52);
                        colorFiltering.Green = new IntRange(50, 52);
                        colorFiltering.Blue  = new IntRange(50, 52);
                        // apply the filter
                        var image = colorFiltering.Apply(captureImage);

                        var gfilter = Grayscale.CommonAlgorithms.BT709;
                        source = gfilter.Apply(image);
                    }

                    {
                        // create filter
                        var colorFiltering = new ColorFiltering();
                        // set channels' ranges to keep
                        colorFiltering.Red   = new IntRange(194, 204);
                        colorFiltering.Green = new IntRange(211, 221);
                        colorFiltering.Blue  = new IntRange(165, 170);
                        // apply the filter
                        var image = colorFiltering.Apply(captureImage);

                        var gfilter = Grayscale.CommonAlgorithms.BT709;
                        overlay = gfilter.Apply(image);
                    }

                    // create filter
                    var mergeFilter = new Merge(overlay);
                    var resultImage = mergeFilter.Apply(source);

                    if (DEBUGING == true)
                    {
                        resultImage.Save("./output/bounds.png");
                    }

                    var shapeChecker = new SimpleShapeChecker();

                    var bc = new BlobCounter();
                    bc.FilterBlobs = true;
                    bc.MinHeight   = 5;
                    bc.MinWidth    = 5;
                    bc.ProcessImage(resultImage);

                    var blobs = bc.GetObjectsInformation();

                    var size = new Size();

                    var left   = 0;
                    var top    = 0;
                    var right  = 0;
                    var height = 0;

                    foreach (var blob in blobs)
                    {
                        var points = bc.GetBlobsEdgePoints(blob);
                        if (shapeChecker.IsCircle(points))
                        {
                            right = blob.Rectangle.Left;
                        }
                        else if (shapeChecker.IsQuadrilateral(points))
                        {
                            left   = blob.Rectangle.Right;
                            top    = blob.Rectangle.Top;
                            height = blob.Rectangle.Height;

                            const double HERO_RATIO = 0.76;
                            size.Width  = (int)Math.Round(height * HERO_RATIO);
                            size.Height = height;
                        }
                    }

                    var bounds  = new Rectangle(left, top, right - left, height);
                    var spacing = (bounds.Width - (size.Width * 10)) / 11;
                    bounds.X     += spacing;
                    bounds.Width -= (spacing * 2);

                    if (DEBUGING == true)
                    {
                        Crop filter      = new Crop(bounds);
                        var  imageHeader = filter.Apply(captureImage);
                        imageHeader.Save("./output/header.png");
                    }

                    var collection = new List <Bitmap>();
                    for (int i = 0; i < 10; i++)
                    {
                        var x = (spacing * i) + (size.Width * i) + bounds.X;

                        Crop cropFilter  = new Crop(new Rectangle(x, bounds.Y, size.Width, size.Height));
                        var  cropedImage = cropFilter.Apply(captureImage);
                        collection.Add(cropedImage);

                        if (DEBUGING == true)
                        {
                            cropedImage.Save($"./output/hero[{i}].png");
                        }
                    }

                    var templates    = new List <TemplatePackage>();
                    var resizeFilter = new ResizeBicubic(size.Width, size.Height);
                    var files        = Directory.GetFiles(".//heroes");
                    foreach (var file in files)
                    {
                        var key    = Path.GetFileNameWithoutExtension(file);
                        var heroId = 0;
                        heroKeys.TryGetValue(key, out heroId);

                        var template        = Bitmap.FromFile(file) as Bitmap;
                        var resizedTemplate = resizeFilter.Apply(template);
                        var package         = new TemplatePackage()
                        {
                            Key   = key,
                            Hero  = heroId,
                            Image = resizedTemplate,
                        };
                        templates.Add(package);
                    }

                    var tm   = new ExhaustiveTemplateMatching(0.8f);
                    var data = new List <DataPackage>();
                    for (int i = 0; i < collection.Count; i++)
                    {
                        var image = collection[i];

                        foreach (var template in templates)
                        {
                            var matchings = tm.ProcessImage(image, template.Image);
                            foreach (var match in matchings)
                            {
                                var package = new DataPackage()
                                {
                                    Similarity = match.Similarity,
                                    Bounds     = match.Rectangle,
                                    Key        = template.Key,
                                    Hero       = template.Hero,
                                    Index      = i,
                                };
                                data.Add(package);
                            }
                        }
                    }

                    var heroes = new List <int>();
                    var groups = data.GroupBy(_ => _.Index).OrderBy(_ => _.Key).ToList();
                    foreach (var group in groups)
                    {
                        var item = group.OrderByDescending(_ => _.Similarity).Take(1).FirstOrDefault();

                        heroes.Add(item.Hero);

                        if (DEBUGING == true)
                        {
                            Console.WriteLine("{0}:{1} ({2})", item.Hero, item.Key, item.Similarity);
                        }
                    }

                    Console.WriteLine("Launching Drafter");

                    var roster = string.Join(",", heroes.ToArray());
                    Process.Start("https://hgv-desolator.azurewebsites.net/#/draft?roster=" + roster);

                    Console.WriteLine("");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error");
                // Next Time Gadget. NEXT TIME!
            }
        }