Skip to content

Commit

Permalink
Add debug logging to fort error cases (#1138)
Browse files Browse the repository at this point in the history
Investigating as part of #436.

Adds some debug logging to indicate the state of the fort at the time
these exceptions get thrown.
  • Loading branch information
SapiensAnatis authored Oct 31, 2024
1 parent fd08d56 commit 290835c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
7 changes: 7 additions & 0 deletions DragaliaAPI/DragaliaAPI.Test/Features/Fort/FortServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Time.Testing;
using MockQueryable;
using NSubstitute;
using Range = Moq.Range;

namespace DragaliaAPI.Test.Features.Fort;
Expand Down Expand Up @@ -428,6 +429,9 @@ public async Task BuildStart_InsufficientCarpenters_Throws()
.Setup(x => x.GetFortDetail())
.ReturnsAsync(new DbFortDetail() { ViewerId = 1, CarpenterNum = 1 });
mockFortRepository.Setup(x => x.GetActiveCarpenters()).ReturnsAsync(1);
mockFortRepository
.Setup(x => x.Builds)
.Returns(Array.Empty<DbFortBuild>().AsQueryable().BuildMock());

await fortService
.Invoking(x => x.BuildStart(FortPlants.BlueFlowers, 2, 3))
Expand Down Expand Up @@ -506,6 +510,9 @@ public async Task LevelupStart_InsufficientCarpenters_Throws()
.ReturnsAsync(new DbFortDetail() { ViewerId = 1, CarpenterNum = 1 });
mockFortRepository.Setup(x => x.GetActiveCarpenters()).ReturnsAsync(1);
mockFortRepository.Setup(x => x.GetBuilding(1)).ReturnsAsync(build);
mockFortRepository
.Setup(x => x.Builds)
.Returns(Array.Empty<DbFortBuild>().AsQueryable().BuildMock());

await fortService
.Invoking(x => x.LevelupStart(1))
Expand Down
42 changes: 37 additions & 5 deletions DragaliaAPI/DragaliaAPI/Features/Fort/FortService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -351,11 +351,24 @@ public async Task EndLevelup(long buildId)

DbFortBuild build = await fortRepository.GetBuilding(buildId);

if (
build.BuildStatus is not FortBuildStatus.LevelUp
|| dateTimeProvider.GetUtcNow() < build.BuildEndDate
)
DateTimeOffset time = dateTimeProvider.GetUtcNow();

if (build.BuildStatus is not FortBuildStatus.LevelUp || time < build.BuildEndDate)
{
logger.LogDebug(
"Building {@Build} has not finished levelling up. Current time: {Time}",
new
{
build.BuildId,
build.PlantId,
build.Level,
build.BuildStartDate,
build.BuildEndDate,
},
time
);
throw new InvalidOperationException($"This building has not completed levelling up.");
}

await FinishUpgrade(build, true);
}
Expand Down Expand Up @@ -503,9 +516,28 @@ private async Task Upgrade(FortPlantDetail plantDetail)
// Check Carpenter available
if (fortDetail.WorkingCarpenterNum >= fortDetail.CarpenterNum)
{
var builds = await fortRepository
.Builds.Where(x => x.BuildEndDate != DateTimeOffset.UnixEpoch)
.Select(x => new
{
x.BuildId,
x.PlantId,
x.Level,
x.BuildStartDate,
x.BuildEndDate,
})
.ToListAsync();

logger.LogDebug(
"Failed to perform upgrade {@PlantDetail} - carpenters busy",
plantDetail
);
logger.LogDebug("FortDetail: {@FortDetail}", fortDetail);
logger.LogDebug("Currently in progress builds: {@Builds}", builds);

throw new DragaliaException(
ResultCode.FortBuildCarpenterBusy,
$"All carpenters are currently busy"
"All carpenters are currently busy"
);
}

Expand Down

0 comments on commit 290835c

Please sign in to comment.