示例#1
0
        /// <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.PrintTeamStats();

                // 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");
            }
        }
示例#2
0
        public Stats(ErrorWriter ewriter)
        {
            errorwriter = ewriter;

            int index = 0;
            int tempindex = 0;
            int tableEnd = 0;

            // Initialize AlternateNames
            altSearcher = new AlternateNames(errorwriter);

            // Read Do Not Draft list
            try
            {
                StreamReader reader = new StreamReader(".//DoNotDraft.txt");
                do
                {
                    string temp = reader.ReadLine();
                    doNotDraftList.Add(temp.ToUpper());
                } while (!reader.EndOfStream);
            }
            catch (FileNotFoundException e)
            {
                errorwriter.Write("Could not find DoNotDraft.txt ( " + e.Message + ")");
            }
            catch (Exception e)
            {
                errorwriter.Write("Error making the do not draft list ( " + e.Message + ")");
            }

            // Read source for player and team lists
            WebFetch fetch = new WebFetch(FPLSTATS, errorwriter);
            string source = fetch.pageContent;

            // Locate start of team table
            index = source.IndexOf("<table class='highlight'>");

            // Locate end of team table
            tableEnd = source.IndexOf("</table>", index);

            // Get info for all teams
            index = source.IndexOf("http://www.teamliquid.net/tlpd/sc2-korean", index);
            do
            {
                try
                {
                    // Get team name
                    tempindex = source.IndexOf(">", index) + 1;
                    index = source.IndexOf("<", tempindex);
                    string name = source.Substring(tempindex, index - tempindex);

                    // Skip 15 columns
                    for (int i = 0; i < 15; i++)
                    {
                        index = source.IndexOf("<td", index) + 3;
                    }

                    // Read team cost
                    tempindex = source.IndexOf("<td", index) + 3;
                    tempindex = source.IndexOf(">", tempindex) + 1;
                    index = source.IndexOf("<", tempindex);
                    int cost = Convert.ToInt32(source.Substring(tempindex, index - tempindex));

                    teamList.Add(name.ToUpper(), new Team(name, cost, ARRAYSIZE, TOTALGAMES));
                }
                catch (Exception e)
                {
                    errorwriter.Write("Error reading team list (" + e.Message + ")");
                }
                finally
                {
                    index = source.IndexOf("http://www.teamliquid.net/tlpd/sc2-korean", index);
                }
            } while (index < tableEnd && index != -1);

            // Output log entry
            errorwriter.Write("Info for " + teamList.Count + " teams read");

            // Locate start of player table
            index = source.IndexOf("<table class='highlight'>", tableEnd);

            // Locate end of player table
            tableEnd = source.IndexOf("</table>", index);

            // Get info for all players
            index = source.IndexOf("<img src='/tlpd/images/", index);
            do
            {
                try
                {
                    // Get race
                    index += "<img src='/tlpd/images/".Length;
                    string race = source.Substring(index, 1).ToUpper();

                    // Get name
                    index = source.IndexOf("http://www.teamliquid.net/tlpd/sc2-korean", index);
                    tempindex = source.IndexOf(">", index) + 1;
                    index = source.IndexOf("<", tempindex);
                    string name = source.Substring(tempindex, index - tempindex);

                    // Get team
                    index = source.IndexOf("http://www.teamliquid.net/tlpd/sc2-korean", index);
                    tempindex = source.IndexOf(">", index) + 1;
                    index = source.IndexOf("<", tempindex);
                    string team = FullTeamName(source.Substring(tempindex, index - tempindex));

                    // Skip 12 columns
                    for (int i = 0; i < 12; i++)
                    {
                        index = source.IndexOf("<td", index) + 3;
                    }

                    // Read player cost
                    tempindex = source.IndexOf("<td", index) + 4;
                    tempindex = source.IndexOf(">", tempindex) + 1;
                    index = source.IndexOf("<", tempindex);
                    int cost = Convert.ToInt32(source.Substring(tempindex, index - tempindex));

                    playerList.Add(name.ToUpper(), new Player(name, cost, race, team, ARRAYSIZE, TOTALGAMES));
                }
                catch (Exception e)
                {
                    errorwriter.Write("Error reading player list (" + e.Message + ")");
                }
                finally
                {
                    index = source.IndexOf("<img src='/tlpd/images/", index);
                }
            } while (index < tableEnd && index != -1);

            // Output log entry
            errorwriter.Write("Info for " + playerList.Count + " players read");
        }
示例#3
0
        /// <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");
        }
示例#4
0
        /// <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);

                // 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.PrintTeamStats();

                // 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.");
            }
        }