Files
Shogi/Gameboard.ShogiUI.Sockets/Managers/ClientActionHandlers/JoinGameHandler.cs

49 lines
1.5 KiB
C#

using Gameboard.Shogi.Api.ServiceModels.Messages;
using Gameboard.ShogiUI.Sockets.Repositories;
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages;
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
using Newtonsoft.Json;
using System.Net.WebSockets;
using System.Threading.Tasks;
namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
{
public class JoinGameHandler : IActionHandler
{
private readonly IGameboardRepository gameboardRepository;
private readonly ISocketCommunicationManager communicationManager;
public JoinGameHandler(
ISocketCommunicationManager communicationManager,
IGameboardRepository gameboardRepository)
{
this.gameboardRepository = gameboardRepository;
this.communicationManager = communicationManager;
}
public async Task Handle(WebSocket socket, string json, string userName)
{
var request = JsonConvert.DeserializeObject<JoinGameRequest>(json);
var response = new JoinGameResponse(ClientAction.JoinGame)
{
PlayerName = userName
};
var joinGameResponse = await gameboardRepository.PutJoinPublicSession(new PutJoinPublicSession
{
PlayerName = userName,
SessionName = request.GameName
});
if (joinGameResponse.JoinSucceeded)
{
response.GameName = request.GameName;
}
else
{
response.Error = "Game is full.";
}
await communicationManager.BroadcastToAll(response);
}
}
}