//create a new function to display the highschore static void DisplayHighScores() { //tell user to view highscores Console.WriteLine("\n\nPress any key to view high scores..."); //clear console everytime to show highscore on a blank screen Console.ReadKey(); Console.Clear(); //write the high score text Console.WriteLine("Dragon Combat High Scores!"); Console.WriteLine("=============================="); //create a new connection to the database spDrewEntities db = new spDrewEntities(); //get the high score list //pull from our HighScores database where the game is "Guess That Number, order by highest score, and only take the top 10 scores List <HighScore> highScoreList = db.HighScores.Where(x => x.Game == "Dragon Combat 2.0").OrderBy(x => x.Score).Take(10).ToList(); //make a foreach loop to print out these scores to the console foreach (var highScore in highScoreList) { Console.WriteLine("{0}. {1} - {2} rounds on {3}", highScoreList.IndexOf(highScore) + 1, highScore.Name, highScore.Score, highScore.DateCreated); } }
//add the highscores to the database static void AddHighScore(int playerScore) { //get the name for the player Console.WriteLine("\n\nYour name:\n"); string playerName = Console.ReadLine(); //create a gateway to the database spDrewEntities db = new spDrewEntities(); //create a new highscore object HighScore newHighscore = new HighScore(); newHighscore.DateCreated = DateTime.Now; newHighscore.Game = "Dragon Combat 2.0"; newHighscore.Name = playerName; newHighscore.Score = playerScore; //add it to the database db.HighScores.Add(newHighscore); //always save changes, but only need to type this one time db.SaveChanges(); }