massive checkpoint
This commit is contained in:
@@ -1,54 +0,0 @@
|
||||
using Gameboard.ShogiUI.Sockets.Models;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket;
|
||||
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 : ICreateGameHandler
|
||||
{
|
||||
private readonly IGameboardManager manager;
|
||||
private readonly ISocketConnectionManager connectionManager;
|
||||
|
||||
public CreateGameHandler(
|
||||
ISocketConnectionManager communicationManager,
|
||||
IGameboardManager manager)
|
||||
{
|
||||
this.manager = manager;
|
||||
this.connectionManager = communicationManager;
|
||||
}
|
||||
|
||||
public async Task Handle(CreateGameRequest request, string userName)
|
||||
{
|
||||
var model = new SessionMetadata(request.GameName, request.IsPrivate, userName, null);
|
||||
var success = await manager.CreateSession(model);
|
||||
|
||||
if (!success)
|
||||
{
|
||||
var error = new CreateGameResponse()
|
||||
{
|
||||
Error = "Unable to create game with this name."
|
||||
};
|
||||
await connectionManager.BroadcastToPlayers(error, userName);
|
||||
}
|
||||
|
||||
var response = new CreateGameResponse()
|
||||
{
|
||||
PlayerName = userName,
|
||||
Game = model.ToServiceModel()
|
||||
};
|
||||
|
||||
var task = request.IsPrivate
|
||||
? connectionManager.BroadcastToPlayers(response, userName)
|
||||
: connectionManager.BroadcastToAll(response);
|
||||
|
||||
await task;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
using Gameboard.ShogiUI.Sockets.Repositories;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Types;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
{
|
||||
public interface IListGamesHandler
|
||||
{
|
||||
Task Handle(ListGamesRequest request, string userName);
|
||||
}
|
||||
|
||||
public class ListGamesHandler : IListGamesHandler
|
||||
{
|
||||
private readonly ISocketConnectionManager communicationManager;
|
||||
private readonly IGameboardRepository repository;
|
||||
|
||||
public ListGamesHandler(
|
||||
ISocketConnectionManager communicationManager,
|
||||
IGameboardRepository repository)
|
||||
{
|
||||
this.communicationManager = communicationManager;
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
public async Task Handle(ListGamesRequest _, string userName)
|
||||
{
|
||||
var sessions = await repository.ReadSessionMetadatas();
|
||||
var games = sessions.Select(s => new Game(s.Name, s.Player1, s.Player2)).ToList();
|
||||
|
||||
var response = new ListGamesResponse()
|
||||
{
|
||||
Games = games
|
||||
};
|
||||
|
||||
await communicationManager.BroadcastToPlayers(response, userName);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
using Gameboard.ShogiUI.Sockets.Models;
|
||||
using Gameboard.ShogiUI.Sockets.Repositories;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Types;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
{
|
||||
public interface ILoadGameHandler
|
||||
{
|
||||
Task Handle(LoadGameRequest request, string userName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Subscribes a user to messages for a session and loads that session into the BoardManager for playing.
|
||||
/// </summary>
|
||||
public class LoadGameHandler : ILoadGameHandler
|
||||
{
|
||||
private readonly ILogger<LoadGameHandler> logger;
|
||||
private readonly IGameboardRepository gameboardRepository;
|
||||
private readonly ISocketConnectionManager communicationManager;
|
||||
|
||||
public LoadGameHandler(
|
||||
ILogger<LoadGameHandler> logger,
|
||||
ISocketConnectionManager communicationManager,
|
||||
IGameboardRepository gameboardRepository)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.gameboardRepository = gameboardRepository;
|
||||
this.communicationManager = communicationManager;
|
||||
}
|
||||
|
||||
public async Task Handle(LoadGameRequest request, string userName)
|
||||
{
|
||||
var sessionModel = await gameboardRepository.ReadSession(request.GameName);
|
||||
if (sessionModel == null)
|
||||
{
|
||||
logger.LogWarning("{action} - {user} was unable to load session named {session}.", ClientAction.LoadGame, userName, request.GameName);
|
||||
var error = new LoadGameResponse() { Error = "Game not found." };
|
||||
await communicationManager.BroadcastToPlayers(error, userName);
|
||||
return;
|
||||
}
|
||||
|
||||
communicationManager.SubscribeToGame(sessionModel, userName);
|
||||
|
||||
var response = new LoadGameResponse()
|
||||
{
|
||||
Game = new SessionMetadata(sessionModel).ToServiceModel(),
|
||||
BoardState = sessionModel.Shogi.ToServiceModel(),
|
||||
MoveHistory = sessionModel.Shogi.MoveHistory.Select(_ => _.ToServiceModel()).ToList()
|
||||
};
|
||||
await communicationManager.BroadcastToPlayers(response, userName);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
{
|
||||
public interface IMoveHandler
|
||||
{
|
||||
Task Handle(MoveRequest request, string userName);
|
||||
}
|
||||
public class MoveHandler : IMoveHandler
|
||||
{
|
||||
private readonly IGameboardManager gameboardManager;
|
||||
private readonly ISocketConnectionManager connectionManager;
|
||||
public MoveHandler(
|
||||
ISocketConnectionManager connectionManager,
|
||||
IGameboardManager gameboardManager)
|
||||
{
|
||||
this.gameboardManager = gameboardManager;
|
||||
this.connectionManager = connectionManager;
|
||||
}
|
||||
|
||||
public async Task Handle(MoveRequest request, string userName)
|
||||
{
|
||||
Models.Move moveModel;
|
||||
if (request.Move.PieceFromCaptured.HasValue)
|
||||
{
|
||||
moveModel = new Models.Move(request.Move.PieceFromCaptured.Value, request.Move.To);
|
||||
}
|
||||
else
|
||||
{
|
||||
moveModel = new Models.Move(request.Move.From!, request.Move.To, request.Move.IsPromotion);
|
||||
}
|
||||
|
||||
var session = await gameboardManager.ReadSession(request.GameName);
|
||||
if (session != null)
|
||||
{
|
||||
var shogi = session.Shogi;
|
||||
var moveSuccess = shogi.Move(moveModel);
|
||||
if (moveSuccess)
|
||||
{
|
||||
await gameboardManager.CreateBoardState(session.Name, shogi);
|
||||
var response = new MoveResponse()
|
||||
{
|
||||
GameName = request.GameName,
|
||||
PlayerName = userName,
|
||||
Move = moveModel.ToServiceModel()
|
||||
};
|
||||
await connectionManager.BroadcastToPlayers(response, session.Player1, session.Player2);
|
||||
}
|
||||
else
|
||||
{
|
||||
var response = new MoveResponse()
|
||||
{
|
||||
Error = "Invalid move."
|
||||
};
|
||||
await connectionManager.BroadcastToPlayers(response, userName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user