before deleting Rules
This commit is contained in:
@@ -1,61 +1,55 @@
|
||||
using Gameboard.Shogi.Api.ServiceModels.Messages;
|
||||
using Gameboard.ShogiUI.Sockets.Repositories;
|
||||
using Gameboard.ShogiUI.Sockets.Models;
|
||||
using Gameboard.ShogiUI.Sockets.Repositories.RepositoryManagers;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
{
|
||||
public interface ICreateGameHandler
|
||||
{
|
||||
Task Handle(CreateGameRequest request, string userName);
|
||||
}
|
||||
|
||||
// TODO: This doesn't need to be a socket action.
|
||||
// It can be an API route and still tell socket connections about the new session.
|
||||
public class CreateGameHandler : IActionHandler
|
||||
public class CreateGameHandler : ICreateGameHandler
|
||||
{
|
||||
private readonly IGameboardRepository repository;
|
||||
private readonly IGameboardRepositoryManager manager;
|
||||
private readonly ISocketCommunicationManager communicationManager;
|
||||
|
||||
public CreateGameHandler(
|
||||
ISocketCommunicationManager communicationManager,
|
||||
IGameboardRepository repository)
|
||||
IGameboardRepositoryManager manager)
|
||||
{
|
||||
this.repository = repository;
|
||||
this.manager = manager;
|
||||
this.communicationManager = communicationManager;
|
||||
}
|
||||
|
||||
public async Task Handle(string json, string userName)
|
||||
public async Task Handle(CreateGameRequest request, string userName)
|
||||
{
|
||||
var request = JsonConvert.DeserializeObject<CreateGameRequest>(json);
|
||||
var sessionName = await repository.PostSession(new PostSession
|
||||
var model = new Session(request.GameName, request.IsPrivate, userName);
|
||||
var success = await manager.CreateSession(model);
|
||||
|
||||
if (!success)
|
||||
{
|
||||
SessionName = request.GameName,
|
||||
PlayerName = userName,
|
||||
IsPrivate = request.IsPrivate
|
||||
});
|
||||
var error = new CreateGameResponse(request.Action)
|
||||
{
|
||||
Error = "Unable to create game with this name."
|
||||
};
|
||||
await communicationManager.BroadcastToPlayers(error, userName);
|
||||
}
|
||||
|
||||
var response = new CreateGameResponse(request.Action)
|
||||
{
|
||||
PlayerName = userName,
|
||||
Game = new Game
|
||||
{
|
||||
GameName = sessionName,
|
||||
Players = new[] { userName }
|
||||
}
|
||||
Game = model.ToServiceModel()
|
||||
};
|
||||
|
||||
if (string.IsNullOrWhiteSpace(sessionName))
|
||||
{
|
||||
response.Error = "Game already exists.";
|
||||
}
|
||||
var task = request.IsPrivate
|
||||
? communicationManager.BroadcastToPlayers(response, userName)
|
||||
: communicationManager.BroadcastToAll(response);
|
||||
|
||||
if (request.IsPrivate)
|
||||
{
|
||||
await communicationManager.BroadcastToPlayers(response, userName);
|
||||
}
|
||||
else
|
||||
{
|
||||
await communicationManager.BroadcastToAll(response);
|
||||
}
|
||||
await task;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user