public void Add(HighScore newScore) { if (IsNewHighScore(newScore)) { // Add the new highscore List<HighScore> scores = Scores; scores.Add(newScore); HighScore lastElement = scores.OrderByDescending(a => a.Percent).ThenByDescending(a => a.Date).ElementAt(5); scores.Remove(lastElement); storageProvider.SaveHighScores(scores); } }
// Display Score private void DisplayScore(object sender, EventArgs e) { // calculate the players score HighScore newHighScore = new HighScore() { QuestionsCorrect = _game.Score, TotalQuestions = _game.TotalQuestions, Date = DateTime.Now }; // display the score grid HighScoreGridVisibility = Visibility.Visible; HighScoreText = String.Format(AppResources.HighScoreText, newHighScore.Percent); // load the high score table HighScoreTable hst = new HighScoreTable(_storageProvider); if (hst.IsNewHighScore(newHighScore) && !_scoreEntered) { HighScoreAnnounceText = AppResources.HighScoreAnnounceText; HighScoreNameVisibility = Visibility.Visible; HighScoreNameLabelVisibility = Visibility.Visible; } }
public void HighScoreContinueButtonPressed(object sender, EventArgs e) { // was this a high score? HighScore newHighScore = new HighScore() { QuestionsCorrect = _game.Score, TotalQuestions = _game.TotalQuestions, Date = DateTime.Now }; HighScoreTable hst = new HighScoreTable(_storageProvider); if (hst.IsNewHighScore(newHighScore) && !_scoreEntered) { // enter in to the record books newHighScore.Name = HighScoreNameText; if (HighScoreNameText == "") newHighScore.Name = AppResources.AnonymousUser; hst.Add(newHighScore); _scoreEntered = true; // hack to allow us to reset the high score if (HighScoreNameText == AppResources.ResetPassword) hst.Reset(); } // update the view model HighScoreGridVisibility = Visibility.Collapsed; // update the view model in case user hits back button from home screen HighScoreNameVisibility = Visibility.Collapsed; HighScoreNameLabelVisibility = Visibility.Collapsed; // signify that the game has well and truly ended this.GameEnded(this,new EventArgs()); }
public bool IsNewHighScore(HighScore newScore) { // copy the list into local List<HighScore> localScores = Scores.ToList<HighScore>(); // add and see what position it would make localScores.Add(newScore); // return true if the new score makes the top scores int newIndex = localScores.OrderByDescending(a => a.Percent).ThenByDescending(a => a.Date).ToList<HighScore>().IndexOf(newScore); return newIndex < Scores.Count(); }