using System; using System.Collections.Generic; using System.Text; using System.Xml.Serialization; namespace DarkWynter.Engine.HighScores { /// <summary> /// This class handles the loading and saving of the HighScore list /// It provides the fileName and path as constants /// </summary> public class HighScoreHandler { const String fileName = "highScores.sav"; const String filePath = "FileIO"; List<SaveData> scores; SaveScore save; LoadHighScores load; const int MAX_HIGH_SCORES = 10;//should be the total # in the file /// <summary> /// constructor that sets everything up... /// </summary> public HighScoreHandler() { scores = new List<SaveData>(); scores.Capacity = MAX_HIGH_SCORES; save = new SaveScore(filePath,fileName); load = new LoadHighScores(filePath,fileName); }//end constructor /// <summary> /// Saves the data /// </summary> /// <param name="initials">String representing the players initals</param> /// <param name="score"></param> public void Save(String initials, int score) { Load();//load the scores list so not null SaveData data = new SaveData(initials, score); scores.Add(data);//add to list Sort();//put them in order before saving save.Save(scores);//save }//Save /// <summary> /// Loads the scores list /// </summary> /// <returns></returns> private List<SaveData> Load() { scores = load.LoadScores(); return scores; } /// <summary> /// sorts the list and makes sure there is only MAX_HIGH_SCORES in the list /// </summary> private void Sort() { List<SaveData> temp = new List<SaveData>(); scores.Sort(CompareScores); if (scores.Count > MAX_HIGH_SCORES) { for (int i = 0; i < MAX_HIGH_SCORES; i++) { temp.Add(scores[i]); } scores = null; scores = temp; } }//end Sort /// <summary> /// just compares SaveData types by their scores /// used by the List.sort method... /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <returns></returns> private int CompareScores(SaveData x, SaveData y) { if (x.Score > y.Score) { return -1; } else if (x.Score < y.Score) { return 1; } else { return 0; } }//end CompareScores /// <summary> /// returns the list of highscores /// </summary> /// <returns></returns> public List<SaveData> GetHighScores() { return Load(); }//GetHighScores /// <summary> /// provides a way to see if a players score is a new highscore or not /// </summary> /// <param name="newScore">current player score</param> /// <returns></returns> public bool CheckForNewHighScore(int newScore) { Load(); //meaning that if the last score is still larger //then you dont have a high score if (scores[MAX_HIGH_SCORES-1].Score > newScore) { return false; } return true; }//end checkFornewHS }//end class }//end namespace