private static void UpdateCurrentlyBuilding(Account acc, Village vill) { vill.Build.CurrentlyBuilding.Clear(); var cb = InfrastructureParser.CurrentlyBuilding(acc.Wb.Html, acc); if (cb == null) { return; // Nothing is currently building } var bldJson = DriverHelper.GetJsObj <string>(acc, "JSON.stringify(bld);"); if (string.IsNullOrEmpty(bldJson)) { return; } var bldJs = JsonConvert.DeserializeObject <List <Bld> >(bldJson); // Combine data from two sources about currently building (JS object and HTML table) // We get time duration and level from HTML // and build location, level and building (type) from JSON for (int i = 0; i < cb.Count; i++) { cb[i].Building = bldJs[i].Building; cb[i].Location = bldJs[i].Location; cb[i].Level = (byte)bldJs[i].Level; vill.Build.CurrentlyBuilding.Add(cb[i]); } }
/// <summary> /// Sends HTTP request to the server and gets number of animals inside the oasis /// </summary> public static int[] GetOasisAnimals(Account acc, Coordinates oasis) { var htmlDoc = new HtmlAgilityPack.HtmlDocument(); string html = ""; switch (acc.AccInfo.ServerVersion) { case Classificator.ServerVersionEnum.T4_4: var ajaxToken = DriverHelper.GetJsObj <string>(acc, "ajaxToken"); var req = new RestSharp.RestRequest { Resource = "/ajax.php?cmd=viewTileDetails", Method = Method.POST, }; req.AddParameter("cmd", "viewTileDetails"); req.AddParameter("x", oasis.x.ToString()); req.AddParameter("y", oasis.y.ToString()); req.AddParameter("ajaxToken", ajaxToken); var resString = HttpHelper.SendPostReq(acc, req); var root = JsonConvert.DeserializeObject <TileDetailsT4_4>(resString); if (root.response.error) { throw new Exception("Unable to get T4.4 tile details!\n" + root.response.error); } html = WebUtility.HtmlDecode(root.response.data.html); break; case Classificator.ServerVersionEnum.T4_5: var bearerToken = DriverHelper.GetBearerToken(acc); var reqMapInfo = new RestSharp.RestRequest { Resource = "/api/v1/ajax/viewTileDetails", Method = Method.POST, RequestFormat = DataFormat.Json }; reqMapInfo.AddHeader("authorization", $"Bearer {bearerToken}"); reqMapInfo.AddHeader("content-type", "application/json; charset=UTF-8"); reqMapInfo.AddJsonBody(oasis); var tileDetails = HttpHelper.SendPostReq(acc, reqMapInfo); var tile = JsonConvert.DeserializeObject <TileDetailsT4_5>(tileDetails); html = WebUtility.HtmlDecode(tile.html); break; } htmlDoc.LoadHtml(html); return(TroopsParser.GetOasisAnimals(htmlDoc)); }
public override async Task <TaskRes> Execute(Account acc) { await acc.Wb.Navigate($"{acc.AccInfo.ServerUrl}/dorf2.php"); // Get Map size var size = DriverHelper.GetJsObj <long>(acc, "window.TravianDefaults.Map.Size.top"); acc.AccInfo.MapSize = (int)size; // Get server speed var speed = DriverHelper.GetJsObj <long>(acc, "Travian.Game.speed"); acc.AccInfo.ServerSpeed = (int)speed; return(TaskRes.Executed); }
public override async Task <TaskRes> Execute(Account acc) { await acc.Wb.Navigate($"{acc.AccInfo.ServerUrl}/dorf2.php"); // Get Map size var size = DriverHelper.GetJsObj <long>(acc, "window.TravianDefaults.Map.Size.top"); acc.AccInfo.MapSize = (int)size; // Get server speed var speed = DriverHelper.GetJsObj <long>(acc, "Travian.Game.speed"); acc.AccInfo.ServerSpeed = (int)speed; // Get server version acc.AccInfo.ServerVersion = (acc.Wb.Html.GetElementbyId("sidebarBoxDailyquests") == null ? Classificator.ServerVersionEnum.T4_5 : Classificator.ServerVersionEnum.T4_4); return(TaskRes.Executed); }
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); }
/// <summary> /// Send raw HTTP request to the server and request the map tiles around the coords. This mimics browser on the map page. /// </summary> public static List <MapTile> GetMapTiles(Account acc, Coordinates coords) { switch (acc.AccInfo.ServerVersion) { case Classificator.ServerVersionEnum.T4_4: var ajaxToken = DriverHelper.GetJsObj <string>(acc, "ajaxToken"); var req = new RestSharp.RestRequest { Resource = "/ajax.php?cmd=mapPositionData", Method = Method.POST, }; req.AddParameter("cmd", "mapPositionData"); req.AddParameter("data[x]", coords.x.ToString()); req.AddParameter("data[y]", coords.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); } return(root.response.data.tiles.Select(x => x.GetMapTile()).ToList()); case Classificator.ServerVersionEnum.T4_5: var bearerToken = DriverHelper.GetBearerToken(acc); var reqMapInfo = new RestSharp.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(coords)); var mapInfoRes = HttpHelper.SendPostReq(acc, reqMapInfo); var mapPosition = new SendMapPositionT4_5.Root() { data = new SendMapPositionT4_5.Data() { x = coords.x, y = coords.y, zoomLevel = 3, ignorePositions = new List <object>() } }; var reqMapPosition = new RestSharp.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); var mapPositionRes = HttpHelper.SendPostReq(acc, reqMapPosition); var mapPositionData = JsonConvert.DeserializeObject <MapPositionDataT4_5>(mapPositionRes); return(mapPositionData.tiles.Select(x => x.GetMapTile()).ToList()); } return(null); }