/// <summary> /// Tries to add a score name pair to the table. /// </summary> /// <param name="scoreNamePair">The score name pair to add.</param> /// <returns>A value indicating whether the pair was added or not.</returns> public bool AddScore(ScoreNamePair scoreNamePair) { if (scoreNamePair.CompareTo(this.Scores[this.Scores.Count - 1]) < 0) { this.Scores.RemoveAt(this.Scores.Count - 1); this.Scores.Add(scoreNamePair); this.Scores.Sort(); return true; } else { return false; } }
/// <summary> /// Adds a score-name pair to the database and checks if it is a new high score or not. /// </summary> /// <param name="scoreNamePair">The score-name pair to add.</param> /// <param name="levelNumber">The level number.</param> /// <returns>A value indicating whether the score was a new high score or not.</returns> public bool AddScore(ScoreNamePair scoreNamePair, int levelNumber) { bool scoreAdded = false; try { scoreAdded = this.AddScoreToTable(scoreNamePair, levelNumber); } catch (WebException) { scoreAdded = false; } catch (DataServiceQueryException) { scoreAdded = false; } catch (DataServiceRequestException) { scoreAdded = false; } return(scoreAdded); }
/// <summary> /// Tries to add a score-name pair to a high table. /// </summary> /// <param name="scoreNamePair">The score-name pair to try to add.</param> /// <param name="levelNumber">The level number.</param> /// <returns>A value indicating if the score has made it onto the high score table.</returns> private bool AddScoreToTable(ScoreNamePair scoreNamePair, int levelNumber) { bool highScore = false; IQueryable <HighScore> scores = from score in this.context.HighScores where score.Level == levelNumber orderby score.Score ascending select score; if (scores.Count() < 10) { this.context.AddToHighScores(HighScore.CreateHighScore(0, levelNumber, scoreNamePair.Name, scoreNamePair.Score)); this.context.SaveChanges(); highScore = true; } else if (scoreNamePair.CompareTo(new ScoreNamePair(scores.First().Score, scores.First().Name)) < 0) { HighScore lowest = scores.First(); lowest.Score = scoreNamePair.Score; lowest.Name = scoreNamePair.Name; this.context.UpdateObject(lowest); this.context.SaveChanges(); highScore = true; } return(highScore); }