public void ClearRankings() { if (ncaa != null) { foreach (Team team in ncaa.GetFBS()) { team.Clear(); team.ClearProjectedPoints(); } foreach (Team team in ncaa.GetFCS()) { team.Clear(); } foreach (Conference conference in ncaa.GetConferences()) { conference.rating = 0; conference.ClearMembers(); } } uxRankings.Items.Clear(); }
/// <summary> /// Calculates each team's opponent records and total points scored/allowed and each team's /// initial rating /// </summary> private void CalculateRankings() { double low = int.MaxValue; foreach (Team team in ncaa.GetFBS()) { foreach (Team opponent in team.GetSchedule()) { team.IncreaseOpponentWins(opponent.GetWins()); team.IncreaseOpponentLosses(opponent.GetLosses()); team.IncreaseOpponentTotalPoints(Convert.ToInt32(opponent.GetTotalPoints())); team.IncreaseOpponentTotalPointsAllowed(Convert.ToInt32(opponent.GetTotalPointsAllowed())); } team.rating += (team.GetPPGvsOppAvg() - team.GetDefensePPGvsOppAvg()) * 5; if (team.rating < low) { low = team.rating; } } foreach (Team team in ncaa.GetFBS()) { team.rating += (0 - (low - 1)); if (team.GetWinPercentage() > 0) { team.rating *= Math.Pow(team.GetWinPercentage(), 2); } else { team.rating *= Math.Pow(.001, 2); } team.rating += 5; if (team.GetOpponentWinPercentage() > 0) { team.rating *= team.GetOpponentWinPercentage(); } else { team.rating *= .001; } DivisionI.Add(team); } foreach (Team team in ncaa.GetFCS()) { foreach (Team opponent in team.GetSchedule()) { team.IncreaseOpponentWins(opponent.GetWins()); team.IncreaseOpponentLosses(opponent.GetLosses()); team.IncreaseOpponentTotalPoints(Convert.ToInt32(opponent.GetTotalPoints())); team.IncreaseOpponentTotalPointsAllowed(Convert.ToInt32(opponent.GetTotalPointsAllowed())); } team.rating += (team.GetPPGvsOppAvg() - team.GetDefensePPGvsOppAvg()) * (5 / 2); if (team.rating < low) { low = team.rating; } } foreach (Team team in ncaa.GetFCS()) { team.rating += (0 - (low - 1)); if (team.GetWinPercentage() > 0) { team.rating *= Math.Pow(team.GetWinPercentage(), 2); } else { team.rating *= Math.Pow(.001, 2); } team.rating += 5; if (team.GetOpponentWinPercentage() > 0) { team.rating *= team.GetOpponentWinPercentage(); } else { team.rating *= .001; } DivisionI.Add(team); } SortRankings(DivisionI); }
/// <summary> /// Takes the rankings and writes them to a text file and sends that file /// as an attachment in an email. Also writes them to an excel file and /// attaches that to the email as well /// </summary> public void ExportRankings() { StringBuilder text = new StringBuilder(); int i = 1; List <Team> FBSRankings = ncaa.GetFBS().OrderBy(o => o.rating).Reverse().ToList(); List <Team> FCSRankings = ncaa.GetFCS().OrderBy(o => o.rating).Reverse().ToList(); Application xlApp = new Application(); Workbook xlWorkBook = xlApp.Workbooks.Add(); Worksheet FBSRankSheet = (Worksheet)xlWorkBook.Worksheets.get_Item(1); Worksheet FCSRankSheet = (Worksheet)xlWorkBook.Worksheets.get_Item(2); Worksheet SOSRankSheet = (Worksheet)xlWorkBook.Worksheets.get_Item(3); FBSRankSheet.Columns[1].ColumnWidth = 5; FBSRankSheet.Columns[2].ColumnWidth = 20; FBSRankSheet.Cells[1, 2] = "Rankings (" + DateTime.Today.Date.Month + "/" + DateTime.Today.Date.Day + "/" + DateTime.Today.Date.Year + ")"; int rank_index = 1; foreach (Team team in FBSRankings) { FBSRankSheet.Cells[rank_index + 1, 1] = rank_index; FBSRankSheet.Cells[rank_index + 1, 2] = team.GetTeamName(); FBSRankSheet.Cells[rank_index + 1, 3] = String.Format("{0:0.000}", team.rating); rank_index++; } FCSRankSheet.Columns[1].ColumnWidth = 5; FCSRankSheet.Columns[2].ColumnWidth = 20; FCSRankSheet.Cells[1, 2] = "Rankings (" + DateTime.Today.Date.Month + "/" + DateTime.Today.Date.Day + "/" + DateTime.Today.Date.Year + ")"; rank_index = 1; foreach (Team team in FCSRankings) { FCSRankSheet.Cells[rank_index + 1, 1] = rank_index; FCSRankSheet.Cells[rank_index + 1, 2] = team.GetTeamName(); FCSRankSheet.Cells[rank_index + 1, 3] = String.Format("{0:0.000}", team.rating); rank_index++; } SOSRankSheet.Columns[1].ColumnWidth = 5; SOSRankSheet.Columns[2].ColumnWidth = 20; SOSRankSheet.Cells[1, 2] = "Strength of Schedule (" + DateTime.Today.Date.Month + "/" + DateTime.Today.Date.Day + "/" + DateTime.Today.Date.Year + ")"; rank_index = 1; List <Team> SOS = ncaa.GetFBS().OrderBy(o => o.GetStrength()).ToList(); foreach (Team team in SOS) { SOSRankSheet.Cells[rank_index + 1, 1] = rank_index; SOSRankSheet.Cells[rank_index + 1, 2] = team.GetTeamName(); rank_index++; } xlApp.DisplayAlerts = false; FBSRankSheet.Name = "FBS Rankings"; FCSRankSheet.Name = "FCS Rankings"; SOSRankSheet.Name = "SOS Rank"; xlWorkBook.SaveAs("Rankings.xlsx"); xlWorkBook.Close(); foreach (Team team in FBSRankings) { if (team.GetFBSRank() < 100 && team.GetTeamName().Length < 4) { text.Append(i + ". " + team.GetTeamName() + "\t\t\t\t" + team.rating + System.Environment.NewLine); i++; } else if (team.GetFBSRank() < 100 && team.GetTeamName().Length < 12) { text.Append(i + ". " + team.GetTeamName() + "\t\t\t" + team.rating + System.Environment.NewLine); i++; } else if (team.GetFBSRank() < 100 && team.GetTeamName().Length < 20) { text.Append(i + ". " + team.GetTeamName() + "\t\t" + team.rating + System.Environment.NewLine); i++; } else if (team.GetFBSRank() < 10 && team.GetTeamName().Length < 13) { text.Append(i + ". " + team.GetTeamName() + "\t\t\t" + team.rating + System.Environment.NewLine); i++; } else if (team.GetFBSRank() < 100 && team.GetTeamName().Length < 12) { text.Append(i + ". " + team.GetTeamName() + "\t\t\t" + team.rating + System.Environment.NewLine); i++; } else if (team.GetFBSRank() > 99 && team.GetTeamName().Length < 11) { text.Append(i + ". " + team.GetTeamName() + "\t\t\t" + team.rating + System.Environment.NewLine); i++; } else if (team.GetFBSRank() > 99 && team.GetTeamName().Length < 20) { text.Append(i + ". " + team.GetTeamName() + "\t\t" + team.rating + System.Environment.NewLine); i++; } else { text.Append(i + ". " + team.GetTeamName() + "\t" + team.rating + System.Environment.NewLine); i++; } } using (StreamWriter sw = new StreamWriter(scoresPath + "Rankings" + year + ".txt")) { sw.Write(text); } StreamReader sr = new StreamReader(auth); string password = sr.ReadLine(); sr.Close(); password = password.ToLower(); password = password.Remove(10, 3); MailMessage mail = new MailMessage(); SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"); mail.From = new MailAddress("*****@*****.**"); mail.To.Add("*****@*****.**"); mail.Subject = "Rankings"; mail.Body = "This weeks CFB rankings"; Attachment txtfile, xlfile; txtfile = new Attachment(scoresPath + "Rankings" + year + ".txt"); xlfile = new Attachment(excelPath + "Rankings.xlsx"); mail.Attachments.Add(txtfile); mail.Attachments.Add(xlfile); var smtp = new SmtpClient { Host = "smtp.gmail.com", Port = 587, EnableSsl = true, DeliveryMethod = SmtpDeliveryMethod.Network, UseDefaultCredentials = false, Credentials = new NetworkCredential("*****@*****.**", password), Timeout = 2000000, }; smtp.Send(mail); txtfile.Dispose(); xlfile.Dispose(); }
/// <summary> /// Adds scores posted on the internet to a text file /// </summary> /// <param name="ncaa">list of all teams</param> /// <param name="week">week of scores to add</param> /// <returns>the message to display to the user based on the result</returns> public static string AddScores(NCAA ncaa, string week) { WebClient client = new WebClient(); Team team1 = null, team2 = null; string ID, team1ID, team2ID, team1Score = null, team2Score = null; string fcsdstring = client.DownloadString("http://stats.washingtonpost.com/cfb/scoreboard.asp?conf=fcs%3A-2&week=" + week); string dstring = client.DownloadString("http://stats.washingtonpost.com/cfb/scoreboard.asp?conf=-1&week=" + week); string[] sep = new string[] { "team=" }; string[] sep2 = new string[] { "10%" }; string[] ScoreString, ScoreString1, ScoreString2; string[] firstFCSSep = new string[] { "GAMEZONELINKSTART" }; string[] parsed = dstring.Split(sep, StringSplitOptions.None); string[] fcsGames = fcsdstring.Split(firstFCSSep, StringSplitOptions.None); string[,] fcsTeams = new string[200, 3]; string[] teams; List <string> games = new List <string>(); StreamReader sr = new StreamReader(SCORES_PATH + "collegefootballscores2017.txt"); using (sr) { while (!sr.EndOfStream) { games.Add(sr.ReadLine()); } } string ID1 = "", ID2 = ""; for (int i = 1; i < parsed.Length; i++) { int p = parsed[i].IndexOf(@">"); ID = parsed[i].Substring(0, p); string checkID = ID; checkID = checkID.Remove(checkID.Length - 1); ScoreString = parsed[i].Split(sep2, StringSplitOptions.None); string score; if (checkID != ID1 && checkID != ID2) { try { score = ScoreString[ScoreString.Length - 1].Remove(0, 2); } catch { return("Week has not concluded"); } if (Char.IsNumber(score[2])) { score = score.Remove(3, score.Length - 3); } else if (Char.IsNumber(score[1])) { score = score.Remove(2, score.Length - 2); } else { score = score.Remove(1, score.Length - 1); } int index = 0; foreach (char c in ID) { if (!Char.IsNumber(c)) { ID = ID.Remove(index); } index++; } if (team1 == null) { foreach (Team fbsteam in ncaa.GetFBS()) { if (fbsteam.GetTeamID().Equals(ID)) { ID1 = ID; team1 = fbsteam; team1Score = score; } } if (team1 == null) { foreach (Team fcsteam in ncaa.GetFCS()) { if (fcsteam.GetTeamID().Equals(ID)) { ID1 = ID; team1 = fcsteam; team1Score = score; } } } } else { foreach (Team team in ncaa.GetFBS()) { if (team.GetTeamID().Equals(ID)) { ID2 = ID; team2 = team; team2Score = score; games.Add(team1.GetTeamName() + "-" + team1Score + "-" + team2.GetTeamName() + "-" + team2Score); team1 = null; team2 = null; } } } } } //begin fcs code if (week != "18" && week != "19") { try { int ind = 0; string checkID1 = "", checkID2 = ""; for (int i = 0; i < fcsGames.Length; i++) { teams = fcsGames[i].Split(sep, StringSplitOptions.None); int p = teams[teams.Length - 2].IndexOf(@">"); team1ID = teams[teams.Length - 2].Substring(0, p); int index = 0; foreach (char c in team1ID) { if (!Char.IsNumber(c)) { team1ID = team1ID.Remove(index); } index++; } p = teams[teams.Length - 1].IndexOf(@">"); team2ID = teams[teams.Length - 1].Substring(0, p); team2ID = team2ID.Remove(team2ID.Length - 1); if (checkID1 != team1ID && checkID2 != team2ID) { if (checkID1 != team2ID && checkID2 != team1ID) { fcsTeams[ind, 0] = teams[0]; fcsTeams[ind, 1] = teams[teams.Length - 2]; fcsTeams[ind, 2] = teams[teams.Length - 1]; checkID1 = team1ID; checkID2 = team2ID; ind++; } } } } catch { return(Messages.WEEK_NOT_CONCLUDED_MESSAGE); } for (int i = 0; i < fcsGames.Length; i++) { if (fcsTeams[i, 2] != null) { int p = fcsTeams[i, 1].IndexOf(@">"); team1ID = fcsTeams[i, 1].Substring(0, p); int index = 0; foreach (char c in team1ID) { if (!Char.IsNumber(c)) { team1ID = team1ID.Remove(index); } index++; } p = fcsTeams[i, 2].IndexOf(@">"); team2ID = fcsTeams[i, 2].Substring(0, p); index = 0; foreach (char c in team2ID) { if (!Char.IsNumber(c)) { team2ID = team2ID.Remove(index); } index++; } ScoreString1 = fcsTeams[i, 1].Split(sep2, StringSplitOptions.None); ScoreString2 = fcsTeams[i, 2].Split(sep2, StringSplitOptions.None); string score1 = ScoreString1[ScoreString1.Length - 1].Remove(0, 2); string score2 = ScoreString2[ScoreString2.Length - 1].Remove(0, 2); if (Char.IsNumber(score1[2])) { score1 = score1.Remove(3, score1.Length - 3); } else if (Char.IsNumber(score1[1])) { score1 = score1.Remove(2, score1.Length - 2); } else { score1 = score1.Remove(1, score1.Length - 1); } if (Char.IsNumber(score2[2])) { score2 = score2.Remove(3, score2.Length - 3); } else if (Char.IsNumber(score2[1])) { score2 = score2.Remove(2, score2.Length - 2); } else { score2 = score2.Remove(1, score2.Length - 1); } foreach (Team fcsTeam in ncaa.GetFCS()) { if (fcsTeam.GetTeamID().Equals(team1ID)) { team1 = fcsTeam; } else if (fcsTeam.GetTeamID().Equals(team2ID)) { team2 = fcsTeam; } } if (team1 != null && team2 != null) { games.Add(team1.GetTeamName() + "-" + score1 + "-" + team2.GetTeamName() + "-" + score2); team1 = null; team2 = null; } else if (team1 == null || team2 == null) { team1 = null; team2 = null; } } } } //end fcs code //write to file here StreamWriter sw = new StreamWriter(SCORES_PATH + "collegefootballscores2017.txt"); using (sw) { foreach (string s in games) { sw.WriteLine(s); } } return(Messages.SCORES_ADDED_MESSAGE); }
public void TestFCSTeamCount() { Assert.AreEqual(124, sut.GetFCS().Count); }
/// <summary> /// Simulates the games scheduled for the week passed in /// </summary> /// <param name="ncaa">list of all teams</param> /// <param name="week">the week to simulate</param> /// <returns>a sucess message or the error message if there is one</returns> public static string SimWeek(NCAA ncaa, string week) { WebClient client = new WebClient(); Team team1 = null, team2 = null; string ID, team1ID, team2ID; string fcsdstring = client.DownloadString("http://stats.washingtonpost.com/cfb/scoreboard.asp?conf=fcs%3A-2&week=" + week); string dstring = client.DownloadString("http://stats.washingtonpost.com/cfb/scoreboard.asp?conf=-1&week=" + week); string[] sep = new string[] { "team=" }; string[] firstFCSSep = new string[] { "GAMEZONELINKSTART" }; string[] parsed = dstring.Split(sep, StringSplitOptions.None); string[] fcsGames = fcsdstring.Split(firstFCSSep, StringSplitOptions.None); string[] teams = null; string[,] fcsTeams = new string[200, 3]; for (int i = 1; i < parsed.Length; i++) { int p = parsed[i].IndexOf(@">"); ID = parsed[i].Substring(0, p); int index = 0; foreach (char c in ID) { if (!Char.IsNumber(c)) { ID = ID.Remove(index); } index++; } if (i % 2 != 0) { foreach (Team fbsteam in ncaa.GetFBS()) { if (fbsteam.GetTeamID().Equals(ID)) { team1 = fbsteam; } } if (team1 == null) { foreach (Team fcsteam in ncaa.GetFCS()) { if (fcsteam.GetTeamID().Equals(ID)) { team1 = fcsteam; } } } } else { foreach (Team team in ncaa.GetFBS()) { if (team.GetTeamID().Equals(ID)) { team2 = team; SimGame(team1, team2); team1 = null; team2 = null; } } } } //begin fcs try { for (int i = 0; i < fcsGames.Length; i++) { teams = fcsGames[i].Split(sep, StringSplitOptions.None); for (int j = 0; j < teams.Length; j++) { fcsTeams[i, j] = teams[j]; } } } catch { ClearWeek(); return("Week has already finished or is in progress."); } for (int i = 0; i < fcsGames.Length; i++) { if (fcsTeams[i, 2] != null) { int p = fcsTeams[i, 1].IndexOf(@">"); team1ID = fcsTeams[i, 1].Substring(0, p); int index = 0; foreach (char c in team1ID) { if (!Char.IsNumber(c)) { team1ID = team1ID.Remove(index); } index++; } p = fcsTeams[i, 2].IndexOf(@">"); team2ID = fcsTeams[i, 2].Substring(0, p); index = 0; foreach (char c in team2ID) { if (!Char.IsNumber(c)) { team2ID = team2ID.Remove(index); } index++; } foreach (Team fcsTeam in ncaa.GetFCS()) { if (fcsTeam.GetTeamID().Equals(team1ID)) { team1 = fcsTeam; } else if (fcsTeam.GetTeamID().Equals(team2ID)) { team2 = fcsTeam; } } if (team1 != null && team2 != null) { SimGame(team1, team2); team1 = null; team2 = null; } } } //end fcs code return("Success"); }
/// <summary> /// Writes the simulated scores to a Microsoft Excel file /// </summary> /// <param name="ncaa">list of all teams</param> /// <param name="week">week that was simulated</param> public static void WriteSimmedScores(NCAA ncaa, int week) { string fileName = scoresPath + "SimmedWeek.txt"; string xlfileName = excelPath + "SimmedWeek.xlsx"; StringBuilder scores = new StringBuilder(); Application xlApp = new Application(); Workbook xlWorkBook = xlApp.Workbooks.Add(); Worksheet xlWorkSheet = (Worksheet)xlWorkBook.Worksheets.get_Item(1); xlWorkSheet.Columns[1].ColumnWidth = 23; xlWorkSheet.Columns[2].ColumnWidth = 4; xlWorkSheet.Columns[3].ColumnWidth = 4; xlWorkSheet.Columns[4].ColumnWidth = 23; xlWorkSheet.Cells[1, 1] = "Week " + Convert.ToString(week); List <Team> FBSRankings = ncaa.GetFBS().OrderBy(o => o.rating).Reverse().ToList(); List <Team> FCSRankings = ncaa.GetFCS().OrderBy(o => o.rating).Reverse().ToList(); int k = 1; foreach (Team team in FBSRankings) { team.SetFBSRank(k); k++; } foreach (Team team in FCSRankings) { team.SetFBSRank(FBSRankings.Count + 2); } int i = 2; foreach (Game g in Week) { scores.Append(g.GetAwayTeam().GetTeamName() + ": " + g.GetAwayScore() + " "); scores.AppendLine(g.GetHomeTeam().GetTeamName() + ": " + g.GetHomeScore()); if (g.GetAwayTeam().GetFBSRank() <= 25) { xlWorkSheet.Cells[i, 1] = "#" + g.GetAwayTeam().GetFBSRank() + " " + g.GetAwayTeam().GetTeamName(); } else { xlWorkSheet.Cells[i, 1] = g.GetAwayTeam().GetTeamName(); } xlWorkSheet.Cells[i, 2] = g.GetAwayScore(); xlWorkSheet.Cells[i, 3] = g.GetHomeScore(); if (g.GetHomeTeam().GetFBSRank() <= 25) { xlWorkSheet.Cells[i, 4] = "#" + g.GetHomeTeam().GetFBSRank() + " " + g.GetHomeTeam().GetTeamName(); } else { xlWorkSheet.Cells[i, 4] = g.GetHomeTeam().GetTeamName(); } i++; } using (StreamWriter sw = new StreamWriter(fileName)) { sw.Write(scores.ToString()); } xlApp.DisplayAlerts = false; xlWorkBook.SaveAs("SimmedWeek.xlsx"); xlWorkBook.Close(); RankingExporter.UploadToDrive(xlfileName); System.Diagnostics.Process.Start(xlfileName); scores.Clear(); Week.Clear(); foreach (Team t in ncaa.GetFBS()) { t.ClearProjectedPoints(); } }
/// <summary> /// Takes the scoreline and adds the result to the proper teams' stats. /// Increases each team's wins or losses and points scored and points allowed /// </summary> /// <param name="scoreline">the score for one game</param> private static void CompileScores(string[] scoreline) { foreach (Team team in ncaa.GetFBS()) { if (scoreline[0].Equals(team.GetTeamName())) { team.IncreaseTotalPoints(Convert.ToInt32(scoreline[1])); team.IncreaseTotalPointsAllowed(Convert.ToInt32(scoreline[3])); if (Convert.ToInt32(scoreline[1]) > Convert.ToInt32(scoreline[3])) { team.IncreaseWins(); } else { team.IncreaseLosses(); } foreach (Team opponent in ncaa.GetFBS()) { if (scoreline[2].Equals(opponent.GetTeamName())) { team.AddOpponent(opponent); } } foreach (Team opponent in ncaa.GetFCS()) { if (scoreline[2].Equals(opponent.GetTeamName())) { team.AddOpponent(opponent); } } } else if (scoreline[2].Equals(team.GetTeamName())) { team.IncreaseTotalPoints(Convert.ToInt32(scoreline[3])); team.IncreaseTotalPointsAllowed(Convert.ToInt32(scoreline[1])); if (Convert.ToInt32(scoreline[3]) > Convert.ToInt32(scoreline[1])) { team.IncreaseWins(); } else { team.IncreaseLosses(); } foreach (Team opponent in ncaa.GetFBS()) { if (scoreline[0].Equals(opponent.GetTeamName())) { team.AddOpponent(opponent); } } foreach (Team opponent in ncaa.GetFCS()) { if (scoreline[0].Equals(opponent.GetTeamName())) { team.AddOpponent(opponent); } } } } foreach (Team team in ncaa.GetFCS()) { if (scoreline[0].Equals(team.GetTeamName())) { team.IncreaseTotalPoints(Convert.ToInt32(scoreline[1])); team.IncreaseTotalPointsAllowed(Convert.ToInt32(scoreline[3])); if (Convert.ToInt32(scoreline[1]) > Convert.ToInt32(scoreline[3])) { team.IncreaseWins(); } else { team.IncreaseLosses(); } foreach (Team opponent in ncaa.GetFBS()) { if (scoreline[2].Equals(opponent.GetTeamName())) { team.AddOpponent(opponent); } } foreach (Team opponent in ncaa.GetFCS()) { if (scoreline[2].Equals(opponent.GetTeamName())) { team.AddOpponent(opponent); } } } else if (scoreline[2].Equals(team.GetTeamName())) { team.IncreaseTotalPoints(Convert.ToInt32(scoreline[3])); team.IncreaseTotalPointsAllowed(Convert.ToInt32(scoreline[1])); if (Convert.ToInt32(scoreline[3]) > Convert.ToInt32(scoreline[1])) { team.IncreaseWins(); } else { team.IncreaseLosses(); } foreach (Team opponent in ncaa.GetFBS()) { if (scoreline[0].Equals(opponent.GetTeamName())) { team.AddOpponent(opponent); } } foreach (Team opponent in ncaa.GetFCS()) { if (scoreline[0].Equals(opponent.GetTeamName())) { team.AddOpponent(opponent); } } } } }