before deleting Rules

This commit is contained in:
2021-05-08 10:26:04 -05:00
parent 05a9c71499
commit f8f779e84c
80 changed files with 1109 additions and 832 deletions

View File

@@ -1,61 +1,55 @@
using Gameboard.Shogi.Api.ServiceModels.Messages;
using Gameboard.ShogiUI.Sockets.Repositories;
using Gameboard.ShogiUI.Sockets.Models;
using Gameboard.ShogiUI.Sockets.Repositories.RepositoryManagers;
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages;
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Threading.Tasks;
namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
{
public interface ICreateGameHandler
{
Task Handle(CreateGameRequest request, string userName);
}
// TODO: This doesn't need to be a socket action.
// It can be an API route and still tell socket connections about the new session.
public class CreateGameHandler : IActionHandler
public class CreateGameHandler : ICreateGameHandler
{
private readonly IGameboardRepository repository;
private readonly IGameboardRepositoryManager manager;
private readonly ISocketCommunicationManager communicationManager;
public CreateGameHandler(
ISocketCommunicationManager communicationManager,
IGameboardRepository repository)
IGameboardRepositoryManager manager)
{
this.repository = repository;
this.manager = manager;
this.communicationManager = communicationManager;
}
public async Task Handle(string json, string userName)
public async Task Handle(CreateGameRequest request, string userName)
{
var request = JsonConvert.DeserializeObject<CreateGameRequest>(json);
var sessionName = await repository.PostSession(new PostSession
var model = new Session(request.GameName, request.IsPrivate, userName);
var success = await manager.CreateSession(model);
if (!success)
{
SessionName = request.GameName,
PlayerName = userName,
IsPrivate = request.IsPrivate
});
var error = new CreateGameResponse(request.Action)
{
Error = "Unable to create game with this name."
};
await communicationManager.BroadcastToPlayers(error, userName);
}
var response = new CreateGameResponse(request.Action)
{
PlayerName = userName,
Game = new Game
{
GameName = sessionName,
Players = new[] { userName }
}
Game = model.ToServiceModel()
};
if (string.IsNullOrWhiteSpace(sessionName))
{
response.Error = "Game already exists.";
}
var task = request.IsPrivate
? communicationManager.BroadcastToPlayers(response, userName)
: communicationManager.BroadcastToAll(response);
if (request.IsPrivate)
{
await communicationManager.BroadcastToPlayers(response, userName);
}
else
{
await communicationManager.BroadcastToAll(response);
}
await task;
}
}
}

View File

@@ -1,15 +0,0 @@
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
using System.Threading.Tasks;
namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
{
public interface IActionHandler
{
/// <summary>
/// Responsible for parsing json and handling the request.
/// </summary>
Task Handle(string json, string userName);
}
public delegate IActionHandler ActionHandlerResolver(ClientAction action);
}

View File

