1
0
mirror of synced 2025-01-18 14:24:02 +01:00

Fix alive, migrate write, add online matching stub, start rank

This commit is contained in:
asesidaa 2023-02-14 02:47:08 +08:00
parent 5c85e83c3e
commit 02a241567c
17 changed files with 341 additions and 139 deletions

View File

@ -12,6 +12,7 @@
<ItemGroup>
<PackageReference Include="ChoETL" Version="1.2.1.52" />
<PackageReference Include="FlexLabs.EntityFrameworkCore.Upsert" Version="7.0.0" />
<PackageReference Include="MediatR" Version="11.1.0" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="11.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
@ -21,4 +22,8 @@
<PackageReference Include="Throw" Version="1.3.1" />
</ItemGroup>
<ItemGroup>
<Folder Include="Game\Rank" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,9 @@
namespace Application.Common.Helpers;
public static class TimeHelper
{
public static string CurrentTimeToString()
{
return DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
}
}

View File

@ -1,6 +1,18 @@
namespace Application.Game.Card.OnlineMatching;
using Application.Common.Models;
using Application.Interfaces;
public class StartOnlineMatchingCommand
namespace Application.Game.Card.OnlineMatching;
public record StartOnlineMatchingCommand(long CardId, string Data) : IRequestWrapper<string>;
public class StartOnlineMatchingCommandHandler : CardRequestHandlerBase<StartOnlineMatchingCommand, string>
{
public StartOnlineMatchingCommandHandler(ICardDependencyAggregate aggregate) : base(aggregate)
{
}
public override Task<ServiceResult<string>> Handle(StartOnlineMatchingCommand request, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
}

View File

@ -1,6 +1,18 @@
namespace Application.Game.Card.OnlineMatching;
using Application.Common.Models;
using Application.Interfaces;
public class UpdateOnlineMatchingCommand
namespace Application.Game.Card.OnlineMatching;
public record UpdateOnlineMatchingCommand(long CardId, string Data) : IRequestWrapper<string>;
public class UpdateOnlineMatchingCommandHandler : CardRequestHandlerBase<UpdateOnlineMatchingCommand, string>
{
public UpdateOnlineMatchingCommandHandler(ICardDependencyAggregate aggregate) : base(aggregate)
{
}
public override Task<ServiceResult<string>> Handle(UpdateOnlineMatchingCommand request, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
}

View File

@ -1,6 +1,18 @@
namespace Application.Game.Card.OnlineMatching;
using Application.Common.Models;
using Application.Interfaces;
public class UploadOnlineMatchingResultCommand
namespace Application.Game.Card.OnlineMatching;
public record UploadOnlineMatchingResultCommand(long CardId, string Data) : IRequestWrapper<string>;
public class UploadOnlineMatchingResultCommandHandler : CardRequestHandlerBase<UploadOnlineMatchingResultCommand, string>
{
public UploadOnlineMatchingResultCommandHandler(ICardDependencyAggregate aggregate) : base(aggregate)
{
}
public override Task<ServiceResult<string>> Handle(UploadOnlineMatchingResultCommand request, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
}

View File

@ -0,0 +1,42 @@
using Application.Common.Extensions;
using Application.Common.Models;
using Application.Dto;
using Application.Interfaces;
using Application.Mappers;
using Domain.Enums;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
namespace Application.Game.Card.Write;
public record WriteCardBDataCommand(long CardId, string Data) : IRequestWrapper<string>;
public class WriteCardBDataCommandHandler : CardRequestHandlerBase<WriteCardBDataCommand, string>
{
private readonly ILogger<WriteCardBDataCommandHandler> logger;
public WriteCardBDataCommandHandler(ICardDependencyAggregate aggregate, ILogger<WriteCardBDataCommandHandler> logger) : base(aggregate)
{
this.logger = logger;
}
public override async Task<ServiceResult<string>> Handle(WriteCardBDataCommand request, CancellationToken cancellationToken)
{
var exists = await CardDbContext.CardMains.AnyAsync(card => card.CardId == request.CardId, cancellationToken: cancellationToken);
if (!exists)
{
logger.LogWarning("Card id: {CardId} does not exist!", request.CardId);
return ServiceResult.Failed<string>(
new ServiceError($"Card id: {request.CardId} does not exist!", (int)CardReturnCode.CardNotRegistered));
}
var dto = request.Data.DeserializeCardData<CardBDatumDto>();
var data = dto.DtoToCardBDatum();
data.CardId = request.CardId;
CardDbContext.CardBdata.Upsert(data);
await CardDbContext.SaveChangesAsync(cancellationToken);
return new ServiceResult<string>(request.Data);
}
}

View File

@ -0,0 +1,53 @@
using Application.Common.Extensions;
using Application.Common.Helpers;
using Application.Common.Models;
using Application.Dto;
using Application.Interfaces;
using Application.Mappers;
using Domain.Enums;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
namespace Application.Game.Card.Write;
public record WriteCardCommand(long CardId, string Data) : IRequestWrapper<string>;
public class WriteCardCommandHandler : CardRequestHandlerBase<WriteCardCommand, string>
{
private readonly ILogger<WriteCardCommandHandler> logger;
public WriteCardCommandHandler(ICardDependencyAggregate aggregate, ILogger<WriteCardCommandHandler> logger) : base(aggregate)
{
this.logger = logger;
}
public override async Task<ServiceResult<string>> Handle(WriteCardCommand request, CancellationToken cancellationToken)
{
var dto = request.Data.DeserializeCardData<CardDto>();
dto.CardId = request.CardId;
var card = await CardDbContext.CardMains.FirstOrDefaultAsync(card => card.CardId == request.CardId, cancellationToken: cancellationToken);
if (card is null)
{
logger.LogInformation("Creating new card {CardId}", request.CardId);
card = dto.CardDtoToCardMain();
card.Created = TimeHelper.CurrentTimeToString();
CardDbContext.CardMains.Add(card);
}
else
{
logger.LogInformation("Updating {CardId}", request.CardId);
card.Fcol1 = dto.Fcol1;
card.Fcol2 = dto.Fcol2;
card.Fcol3 = dto.Fcol3;
card.ScoreI1 = dto.ScoreI1;
card.Modified = TimeHelper.CurrentTimeToString();
CardDbContext.CardMains.Update(card);
}
await CardDbContext.SaveChangesAsync(cancellationToken);
return new ServiceResult<string>(request.Data);
}
}

View File

@ -0,0 +1,43 @@
using Application.Common.Extensions;
using Application.Common.Models;
using Application.Dto;
using Application.Interfaces;
using Application.Mappers;
using Domain.Enums;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
namespace Application.Game.Card.Write;
public record WriteCardDetailCommand(long CardId, string Data) : IRequestWrapper<string>;
public class WriteCardDetailCommandHandler : CardRequestHandlerBase<WriteCardDetailCommand, string>
{
private readonly ILogger<WriteCardDetailCommandHandler> logger;
public WriteCardDetailCommandHandler(ICardDependencyAggregate aggregate, ILogger<WriteCardDetailCommandHandler> logger) : base(aggregate)
{
this.logger = logger;
}
public override async Task<ServiceResult<string>> Handle(WriteCardDetailCommand request, CancellationToken cancellationToken)
{
var exists = await CardDbContext.CardMains.AnyAsync(card => card.CardId == request.CardId, cancellationToken: cancellationToken);
if (!exists)
{
logger.LogWarning("Card id: {CardId} does not exist!", request.CardId);
return ServiceResult.Failed<string>(
new ServiceError($"Card id: {request.CardId} does not exist!", (int)CardReturnCode.CardNotRegistered));
}
var dto = request.Data.DeserializeCardData<CardDetailDto>();
var detail = dto.DtoToCardDetail();
detail.CardId = request.CardId;
detail.LastPlayTime = DateTime.Now;
CardDbContext.CardDetails.Upsert(detail);
await CardDbContext.SaveChangesAsync(cancellationToken);
return new ServiceResult<string>(request.Data);
}
}

View File

@ -8,4 +8,6 @@ namespace Application.Mappers;
public static partial class CardBDatumMapper
{
public static partial CardBDatumDto CardBDatumToDto(this CardBdatum cardBdatum);
public static partial CardBdatum DtoToCardBDatum(this CardBDatumDto dto);
}

View File

@ -8,6 +8,5 @@ namespace Application.Mappers;
public static partial class CardMapper
{
public static partial CardDto CardMainToCardDto(this CardMain cardMain);
public static partial CardMain CardDtoToCardMain(this CardDto cardDto);
}

View File

@ -0,0 +1,10 @@
namespace Domain.Enums;
public enum RankingCommandType
{
GlobalRank = 4119,
PlayNumRank = 6657,
EventRank = 6661,
MonthlyRank = 6666,
ShopRank = 4098
}

View File

@ -1,3 +1,16 @@
{
"Kestrel": {
"Endpoints": {
"Http": {
"Url": "http://0.0.0.0:80"
},
"Https": {
"Url": "https://0.0.0.0:443"
},
"Test": {
"Url": "http://0.0.0.0:5107"
}
}
},
"ServerIp": "127.0.0.1"
}

View File

@ -17,7 +17,7 @@ public class AliveController : ControllerBase
return Ok(response);
}
[HttpGet("/{id}/Alive.txt")]
[HttpGet("{id}/Alive.txt")]
public IActionResult GetAliveFile()
{
return Ok("");

View File

@ -2,6 +2,7 @@
using Application.Common.Models;
using Application.Game.Card;
using Application.Game.Card.Management;
using Application.Game.Card.OnlineMatching;
using Application.Game.Card.Read;
using Application.Game.Card.Session;
using Application.Game.Card.Write;
@ -28,120 +29,69 @@ public class CardController : BaseController<CardController>
}
request.Data = WebUtility.UrlDecode(request.Data);
var result = ServiceResult.Failed<string>(ServiceError.DefaultError);
ServiceResult<string> result;
switch (cardCommandType)
{
case CardCommandType.CardReadRequest:
case CardCommandType.CardWriteRequest:
{
switch (cardRequestType)
result = cardRequestType switch
{
case CardRequestType.ReadCard:
result = await Mediator.Send(new ReadCardQuery(request.CardId));
break;
case CardRequestType.ReadCardDetail:
result = await Mediator.Send(new ReadCardDetailQuery(request.CardId, request.Data));
break;
case CardRequestType.ReadCardDetails:
result = await Mediator.Send(new ReadAllCardDetailsQuery(request.CardId));
break;
case CardRequestType.ReadCardBData:
result = await Mediator.Send(new ReadCardBDataQuery(request.CardId));
break;
case CardRequestType.ReadAvatar:
result = await Mediator.Send(new ReadAvatarQuery(request.CardId));
break;
case CardRequestType.ReadItem:
result = await Mediator.Send(new ReadItemQuery(request.CardId));
break;
case CardRequestType.ReadSkin:
result = await Mediator.Send(new ReadSkinQuery(request.CardId));
break;
case CardRequestType.ReadTitle:
result = await Mediator.Send(new ReadTitleQuery(request.CardId));
break;
case CardRequestType.ReadNavigator:
result = await Mediator.Send(new ReadNavigatorQuery(request.CardId));
break;
case CardRequestType.ReadSoundEffect:
result = await Mediator.Send(new ReadSoundEffectQuery(request.CardId));
break;
case CardRequestType.ReadMusic:
result = await Mediator.Send(new ReadMusicQuery(request.CardId));
break;
case CardRequestType.ReadMusicAou:
result = await Mediator.Send(new ReadMusicAouQuery(request.CardId));
break;
case CardRequestType.ReadMusicExtra:
result = await Mediator.Send(new ReadMusicExtraQuery(request.CardId));
break;
case CardRequestType.ReadEventReward:
result = await Mediator.Send(new ReadEventRewardQuery(request.CardId));
break;
case CardRequestType.ReadCoin:
result = await Mediator.Send(new ReadCoinQuery(request.CardId));
break;
case CardRequestType.ReadUnlockReward:
result = await Mediator.Send(new ReadUnlockRewardQuery(request.CardId));
break;
case CardRequestType.ReadUnlockKeynum:
result = await Mediator.Send(new ReadUnlockKeynumQuery(request.CardId));
break;
case CardRequestType.ReadGetMessage:
result = await Mediator.Send(new ReadGetMessageQuery(request.CardId));
break;
case CardRequestType.ReadCond:
result = await Mediator.Send(new ReadCondQuery(request.CardId));
break;
case CardRequestType.ReadTotalTrophy:
result = await Mediator.Send(new ReadTotalTrophyQuery(request.CardId));
break;
case CardRequestType.GetSession:
case CardRequestType.StartSession:
result = await Mediator.Send(new GetSessionCommand(request.CardId, request.Mac));
break;
case CardRequestType.WriteCard:
break;
case CardRequestType.WriteCardDetail:
break;
case CardRequestType.WriteCardBData:
break;
case CardRequestType.WriteAvatar:
result = await Mediator.Send(new WriteAvatarCommand(request.CardId, request.Data));
break;
case CardRequestType.WriteItem:
result = await Mediator.Send(new WriteItemCommand(request.CardId, request.Data));
break;
case CardRequestType.WriteTitle:
result = await Mediator.Send(new WriteTitleCommand(request.CardId, request.Data));
break;
case CardRequestType.WriteMusicDetail:
result = await Mediator.Send(new WriteMusicDetailCommand(request.CardId, request.Data));
break;
case CardRequestType.WriteNavigator:
result = await Mediator.Send(new WriteNavigatorCommand(request.CardId, request.Data));
break;
case CardRequestType.WriteCoin:
result = await Mediator.Send(new WriteCoinCommand(request.CardId, request.Data));
break;
case CardRequestType.WriteSkin:
result = await Mediator.Send(new WriteSkinCommand(request.CardId, request.Data));
break;
case CardRequestType.WriteUnlockKeynum:
result = await Mediator.Send(new WriteUnlockKeynumCommand(request.CardId, request.Data));
break;
case CardRequestType.WriteSoundEffect:
result = await Mediator.Send(new WriteSoundEffectCommand(request.CardId, request.Data));
break;
case CardRequestType.StartOnlineMatching:
break;
case CardRequestType.UpdateOnlineMatching:
break;
case CardRequestType.UploadOnlineMatchingResult:
break;
default:
throw new ArgumentOutOfRangeException(message: "Should not happen", paramName:null);
}
CardRequestType.ReadCard => await Mediator.Send(new ReadCardQuery(request.CardId)),
CardRequestType.ReadCardDetail => await Mediator.Send(
new ReadCardDetailQuery(request.CardId, request.Data)),
CardRequestType.ReadCardDetails => await Mediator.Send(new ReadAllCardDetailsQuery(request.CardId)),
CardRequestType.ReadCardBData => await Mediator.Send(new ReadCardBDataQuery(request.CardId)),
CardRequestType.ReadAvatar => await Mediator.Send(new ReadAvatarQuery(request.CardId)),
CardRequestType.ReadItem => await Mediator.Send(new ReadItemQuery(request.CardId)),
CardRequestType.ReadSkin => await Mediator.Send(new ReadSkinQuery(request.CardId)),
CardRequestType.ReadTitle => await Mediator.Send(new ReadTitleQuery(request.CardId)),
CardRequestType.ReadMusic => await Mediator.Send(new ReadMusicQuery(request.CardId)),
CardRequestType.ReadEventReward => await Mediator.Send(new ReadEventRewardQuery(request.CardId)),
CardRequestType.ReadNavigator => await Mediator.Send(new ReadNavigatorQuery(request.CardId)),
CardRequestType.ReadMusicExtra => await Mediator.Send(new ReadMusicExtraQuery(request.CardId)),
CardRequestType.ReadMusicAou => await Mediator.Send(new ReadMusicAouQuery(request.CardId)),
CardRequestType.ReadCoin => await Mediator.Send(new ReadCoinQuery(request.CardId)),
CardRequestType.ReadUnlockReward => await Mediator.Send(new ReadUnlockRewardQuery(request.CardId)),
CardRequestType.ReadUnlockKeynum => await Mediator.Send(new ReadUnlockKeynumQuery(request.CardId)),
CardRequestType.ReadSoundEffect => await Mediator.Send(new ReadSoundEffectQuery(request.CardId)),
CardRequestType.ReadGetMessage => await Mediator.Send(new ReadGetMessageQuery(request.CardId)),
CardRequestType.ReadCond => await Mediator.Send(new ReadCondQuery(request.CardId)),
CardRequestType.ReadTotalTrophy => await Mediator.Send(new ReadTotalTrophyQuery(request.CardId)),
CardRequestType.GetSession or CardRequestType.StartSession => await Mediator.Send(
new GetSessionCommand(request.CardId, request.Mac)),
CardRequestType.WriteCard =>
await Mediator.Send(new WriteCardCommand(request.CardId, request.Data)),
CardRequestType.WriteCardDetail => await Mediator.Send(
new WriteCardDetailCommand(request.CardId, request.Data)),
CardRequestType.WriteCardBData => await Mediator.Send(
new WriteCardBDataCommand(request.CardId, request.Data)),
CardRequestType.WriteAvatar => await Mediator.Send(new WriteAvatarCommand(request.CardId,
request.Data)),
CardRequestType.WriteItem =>
await Mediator.Send(new WriteItemCommand(request.CardId, request.Data)),
CardRequestType.WriteTitle => await Mediator.Send(new WriteTitleCommand(request.CardId,
request.Data)),
CardRequestType.WriteMusicDetail => await Mediator.Send(
new WriteMusicDetailCommand(request.CardId, request.Data)),
CardRequestType.WriteNavigator => await Mediator.Send(
new WriteNavigatorCommand(request.CardId, request.Data)),
CardRequestType.WriteCoin =>
await Mediator.Send(new WriteCoinCommand(request.CardId, request.Data)),
CardRequestType.WriteSkin =>
await Mediator.Send(new WriteSkinCommand(request.CardId, request.Data)),
CardRequestType.WriteUnlockKeynum => await Mediator.Send(
new WriteUnlockKeynumCommand(request.CardId, request.Data)),
CardRequestType.WriteSoundEffect => await Mediator.Send(
new WriteSoundEffectCommand(request.CardId, request.Data)),
CardRequestType.StartOnlineMatching => await Mediator.Send(
new StartOnlineMatchingCommand(request.CardId, request.Data)),
CardRequestType.UpdateOnlineMatching => await Mediator.Send(
new UpdateOnlineMatchingCommand(request.CardId, request.Data)),
CardRequestType.UploadOnlineMatchingResult => await Mediator.Send(
new UploadOnlineMatchingResultCommand(request.CardId, request.Data)),
_ => throw new ArgumentOutOfRangeException(nameof(cardRequestType), cardRequestType, "Should not happen")
};
break;
}
case CardCommandType.RegisterRequest:
@ -151,7 +101,7 @@ public class CardController : BaseController<CardController>
result = await Mediator.Send(new CardReissueCommand(request.CardId));
break;
default:
throw new ArgumentOutOfRangeException(message: "Should not happen", paramName:null);
throw new ArgumentOutOfRangeException(nameof(cardCommandType), cardCommandType, "Should not happen");
}
if (result.Succeeded)

View File

@ -0,0 +1,34 @@
using Domain.Enums;
using Microsoft.AspNetCore.Mvc;
using Throw;
namespace MainServer.Controllers.Game;
[ApiController]
[Route("ranking")]
public class RankingController : BaseController<RankingController>
{
[HttpGet("ranking.php")]
public async Task<ActionResult<string>> Ranking([FromQuery(Name = "cmd_type")] int rankType)
{
var type = (RankingCommandType)rankType;
type.Throw().IfOutOfRange();
switch (type)
{
case RankingCommandType.GlobalRank:
break;
case RankingCommandType.PlayNumRank:
break;
case RankingCommandType.EventRank:
break;
case RankingCommandType.MonthlyRank:
break;
case RankingCommandType.ShopRank:
break;
default:
throw new ArgumentOutOfRangeException(nameof(type), type, "Should not happen!");
}
return "";
}
}

View File

@ -10,18 +10,18 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CertificateManager" Version="1.0.8"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.2"/>
<PackageReference Include="CertificateManager" Version="1.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="7.0.3"/>
<PackageReference Include="Serilog.AspNetCore" Version="6.1.1-dev-00293"/>
<PackageReference Include="Serilog.Expressions" Version="3.4.2-dev-00119"/>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0"/>
<PackageReference Include="System.Security.Cryptography.X509Certificates" Version="4.3.2"/>
<PackageReference Include="Throw" Version="1.3.1"/>
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="7.0.3" />
<PackageReference Include="Serilog.AspNetCore" Version="6.1.1-dev-00293" />
<PackageReference Include="Serilog.Expressions" Version="3.4.2-dev-00119" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="System.Security.Cryptography.X509Certificates" Version="4.3.2" />
<PackageReference Include="Throw" Version="1.3.1" />
</ItemGroup>
<ItemGroup>
@ -68,13 +68,13 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Infrastructure\Infrastructure.csproj"/>
<ProjectReference Include="..\Infrastructure\Infrastructure.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Controllers\API"/>
<Folder Include="Logs"/>
<Folder Include="wwwroot"/>
<Folder Include="Controllers\API" />
<Folder Include="Logs" />
<Folder Include="wwwroot" />
</ItemGroup>
</Project>

View File

@ -8,6 +8,7 @@ using Infrastructure.Persistence;
using MainServer.Filters;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.FileProviders;
using Serilog;
using Serilog.Extensions.Logging;
using Throw;
@ -64,9 +65,16 @@ try
builder.Services.AddApplication();
builder.Services.AddInfrastructure(builder.Configuration);
builder.Services.AddResponseCompression(options =>
{
options.EnableForHttps = true;
});
var app = builder.Build();
app.UseResponseCompression();
using (var scope = app.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<CardDbContext>();
@ -96,13 +104,11 @@ try
// Add content type for .cmp and .evt files as static files with unknown file extensions return 404 by default
// See https://learn.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-7.0#fileextensioncontenttypeprovider
// ReSharper disable once UseObjectOrCollectionInitializer
var provider = new FileExtensionContentTypeProvider();
provider.Mappings[".cmp"] = "text/plain";
provider.Mappings[".evt"] = "text/plain";
app.UseStaticFiles(new StaticFileOptions
{
ContentTypeProvider = provider
});
var contentTypeProvider = new FileExtensionContentTypeProvider();
contentTypeProvider.Mappings[".cmp"] = "text/plain";
contentTypeProvider.Mappings[".evt"] = "text/plain";
app.UseStaticFiles();
app.MapControllers();