From f0a1826b857e5414262be1a6c67c3e0163735b51 Mon Sep 17 00:00:00 2001 From: Slendy Date: Mon, 28 Aug 2023 16:32:58 -0500 Subject: [PATCH] Only update score timestamps when you get more points --- .../Controllers/Slots/ScoreController.cs | 18 +- .../Unit/Controllers/ScoreControllerTests.cs | 161 ++++++++++++++++++ 2 files changed, 171 insertions(+), 8 deletions(-) create mode 100644 ProjectLighthouse.Tests.GameApiTests/Unit/Controllers/ScoreControllerTests.cs diff --git a/ProjectLighthouse.Servers.GameServer/Controllers/Slots/ScoreController.cs b/ProjectLighthouse.Servers.GameServer/Controllers/Slots/ScoreController.cs index 544e5799..79ae3163 100644 --- a/ProjectLighthouse.Servers.GameServer/Controllers/Slots/ScoreController.cs +++ b/ProjectLighthouse.Servers.GameServer/Controllers/Slots/ScoreController.cs @@ -136,14 +136,10 @@ public class ScoreController : ControllerBase .Where(s => s.UserId == token.UserId) .Where(s => s.Type == score.Type) .FirstOrDefaultAsync(); - if (existingScore != null) + + if (existingScore == null) { - existingScore.Points = Math.Max(existingScore.Points, score.Points); - existingScore.Timestamp = TimeHelper.TimestampMillis; - } - else - { - ScoreEntity playerScore = new() + existingScore = new ScoreEntity { UserId = token.UserId, Type = score.Type, @@ -152,7 +148,13 @@ public class ScoreController : ControllerBase ChildSlotId = childId, Timestamp = TimeHelper.TimestampMillis, }; - this.database.Scores.Add(playerScore); + this.database.Scores.Add(existingScore); + } + + if (score.Points > existingScore.Points) + { + existingScore.Points = score.Points; + existingScore.Timestamp = TimeHelper.TimestampMillis; } await this.database.SaveChangesAsync(); diff --git a/ProjectLighthouse.Tests.GameApiTests/Unit/Controllers/ScoreControllerTests.cs b/ProjectLighthouse.Tests.GameApiTests/Unit/Controllers/ScoreControllerTests.cs new file mode 100644 index 00000000..96dd547a --- /dev/null +++ b/ProjectLighthouse.Tests.GameApiTests/Unit/Controllers/ScoreControllerTests.cs @@ -0,0 +1,161 @@ +using System.Linq; +using System.Threading.Tasks; +using LBPUnion.ProjectLighthouse.Database; +using LBPUnion.ProjectLighthouse.Servers.GameServer.Controllers.Slots; +using LBPUnion.ProjectLighthouse.Tests.Helpers; +using LBPUnion.ProjectLighthouse.Types.Entities.Level; +using Microsoft.AspNetCore.Mvc; +using Xunit; + +namespace ProjectLighthouse.Tests.GameApiTests.Unit.Controllers; + +[Trait("Category", "Unit")] +public class ScoreControllerTests +{ + [Fact] + public async Task SubmitScore_ShouldSubmitValidScore_WhenNoExistingScore() + { + DatabaseContext database = await MockHelper.GetTestDatabase(); + + SlotEntity slot = new() + { + CreatorId = 1, + SlotId = 1, + }; + database.Slots.Add(slot); + await database.SaveChangesAsync(); + + ScoreController scoreController = new(database); + const string xmlBody = """ + + 1 + 10 + unittest + + """; + scoreController.SetupTestController(xmlBody); + IActionResult result = await scoreController.SubmitScore("user", 1, 0); + Assert.IsType(result); + Assert.NotNull(database.Scores.FirstOrDefault(s => s.Type == 1 && s.SlotId == 1 && s.UserId == 1)); + } + + [Fact] + public async Task SubmitScore_ShouldUpdateScore_WhenBetterThanExistingScore() + { + DatabaseContext database = await MockHelper.GetTestDatabase(); + + SlotEntity slot = new() + { + CreatorId = 1, + SlotId = 1, + }; + database.Slots.Add(slot); + + ScoreEntity score = new() + { + SlotId = 1, + Type = 1, + UserId = 1, + Points = 5, + Timestamp = 0, + }; + database.Scores.Add(score); + await database.SaveChangesAsync(); + + ScoreController scoreController = new(database); + const string xmlBody = """ + + 1 + 10 + unittest + + """; + scoreController.SetupTestController(xmlBody); + IActionResult result = await scoreController.SubmitScore("user", 1, 0); + Assert.IsType(result); + ScoreEntity? newScore = database.Scores.FirstOrDefault(s => s.Type == 1 && s.SlotId == 1 && s.UserId == 1); + Assert.NotNull(newScore); + Assert.NotEqual(0, newScore.Timestamp); + Assert.Equal(10, newScore.Points); + } + + [Fact] + public async Task SubmitScore_ShouldNotUpdateScore_WhenEqualToExistingScore() + { + DatabaseContext database = await MockHelper.GetTestDatabase(); + + SlotEntity slot = new() + { + CreatorId = 1, + SlotId = 1, + }; + database.Slots.Add(slot); + + ScoreEntity score = new() + { + SlotId = 1, + Type = 1, + UserId = 1, + Points = 10, + Timestamp = 0, + }; + database.Scores.Add(score); + await database.SaveChangesAsync(); + + ScoreController scoreController = new(database); + const string xmlBody = """ + + 1 + 10 + unittest + + """; + scoreController.SetupTestController(xmlBody); + IActionResult result = await scoreController.SubmitScore("user", 1, 0); + Assert.IsType(result); + ScoreEntity? newScore = database.Scores.FirstOrDefault(s => s.Type == 1 && s.SlotId == 1 && s.UserId == 1); + Assert.NotNull(newScore); + Assert.Equal(0, newScore.Timestamp); + Assert.Equal(10, newScore.Points); + } + + [Fact] + public async Task SubmitScore_ShouldNotUpdateScore_WhenLessThanExistingScore() + { + DatabaseContext database = await MockHelper.GetTestDatabase(); + + SlotEntity slot = new() + { + CreatorId = 1, + SlotId = 1, + }; + database.Slots.Add(slot); + + ScoreEntity score = new() + { + SlotId = 1, + Type = 1, + UserId = 1, + Points = 10, + Timestamp = 0, + }; + database.Scores.Add(score); + await database.SaveChangesAsync(); + + ScoreController scoreController = new(database); + const string xmlBody = """ + + 1 + 5 + unittest + + """; + scoreController.SetupTestController(xmlBody); + IActionResult result = await scoreController.SubmitScore("user", 1, 0); + Assert.IsType(result); + ScoreEntity? newScore = database.Scores.FirstOrDefault(s => s.Type == 1 && s.SlotId == 1 && s.UserId == 1); + Assert.NotNull(newScore); + Assert.Equal(0, newScore.Timestamp); + Assert.Equal(10, newScore.Points); + } +} \ No newline at end of file