@@ -1,13 +1,14 @@
using Gameboard.Shogi.Api.ServiceModels.Messages;
using Gameboard.ShogiUI.Sockets.Repositories;
using Gameboard.ShogiUI.Sockets.Repositories;
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages;
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
using Newtonsoft.Json;
using System.Threading.Tasks;
namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
{
public class JoinByCodeHandler : IActionHandler
public interface IJoinByCodeHandler
{
Task Handle(JoinByCodeRequest request, string userName);
}
public class JoinByCodeHandler : IJoinByCodeHandler
{
private readonly IGameboardRepository repository;
private readonly ISocketCommunicationManager communicationManager;
@@ -20,44 +21,44 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
this.communicationManager = communicationManager;
}
public async Task Handle(string json, string userName)
public async Task Handle(JoinByCodeRequest request, string userName)
{
var request = JsonConvert.DeserializeObject<JoinByCode>(json);
var sessionName = await repository.PostJoinPrivateSession(new PostJoinPrivateSession
{
PlayerName = userName,
JoinCode = request.JoinCode
});
//var request = JsonConvert.DeserializeObject<JoinByCode>(json);
//var sessionName = await repository.PostJoinPrivateSession(new PostJoinPrivateSession
//{
// PlayerName = userName,
// JoinCode = request.JoinCode
//});
if (sessionName == null)
{
var response = new JoinGameResponse(ClientAction.JoinByCode)
{
PlayerName = userName,
GameName = sessionName,
Error = "Error joining game."
};
await communicationManager.BroadcastToPlayers(response, userName);
}
else
{
// Other members of the game see a regular JoinGame occur.
var response = new JoinGameResponse(ClientAction.JoinGame)
{
PlayerName = userName,
GameName = sessionName
};
// At this time, userName hasn't subscribed and won't receive this message.
await communicationManager.BroadcastToGame(sessionName, response);
//if (sessionName == null)
//{
// var response = new JoinGameResponse(ClientAction.JoinByCode)
// {
// PlayerName = userName,
// GameName = sessionName,
// Error = "Error joining game."
// };
// await communicationManager.BroadcastToPlayers(response, userName);
//}
//else
//{
// // Other members of the game see a regular JoinGame occur.
// var response = new JoinGameResponse(ClientAction.JoinGame)
// {
// PlayerName = userName,
// GameName = sessionName
// };
// // At this time, userName hasn't subscribed and won't receive this message.
// await communicationManager.BroadcastToGame(sessionName, response);
// The player joining sees the JoinByCode occur.
response = new JoinGameResponse(ClientAction.JoinByCode)
{
PlayerName = userName,
GameName = sessionName
};
await communicationManager.BroadcastToPlayers(response, userName);
}
// // The player joining sees the JoinByCode occur.
// response = new JoinGameResponse(ClientAction.JoinByCode)
// {
// PlayerName = userName,
// GameName = sessionName
// };
// await communicationManager.BroadcastToPlayers(response, userName);
//}
}
}
}

View File

@@ -1,13 +1,14 @@
using Gameboard.Shogi.Api.ServiceModels.Messages;
using Gameboard.ShogiUI.Sockets.Repositories;
using Gameboard.ShogiUI.Sockets.Repositories;
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages;
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
using Newtonsoft.Json;
using System.Threading.Tasks;
namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
{
public class JoinGameHandler : IActionHandler
public interface IJoinGameHandler
{
Task Handle(JoinGameRequest request, string userName);
}
public class JoinGameHandler : IJoinGameHandler
{
private readonly IGameboardRepository gameboardRepository;
private readonly ISocketCommunicationManager communicationManager;
@@ -19,30 +20,30 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
this.communicationManager = communicationManager;
}
public async Task Handle(string json, string userName)
public async Task Handle(JoinGameRequest request, string userName)
{
var request = JsonConvert.DeserializeObject<JoinGameRequest>(json);
//var request = JsonConvert.DeserializeObject<JoinGameRequest>(json);
var joinSucceeded = await gameboardRepository.PutJoinPublicSession(new PutJoinPublicSession
{
PlayerName = userName,
SessionName = request.GameName
});
//var joinSucceeded = await gameboardRepository.PutJoinPublicSession(new PutJoinPublicSession
//{
// PlayerName = userName,
// SessionName = request.GameName
//});
var response = new JoinGameResponse(ClientAction.JoinGame)
{
PlayerName = userName,
GameName = request.GameName
};
if (joinSucceeded)
{
await communicationManager.BroadcastToAll(response);
}
else
{
response.Error = "Game is full.";
await communicationManager.BroadcastToPlayers(response, userName);
}
//var response = new JoinGameResponse(ClientAction.JoinGame)
//{
// PlayerName = userName,
// GameName = request.GameName
//};
//if (joinSucceeded)
//{
// await communicationManager.BroadcastToAll(response);
//}
//else
//{
// response.Error = "Game is full.";
// await communicationManager.BroadcastToPlayers(response, userName);
//}
}
}
}

