diff --git a/TaikoLocalServer/Handlers/PurchaseSongCommand.cs b/TaikoLocalServer/Handlers/PurchaseSongCommand.cs index b7c217b..cdd9d69 100644 --- a/TaikoLocalServer/Handlers/PurchaseSongCommand.cs +++ b/TaikoLocalServer/Handlers/PurchaseSongCommand.cs @@ -3,13 +3,66 @@ using Throw; namespace TaikoLocalServer.Handlers; -public record PurchaseSongCommand(uint Baid, uint SongNo, uint TokenId, uint Price) : IRequest; +public record PurchaseSongCommand(uint Baid, uint SongNo, uint Type, uint TokenId, uint Price) : IRequest; + +public record PurchaseSongCommandCN(uint Baid, uint SongNo, uint TokenId, uint Price) : IRequest; public class PurchaseSongCommandHandler(TaikoDbContext context, ILogger logger) : IRequestHandler { public async Task Handle(PurchaseSongCommand request, CancellationToken cancellationToken) + { + var user = await context.UserData + .Include(u => u.Tokens) + .FirstOrDefaultAsync(u => u.Baid == request.Baid, cancellationToken); + user.ThrowIfNull($"User with baid {request.Baid} does not exist!"); + + var token = user.Tokens.FirstOrDefault(t => t.Id == request.TokenId); + + if (token is not null && token.Count >= request.Price) + { + token.Count -= (int)request.Price; + } + else + { + logger.LogError("User with baid {Baid} does not have enough tokens to purchase song with id {SongNo}!", request.Baid, request.SongNo); + return new CommonSongPurchaseResponse { Result = 0 }; + } + + if (request.Type == 1) + { + if (user.UnlockedUraSongIdList.Contains(request.SongNo)) + { + logger.LogWarning("User with baid {Baid} already has song with id {SongNo} unlocked!", request.Baid, request.SongNo); + return new CommonSongPurchaseResponse { Result = 0 }; + } + + user.UnlockedUraSongIdList.Add(request.SongNo); + } + else + { + if (user.UnlockedSongIdList.Contains(request.SongNo)) + { + logger.LogWarning("User with baid {Baid} already has song with id {SongNo} unlocked!", request.Baid, request.SongNo); + return new CommonSongPurchaseResponse { Result = 0 }; + } + + user.UnlockedSongIdList.Add(request.SongNo); + } + + context.UserData.Update(user); + await context.SaveChangesAsync(cancellationToken); + return new CommonSongPurchaseResponse { Result = 1, TokenCount = token.Count }; + } +} + + +public class PurchaseSongCommandHandlerCN(TaikoDbContext context, ILogger logger) + : IRequestHandler +{ + + public async Task Handle(PurchaseSongCommandCN request, CancellationToken cancellationToken) { var user = await context.UserData .Include(u => u.Tokens) @@ -33,6 +86,7 @@ public class PurchaseSongCommandHandler(TaikoDbContext context, ILogger