// this is old and not used anymore
        public static void FarmMatchPage1(int matchId, int campaignId)
        {
            // get the webpage
            WebRequest webRequest = WebRequest.Create("http://stats.mmdl.org/index.php?view=match&matchid=" + matchId.ToString());
            WebResponse webResponse = webRequest.GetResponse();
            Stream responseStream = webResponse.GetResponseStream();
            StreamReader rReader = new StreamReader(responseStream);
            string sWebpageText = rReader.ReadToEnd();

            // make sure that the webpage contains the match table
            if (!sWebpageText.Contains(@"<table id=""match_table"">"))
                throw new InvalidOperationException();

            // html agility pack to parse
            var doc = new HtmlAgilityPack.HtmlDocument();
            doc.LoadHtml(sWebpageText);

            // get the match number
            var h3nodes = doc.DocumentNode.SelectNodes("//h3");
            var matchDesc = h3nodes[2].InnerHtml;

            // get the squads competing
            var h4nodes = doc.DocumentNode.SelectNodes("//h4");
            var squadDesc = h4nodes[0].InnerHtml;

            Match match = new Match(matchDesc, squadDesc, campaignId);

            // get the match table html
            var docMatchTable = doc.DocumentNode.SelectSingleNode("//table[@id='match_table']");
            doc.LoadHtml(docMatchTable.OuterHtml);

            var tableRows = docMatchTable.SelectNodes("//tr");

            Game game = new Game();
            GameType gameType = new GameType();
            int gameNumber = 1;
            bool isHomeWin;
            DartsPlayer awayPlayer, homePlayer;
            #if FALSE
            ValidateMatchTable(tableRows);
            #endif
            for (int i = 0; i < tableRows.ToList<HtmlAgilityPack.HtmlNode>().Count; i++)
            {
                if (tableRows[i].FirstChild.Name == "th" && tableRows[i].InnerText == "Totals")
                {
                    game.CalculateAndCommitGame(match);
                    break;
                }
                else if (tableRows[i].FirstChild.Name == "th")
                {
                    gameType = Game.GetGameTypeFromString(tableRows[i].InnerText);
                }
                else
                {
                    var x = tableRows[i].ChildNodes;
                    List<string> rowValues = new List<string>();

                    for (int j = 0; j < x.ToList<HtmlAgilityPack.HtmlNode>().Count; j++)
                    {
                        if (tableRows[i].ChildNodes[j].Name == "td")
                        {
                            rowValues.Add(tableRows[i].ChildNodes[j].InnerText);
                        }
                    }
                    //match.AwaySquad.DartsPlayers.Add("Paul Cedrone", DartsPlayer.GetPlayer(10408));
                    switch (rowValues.Count)
                    {
                        case 2:
                            if (rowValues[0] != "&nbsp;" && rowValues[0].Trim() != "")
                            {
                                if (!match.AwaySquad.DartsPlayers.ContainsKey(rowValues[0].ToString()))
                                {
                                    int n = DartsPlayer.CreateDummyPlayer(rowValues[0].ToString(), match.AwaySquad.SquadId, match.MatchId);
                                    match.AwaySquad.DartsPlayers.Add(DartsPlayer.GetPlayer(n).Name, DartsPlayer.GetPlayer(n));
                                }
                                if (!game.AwayDartsPlayers.Contains(match.AwaySquad.DartsPlayers[rowValues[0].ToString()]))
                                    game.AwayDartsPlayers.Add(match.AwaySquad.DartsPlayers[rowValues[0].ToString()]);
                            }
                            if (rowValues[1] != "&nbsp;" && rowValues[1].Trim() != "")
                            {
                                if (!match.HomeSquad.DartsPlayers.ContainsKey(rowValues[1].ToString()))
                                {
                                    int n = DartsPlayer.CreateDummyPlayer(rowValues[1].ToString(), match.HomeSquad.SquadId, match.MatchId);
                                    match.HomeSquad.DartsPlayers.Add(DartsPlayer.GetPlayer(n).Name, DartsPlayer.GetPlayer(n));
                                }
                                if (!game.HomeDartsPlayers.Contains(match.HomeSquad.DartsPlayers[rowValues[1].ToString()]))
                                game.HomeDartsPlayers.Add(match.HomeSquad.DartsPlayers[rowValues[1].ToString()]);
                            }
                            break;
                        case 5:
                            if (game.GameNumber != 0)
                            {
                                game.CalculateAndCommitGame(match); // john -
                            }

                            game = new Game();

                            game._GameType = gameType;
                            game.GameNumber = gameNumber; gameNumber++;
                            if (rowValues[1] != "&nbsp;" && rowValues[1].Trim() != "")
                            {
                                if (!match.AwaySquad.DartsPlayers.ContainsKey(rowValues[1].ToString()))
                                {
                                    int n = DartsPlayer.CreateDummyPlayer(rowValues[1].ToString(), match.AwaySquad.SquadId, match.MatchId);
                                    match.AwaySquad.DartsPlayers.Add(DartsPlayer.GetPlayer(n).Name, DartsPlayer.GetPlayer(n));
                                }
                                if (!game.AwayDartsPlayers.Contains(match.AwaySquad.DartsPlayers[rowValues[1].ToString()]))
                                    game.AwayDartsPlayers.Add(match.AwaySquad.DartsPlayers[rowValues[1].ToString()]);
                            }
                            game.IsHomeWin = (0 == Convert.ToInt32(rowValues[2]));
                            if (rowValues[3] != "&nbsp;" && rowValues[3].Trim() != "")
                            {
                                if (!match.HomeSquad.DartsPlayers.ContainsKey(rowValues[3].ToString()))
                                {
                                    int n = DartsPlayer.CreateDummyPlayer(rowValues[3].ToString(), match.HomeSquad.SquadId, match.MatchId);
                                    match.HomeSquad.DartsPlayers.Add(DartsPlayer.GetPlayer(n).Name, DartsPlayer.GetPlayer(n));
                                }
                                if (!game.HomeDartsPlayers.Contains(match.HomeSquad.DartsPlayers[rowValues[3].ToString()]))
                                    game.HomeDartsPlayers.Add(match.HomeSquad.DartsPlayers[rowValues[3].ToString()]);
                            }
                            //Game.InsertNew
                            break;
                        default:
                            throw (new InvalidOperationException("weird table structure man"));
                    }
                }
            }
        }
        public static void FarmGame(string matchDescription, string squadDescription, int gameNumber, 
            string _gameType, int _isHomeWin, string awayPlayers, string homePlayers)
        {
            int matchID = Convert.ToInt32(matchDescription.Replace("Match #", ""));

            if (arrBadMatches.Contains(matchID))
                return;

            Campaign campaign = Campaign.GetCampaignFromMatch(matchID);
            Match match = new Match(matchDescription, squadDescription, campaign.ID);
            Squad AwaySquad = new Squad(); Squad HomeSquad = new Squad();
            Squad.GetSquadsFromDesc(squadDescription, campaign, ref AwaySquad, ref HomeSquad);

            Game game = new Game();
            game._GameType = Game.GetGameTypeFromString(_gameType);
            game.IsHomeWin = _isHomeWin == 1;
            game.GameNumber = gameNumber;

            SqlConnection connSql = new SqlConnection(
                        Gravoc.Encryption.Encryption.Decrypt(Properties.Settings.Default.ConnectionString));
            connSql.Open();

            SqlCommand cmdSql = new SqlCommand("update match set awaySquad = @a, homeSquad = @h where id = @m", connSql);
            cmdSql.Parameters.AddWithValue("@m", matchID);
            cmdSql.Parameters.AddWithValue("@a", AwaySquad.SquadId);
            cmdSql.Parameters.AddWithValue("@h", HomeSquad.SquadId);

            cmdSql.ExecuteNonQuery();
            connSql.Close();

            awayPlayers.Split(new string[] {"|~|"}, StringSplitOptions.RemoveEmptyEntries);
            foreach (string playerName in awayPlayers.Split(new string[] { "|~|" }, StringSplitOptions.RemoveEmptyEntries))
            {
                if (!AwaySquad.DartsPlayers.ContainsKey(playerName))
                {
                    int n = DartsPlayer.CreateDummyPlayer(playerName, AwaySquad.SquadId, match.MatchId);
                    AwaySquad.DartsPlayers.Add(DartsPlayer.GetPlayer(n).Name, DartsPlayer.GetPlayer(n));
                }
                if (!game.AwayDartsPlayers.Contains(AwaySquad.DartsPlayers[playerName]))
                    game.AwayDartsPlayers.Add(AwaySquad.DartsPlayers[playerName]);
                else
                {
                    arrBadMatches.Add(match.MatchId);
                    match.MarkBadMatch();
                    return;
                }
            }

            foreach (string playerName in homePlayers.Split(new string[] { "|~|" }, StringSplitOptions.RemoveEmptyEntries))
            {
                if (!HomeSquad.DartsPlayers.ContainsKey(playerName))
                {
                    int n = DartsPlayer.CreateDummyPlayer(playerName, HomeSquad.SquadId, match.MatchId);
                    HomeSquad.DartsPlayers.Add(DartsPlayer.GetPlayer(n).Name, DartsPlayer.GetPlayer(n));
                }
                if (!game.HomeDartsPlayers.Contains(HomeSquad.DartsPlayers[playerName]))
                    game.HomeDartsPlayers.Add(HomeSquad.DartsPlayers[playerName]);
                else
                {
                    arrBadMatches.Add(match.MatchId);
                    match.MarkBadMatch();
                    return;
                }
            }

            game.CalculateAndCommitGame(match);
        }