All the code

This commit is contained in:
2020-12-13 14:31:23 -06:00
parent 9c3d67a07e
commit 1bbab8fe8f
49 changed files with 1878 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
using Gameboard.Shogi.Api.ServiceModels.Messages;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Net.WebSockets;
using System.Threading.Tasks;
using Websockets.Repositories;
using Websockets.ServiceModels.Messages;
using Websockets.ServiceModels.Types;
namespace Websockets.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.PostJoinGame(request.GameName, new PostJoinGame
{
PlayerName = userName
});
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);
}
}
}