massive checkpoint
This commit is contained in:
@@ -1,15 +0,0 @@
|
||||
using FluentValidation;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Types;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Services.RequestValidators
|
||||
{
|
||||
public class CreateGameRequestValidator : AbstractValidator<CreateGameRequest>
|
||||
{
|
||||
public CreateGameRequestValidator()
|
||||
{
|
||||
RuleFor(_ => _.Action).Equal(ClientAction.CreateGame);
|
||||
RuleFor(_ => _.GameName).NotEmpty();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
using FluentValidation;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Types;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Services.RequestValidators
|
||||
{
|
||||
public class ListGamesRequestValidator : AbstractValidator<ListGamesRequest>
|
||||
{
|
||||
public ListGamesRequestValidator()
|
||||
{
|
||||
RuleFor(_ => _.Action).Equal(ClientAction.ListGames);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
using FluentValidation;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Types;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Services.RequestValidators
|
||||
{
|
||||
public class LoadGameRequestValidator : AbstractValidator<LoadGameRequest>
|
||||
{
|
||||
public LoadGameRequestValidator()
|
||||
{
|
||||
RuleFor(_ => _.Action).Equal(ClientAction.LoadGame);
|
||||
RuleFor(_ => _.GameName).NotEmpty();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
using FluentValidation;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Types;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Services.RequestValidators
|
||||
{
|
||||
public class MoveRequestValidator : AbstractValidator<MoveRequest>
|
||||
{
|
||||
public MoveRequestValidator()
|
||||
{
|
||||
RuleFor(_ => _.Action).Equal(ClientAction.Move);
|
||||
RuleFor(_ => _.GameName).NotEmpty();
|
||||
RuleFor(_ => _.Move.From)
|
||||
.Null()
|
||||
.When(_ => _.Move.PieceFromCaptured.HasValue)
|
||||
.WithMessage("Move.From and Move.PieceFromCaptured are mutually exclusive properties.");
|
||||
RuleFor(_ => _.Move.From)
|
||||
.NotEmpty()
|
||||
.When(_ => !_.Move.PieceFromCaptured.HasValue)
|
||||
.WithMessage("Move.From and Move.PieceFromCaptured are mutually exclusive properties.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -31,65 +31,44 @@ namespace Gameboard.ShogiUI.Sockets.Services
|
||||
private readonly ILogger<SocketService> logger;
|
||||
private readonly ISocketConnectionManager communicationManager;
|
||||
private readonly IGameboardRepository gameboardRepository;
|
||||
private readonly ISocketTokenManager tokenManager;
|
||||
private readonly ICreateGameHandler createGameHandler;
|
||||
private readonly IGameboardManager gameboardManager;
|
||||
private readonly ISocketTokenCache tokenManager;
|
||||
private readonly IJoinByCodeHandler joinByCodeHandler;
|
||||
private readonly IJoinGameHandler joinGameHandler;
|
||||
private readonly IListGamesHandler listGamesHandler;
|
||||
private readonly ILoadGameHandler loadGameHandler;
|
||||
private readonly IMoveHandler moveHandler;
|
||||
private readonly IValidator<CreateGameRequest> createGameRequestValidator;
|
||||
private readonly IValidator<JoinByCodeRequest> joinByCodeRequestValidator;
|
||||
private readonly IValidator<JoinGameRequest> joinGameRequestValidator;
|
||||
private readonly IValidator<ListGamesRequest> listGamesRequestValidator;
|
||||
private readonly IValidator<LoadGameRequest> loadGameRequestValidator;
|
||||
private readonly IValidator<MoveRequest> moveRequestValidator;
|
||||
|
||||
public SocketService(
|
||||
ILogger<SocketService> logger,
|
||||
ISocketConnectionManager communicationManager,
|
||||
IGameboardRepository gameboardRepository,
|
||||
ISocketTokenManager tokenManager,
|
||||
ICreateGameHandler createGameHandler,
|
||||
IGameboardManager gameboardManager,
|
||||
ISocketTokenCache tokenManager,
|
||||
IJoinByCodeHandler joinByCodeHandler,
|
||||
IJoinGameHandler joinGameHandler,
|
||||
IListGamesHandler listGamesHandler,
|
||||
ILoadGameHandler loadGameHandler,
|
||||
IMoveHandler moveHandler,
|
||||
IValidator<CreateGameRequest> createGameRequestValidator,
|
||||
IValidator<JoinByCodeRequest> joinByCodeRequestValidator,
|
||||
IValidator<JoinGameRequest> joinGameRequestValidator,
|
||||
IValidator<ListGamesRequest> listGamesRequestValidator,
|
||||
IValidator<LoadGameRequest> loadGameRequestValidator,
|
||||
IValidator<MoveRequest> moveRequestValidator
|
||||
IValidator<JoinGameRequest> joinGameRequestValidator
|
||||
) : base()
|
||||
{
|
||||
this.logger = logger;
|
||||
this.communicationManager = communicationManager;
|
||||
this.gameboardRepository = gameboardRepository;
|
||||
this.gameboardManager = gameboardManager;
|
||||
this.tokenManager = tokenManager;
|
||||
this.createGameHandler = createGameHandler;
|
||||
this.joinByCodeHandler = joinByCodeHandler;
|
||||
this.joinGameHandler = joinGameHandler;
|
||||
this.listGamesHandler = listGamesHandler;
|
||||
this.loadGameHandler = loadGameHandler;
|
||||
this.moveHandler = moveHandler;
|
||||
this.createGameRequestValidator = createGameRequestValidator;
|
||||
this.joinByCodeRequestValidator = joinByCodeRequestValidator;
|
||||
this.joinGameRequestValidator = joinGameRequestValidator;
|
||||
this.listGamesRequestValidator = listGamesRequestValidator;
|
||||
this.loadGameRequestValidator = loadGameRequestValidator;
|
||||
this.moveRequestValidator = moveRequestValidator;
|
||||
}
|
||||
|
||||
public async Task HandleSocketRequest(HttpContext context)
|
||||
{
|
||||
string? userName = null;
|
||||
if (context.Request.Cookies.ContainsKey(SocketController.WebSessionKey))
|
||||
var user = await gameboardManager.ReadUser(context.User);
|
||||
if (user?.WebSessionId != null)
|
||||
{
|
||||
// Guest account
|
||||
var webSessionId = Guid.Parse(context.Request.Cookies[SocketController.WebSessionKey]!);
|
||||
userName = (await gameboardRepository.ReadGuestUser(webSessionId))?.Name;
|
||||
userName = tokenManager.GetUsername(user.WebSessionId.Value);
|
||||
}
|
||||
else if (context.Request.Query.Keys.Contains("token"))
|
||||
{
|
||||
@@ -123,24 +102,6 @@ namespace Gameboard.ShogiUI.Sockets.Services
|
||||
}
|
||||
switch (request.Action)
|
||||
{
|
||||
case ClientAction.ListGames:
|
||||
{
|
||||
var req = JsonConvert.DeserializeObject<ListGamesRequest>(message);
|
||||
if (await ValidateRequestAndReplyIfInvalid(socket, listGamesRequestValidator, req))
|
||||
{
|
||||
await listGamesHandler.Handle(req, userName);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ClientAction.CreateGame:
|
||||
{
|
||||
var req = JsonConvert.DeserializeObject<CreateGameRequest>(message);
|
||||
if (await ValidateRequestAndReplyIfInvalid(socket, createGameRequestValidator, req))
|
||||
{
|
||||
await createGameHandler.Handle(req, userName);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ClientAction.JoinGame:
|
||||
{
|
||||
var req = JsonConvert.DeserializeObject<JoinGameRequest>(message);
|
||||
@@ -159,24 +120,6 @@ namespace Gameboard.ShogiUI.Sockets.Services
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ClientAction.LoadGame:
|
||||
{
|
||||
var req = JsonConvert.DeserializeObject<LoadGameRequest>(message);
|
||||
if (await ValidateRequestAndReplyIfInvalid(socket, loadGameRequestValidator, req))
|
||||
{
|
||||
await loadGameHandler.Handle(req, userName);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ClientAction.Move:
|
||||
{
|
||||
var req = JsonConvert.DeserializeObject<MoveRequest>(message);
|
||||
if (await ValidateRequestAndReplyIfInvalid(socket, moveRequestValidator, req))
|
||||
{
|
||||
await moveHandler.Handle(req, userName);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException ex)
|
||||
|
||||
Reference in New Issue
Block a user