//Pre: The map name and the current user //Post: A boolean for whether the map save exists //Desc: A method for checking whether a save of a map exists public bool CheckSaveExists(string mapName, string currentUser) { //If the current map exists if (onlineHelper.CheckFileExists(currentUser + "_" + mapName + SAVE_MAP_SUFFIX)) { //Returns that the map exists return(true); } //Returns that the map does not exist return(false); }
//Pre: The user profile, the amount of enemies killed, the amount of health lost, whether the player died again, the amount of extra game time, and the amount of projecitles shot //Post: A boolean for whether the profile stats were updated //Desc: A method for updating the profile stats for a specific profile public bool UpdateProfileStats(string userProfile, int extraEnemiesKilled, int extraHealthLost, int extraDeath, float extraGameTime, int extraBulletsShot) { //If the file for the profile stats exists if (onlineHelper.CheckFileExists(userProfile + FILE_NAME_SUFFIX)) { //If the file is downloaded if (onlineHelper.DownloadFile(userProfile + FILE_PATH_SUFFIX, userProfile + FILE_NAME_SUFFIX)) { //A stream reader is created to read the file reader = new StreamReader(userProfile + FILE_PATH_SUFFIX); //All the data from the file is read EnemiesKilled = Convert.ToInt32(reader.ReadLine()); HealthLost = Convert.ToInt32(reader.ReadLine()); DeathAmount = Convert.ToInt32(reader.ReadLine()); TotalGameTime = (float)Convert.ToDouble(reader.ReadLine()); BulletsShot = Convert.ToInt32(reader.ReadLine()); //The stream reader is closed reader.Close(); //All the extra values are added to the total values for the profile EnemiesKilled += extraEnemiesKilled; HealthLost += extraHealthLost; DeathAmount += extraDeath; TotalGameTime += extraGameTime; BulletsShot += extraBulletsShot; //A stream writer is created to save all the data in a new profile stats file writer = new StreamWriter(userProfile + FILE_PATH_SUFFIX); //All the data is writtern to the file writer.WriteLine(EnemiesKilled); writer.WriteLine(HealthLost); writer.WriteLine(DeathAmount); writer.WriteLine(TotalGameTime); writer.WriteLine(BulletsShot); //The stream writer is closed writer.Close(); //If the new file is uploaded if (onlineHelper.UpdateFile(userProfile + FILE_NAME_SUFFIX, userProfile + FILE_PATH_SUFFIX, FILE_TYPE, FILE_DESCRIPTION)) { //Returns that the profile stats were updated for the current profile return(true); } } //if the file was not downloaded else { //Returns that the profile stats were not updated for the current profile return(false); } } //If the profile stats for the current profile do not exist else { //A stream writer is created to write all the data to a file writer = new StreamWriter(userProfile + FILE_PATH_SUFFIX); //All the data is written to the profile stats file writer.WriteLine(extraEnemiesKilled); writer.WriteLine(extraHealthLost); writer.WriteLine(extraDeath); writer.WriteLine(extraGameTime); writer.WriteLine(extraBulletsShot); //The stream writer is closed writer.Close(); //If the file is uploaded onto the server if (onlineHelper.UpdateFile(userProfile + FILE_NAME_SUFFIX, userProfile + FILE_PATH_SUFFIX, FILE_TYPE, FILE_DESCRIPTION)) { //Returns that the profile stats were updated for the current profile return(true); } } //Returns that the profile stats were not updated for the current profile return(false); }
//Pre: The map name, the user name, and the elapsed time of the user //Post: A boolean for whether the highscore was updated //Desc: A method for updating the highscores of a map public bool UpdateHighscoresMap(string mapName, string user, float elapsedTime) { //Temporarily used lists for the names and times List <string> tempNames = new List <string>(); List <float> tempTimes = new List <float>(); //If the list of usernames is not null if (UserNames != null) { //Clears the usernames and times for each user UserNames.Clear(); UserTimes.Clear(); } //Checks if the file exists if (onlineHelper.CheckFileExists(mapName + FILE_NAME_SUFFIX)) { //Downloads the file onlineHelper.DownloadFile(mapName + FILE_PATH_SUFFIX, mapName + FILE_NAME_SUFFIX); //Creates a stream reader for the file reader = new StreamReader(mapName + FILE_PATH_SUFFIX); //Gets the score count using the reader from the file int scoreCount = Convert.ToInt32(reader.ReadLine()); //Loop for every score saved in the file for (int i = 0; i < scoreCount; i++) { //Adds the neames and times to the lists from the file tempNames.Add(reader.ReadLine()); tempTimes.Add((float)Convert.ToDouble(reader.ReadLine())); } //Closes the stream reader reader.Close(); //Adds the new user and their time tempNames.Add(user); tempTimes.Add(elapsedTime); //Sorts the new times SortTimes(tempNames, tempTimes); //If the amount of scores saved is greater then the max amount of scores if (UserNames.Count > MAX_SCORES) { //Removes the last score UserNames.RemoveAt(UserNames.Count - 1); UserTimes.RemoveAt(UserTimes.Count - 1); } //Creates the stream writer with the file path writer = new StreamWriter(mapName + FILE_PATH_SUFFIX); //Writes the amount of scores to the file writer.WriteLine(UserNames.Count); //Loop for every score for (int i = 0; i < UserNames.Count; i++) { //Writers the user name and it's time to the file writer.WriteLine(UserNames[i]); writer.WriteLine(UserTimes[i]); } //Closes the file writer.Close(); //If the file was updates if (onlineHelper.UpdateFile(mapName + FILE_NAME_SUFFIX, mapName + FILE_PATH_SUFFIX, FILE_TYPE, FILE_DESCRIPTION)) { //Returns that the file was updateed return(true); } } //If the file does not exist else { //Resets the names and the times for each name UserNames = new List <string>(); UserTimes = new List <float>(); //Adds the new user and their time UserNames.Add(user); UserTimes.Add(elapsedTime); //A stream writer is created with the path of the current map writer = new StreamWriter(mapName + FILE_PATH_SUFFIX); //Writes the amount of scores in the file writer.WriteLine(UserNames.Count); //Writes the new user name and time to the file writer.WriteLine(UserNames[0]); writer.WriteLine(UserTimes[0]); //Closes the stream writer writer.Close(); //If the file was uploaded if (onlineHelper.UpdateFile(mapName + FILE_NAME_SUFFIX, mapName + FILE_PATH_SUFFIX, FILE_TYPE, FILE_DESCRIPTION)) { //Returns that the file was uploaded return(true); } } //Returns that the file was not uploaded return(false); }