Example #1
0
        public void Add(Score score)
        {
            if (score == null) throw new ArgumentNullException("score");

            Application.Current.Dispatcher.Invoke(() =>
            {
                if (TopScores.Count < NUMBER_TOP_SCORES)
                {
                    // Insert new top score
                    InsertScoreAtPosition(TopScores, score);
                }
                else if (TopScores.Any(s => s.Duration > score.Duration))
                {
                    // Take last entry
                    var worstScore = TopScores.Last();
                    TopScores.Remove(worstScore);

                    // Insert worst score to normal list at the beginning
                    Scores.Insert(0, worstScore);

                    // Insert new top score
                    InsertScoreAtPosition(TopScores, score);
                }
                else
                {
                    // Insert into normal list
                    InsertScoreAtPosition(Scores, score);
                }

                UpdateRank();
            });
        }
        public IEnumerable<Score> LoadScores(string fileName)
        {
            var scores = new List<Score>();

            try {
                using (var reader = XmlReader.Create(fileName))
                {
                    while (reader.Read())
                    {
                        // Only detect start elements.
                        if (!reader.IsStartElement()) continue;

                        // Get element name and switch on it.
                        if (reader.Name == "Score")
                        {
                            var score = new Score();

                            var nameAttribute = reader["Name"];
                            if (nameAttribute != null)
                            {
                                score.Name = nameAttribute;
                            }

                            var durationAttribute = reader["Duration"];
                            if (durationAttribute != null)
                            {
                                score.Duration = TimeSpan.Parse(durationAttribute);
                            }

                            scores.Add(score);
                        }
                    }
                }
            }
            catch
            {
                Console.WriteLine("Could not load scores");
            }
            return scores;
        }
Example #3
0
        private void InsertScoreAtPosition(ObservableCollection<Score> scores, Score score)
        {
            if (scores == null) throw new ArgumentNullException("scores");

            Application.Current.Dispatcher.Invoke(() =>
            {
                for (var i = 0; i < scores.Count; i++)
                {
                    if (scores.ElementAt(i).Duration <= score.Duration) continue;

                    scores.Insert(i, score);
                    return;
                }

                scores.Add(score);
            });
        }