using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
namespace DarkWynter.Engine.HighScores
{
///
/// This class handles the loading and saving of the HighScore list
/// It provides the fileName and path as constants
///
public class HighScoreHandler
{
const String fileName = "highScores.sav";
const String filePath = "FileIO";
List scores;
SaveScore save;
LoadHighScores load;
const int MAX_HIGH_SCORES = 10;//should be the total # in the file
///
/// constructor that sets everything up...
///
public HighScoreHandler()
{
scores = new List();
scores.Capacity = MAX_HIGH_SCORES;
save = new SaveScore(filePath,fileName);
load = new LoadHighScores(filePath,fileName);
}//end constructor
///
/// Saves the data
///
/// String representing the players initals
///
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
///
/// Loads the scores list
///
///
private List Load()
{
scores = load.LoadScores();
return scores;
}
///
/// sorts the list and makes sure there is only MAX_HIGH_SCORES in the list
///
private void Sort()
{
List temp = new List();
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
///
/// just compares SaveData types by their scores
/// used by the List.sort method...
///
///
///
///
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
///
/// returns the list of highscores
///
///
public List GetHighScores()
{
return Load();
}//GetHighScores
///
/// provides a way to see if a players score is a new highscore or not
///
/// current player score
///
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