コード例 #1
0
        private async Task <bool> ScanPage(List <Image <Rgba32> > images, AndroidConnector android, ScanInfo scanInfo, List <ScanResultEquip> result)
        {
            string codename = scanInfo.RealName.ToLower();

            codename = codename.Replace(" ", "_");
            codename = codename.Replace("[1]", "1s");
            codename = codename.Replace("[2]", "2s");


            using (var image = Image.Load <Rgba32>("shopitems.png"))
            {
                //first, look for the top blue line of the item box (RGB(171, 210, 243) if not scrolled half a pixel)
                int starty = FindFirstResult(image);
                Program.log.Log(scanInfo.RealName, $"Item list starting at {starty}");
                await ClickShopItem(android, 0, starty - 230);

                //go through all 8 pictures
                for (int ii = 0; ii < 8; ii++)
                {
                    int newstarty = FindFirstResult(image);
                    if (newstarty != starty)
                    {
                        Program.log.Log(scanInfo.RealName, $"Scrolled a bit, adjusting");
                        starty = newstarty;
                    }
                    if (starty + 180 * (ii / 2) + 132 > 902)
                    {
                        continue;
                    }

                    //make a capture of the item, so we don't scan items twice
                    var subImage = image.Clone(ctx => ctx.Crop(new Rectangle(595 + 600 * (ii % 2), starty + 180 * (ii / 2), 400, 132)));

                    if (TestIfScanned(subImage, images))
                    {
                        Program.log.Log(scanInfo.RealName, $"Item {ii} already scanned, skipping");
                        continue;
                    }
                    images.Add(subImage);
                    subImage.Save($"unknown/unknown{Directory.GetFiles("unknown").Length}.png");

                    //click it as visualisation
                    await ClickShopItem(android, ii, starty - 230);

                    //check if item has multiple on sale. If it does, we don't have to scan it because it won't have an enchantment
                    var rect = new Rectangle(694 + 600 * (ii % 2), starty + 103 + 180 * (ii / 2), 33, 28);
                    using (var amountImage = image.Clone(ctx => ctx.Crop(rect)))
                    {
                        //image.Clone(ctx => ctx.Crop(rect)).Save($"unknown/unknown{Directory.GetFiles("unknown").Length}.png");
                        if (!File.Exists($"data/equip_br/{codename}.png"))
                        {
                            if (!Directory.Exists($"data/equip_br/{codename}"))
                            {
                                Directory.CreateDirectory($"data/equip_br/{codename}");
                            }
                            amountImage.Save($"data/equip_br/{codename}/{Directory.GetFiles($"data/equip_br/{codename}/").Length}.png");
                        }
                        else
                        {
                            using (var noamount = Image.Load <Rgba32>($"data/equip_br/{codename}.png"))
                            {
                                int dist = ImageDistance(amountImage, noamount);
                                if (dist > 100)
                                {
                                    Program.log.Log(scanInfo.RealName, $"Item {ii} has an amount, not scanning. Distance {dist}");
                                    Console.WriteLine($"- Item {ii} has an amount, not scanning! Distance {dist}");
                                    continue;
                                }
                            }
                        }
                    }

                    //scan item for price and enchantments
                    await Task.Delay(100);

                    Console.WriteLine($"- Scanning item {ii}");
                    await ClickShopBuyButton(android);

                    await Task.Delay(1000);

                    Console.WriteLine("- Scanning item");
                    await android.Screenshot("shopresult.png");

                    ScanResultEquip priceInfo = await ParseResultWindowEquip("shopresult.png", scanInfo, android);

                    result.Add(priceInfo);
                    await Task.Delay(100);
                    await ClickShopCloseItem(android);

                    if (priceInfo.Error) //if error, last one is found
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
コード例 #2
0
        public async Task <ScanResultEquip> ParseResultWindowEquip(string fileName, ScanInfo scanInfo, AndroidConnector android)
        {
            ScanResultEquip exchangeInfo = new ScanResultEquip()
            {
                Found    = true,
                ScanInfo = scanInfo
            };

            using (var image = Image.Load <Rgba32>(fileName))
            {
                image.Clone(ctx => ctx.Crop(new Rectangle(996, 159, 549, 66))).Save($"itemname.png");
                string itemName = GetTextFromImage("itemname.png");
                if (itemName.ToLower() != scanInfo.RealName.ToLower())
                {
                    scanInfo.Message = $"Something is wrong, names do NOT match. Expected {scanInfo.RealName.ToLower()} but got {itemName.ToLower()}";
                    return(ScanResult.BuildError <ScanResultEquip>(scanInfo));
                }

                exchangeInfo.Price    = int.Parse(GetPriceEquip(image), CultureInfo.InvariantCulture);
                exchangeInfo.SnapTime = GetSnapTime(image);

                int i = 0;
                //scan for multiple items
                bool enchanted   = false;
                bool foundRefine = false;
                while (!Program.CancelScan)
                {
                    using (var image2 = Image.Load <Rgba32>(fileName))
                        image2.Clone(ctx => ctx.Crop(new Rectangle(383, 261, 553, 453))).Save($"enchant{i}.png");
                    string hasEnchant = GetTextFromImage($"enchant{i}.png");
                    Console.WriteLine($"- Text Read: \n\n{hasEnchant}\n\n");
                    if (hasEnchant.ToLower().Contains("refine ") && !foundRefine && !enchanted && hasEnchant.ToLower().IndexOf("refine ") != hasEnchant.ToLower().IndexOf("refine +6 effective"))
                    {
                        string refine = hasEnchant.ToLower();
                        refine = refine.Replace("\r\n", "\n");
                        while (refine.Contains("\n\n"))
                        {
                            refine = refine.Replace("\n\n", "\n");
                        }
                        refine = refine.Substring(refine.IndexOf("\nrefine ") + 8).Trim();
                        if (refine.IndexOf("\n") > 0)
                        {
                            refine = refine.Substring(0, refine.IndexOf("\n")).Trim();
                        }
                        Console.WriteLine(refine);
                        int refineLevel = 0;
                        if (refine.Contains("/"))
                        {
                            try
                            {
                                refineLevel = int.Parse(refine.Substring(0, refine.IndexOf("/")));
                                foundRefine = true;
                            }
                            catch (FormatException e)
                            {
                                scanInfo.Message = "Something is wrong, error parsing refine level: " + refine;
                                Console.WriteLine(e);
                                return(ScanResult.BuildError <ScanResultEquip>(scanInfo));
                            }
                        }
                        if (refine.Contains("atk+"))
                        {
                            int atk = int.Parse(refine.Substring(refine.IndexOf("atk+") + 4));
                            foundRefine = true;
                            //TODO: check if atk matches refineLevel
                        }
                        exchangeInfo.RefinementLevel = refineLevel;
                    }


                    if (hasEnchant.ToLower().Contains("enchanted"))
                    {
                        enchanted = true;
                    }
                    if (hasEnchant.ToLower().Contains("equipment upgrade"))
                    {
                        if (enchanted)
                        {
                            MemoryStream ms          = new MemoryStream();
                            var          jpegEncoder = new SixLabors.ImageSharp.Formats.Jpeg.JpegEncoder {
                                Quality = 10
                            };
                            using (var image2 = Image.Load <Rgba32>($"enchant{i}.png"))
                                image2.Clone(ctx => ctx.Resize(new ResizeOptions {
                                    Size = image2.Size() / 2
                                })).SaveAsJpeg(ms, jpegEncoder);

                            exchangeInfo.EnchantmentImage = System.Convert.ToBase64String(ms.GetBuffer());
                            if (!hasEnchant.ToLower().Contains("enchanted"))
                            {
                                Console.WriteLine("Scrolled too far");
                                exchangeInfo.Enchantments = new List <string>()
                                {
                                    "scrolled too far"
                                };
                                break;
                            }
                            hasEnchant = hasEnchant.ToLower();
                            hasEnchant = hasEnchant.Replace("\r\n", "\n");
                            while (hasEnchant.Contains("\n\n"))
                            {
                                hasEnchant = hasEnchant.Replace("\n\n", "\n");
                            }
                            hasEnchant = hasEnchant.Substring(hasEnchant.IndexOf("enchanted attribute:") + 20).Trim();
                            hasEnchant = hasEnchant.Substring(0, hasEnchant.IndexOf("equipment upgrade")).Trim();

                            hasEnchant = hasEnchant.Replace("mapr ", "maxhp ");
                            exchangeInfo.Enchantments = hasEnchant.Split("\n", 4).ToList();
                        }
                        break;
                    }
                    if (hasEnchant.ToLower().Contains("exchange price"))
                    {
                        Console.WriteLine("Scrolled wayyyyyy too far");
                        break;
                    }


                    await android.Swipe(555, 500, 555, 300, 500);

                    await Task.Delay(100);

                    await android.Tap(1500, 960);

                    await android.Screenshot(fileName);

                    i++;
                }
                exchangeInfo.Found = true;
            }
            return(exchangeInfo);
        }