checkpoint
This commit is contained in:
@@ -1,45 +1,47 @@
|
||||
using Gameboard.Shogi.Api.ServiceModels.Messages;
|
||||
using Gameboard.ShogiUI.Sockets.Extensions;
|
||||
using Gameboard.ShogiUI.Sockets.Managers.Utility;
|
||||
using Gameboard.ShogiUI.Sockets.Repositories;
|
||||
using Service = Gameboard.ShogiUI.Sockets.ServiceModels.Socket;
|
||||
using Newtonsoft.Json;
|
||||
using System.Net.WebSockets;
|
||||
using System.Threading.Tasks;
|
||||
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(WebSocket socket, string json, string userName)
|
||||
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 serialized = JsonConvert.SerializeObject(
|
||||
new Service.Messages.ErrorResponse(Service.Types.ClientAction.Move)
|
||||
{
|
||||
Error = "Error: moving piece from tile to the same tile."
|
||||
});
|
||||
await socket.SendTextAsync(serialized);
|
||||
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 session = (await gameboardRepository.GetGame(request.GameName)).Session;
|
||||
await gameboardRepository.PostMove(request.GameName, new PostMove(Mapper.Map(moveModel)));
|
||||
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)
|
||||
{
|
||||
@@ -47,7 +49,7 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
PlayerName = userName,
|
||||
Move = moveModel.ToServiceModel()
|
||||
};
|
||||
await communicationManager.BroadcastToGame(session.Name, response);
|
||||
await communicationManager.BroadcastToGame(request.GameName, response);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user