/// <summary> /// Saves the highscore, if there's 10 highscores already, removes the last one from the list before saving, and sorts the list /// Asks the name user wants to put, removes '|' with ' ' so won't mess up the serialization /// Toggles grid for asking name collapsed /// </summary> /// <param name="sender">sender of the event</param> /// <param name="e">eventarguments</param> private void ButtonHighScore_Click_1(object sender, RoutedEventArgs e) { PlayerHighScoreItem newPlayerScore = new PlayerHighScoreItem(); String name = TextBoxHighScore.Text.Replace('|', ' '); newPlayerScore.Name = name; newPlayerScore.Score = newScore; if (listHighScores.Count >= 10) { listHighScores.RemoveAt(9); } listHighScores.Add(newPlayerScore); listHighScores.Sort(); UpdateGridScores(); GridAskName.Visibility = Visibility.Collapsed; }
/// <summary> /// Saves the highscore, if there's 10 highscores already, removes the last one from the list before saving, and sorts the list /// Asks the name user wants to put, removes '|' with ' ' so won't mess up the serialization /// Toggles grid for asking name collapsed /// </summary> private void EnterHighScore() { PlayerHighScoreItem newPlayerScore = new PlayerHighScoreItem(); String name = TextBoxHighScore.Text.Replace('|', ' '); newPlayerScore.Name = name; newPlayerScore.Score = newScore; if (listHighScores.Count >= 10) { listHighScores.RemoveAt(9); } listHighScores.Add(newPlayerScore); listHighScores.Sort(); UpdateGridScores(); GridAskName.Visibility = Visibility.Collapsed; }
/// <summary> /// Loads highscores from scores.txt, if it exists /// </summary> private async Task LoadFromFile() { String filename = "scores.txt"; StorageFolder folder = ApplicationData.Current.LocalFolder; StorageFile file; try { file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appdata:///local/" + filename)); } catch (FileNotFoundException) { return; } string content = await FileIO.ReadTextAsync(file); string[] separator = new string[] { "\r\n" }; string[] scoresArray = content.Split(separator, StringSplitOptions.None); for (int i = 0; i < scoresArray.Length; i++) { char[] separatorChar = new char[] { '|' }; string[] oneScoreArray = scoresArray[i].Split(separatorChar, StringSplitOptions.None); PlayerHighScoreItem item = new PlayerHighScoreItem(); try { item.Name = oneScoreArray[0]; item.Score = Convert.ToInt16(oneScoreArray[1]); } catch (Exception e) { if (e is IndexOutOfRangeException || e is FormatException) { break; } else throw; } if ((item.Name.Equals("") != true) || (item.Score > 0)) listHighScores.Add(item); } listHighScores.Sort(); UpdateGridScores(); }