49 lines
1.5 KiB
C#
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.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(string json, string userName)
|
|
{
|
|
var request = JsonConvert.DeserializeObject<JoinGameRequest>(json);
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
}
|