private void button_parseStage_Click(object sender, RoutedEventArgs e) { // TODO: move all parsing code to an external module // TODO: input format should not be hardcoded // get text from textbox var lineCount = textBox_resultsInput.LineCount; if (lineCount < 1) { return; } var lines = textBox_resultsInput.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None); // parse into stage data var stageData = new Stage(); char[] separator = { '\t' }; foreach (var line in lines) { var splitLine = line.Split(separator, StringSplitOptions.RemoveEmptyEntries); var splitCount = splitLine.Count(); for (int i = 0; i < splitCount; i++) { splitLine[i] = splitLine[i].Trim(); } if (splitCount == 5) { stageData.AddDriver(new DriverTime(Int32.Parse(splitLine[0]), 0, "", splitLine[1], splitLine[2], splitLine[3], splitLine[4])); } else if (splitCount == 6) { stageData.AddDriver(new DriverTime(Int32.Parse(splitLine[0]), 0, "", splitLine[2], splitLine[3], splitLine[4], splitLine[5], splitLine[1])); } else if (splitCount == 7) { stageData.AddDriver(new DriverTime(Int32.Parse(splitLine[0]), 0, "", splitLine[3], splitLine[4], splitLine[5], splitLine[6], splitLine[2])); } else { label_statusMessage.Content = "Parse failure"; label_statusMessage.Foreground = Brushes.Red; return; } } rallyData.AddStage(stageData); textBox_resultsInput.Clear(); label_statusMessage.Content = String.Format("SS{0} Has been parsed.", rallyData.StageCount); label_statusMessage.Foreground = Brushes.Green; }
/// <summary> /// Creates the URIs and parses responses into Rally objects /// </summary> public Rally GetRallyData(string eventId, IProgress <int> progress) { try { var uriBuilder = new UriBuilder(ApiURL); var query = HttpUtility.ParseQueryString(uriBuilder.Query); query["eventId"] = eventId; query["noCache"] = DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds.ToString(); // get the overall results and stage count query["page"] = "1"; query["stageId"] = "0"; // overall results uriBuilder.Query = query.ToString(); var requestUri = uriBuilder.ToString(); var rallyDataResult = ExecuteRequest(requestUri).Result; var totalStages = rallyDataResult.TotalStages; var rallyData = new Rally(); // for each stage for (int i = 1; i <= totalStages; i++) { var currentPage = 1; var totalPages = 99; var leaderboardTotal = 99; var stageData = new Stage(); // for each page in stage var retryCount = 0; while (currentPage <= totalPages) { // return what we have, the rally may not have been finished yet // todo: status messaging? if (retryCount >= 10) { return(rallyData); } query["page"] = currentPage.ToString(); query["stageId"] = i.ToString(); // stage number uriBuilder.Query = query.ToString(); requestUri = uriBuilder.ToString(); rallyDataResult = ExecuteRequest(requestUri).Result; // sometimes we don't get entries, if so, retry the request (this might be fixed by storing cookies) if (rallyDataResult == null || rallyDataResult.Entries == null || rallyDataResult.Entries.Count < 1) { retryCount++; continue; } leaderboardTotal = rallyDataResult.LeaderboardTotal; totalPages = rallyDataResult.Pages; if (totalPages == 0 || leaderboardTotal == 0) { break; } // for each driver in page foreach (var entry in rallyDataResult.Entries) { stageData.AddDriver(new DriverTime(entry.Position, entry.PlayerId, entry.Name, entry.VehicleName, entry.Time, entry.DiffFirst)); } // increment page currentPage++; } if (stageData.Count != leaderboardTotal) { throw new Exception("Racenet data incomplete for stage " + i); } rallyData.AddStage(stageData); progress.Report(rallyData.StageCount); } return(rallyData); } catch (Exception) { return(null); } }
/// <summary> /// Creates the URIs and parses responses into Rally objects /// </summary> public Rally GetRallyData(string leagueUrl, string eventId, IProgress <int> progress) { try { // the uri might be able to be null for PC leagues, but is required for console leagues // this sets the session state on the server and stores the cookie with the ASP.NET session ID for later if (!string.IsNullOrWhiteSpace(leagueUrl)) { var mainPageResult = httpClient.GetAsync(leagueUrl).Result; if (mainPageResult == null || mainPageResult.StatusCode != HttpStatusCode.OK) { throw new Exception("Failed to get data from main league page, error: " + mainPageResult.StatusCode); } } // example string for reference //string apiUrl = "https://www.dirtgame.com/uk/api/event?assists=any&eventId=95576&group=all&leaderboard=true&nameSearch=&noCache=1463699433315&number=10&page=1&stageId=0&wheel=any"; string apiUrl = "https://www.dirtgame.com/api/event?assists=any&group=all&leaderboard=true&nameSearch=&number=10&wheel=any"; var uriBuilder = new UriBuilder(apiUrl); var query = HttpUtility.ParseQueryString(uriBuilder.Query); query["eventId"] = eventId; query["noCache"] = DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds.ToString(); // get the overall results and stage count query["page"] = "1"; query["stageId"] = "0"; // overall results uriBuilder.Query = query.ToString(); var requestUri = uriBuilder.ToString(); var rallyDataResult = ExecuteRequest(requestUri).Result; var totalStages = rallyDataResult.TotalStages; var rallyData = new Rally(); // for each stage for (int i = 1; i <= totalStages; i++) { var currentPage = 1; var totalPages = 99; var leaderboardTotal = 99; var stageData = new Stage(); // for each page in stage var retryCount = 0; while (currentPage <= totalPages) { // return what we have, the rally may not have been finished yet // todo: status messaging? if (retryCount >= 10) { return(rallyData); } query["page"] = currentPage.ToString(); query["stageId"] = i.ToString(); // stage number uriBuilder.Query = query.ToString(); requestUri = uriBuilder.ToString(); rallyDataResult = ExecuteRequest(requestUri).Result; // sometimes we don't get entries, if so, retry the request (this might be fixed by storing cookies) if (rallyDataResult == null || rallyDataResult.Entries == null || rallyDataResult.Entries.Count < 1) { retryCount++; continue; } leaderboardTotal = rallyDataResult.LeaderboardTotal; totalPages = rallyDataResult.Pages; if (totalPages == 0 || leaderboardTotal == 0) { break; } // for each driver in page foreach (var entry in rallyDataResult.Entries) { stageData.AddDriver(new DriverTime(entry.Position, entry.PlayerId, entry.Name, entry.ProfileUrl, entry.VehicleName, entry.Time, entry.DiffFirst)); } // increment page currentPage++; } if (stageData.Count != leaderboardTotal) { throw new Exception("Racenet data incomplete for stage " + i); } rallyData.AddStage(stageData); progress.Report(rallyData.StageCount); } return(rallyData); } catch (Exception) { return(null); } }