diff --git a/Application/Application.csproj b/Application/Application.csproj index a11bca2..4fb7069 100644 --- a/Application/Application.csproj +++ b/Application/Application.csproj @@ -21,4 +21,10 @@ + + + + + + diff --git a/Application/Common/Extensions/XmlSerializationExtensions.cs b/Application/Common/Extensions/XmlSerializationExtensions.cs index 5e5efc2..ae5240a 100644 --- a/Application/Common/Extensions/XmlSerializationExtensions.cs +++ b/Application/Common/Extensions/XmlSerializationExtensions.cs @@ -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(this T source, string xpath) where T : class + { + var buffer = new StringBuilder(); + using var writer = new ChoXmlWriter(buffer).WithXPath(xpath).UseXmlSerialization(); + writer.Configuration.OmitXmlDeclaration = false; + writer.Configuration.DoNotEmitXmlNamespace = true; + writer.Write(source); + return buffer.ToString(); + } + + public static string SerializeCardDataList(this IEnumerable source, string xpath) where T : class + { + var buffer = new StringBuilder(); + using var writer = new ChoXmlWriter(buffer).WithXPath(xpath).UseXmlSerialization(); + writer.Configuration.OmitXmlDeclaration = false; + writer.Configuration.DoNotEmitXmlNamespace = true; + writer.Write(source); + return buffer.ToString(); + } } \ No newline at end of file diff --git a/Application/Dto/SessionDto.cs b/Application/Dto/SessionDto.cs new file mode 100644 index 0000000..abc508d --- /dev/null +++ b/Application/Dto/SessionDto.cs @@ -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; } +} \ No newline at end of file diff --git a/Application/Game/Card/CardRegisterCommand.cs b/Application/Game/Card/Management/CardRegisterCommand.cs similarity index 87% rename from Application/Game/Card/CardRegisterCommand.cs rename to Application/Game/Card/Management/CardRegisterCommand.cs index 1d94fc5..becefff 100644 --- a/Application/Game/Card/CardRegisterCommand.cs +++ b/Application/Game/Card/Management/CardRegisterCommand.cs @@ -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; diff --git a/Application/Game/Card/Management/CardReissueCommand.cs b/Application/Game/Card/Management/CardReissueCommand.cs new file mode 100644 index 0000000..4fd7e91 --- /dev/null +++ b/Application/Game/Card/Management/CardReissueCommand.cs @@ -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; + +public class CardReissueCommandHandler : CardRequestHandlerBase +{ + public CardReissueCommandHandler(ICardDependencyAggregate aggregate) : base(aggregate) + { + } + + public override Task> Handle(CardReissueCommand request, CancellationToken cancellationToken) + { + // TODO: Support actual reissue + var result = $"{(int)CardReturnCode.NotReissue}"; + return Task.FromResult(new ServiceResult(result)); + } +} \ No newline at end of file diff --git a/Application/Game/Card/Session/GetSessionCommand.cs b/Application/Game/Card/Session/GetSessionCommand.cs new file mode 100644 index 0000000..7c23ad4 --- /dev/null +++ b/Application/Game/Card/Session/GetSessionCommand.cs @@ -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; + +public class GetSessionCommandHandler : CardRequestHandlerBase +{ + private const string SESSION_XPATH = "/root/session"; + + public GetSessionCommandHandler(ICardDependencyAggregate aggregate) : base(aggregate) + { + } + + public override Task> 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(session.SerializeCardData(SESSION_XPATH))); + } +} \ No newline at end of file diff --git a/Domain/Enums/CardCommandType.cs b/Domain/Enums/CardCommandType.cs index 2bb09ff..44a9a07 100644 --- a/Domain/Enums/CardCommandType.cs +++ b/Domain/Enums/CardCommandType.cs @@ -1,4 +1,4 @@ -namespace Domain; +namespace Domain.Enums; public enum CardCommandType { diff --git a/Domain/Enums/CardRequestType.cs b/Domain/Enums/CardRequestType.cs index c593f45..46fd8a9 100644 --- a/Domain/Enums/CardRequestType.cs +++ b/Domain/Enums/CardRequestType.cs @@ -1,4 +1,4 @@ -namespace Domain; +namespace Domain.Enums; public enum CardRequestType { diff --git a/Domain/Enums/CardReturnCode.cs b/Domain/Enums/CardReturnCode.cs index 341e4f8..46b9a33 100644 --- a/Domain/Enums/CardReturnCode.cs +++ b/Domain/Enums/CardReturnCode.cs @@ -1,4 +1,4 @@ -namespace Domain; +namespace Domain.Enums; public enum CardReturnCode { @@ -23,5 +23,5 @@ public enum CardReturnCode /// /// Server side validation error /// - Unknown = 999 + Unknown = -1 } \ No newline at end of file diff --git a/MainServer/Controllers/Game/CardController.cs b/MainServer/Controllers/Game/CardController.cs index 85eb907..99c889d 100644 --- a/MainServer/Controllers/Game/CardController.cs +++ b/MainServer/Controllers/Game/CardController.cs @@ -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 switch (cardCommandType) { case CardCommandType.CardReadRequest: + case CardCommandType.CardWriteRequest: { switch (cardRequestType) { @@ -72,8 +76,8 @@ public class CardController : BaseController 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 } 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);