56 lines
1.9 KiB
C#
56 lines
1.9 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 Microsoft.Extensions.Logging;
|
|
using Newtonsoft.Json;
|
|
using System.Net.WebSockets;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
|
{
|
|
public class JoinGameHandler : IActionHandler
|
|
{
|
|
private readonly ILogger<JoinGameHandler> logger;
|
|
private readonly IGameboardRepository gameboardRepository;
|
|
private readonly ISocketCommunicationManager communicationManager;
|
|
public JoinGameHandler(
|
|
ILogger<JoinGameHandler> logger,
|
|
ISocketCommunicationManager communicationManager,
|
|
IGameboardRepository gameboardRepository)
|
|
{
|
|
this.logger = logger;
|
|
this.gameboardRepository = gameboardRepository;
|
|
this.communicationManager = communicationManager;
|
|
}
|
|
|
|
public async Task Handle(WebSocket socket, string json, string userName)
|
|
{
|
|
logger.LogInformation("Socket Request \n{0}\n", new[] { json });
|
|
var request = JsonConvert.DeserializeObject<JoinGameRequest>(json);
|
|
var response = new JoinGameResponse(ClientAction.JoinGame)
|
|
{
|
|
PlayerName = userName
|
|
};
|
|
|
|
var joinGameResponse = await gameboardRepository.PutJoinPublicSession(request.GameName, new PutJoinPublicSession
|
|
{
|
|
PlayerName = userName,
|
|
SessionName = request.GameName
|
|
});
|
|
|
|
if (joinGameResponse.JoinSucceeded)
|
|
{
|
|
response.GameName = request.GameName;
|
|
}
|
|
else
|
|
{
|
|
response.Error = "Game is full or code is incorrect.";
|
|
}
|
|
var serialized = JsonConvert.SerializeObject(response);
|
|
logger.LogInformation("Socket Response \n{0}\n", new[] { serialized });
|
|
await communicationManager.BroadcastToAll(serialized);
|
|
}
|
|
}
|
|
}
|