public void CalculateHandicap(string RoundType) { if (!_database.Table <Scores>().Where(s => s.RoundType == RoundType).Any()) { //will throw an error if there are no scores yet in the database for that round type and this is called and it doesnt need to be called with no scores anyways return; } var ScoreData = GetUsedScores(RoundType); if (ScoreData.Count < 5) { return; } var ScoresToUse = GetNumberOfScoresToUse(ScoreData.Count); var LowestScores = GetLowestScoresDifferentials(ScoresToUse, ScoreData); UpdateLowestScoreFlags(ScoresToUse, RoundType); var handicap = LowestScores.Average(); //eventually make every decimal in the database to be a double since the handicap has to be a double and itll make things a lot easier handicap = Convert.ToDouble(handicap.ToString("0.#")); if (RoundType == "18") { if (Preferences.ContainsKey("Handicap18")) { //only insert the handicap into the handicap history table if it is different than the current handicap if (Convert.ToDouble(handicap) != Preferences.Get("Handicap18", -1.0)) { var hdcp = new Handicap { Date = GetLastEnteredScoreDate(RoundType), Number = handicap, Type = RoundType }; SaveHandicap(hdcp); } Preferences.Set("Handicap18", handicap); } else { //this is the first time the user has gotten 5 scores to get a handicap calculated so insert the handicap as the first history var hdcp = new Handicap { Date = GetLastEnteredScoreDate(RoundType), Number = handicap, Type = RoundType }; Preferences.Set("Handicap18", handicap); SaveHandicap(hdcp); } } else { if (Preferences.ContainsKey("Handicap9")) { if (Convert.ToDouble(handicap) != Preferences.Get("Handicap9", -1.0)) { var hdcp = new Handicap { Date = GetLastEnteredScoreDate(RoundType), Number = handicap, Type = RoundType }; Preferences.Set("Handicap9", handicap); SaveHandicap(hdcp); } } else { //first time a user has gotten 5 scores to get a handicap var hdcp = new Handicap { Date = GetLastEnteredScoreDate(RoundType), Number = handicap, Type = RoundType }; Preferences.Set("Handicap9", handicap); SaveHandicap(hdcp); } } }
public int SaveHandicap(Handicap handicap) { return(_database.Insert(handicap)); }