public ActionResult ShowHighScores() { HighScoreContext context = new HighScoreContext(); List <Score> data = context.Scores.OrderByDescending(x => x.PlayerScore).Take(100).ToList(); return(PartialView("_HighScores", data)); }
private void AddNewHighScore(int score) { HighScoreContext context = new HighScoreContext(); Console.SetCursorPosition(49, 19); Console.ForegroundColor = ConsoleColor.DarkYellow; Console.WriteLine("New Highscore!"); Console.ResetColor(); Console.SetCursorPosition(47, 20); Console.Write("Enter your name: "); string userName = Console.ReadLine(); while (string.IsNullOrWhiteSpace(userName)) { Console.WriteLine("You must enter a name"); userName = Console.ReadLine(); } HighScore highScore = new HighScore() { Name = userName, Score = score, Date = DateTime.Now }; context.HighScore.Add(highScore); context.SaveChanges(); }
public void DisplayTop10HighScore() { HighScoreContext context = new HighScoreContext(); Console.Clear(); Console.SetCursorPosition(50, 2); Console.ForegroundColor = ConsoleColor.DarkYellow; Console.WriteLine("Top 10 Highcore"); var HighScoreTop10List = context.HighScore .OrderByDescending(s => s.Score) .ThenByDescending(d => d.Date) .Take(10) .ToList(); Console.ForegroundColor = ConsoleColor.Green; Console.SetCursorPosition(51, 4); Console.WriteLine($"{"Name",-14} Score"); int row = 5; int rank = 1; Console.ForegroundColor = ConsoleColor.Red; foreach (var item in HighScoreTop10List) { Console.SetCursorPosition(45, row); Console.WriteLine($"#{rank + ".",-4} {item.Name,-14} {item.Score}"); rank++; row++; } }
public void HandleScore(Snake snake) { HighScoreContext context = new HighScoreContext(); Console.SetCursorPosition(50, 17); int score = snake.snakeParts.Count - 1; Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine($"Your Score: {score}"); int highScoreCount = context.HighScore.ToList().Count(); int lowestHighScoreInTop10 = 0; if (highScoreCount >= 10) { lowestHighScoreInTop10 = context.HighScore .OrderByDescending(s => s.Score) .ToList()[9].Score; } else { lowestHighScoreInTop10 = context.HighScore .OrderByDescending(s => s.Score) .ToList()[highScoreCount - 1].Score; } if (score >= lowestHighScoreInTop10) { AddNewHighScore(score); Console.Clear(); DisplayTop10HighScore(); } }
public async Task RemoveEleventhElement() { var options = new DbContextOptionsBuilder <HighScoreContext>() .UseInMemoryDatabase(databaseName: "HighScores") .Options; var databaseContext = new HighScoreContext(options); databaseContext.Database.EnsureCreated(); var service = new HighScoreEntryService(databaseContext); for (int i = 0; i < 10; i++) { await service.AddHighScoreEntryAsync(new HighScoreEntry { Points = i * 10, PlayerInitials = "JD" }); } var lastScore = new HighScoreEntry { Points = 99999, PlayerInitials = "JD" }; await service.AddHighScoreEntryAsync(lastScore); Assert.Contains(lastScore, service.GetAllItemsAsync().Result); }
public string GetScores() { HighScoreContext context = new HighScoreContext(); IEnumerable <Score> data = context.Scores.OrderByDescending(x => x.PlayerScore).Take(100); string jsonData = new JavaScriptSerializer().Serialize(data); return(jsonData); }
private void DisplayTop1HighScore() { HighScoreContext context = new HighScoreContext(); var top1HighScore = context.HighScore .OrderByDescending(s => s.Score) .ThenByDescending(d => d.Date) .ToList()[0]; Console.SetCursorPosition(82, 24); Console.ForegroundColor = ConsoleColor.DarkYellow; Console.WriteLine($"#1 Name: {top1HighScore.Name}, Score: {top1HighScore.Score}"); }
public ActionResult SaveHighScore(string playerName, int playerScore) { Score newScore = new Score { Name = playerName, PlayerScore = playerScore }; using (HighScoreContext context = new HighScoreContext()) { context.Scores.Add(newScore); context.SaveChanges(); } return(ShowHighScores()); }
public async Task EmptyAtBeginning() { var options = new DbContextOptionsBuilder <HighScoreContext>() .UseInMemoryDatabase(databaseName: "HighScores") .Options; var databaseContext = new HighScoreContext(options); databaseContext.Database.EnsureCreated(); var service = new HighScoreEntryService(databaseContext); var result = await service.GetAllItemsAsync(); Assert.Empty(result); }
public async Task AddFirstHighScoreEntry() { var options = new DbContextOptionsBuilder <HighScoreContext>() .UseInMemoryDatabase(databaseName: "HighScores") .Options; var databaseContext = new HighScoreContext(options); databaseContext.Database.EnsureCreated(); var service = new HighScoreEntryService(databaseContext); await service.AddHighScoreEntryAsync(new HighScoreEntry { Points = 100, PlayerInitials = "JD" }); var items = await service.GetAllItemsAsync(); Assert.Single(items); }
public async Task SortedDescending() { var options = new DbContextOptionsBuilder <HighScoreContext>() .UseInMemoryDatabase(databaseName: "HighScores") .Options; var databaseContext = new HighScoreContext(options); databaseContext.Database.EnsureCreated(); var service = new HighScoreEntryService(databaseContext); await service.AddHighScoreEntryAsync(new HighScoreEntry { Points = 10, PlayerInitials = "JD" }); await service.AddHighScoreEntryAsync(new HighScoreEntry { Points = 5, PlayerInitials = "JD" }); var highScores = await service.GetAllItemsAsync(); var sortedDescending = true; foreach (var highScore in highScores) { if (highScore.Points < highScores[highScores.IndexOf(highScore) + 1].Points) { sortedDescending = false; } } Assert.True(sortedDescending); }
public HighScoreEntryService(HighScoreContext context) { _context = context; }
public HighscoreRepository(HighScoreContext context) { _context = context; }
public HighScoreItemsController(HighScoreContext context) { _context = context; }
public HighScoreEntriesController(HighScoreContext context) { _context = context; _service = new HighScoreEntryService(_context); }