View File

@@ -1,16 +1,19 @@
using Gameboard.ShogiUI.Sockets.Models;
using Gameboard.ShogiUI.Sockets.Repositories;
using Gameboard.ShogiUI.Sockets.Repositories;
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages;
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
using Newtonsoft.Json;
using System.Linq;
using System.Threading.Tasks;
namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
{
public interface IListGamesHandler
{
Task Handle(ListGamesRequest request, string userName);
}
// TODO: This doesn't need to be a socket action.
// It can be an HTTP route.
public class ListGamesHandler : IActionHandler
public class ListGamesHandler : IListGamesHandler
{
private readonly ISocketCommunicationManager communicationManager;
private readonly IGameboardRepository repository;
@@ -23,16 +26,10 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
this.repository = repository;
}
public async Task Handle(string json, string userName)
public async Task Handle(ListGamesRequest _, string userName)
{
var request = JsonConvert.DeserializeObject<ListGamesRequest>(json);
var getGamesResponse = string.IsNullOrWhiteSpace(userName)
? await repository.GetGames()
: await repository.GetGames(userName);
var games = getGamesResponse.Sessions
.OrderBy(s => s.Player1 == userName || s.Player2 == userName)
.Select(s => new Session(s).ToServiceModel()); // yuck
var sessions = await repository.ReadSessions();
var games = sessions.Select(s => s.ToServiceModel()); // yuck
var response = new ListGamesResponse(ClientAction.ListGames)
{

View File

@@ -3,16 +3,20 @@ using Gameboard.ShogiUI.Sockets.Repositories;
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages;
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Linq;
using System.Threading.Tasks;
namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
{
public interface ILoadGameHandler
{
Task Handle(LoadGameRequest request, string userName);
}
/// <summary>
/// Subscribes a user to messages for a session and loads that session into the BoardManager for playing.
/// </summary>
public class LoadGameHandler : IActionHandler
public class LoadGameHandler : ILoadGameHandler
{
private readonly ILogger<LoadGameHandler> logger;
private readonly IGameboardRepository gameboardRepository;
@@ -31,35 +35,35 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
this.boardManager = boardManager;
}
public async Task Handle(string json, string userName)
public async Task Handle(LoadGameRequest request, string userName)
{
var request = JsonConvert.DeserializeObject<LoadGameRequest>(json);
var gameTask = gameboardRepository.GetGame(request.GameName);
var moveTask = gameboardRepository.GetMoves(request.GameName);
var readSession = gameboardRepository.ReadSession(request.GameName);
var readStates = gameboardRepository.ReadBoardStates(request.GameName);
var sessionModel = await gameTask;
var sessionModel = await readSession;
if (sessionModel == null)
{
logger.LogWarning("{action} - {user} was unable to load session named {session}.", ClientAction.LoadGame, userName, request.GameName);
var response = new LoadGameResponse(ClientAction.LoadGame) { Error = "Game not found." };
await communicationManager.BroadcastToPlayers(response, userName);
var error = new LoadGameResponse(ClientAction.LoadGame) { Error = "Game not found." };
await communicationManager.BroadcastToPlayers(error, userName);
return;
}
else
communicationManager.SubscribeToGame(sessionModel, userName);
var boardStates = await readStates;
var moveModels = boardStates
.Where(_ => _.Move != null)
.Select(_ => _.Move!.ToRulesModel())
.ToList();
var shogiBoard = new ShogiBoard(moveModels);
boardManager.Add(sessionModel.Name, shogiBoard);
var response = new LoadGameResponse(ClientAction.LoadGame)
{
var moveModels = await moveTask;
communicationManager.SubscribeToGame(sessionModel, userName);
var boardMoves = moveModels.Select(_ => _.ToBoardModel()).ToList();
var shogiBoard = new ShogiBoard(boardMoves);
boardManager.Add(sessionModel.Name, shogiBoard);
var response = new LoadGameResponse(ClientAction.LoadGame)
{
Game = sessionModel.ToServiceModel(),
BoardState = new Models.BoardState(shogiBoard).ToServiceModel()
};
await communicationManager.BroadcastToPlayers(response, userName);
}
Game = sessionModel.ToServiceModel(),
BoardState = new Models.BoardState(shogiBoard).ToServiceModel()
};
await communicationManager.BroadcastToPlayers(response, userName);
}
}
}

View File

@@ -1,14 +1,17 @@
using Gameboard.Shogi.Api.ServiceModels.Messages;
using Gameboard.ShogiUI.Sockets.Models;
using Gameboard.ShogiUI.Sockets.Models;
using Gameboard.ShogiUI.Sockets.Repositories;
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages;
using Newtonsoft.Json;
using System.Threading.Tasks;
using Service = Gameboard.ShogiUI.Sockets.ServiceModels.Socket;
namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
{
public class MoveHandler : IActionHandler
public interface IMoveHandler
{
Task Handle(MoveRequest request, string userName);
}
public class MoveHandler : IMoveHandler
{
private readonly IBoardManager boardManager;
private readonly IGameboardRepository gameboardRepository;
@@ -23,43 +26,43 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
this.communicationManager = communicationManager;
}
public async Task Handle(string json, string userName)
public async Task Handle(MoveRequest request, string userName)
{
var request = JsonConvert.DeserializeObject<Service.Messages.MoveRequest>(json);
var moveModel = new Move(request.Move);
var board = boardManager.Get(request.GameName);
if (board == null)
{
// TODO: Find a flow for this
var response = new Service.Messages.MoveResponse(Service.Types.ClientAction.Move)
{
Error = $"Game isn't loaded. Send a message with the {Service.Types.ClientAction.LoadGame} action first."
};
await communicationManager.BroadcastToPlayers(response, userName);
//var request = JsonConvert.DeserializeObject<Service.Messages.MoveRequest>(json);
//var moveModel = new Move(request.Move);
//var board = boardManager.Get(request.GameName);
//if (board == null)
//{
// // TODO: Find a flow for this
// var response = new Service.Messages.MoveResponse(Service.Types.ClientAction.Move)
// {
// Error = $"Game isn't loaded. Send a message with the {Service.Types.ClientAction.LoadGame} action first."
// };
// await communicationManager.BroadcastToPlayers(response, userName);
}
var boardMove = moveModel.ToBoardModel();
var moveSuccess = board.Move(boardMove);
if (moveSuccess)
{
await gameboardRepository.PostMove(request.GameName, new PostMove(moveModel.ToApiModel()));
var boardState = new BoardState(board);
var response = new Service.Messages.MoveResponse(Service.Types.ClientAction.Move)
{
GameName = request.GameName,
PlayerName = userName,
BoardState = boardState.ToServiceModel()
};
await communicationManager.BroadcastToGame(request.GameName, response);
}
else
{
var response = new Service.Messages.MoveResponse(Service.Types.ClientAction.Move)
{
Error = "Invalid move."
};
await communicationManager.BroadcastToPlayers(response, userName);
}
//}
//var boardMove = moveModel.ToBoardModel();
//var moveSuccess = board.Move(boardMove);
//if (moveSuccess)
//{
// await gameboardRepository.PostMove(request.GameName, new PostMove(moveModel.ToApiModel()));
// var boardState = new BoardState(board);
// var response = new Service.Messages.MoveResponse(Service.Types.ClientAction.Move)
// {
// GameName = request.GameName,
// PlayerName = userName,
// BoardState = boardState.ToServiceModel()
// };
// await communicationManager.BroadcastToGame(request.GameName, response);
//}
//else
//{
// var response = new Service.Messages.MoveResponse(Service.Types.ClientAction.Move)
// {
// Error = "Invalid move."
// };
// await communicationManager.BroadcastToPlayers(response, userName);
//}
}
}
}