예제 #1
0
        /// <summary>
        /// Reads the high scores XML file and returns a collection of PlayerScore objects, each representing a high score
        /// As the high scores XML file is encrypted, this method decypts it.
        /// </summary>
        /// <returns>A collection of PlayerScore objects representing the high scores</returns>
        /// <exception cref="System.Exception">Error occured while reading data.</exception>
        private List <PlayerScore> LoadScores()
        {
            if (File.Exists(FileName))
            {
                try
                {
                    //read the XML file
                    XmlDocument xmlDoc = new XmlDocument
                    {
                        PreserveWhitespace = true
                    };
                    xmlDoc.Load(FileName);

                    // Create a new RSA key.  This key will encrypt a symmetric key,
                    // which will then be imbedded in the XML document.
                    //need to move the key into the webconfig
                    CspParameters parameters = new CspParameters
                    {
                        KeyContainerName = ConfigurationSettings.AppSettings["ScoresFileEncryptionKey"]
                                           //KeyContainerName = "XML_ENC_RSA_KEY"
                    };
                    RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider(parameters);
                    // Decrypt the "Player" element.
                    TrippleDESDocumentEncryption.Decrypt(xmlDoc, rsaKey, "rsaKey");

                    List <PlayerScore> list = new List <PlayerScore>();         //Create a new list
                    XmlNodeList        elementsByTagName = null;
                    elementsByTagName = xmlDoc.GetElementsByTagName(HIGHSCORE); //find each score entry in the XML file
                    foreach (XmlNode node in elementsByTagName)
                    {
                        //for each score found in the XML file, create a new PlayerScore object with the data
                        PlayerScore item = new PlayerScore
                        {
                            Player = node[PLAYER].InnerText,
                            Score  = Convert.ToInt32(node[SCORE].InnerText),
                            Date   = Convert.ToDateTime(node[DATE].InnerText)
                        };
                        list.Add(item); //and add it to the collection
                    }
                    return(list);       //when finished, return this collection
                }
                catch (XmlException)
                {
                    //throw an exception if an error occured
                    throw new Exception("Error occured while reading data.");
                }
            }
            return(new List <PlayerScore>()); //if no XML file exists, return an empty list
        }
예제 #2
0
        /// <summary>
        /// Compares two PlayerScore objects to determine which one has the highest score
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int CompareTo(Object obj)
        {
            PlayerScore scoreToCompare = obj as PlayerScore;

            if (this.Score > scoreToCompare.Score)
            {
                return(1);
            }
            else if (this.Score < scoreToCompare.Score)
            {
                return(-1);
            }
            else
            {
                return(0);
            }
        }
예제 #3
0
        /// <summary>
        /// Adds a new score to the XML file
        /// </summary>
        /// <param name="player">The player name</param>
        /// <param name="score">The player's score</param>
        /// <returns>Return true if the score was a high score, else return false.</returns>
        public bool Add(string player, Int32 score)
        {
            List <PlayerScore> list = LoadScores();   //read the scores currently held in the XML file
            PlayerScore        item = new PlayerScore //create a new PlayerScore object with the data passed to the method
            {
                Player = player,
                Score  = score,
                Date   = DateTime.Now
            };

            //try and add the new score to the high scores
            bool flag = false;

            if (item.Score != 0)       //only add score if it is above 0
            {
                if (list.Count < SIZE) //if the list size is less than the max number of scores
                {
                    list.Add(item);    //then add it
                    flag = true;
                }
                else
                {
                    //else, find the lowest score (possible because the PlayerScore object implements IComparable)
                    list.Sort();
                    PlayerScore lowestScore = list[0];
                    if (lowestScore.Score < item.Score)
                    {
                        //...if the new score is higher than the lowest score on the list then add it
                        list.RemoveAt(0);
                        list.Add(item);
                        flag = true;
                    }
                }
            }

            //if a new score has been added to the high scores list, then re-write the high scores XML file
            if (flag)
            {
                SaveScores(list);
            }
            return(flag); //return true if the score was a high score, else return false.
        }