More organized communication strategy.

This commit is contained in:
2021-02-13 19:14:43 -06:00
parent 1826c07601
commit d76e4f7a8b
13 changed files with 212 additions and 173 deletions

View File

@@ -2,7 +2,6 @@
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;
@@ -11,31 +10,28 @@ 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
var joinGameResponse = await gameboardRepository.PutJoinPublicSession(new PutJoinPublicSession
{
PlayerName = userName
PlayerName = userName,
SessionName = request.GameName
});
if (joinGameResponse.JoinSucceeded)
@@ -44,11 +40,9 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
}
else
{
response.Error = "Game is full or code is incorrect.";
response.Error = "Game is full.";
}
var serialized = JsonConvert.SerializeObject(response);
logger.LogInformation("Socket Response \n{0}\n", new[] { serialized });
await communicationManager.BroadcastToAll(serialized);
await communicationManager.BroadcastToAll(response);
}
}
}