private static void SortHighScores(HighScore highScore)
            {
                List <HighScore> scores = ReadHighScores();
                int lowestOfOldScores   = scores.Count() > 0 ? Convert.ToInt32(scores.Min(x => x.Score)) : 0;
                int newScore            = Convert.ToInt32(highScore.Score);

                if (lowestOfOldScores > newScore) //if the new score is the lowest score, don't add it
                {
                    return;
                }
                XmlDocument xml = new XmlDocument();

                xml.Load(Files.GetDefinitionFilePath(EnumXmlFiles.XmlFileHighScores));
                xml.ChildNodes[1].RemoveAll();                                                           //remove the previous scores from the file -- but we have them saved in 'scores'
                scores.Add(highScore);
                IEnumerable <HighScore> orderedScores = scores.OrderByDescending(x => x.Score).Take(10); //take the 10 best highscores

                foreach (HighScore score in orderedScores)
                {
                    xml.ChildNodes[1].AppendChild(CreateNewHighScore(xml, score));
                }
                xml.Save(Files.GetDefinitionFilePath(EnumXmlFiles.XmlFileHighScores));
            }
Esempio n. 2
0
        public static HighScore ReadHighScoreFromXML(string path)
        {
            HighScore highScore = new HighScore();

            //https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/serialization/how-to-read-object-data-from-an-xml-file
            System.Xml.Serialization.XmlSerializer reader =
                new System.Xml.Serialization.XmlSerializer(typeof(HighScore));
            try
            {
                System.IO.StreamReader file = new System.IO.StreamReader(path);
                highScore = (HighScore)reader.Deserialize(file);
                file.Close();
            }
            catch (IOException e)
            {
                Console.WriteLine(
                    "{0}: The write operation could not " +
                    "be performed because the specified " +
                    "part of the file is locked.",
                    e.GetType().Name);
            }
            return(highScore);
        }
Esempio n. 3
0
 public MainWindow(User user)
 {
     InitializeComponent();
     tbName.Text    = user.name;
     this.highScore = Utils.ReadHighScoreFromXML("scores.xml");
 }
Esempio n. 4
0
 public MainWindow()
 {
     InitializeComponent();
     this.highScore = Utils.ReadHighScoreFromXML("scores.xml");
 }