public override async Task <TaskRes> Execute(Account acc)
        {
            await acc.Wb.Navigate($"{acc.AccInfo.ServerUrl}/karte.php");

            var mainVill = AccountHelper.GetMainVillage(acc);

            var mapTiles = MapHelper.GetMapTiles(acc, mainVill.Coordinates);

            Coordinates closesCoords = GetClosestCoordinates(acc, mapTiles);

            if (closesCoords == null)
            {
                return(TaskRes.Retry);
            }

            acc.NewVillages.Locations.Add(new NewVillage()
            {
                Coordinates = closesCoords,
                Name        = NewVillageHelper.GenerateName(acc),
            });

            return(TaskRes.Executed);
        }
Example #2
0
        public override async Task <TaskRes> Execute(Account acc)
        {
            await acc.Wb.Navigate($"{acc.AccInfo.ServerUrl}/karte.php");

            Coordinates closesCoords = null;

            var mainVill = AccountHelper.GetMainVillage(acc);

            switch (acc.AccInfo.ServerVersion)
            {
            case Classificator.ServerVersionEnum.T4_4:
                var ajaxToken = DriverHelper.GetJsObj <string>(acc, "ajaxToken");

                var req = new RestRequest
                {
                    Resource = "/ajax.php?cmd=mapPositionData",
                    Method   = Method.POST,
                };

                req.AddParameter("cmd", "mapPositionData");
                req.AddParameter("data[x]", mainVill.Coordinates.x.ToString());
                req.AddParameter("data[y]", mainVill.Coordinates.y.ToString());
                req.AddParameter("data[zoomLevel]", "3");
                req.AddParameter("ajaxToken", ajaxToken);

                var resString = HttpHelper.SendPostReq(acc, req);

                var root = JsonConvert.DeserializeObject <MapPositionDataT4_4.Root>(resString);
                if (root.response.error)
                {
                    throw new Exception("Unable to get T4.4 map position data!\n" + root.response.error);
                }

                var mapTiles = root.response.data.tiles.Select(x => x.GetMapTile()).ToList();
                closesCoords = GetClosestCoordinates(acc, mapTiles);
                break;

            case Classificator.ServerVersionEnum.T4_5:
                var bearerToken = DriverHelper.GetBearerToken(acc);

                var reqMapInfo = new RestRequest
                {
                    Resource      = "/api/v1/ajax/mapInfo",
                    Method        = Method.POST,
                    RequestFormat = DataFormat.Json
                };
                reqMapInfo.AddHeader("authorization", $"Bearer {bearerToken}");
                reqMapInfo.AddHeader("content-type", "application/json; charset=UTF-8");
                reqMapInfo.AddJsonBody(GenerateMapInfo(mainVill.Coordinates));

                var mapInfoRes = HttpHelper.SendPostReq(acc, reqMapInfo);

                var mapPosition = new SendMapPositionT4_5.Root()
                {
                    data = new SendMapPositionT4_5.Data()
                    {
                        x               = mainVill.Coordinates.x,
                        y               = mainVill.Coordinates.y,
                        zoomLevel       = 3,
                        ignorePositions = new List <object>()
                    }
                };

                var reqMapPosition = new RestRequest
                {
                    Resource      = "/api/v1/ajax/mapPositionData",
                    Method        = Method.POST,
                    RequestFormat = DataFormat.Json
                };
                reqMapPosition.AddHeader("authorization", $"Bearer {bearerToken}");
                reqMapPosition.AddHeader("content-type", "application/json; charset=UTF-8");
                reqMapPosition.AddJsonBody(mapPosition);
                //reqMapPosition.AddParameter("application/json", , ParameterType.RequestBody);
                var mapPositionRes  = HttpHelper.SendPostReq(acc, reqMapPosition);
                var mapPositionData = JsonConvert.DeserializeObject <MapPositionDataT4_5>(mapPositionRes);

                var mapTilesT45 = mapPositionData.tiles.Select(x => x.GetMapTile()).ToList();
                closesCoords = GetClosestCoordinates(acc, mapTilesT45);
                break;
            }

            if (closesCoords == null)
            {
                return(TaskRes.Retry);
            }

            acc.NewVillages.Locations.Add(new NewVillage()
            {
                coordinates = closesCoords,
                Name        = NewVillageHelper.GenerateName(acc),
            });

            return(TaskRes.Executed);
        }
Example #3
0
        public override async Task <TaskRes> Execute(HtmlDocument htmlDoc, ChromeDriver wb, Files.Models.AccModels.Account acc)
        {
            await acc.Wb.Navigate($"{acc.AccInfo.ServerUrl}/karte.php");

            var mainVill = AccountHelper.GetMainVillage(acc);

            var ajaxToken = await HttpHelper.GetAjaxToken(wb);

            var values = new Dictionary <string, string>
            {
                { "cmd", "mapPositionData" },
                { "data[x]", mainVill.Coordinates.x.ToString() },
                { "data[y]", mainVill.Coordinates.y.ToString() },
                { "data[zoomLevel]", "3" },
                { "ajaxToken", ajaxToken },
            };
            var content = new FormUrlEncodedContent(values);

            var resString = await HttpHelper.SendPostReq(acc, content, "/ajax.php?cmd=mapPositionData");

            var root = JsonConvert.DeserializeObject <MapPositionData.Root>(resString);

            if (!root.response.error)
            {
                var closesCoords = new Coordinates();
                var closest      = 1000.0;
                foreach (var tile in root.response.data.tiles)
                {
                    if (tile.c == null || !tile.c.StartsWith("{k.vt}"))
                    {
                        continue;
                    }

                    // Check if village type meets criteria
                    if (acc.NewVillages.Types.Count != 0)
                    {
                        var num  = (int)Parser.RemoveNonNumeric(tile.c.Split('f')[1]);
                        var type = (Classificator.VillTypeEnum)(++num);
                        if (!acc.NewVillages.Types.Any(x => x == type))
                        {
                            continue;
                        }
                    }

                    Coordinates coords = new Coordinates()
                    {
                        x = Int32.Parse(tile.x),
                        y = Int32.Parse(tile.y),
                    };
                    var distance = MapHelper.CalculateDistance(acc, mainVill.Coordinates, coords);
                    if (closest > distance)
                    {
                        closest      = distance;
                        closesCoords = coords;
                    }
                }

                //acc.Settings.new
                acc.NewVillages.Locations.Add(new Models.VillageModels.NewVillage()
                {
                    coordinates = closesCoords,
                    Name        = NewVillageHelper.GenerateName(acc),
                });
            }
            return(TaskRes.Executed);
        }