Fixed accidentally building the board from player2 perspective.

This commit is contained in:
2021-04-06 19:52:02 -05:00
parent 2d5c6b20b9
commit 05a9c71499
45 changed files with 441 additions and 276 deletions

View File

@@ -5,6 +5,7 @@ using Newtonsoft.Json;
using System.Threading.Tasks;
using Service = Gameboard.ShogiUI.Sockets.ServiceModels.Socket;
namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
{
public class MoveHandler : IActionHandler
@@ -25,31 +26,40 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
public async Task Handle(string json, string userName)
{
var request = JsonConvert.DeserializeObject<Service.Messages.MoveRequest>(json);
// Basic move validation
if (request.Move.To.Equals(request.Move.From))
{
var error = new Service.Messages.ErrorResponse(Service.Types.ClientAction.Move)
{
Error = "Error: moving piece from tile to the same tile."
};
await communicationManager.BroadcastToPlayers(error, userName);
return;
}
var moveModel = new Move(request.Move);
var board = boardManager.Get(request.GameName);
var boardMove = moveModel.ToBoardModel();
//board.Move()
await gameboardRepository.PostMove(request.GameName, new PostMove(moveModel.ToApiModel()));
var response = new Service.Messages.MoveResponse(Service.Types.ClientAction.Move)
if (board == null)
{
GameName = request.GameName,
PlayerName = userName,
Move = moveModel.ToServiceModel()
};
await communicationManager.BroadcastToGame(request.GameName, response);
// 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);
}
}
}
}