/// <summary> /// Responds when the background worker is activated /// </summary> /// <param name="sender">Object calling this function</param> /// <param name="e">Event arguments</param> private void backgroundWorkerBruteForce_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; // Get HTML from Liquipedia backgroundWorkerBruteForce.ReportProgress(0, "Retrieving Liquipedia page"); WebFetch page = new WebFetch(textBoxURL.Text + "&action=edit", errorwriter); if (page.pageContent != string.Empty) { page.ReduceToWikiMarkup(); if (page.pageContent.IndexOf("#REDIRECT") != -1) { int start = page.pageContent.IndexOf("[[") + 2; int end = page.pageContent.IndexOf("]]"); page = new WebFetch(WIKIBASE + page.pageContent.Substring(start, end - start) + "&action=edit", errorwriter); page.ReduceToWikiMarkup(); } // Limit the number of weeks we consider int maxWeeks; bool result = int.TryParse(textBoxWeeks.Text, out maxWeeks); if (!result) { maxWeeks = 0; } // Read in data from the webpage backgroundWorkerBruteForce.ReportProgress(0, "Parsing webpage"); statistics.ParseMarkup(page.pageContent, maxWeeks); // Print all the data we've retrieved statistics.PrintPlayerStats(); statistics.PrintPlayerTrVal(); statistics.PrintTeamStats(); statistics.PrintPlayerTrVal(); // Limit the number of players we consider int maxPlayers; result = int.TryParse(textBoxNumPlayers.Text, out maxPlayers); if (!result) { maxPlayers = 0; } // Brute force the solution backgroundWorkerBruteForce.ReportProgress(0, "Brute forcing solution"); statistics.BruteForceMainNoTrades(maxPlayers); backgroundWorkerBruteForce.ReportProgress(0, "Solution complete"); } else { backgroundWorkerBruteForce.ReportProgress(0, "Couldn't retrieve Liquipedia page"); } }
/// <summary> /// Parses liquipedia match info for win/loss data /// </summary> /// <param name="input">Liquipedia markup</param> /// <param name="maximumWeeks">Maximum weeks to consider</param> public void ParseMarkup(string input, int maximumWeeks) { int index = 0; int tempindex = 0; int week = 0; int match = 0; maxWeeks = maximumWeeks; if (maximumWeeks <= 0 || maximumWeeks > ARRAYSIZE) { maximumWeeks = ARRAYSIZE; } // Loop through markup looking for match data index = input.IndexOf("HiddenSort|Round", index); while (index != -1) { try { // Get the week number index = input.IndexOf("W", index) + 1; week = Convert.ToInt32((input.Substring(index, 1))); if (week > maximumWeeks) { index = input.IndexOf("HiddenSort|Round", index); continue; } // Get the match number index = input.IndexOf("- Match ", index) + "- Match ".Length; match = Convert.ToInt32((input.Substring(index, 1))); string boxData; // If "TeamMatch" isn't found within 20 characters, // there is a good chance that the infobox is stored on a different page // We need to retrieve that box if (input.IndexOf("TeamMatch", index) - index > 20 || input.IndexOf("TeamMatch", index) < 0) { index = input.IndexOf("{{:", index); tempindex = input.IndexOf("\n", index); string url = input.Substring(index, tempindex - index); url = url.Replace("{{:", string.Empty); url = url.Replace("}}", string.Empty); WebFetch boxFetch = new WebFetch(WIKIBASE + url + "&action=edit", errorwriter); boxFetch.ReduceToWikiMarkup(); boxData = boxFetch.pageContent; } else { index = input.IndexOf("TeamMatch", index); boxData = input.Substring(index, input.IndexOf("}}", index) - index); } boxParser(boxData, week); index = input.IndexOf("HiddenSort|Round", index); } catch (Exception e) { errorwriter.Write("Error during markup parsing (" + e.Message + ")"); } } errorwriter.Write("Parse complete"); }
/// <summary> /// Responds when buttonParse is clicked /// </summary> /// <param name="sender">Object calling this function</param> /// <param name="e">Event arguments</param> private void buttonParse_Click(object sender, EventArgs e) { // Get HTML from Liquipedia toolStripStatusLabel.Text = "Retrieving Liquipedia page"; WebFetch page = new WebFetch(textBoxURL.Text + "&action=edit", errorwriter); if (page.pageContent != string.Empty) { page.ReduceToWikiMarkup(); if (page.pageContent.IndexOf("#REDIRECT") != -1) { int start = page.pageContent.IndexOf("[[") + 2; int end = page.pageContent.IndexOf("]]"); page = new WebFetch(WIKIBASE + page.pageContent.Substring(start, end - start) + "&action=edit", errorwriter); page.ReduceToWikiMarkup(); } Stats statistics = new Stats(errorwriter, Convert.ToDouble(textBoxPointMod.Text)); // Limit the number of weeks we consider int maxWeeks; bool result = int.TryParse(textBoxWeeks.Text, out maxWeeks); if (!result) { maxWeeks = 0; } // Read in data from the webpage toolStripStatusLabel.Text = "Parsing webpage"; statistics.ParseMarkup(page.pageContent, maxWeeks); // Print all the data we've retrieved statistics.PrintPlayerStats(); statistics.PrintPlayerTrVal(); statistics.PrintTeamStats(); statistics.PrintTeamTrVal(); // Calculate the best team toolStripStatusLabel.Text = "Calculating best main team"; statistics.BestMainWithoutTrades(); toolStripStatusLabel.Text = "Solution complete"; } else { toolStripStatusLabel.Text = "Error retrieving Liquipedia page"; MessageBox.Show("Could not retrieve page. Check your connection."); } }