56 lines
1.9 KiB
C#
56 lines
1.9 KiB
C#
using Gameboard.Shogi.Api.ServiceModels.Messages;
|
|
using Gameboard.ShogiUI.Sockets.Models;
|
|
using Gameboard.ShogiUI.Sockets.Repositories;
|
|
using Newtonsoft.Json;
|
|
using System.Threading.Tasks;
|
|
using Service = Gameboard.ShogiUI.Sockets.ServiceModels.Socket;
|
|
|
|
namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
|
{
|
|
public class MoveHandler : IActionHandler
|
|
{
|
|
private readonly IBoardManager boardManager;
|
|
private readonly IGameboardRepository gameboardRepository;
|
|
private readonly ISocketCommunicationManager communicationManager;
|
|
public MoveHandler(
|
|
IBoardManager boardManager,
|
|
ISocketCommunicationManager communicationManager,
|
|
IGameboardRepository gameboardRepository)
|
|
{
|
|
this.boardManager = boardManager;
|
|
this.gameboardRepository = gameboardRepository;
|
|
this.communicationManager = communicationManager;
|
|
}
|
|
|
|
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)
|
|
{
|
|
GameName = request.GameName,
|
|
PlayerName = userName,
|
|
Move = moveModel.ToServiceModel()
|
|
};
|
|
await communicationManager.BroadcastToGame(request.GameName, response);
|
|
}
|
|
}
|
|
}
|