mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-07-07 13:51:28 +00:00
Only update score timestamps when you get more points
This commit is contained in:
parent
9f5a85d600
commit
f0a1826b85
2 changed files with 171 additions and 8 deletions
|
@ -136,14 +136,10 @@ public class ScoreController : ControllerBase
|
||||||
.Where(s => s.UserId == token.UserId)
|
.Where(s => s.UserId == token.UserId)
|
||||||
.Where(s => s.Type == score.Type)
|
.Where(s => s.Type == score.Type)
|
||||||
.FirstOrDefaultAsync();
|
.FirstOrDefaultAsync();
|
||||||
if (existingScore != null)
|
|
||||||
|
if (existingScore == null)
|
||||||
{
|
{
|
||||||
existingScore.Points = Math.Max(existingScore.Points, score.Points);
|
existingScore = new ScoreEntity
|
||||||
existingScore.Timestamp = TimeHelper.TimestampMillis;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ScoreEntity playerScore = new()
|
|
||||||
{
|
{
|
||||||
UserId = token.UserId,
|
UserId = token.UserId,
|
||||||
Type = score.Type,
|
Type = score.Type,
|
||||||
|
@ -152,7 +148,13 @@ public class ScoreController : ControllerBase
|
||||||
ChildSlotId = childId,
|
ChildSlotId = childId,
|
||||||
Timestamp = TimeHelper.TimestampMillis,
|
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();
|
await this.database.SaveChangesAsync();
|
||||||
|
|
|
@ -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 = """
|
||||||
|
<playRecord>
|
||||||
|
<type>1</type>
|
||||||
|
<score>10</score>
|
||||||
|
<playerIds>unittest</playerIds>
|
||||||
|
</playRecord>
|
||||||
|
""";
|
||||||
|
scoreController.SetupTestController(xmlBody);
|
||||||
|
IActionResult result = await scoreController.SubmitScore("user", 1, 0);
|
||||||
|
Assert.IsType<OkObjectResult>(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 = """
|
||||||
|
<playRecord>
|
||||||
|
<type>1</type>
|
||||||
|
<score>10</score>
|
||||||
|
<playerIds>unittest</playerIds>
|
||||||
|
</playRecord>
|
||||||
|
""";
|
||||||
|
scoreController.SetupTestController(xmlBody);
|
||||||
|
IActionResult result = await scoreController.SubmitScore("user", 1, 0);
|
||||||
|
Assert.IsType<OkObjectResult>(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 = """
|
||||||
|
<playRecord>
|
||||||
|
<type>1</type>
|
||||||
|
<score>10</score>
|
||||||
|
<playerIds>unittest</playerIds>
|
||||||
|
</playRecord>
|
||||||
|
""";
|
||||||
|
scoreController.SetupTestController(xmlBody);
|
||||||
|
IActionResult result = await scoreController.SubmitScore("user", 1, 0);
|
||||||
|
Assert.IsType<OkObjectResult>(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 = """
|
||||||
|
<playRecord>
|
||||||
|
<type>1</type>
|
||||||
|
<score>5</score>
|
||||||
|
<playerIds>unittest</playerIds>
|
||||||
|
</playRecord>
|
||||||
|
""";
|
||||||
|
scoreController.SetupTestController(xmlBody);
|
||||||
|
IActionResult result = await scoreController.SubmitScore("user", 1, 0);
|
||||||
|
Assert.IsType<OkObjectResult>(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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue