yep
This commit is contained in:
@@ -1,33 +0,0 @@
|
||||
using Gameboard.ShogiUI.Sockets.Models;
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Managers
|
||||
{
|
||||
public interface IActiveSessionManager
|
||||
{
|
||||
void Add(Session session);
|
||||
Session? Get(string sessionName);
|
||||
}
|
||||
|
||||
// TODO: Consider moving this class' functionality into the ConnectionManager class.
|
||||
public class ActiveSessionManager : IActiveSessionManager
|
||||
{
|
||||
private readonly ConcurrentDictionary<string, Session> Sessions;
|
||||
|
||||
public ActiveSessionManager()
|
||||
{
|
||||
Sessions = new ConcurrentDictionary<string, Session>();
|
||||
}
|
||||
|
||||
public void Add(Session session) => Sessions.TryAdd(session.Name, session);
|
||||
|
||||
public Session? Get(string sessionName)
|
||||
{
|
||||
if (Sessions.TryGetValue(sessionName, out var session))
|
||||
{
|
||||
return session;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
using Gameboard.ShogiUI.Sockets.Models;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
@@ -31,14 +31,14 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
|
||||
if (!success)
|
||||
{
|
||||
var error = new CreateGameResponse(request.Action)
|
||||
var error = new CreateGameResponse()
|
||||
{
|
||||
Error = "Unable to create game with this name."
|
||||
};
|
||||
await connectionManager.BroadcastToPlayers(error, userName);
|
||||
}
|
||||
|
||||
var response = new CreateGameResponse(request.Action)
|
||||
var response = new CreateGameResponse()
|
||||
{
|
||||
PlayerName = userName,
|
||||
Game = model.ToServiceModel()
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
using Gameboard.ShogiUI.Sockets.Repositories;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Types;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
@@ -24,7 +24,7 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
{
|
||||
var joinSucceeded = await gameboardManager.AssignPlayer2ToSession(request.GameName, userName);
|
||||
|
||||
var response = new JoinGameResponse(ClientAction.JoinGame)
|
||||
var response = new JoinGameResponse()
|
||||
{
|
||||
PlayerName = userName,
|
||||
GameName = request.GameName
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
using Gameboard.ShogiUI.Sockets.Repositories;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Types;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@@ -29,7 +29,7 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
var sessions = await repository.ReadSessionMetadatas();
|
||||
var games = sessions.Select(s => new Game(s.Name, s.Player1, s.Player2)).ToList();
|
||||
|
||||
var response = new ListGamesResponse(ClientAction.ListGames)
|
||||
var response = new ListGamesResponse()
|
||||
{
|
||||
Games = games
|
||||
};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
using Gameboard.ShogiUI.Sockets.Models;
|
||||
using Gameboard.ShogiUI.Sockets.Repositories;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Types;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
@@ -21,18 +21,15 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
private readonly ILogger<LoadGameHandler> logger;
|
||||
private readonly IGameboardRepository gameboardRepository;
|
||||
private readonly ISocketConnectionManager communicationManager;
|
||||
private readonly IActiveSessionManager boardManager;
|
||||
|
||||
public LoadGameHandler(
|
||||
ILogger<LoadGameHandler> logger,
|
||||
ISocketConnectionManager communicationManager,
|
||||
IGameboardRepository gameboardRepository,
|
||||
IActiveSessionManager boardManager)
|
||||
IGameboardRepository gameboardRepository)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.gameboardRepository = gameboardRepository;
|
||||
this.communicationManager = communicationManager;
|
||||
this.boardManager = boardManager;
|
||||
}
|
||||
|
||||
public async Task Handle(LoadGameRequest request, string userName)
|
||||
@@ -41,15 +38,14 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
if (sessionModel == null)
|
||||
{
|
||||
logger.LogWarning("{action} - {user} was unable to load session named {session}.", ClientAction.LoadGame, userName, request.GameName);
|
||||
var error = new LoadGameResponse(ClientAction.LoadGame) { Error = "Game not found." };
|
||||
var error = new LoadGameResponse() { Error = "Game not found." };
|
||||
await communicationManager.BroadcastToPlayers(error, userName);
|
||||
return;
|
||||
}
|
||||
|
||||
communicationManager.SubscribeToGame(sessionModel, userName);
|
||||
boardManager.Add(sessionModel);
|
||||
|
||||
var response = new LoadGameResponse(ClientAction.LoadGame)
|
||||
var response = new LoadGameResponse()
|
||||
{
|
||||
Game = new SessionMetadata(sessionModel).ToServiceModel(),
|
||||
BoardState = sessionModel.Shogi.ToServiceModel(),
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
using Gameboard.ShogiUI.Sockets.Models;
|
||||
using Gameboard.ShogiUI.Sockets.Repositories;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
{
|
||||
public interface IMoveHandler
|
||||
@@ -12,66 +9,53 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
}
|
||||
public class MoveHandler : IMoveHandler
|
||||
{
|
||||
private readonly IActiveSessionManager boardManager;
|
||||
private readonly IGameboardManager gameboardManager;
|
||||
private readonly ISocketConnectionManager communicationManager;
|
||||
private readonly ISocketConnectionManager connectionManager;
|
||||
public MoveHandler(
|
||||
IActiveSessionManager boardManager,
|
||||
ISocketConnectionManager communicationManager,
|
||||
ISocketConnectionManager connectionManager,
|
||||
IGameboardManager gameboardManager)
|
||||
{
|
||||
this.boardManager = boardManager;
|
||||
this.gameboardManager = gameboardManager;
|
||||
this.communicationManager = communicationManager;
|
||||
this.connectionManager = connectionManager;
|
||||
}
|
||||
|
||||
public async Task Handle(MoveRequest request, string userName)
|
||||
{
|
||||
Move moveModel;
|
||||
Models.Move moveModel;
|
||||
if (request.Move.PieceFromCaptured.HasValue)
|
||||
{
|
||||
moveModel = new Move(request.Move.PieceFromCaptured.Value, request.Move.To);
|
||||
moveModel = new Models.Move(request.Move.PieceFromCaptured.Value, request.Move.To);
|
||||
}
|
||||
else
|
||||
{
|
||||
moveModel = new Move(request.Move.From!, request.Move.To, request.Move.IsPromotion);
|
||||
moveModel = new Models.Move(request.Move.From!, request.Move.To, request.Move.IsPromotion);
|
||||
}
|
||||
|
||||
var board = boardManager.Get(request.GameName);
|
||||
if (board == null)
|
||||
var session = await gameboardManager.ReadSession(request.GameName);
|
||||
if (session != null)
|
||||
{
|
||||
// TODO: Find a flow for this
|
||||
var response = new MoveResponse(ServiceModels.Socket.Types.ClientAction.Move)
|
||||
var shogi = session.Shogi;
|
||||
var moveSuccess = shogi.Move(moveModel);
|
||||
if (moveSuccess)
|
||||
{
|
||||
Error = $"Game isn't loaded. Send a message with the {ServiceModels.Socket.Types.ClientAction.LoadGame} action first."
|
||||
};
|
||||
await communicationManager.BroadcastToPlayers(response, userName);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//var moveSuccess = board.Move(boardMove);
|
||||
//if (moveSuccess)
|
||||
//{
|
||||
// await gameboardRepository.PostMove(request.GameName, new PostMove(moveModel.ToApiModel()));
|
||||
// var boardState = new BoardState(board);
|
||||
// var response = new Service.Messages.MoveResponse(Service.Types.ClientAction.Move)
|
||||
// {
|
||||
// GameName = request.GameName,
|
||||
// PlayerName = userName,
|
||||
// BoardState = boardState.ToServiceModel()
|
||||
// };
|
||||
// await communicationManager.BroadcastToGame(request.GameName, response);
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// var response = new Service.Messages.MoveResponse(Service.Types.ClientAction.Move)
|
||||
// {
|
||||
// Error = "Invalid move."
|
||||
// };
|
||||
// await communicationManager.BroadcastToPlayers(response, userName);
|
||||
//}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,19 +7,20 @@ namespace Gameboard.ShogiUI.Sockets.Managers
|
||||
{
|
||||
public interface IGameboardManager
|
||||
{
|
||||
Task<string> CreateGuestUser();
|
||||
Task<string> CreateGuestUser(Guid webSessionId);
|
||||
Task<bool> IsPlayer1(string sessionName, string playerName);
|
||||
bool IsGuest(string playerName);
|
||||
Task<bool> CreateSession(SessionMetadata session);
|
||||
Task<Session?> ReadSession(string gameName);
|
||||
Task<bool> UpdateSession(Session session);
|
||||
Task<bool> UpdateSession(SessionMetadata session);
|
||||
Task<bool> AssignPlayer2ToSession(string sessionName, string userName);
|
||||
Task<bool> CreateBoardState(string sessionName, Shogi shogi);
|
||||
Task<User?> ReadUser(string userName);
|
||||
Task<User?> ReadUser(Guid webSessionId);
|
||||
}
|
||||
|
||||
public class GameboardManager : IGameboardManager
|
||||
{
|
||||
private const int MaxTries = 3;
|
||||
private const string GuestPrefix = "Guest-";
|
||||
private readonly IGameboardRepository repository;
|
||||
|
||||
public GameboardManager(IGameboardRepository repository)
|
||||
@@ -27,22 +28,30 @@ namespace Gameboard.ShogiUI.Sockets.Managers
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
public async Task<string> CreateGuestUser()
|
||||
public async Task<string> CreateGuestUser(Guid webSessionId)
|
||||
{
|
||||
var count = 0;
|
||||
while (count < MaxTries)
|
||||
{
|
||||
count++;
|
||||
var clientId = $"Guest-{Guid.NewGuid()}";
|
||||
var isCreated = await repository.CreateGuestUser(clientId);
|
||||
var isCreated = await repository.CreateGuestUser(clientId, webSessionId);
|
||||
if (isCreated)
|
||||
{
|
||||
return clientId;
|
||||
}
|
||||
}
|
||||
throw new OperationCanceledException($"Failed to create guest user after {MaxTries} tries.");
|
||||
throw new OperationCanceledException($"Failed to create guest user after {count} tries.");
|
||||
}
|
||||
|
||||
public Task<User?> ReadUser(Guid webSessionId)
|
||||
{
|
||||
return repository.ReadGuestUser(webSessionId);
|
||||
}
|
||||
public Task<User?> ReadUser(string userName)
|
||||
{
|
||||
return repository.ReadUser(userName);
|
||||
}
|
||||
public async Task<bool> IsPlayer1(string sessionName, string playerName)
|
||||
{
|
||||
//var session = await repository.GetGame(sessionName);
|
||||
@@ -65,8 +74,6 @@ namespace Gameboard.ShogiUI.Sockets.Managers
|
||||
return repository.CreateSession(session);
|
||||
}
|
||||
|
||||
public bool IsGuest(string playerName) => playerName.StartsWith(GuestPrefix);
|
||||
|
||||
public Task<Session?> ReadSession(string sessionName)
|
||||
{
|
||||
return repository.ReadSession(sessionName);
|
||||
@@ -77,15 +84,20 @@ namespace Gameboard.ShogiUI.Sockets.Managers
|
||||
/// </summary>
|
||||
/// <param name="session">The session to save.</param>
|
||||
/// <returns>True if the session was saved successfully.</returns>
|
||||
public Task<bool> UpdateSession(Session session)
|
||||
public Task<bool> UpdateSession(SessionMetadata session)
|
||||
{
|
||||
return repository.UpdateSession(session);
|
||||
}
|
||||
|
||||
public Task<bool> CreateBoardState(string sessionName, Shogi shogi)
|
||||
{
|
||||
return repository.CreateBoardState(sessionName, shogi);
|
||||
}
|
||||
|
||||
public async Task<bool> AssignPlayer2ToSession(string sessionName, string userName)
|
||||
{
|
||||
var isSuccess = false;
|
||||
var session = await repository.ReadSession(sessionName);
|
||||
var session = await repository.ReadSessionMetaData(sessionName);
|
||||
if (session != null && !session.IsPrivate && string.IsNullOrEmpty(session.Player2))
|
||||
{
|
||||
session.SetPlayer2(userName);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
using Gameboard.ShogiUI.Sockets.Extensions;
|
||||
using Gameboard.ShogiUI.Sockets.Models;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Interfaces;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using System.Collections.Concurrent;
|
||||
@@ -19,7 +19,7 @@ namespace Gameboard.ShogiUI.Sockets.Managers
|
||||
void SubscribeToBroadcast(WebSocket socket, string playerName);
|
||||
void UnsubscribeFromBroadcastAndGames(string playerName);
|
||||
void UnsubscribeFromGame(string gameName, string playerName);
|
||||
Task BroadcastToPlayers(IResponse response, params string[] playerNames);
|
||||
Task BroadcastToPlayers(IResponse response, params string?[] playerNames);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -84,17 +84,16 @@ namespace Gameboard.ShogiUI.Sockets.Managers
|
||||
}
|
||||
}
|
||||
|
||||
public async Task BroadcastToPlayers(IResponse response, params string[] playerNames)
|
||||
public async Task BroadcastToPlayers(IResponse response, params string?[] playerNames)
|
||||
{
|
||||
var tasks = new List<Task>(playerNames.Length);
|
||||
foreach (var name in playerNames)
|
||||
{
|
||||
if (connections.TryGetValue(name, out var socket))
|
||||
if (!string.IsNullOrEmpty(name) && connections.TryGetValue(name, out var socket))
|
||||
{
|
||||
var serialized = JsonConvert.SerializeObject(response);
|
||||
logger.LogInformation("Response to {0} \n{1}\n", name, serialized);
|
||||
tasks.Add(socket.SendTextAsync(serialized));
|
||||
|
||||
}
|
||||
}
|
||||
await Task.WhenAll(tasks);
|
||||
|
||||
@@ -15,7 +15,7 @@ namespace Gameboard.ShogiUI.Sockets.Managers
|
||||
public class SocketTokenManager : ISocketTokenManager
|
||||
{
|
||||
/// <summary>
|
||||
/// Key is userName
|
||||
/// Key is userName or webSessionId
|
||||
/// </summary>
|
||||
private readonly ConcurrentDictionary<string, Guid> Tokens;
|
||||
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Interfaces;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Managers.Utility
|
||||
{
|
||||
public class JsonRequest
|
||||
{
|
||||
public IRequest Request { get; private set; }
|
||||
public string Json { get; private set; }
|
||||
public JsonRequest(IRequest request, string json)
|
||||
{
|
||||
Request = request;
|
||||
Json = json;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Interfaces;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Managers.Utility
|
||||
{
|
||||
public class Request : IRequest
|
||||
{
|
||||
public ClientAction Action { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user