1
0
mirror of synced 2025-01-31 04:13:43 +01:00

Start card read migration

This commit is contained in:
asesidaa 2023-02-09 23:19:14 +08:00
parent 9c759c3b1a
commit 6eb71437cf
10 changed files with 113 additions and 15 deletions

View File

@ -21,4 +21,10 @@
<PackageReference Include="Throw" Version="1.3.1" />
</ItemGroup>
<ItemGroup>
<Folder Include="Game\Card\OnlineMatching" />
<Folder Include="Game\Card\Read" />
<Folder Include="Game\Card\Write" />
</ItemGroup>
</Project>

View File

@ -1,7 +1,8 @@
using ChoETL;
using System.Text;
using ChoETL;
using Throw;
namespace Application.Common;
namespace Application.Common.Extensions;
public static class XmlSerializationExtensions
{
@ -14,4 +15,24 @@ public static class XmlSerializationExtensions
return result;
}
public static string SerializeCardData<T>(this T source, string xpath) where T : class
{
var buffer = new StringBuilder();
using var writer = new ChoXmlWriter<T>(buffer).WithXPath(xpath).UseXmlSerialization();
writer.Configuration.OmitXmlDeclaration = false;
writer.Configuration.DoNotEmitXmlNamespace = true;
writer.Write(source);
return buffer.ToString();
}
public static string SerializeCardDataList<T>(this IEnumerable<T> source, string xpath) where T : class
{
var buffer = new StringBuilder();
using var writer = new ChoXmlWriter<T>(buffer).WithXPath(xpath).UseXmlSerialization();
writer.Configuration.OmitXmlDeclaration = false;
writer.Configuration.DoNotEmitXmlNamespace = true;
writer.Write(source);
return buffer.ToString();
}
}

View File

@ -0,0 +1,21 @@
using System.Xml.Serialization;
namespace Application.Dto;
public class SessionDto
{
[XmlElement(ElementName = "card_id")]
public long CardId { get; set; }
[XmlElement(ElementName = "mac_addr")]
public string Mac { get; set; } = "000000000000";
[XmlElement(ElementName = "session_id")]
public string SessionId { get; set; } = "12345678901234567890123456789012";
[XmlElement(ElementName = "expires")]
public int Expires { get; set; }
[XmlElement(ElementName = "player_id")]
public int PlayerId { get; set; }
}

View File

@ -1,14 +1,10 @@
using Application.Common;
using Application.Common.Exceptions;
using Application.Common.Extensions;
using Application.Common.Models;
using Application.Dto;
using Application.Interfaces;
using Application.Mappers;
using MediatR;
using Microsoft.Extensions.Logging;
using Throw;
namespace Application.Game.Card;
namespace Application.Game.Card.Management;
public record CardRegisterCommand(long CardId, string Data) : IRequestWrapper<string>;

View File

@ -0,0 +1,21 @@
using Application.Common.Models;
using Application.Interfaces;
using Domain.Enums;
namespace Application.Game.Card.Management;
public record CardReissueCommand(long CardId) : IRequestWrapper<string>;
public class CardReissueCommandHandler : CardRequestHandlerBase<CardReissueCommand, string>
{
public CardReissueCommandHandler(ICardDependencyAggregate aggregate) : base(aggregate)
{
}
public override Task<ServiceResult<string>> Handle(CardReissueCommand request, CancellationToken cancellationToken)
{
// TODO: Support actual reissue
var result = $"{(int)CardReturnCode.NotReissue}";
return Task.FromResult(new ServiceResult<string>(result));
}
}

View File

@ -0,0 +1,30 @@
using Application.Common.Extensions;
using Application.Common.Models;
using Application.Dto;
using Application.Interfaces;
namespace Application.Game.Card.Session;
public record GetSessionCommand(long CardId, string Mac) : IRequestWrapper<string>;
public class GetSessionCommandHandler : CardRequestHandlerBase<GetSessionCommand, string>
{
private const string SESSION_XPATH = "/root/session";
public GetSessionCommandHandler(ICardDependencyAggregate aggregate) : base(aggregate)
{
}
public override Task<ServiceResult<string>> Handle(GetSessionCommand request, CancellationToken cancellationToken)
{
var session = new SessionDto
{
CardId = request.CardId,
Mac = request.Mac,
PlayerId = 1,
Expires = 9999,
SessionId = "12345678901234567890123456789012"
};
return Task.FromResult(new ServiceResult<string>(session.SerializeCardData(SESSION_XPATH)));
}
}

View File

@ -1,4 +1,4 @@
namespace Domain;
namespace Domain.Enums;
public enum CardCommandType
{

View File

@ -1,4 +1,4 @@
namespace Domain;
namespace Domain.Enums;
public enum CardRequestType
{

View File

@ -1,4 +1,4 @@
namespace Domain;
namespace Domain.Enums;
public enum CardReturnCode
{
@ -23,5 +23,5 @@ public enum CardReturnCode
/// <summary>
/// Server side validation error
/// </summary>
Unknown = 999
Unknown = -1
}

View File

@ -1,7 +1,10 @@
using System.Net;
using Application.Common.Models;
using Application.Game.Card;
using Application.Game.Card.Management;
using Application.Game.Card.Session;
using Domain;
using Domain.Enums;
using Microsoft.AspNetCore.Mvc;
using Throw;
@ -28,6 +31,7 @@ public class CardController : BaseController<CardController>
switch (cardCommandType)
{
case CardCommandType.CardReadRequest:
case CardCommandType.CardWriteRequest:
{
switch (cardRequestType)
{
@ -72,8 +76,8 @@ public class CardController : BaseController<CardController>
case CardRequestType.ReadTotalTrophy:
break;
case CardRequestType.GetSession:
break;
case CardRequestType.StartSession:
result = await Mediator.Send(new GetSessionCommand(request.CardId, request.Mac));
break;
case CardRequestType.WriteCard:
@ -111,12 +115,11 @@ public class CardController : BaseController<CardController>
}
break;
}
case CardCommandType.CardWriteRequest:
break;
case CardCommandType.RegisterRequest:
result = await Mediator.Send(new CardRegisterCommand(request.CardId, request.Data));
break;
case CardCommandType.ReissueRequest:
result = await Mediator.Send(new CardReissueCommand(request.CardId));
break;
default:
throw new ArgumentOutOfRangeException(message: "Should not happen", paramName:null);