massive checkpoint

This commit is contained in:
2021-09-03 22:43:06 -05:00
parent bb1d2c491c
commit 2a3b7b32b4
40 changed files with 456 additions and 738 deletions

View File

@@ -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;
}
}
}

View File

@@ -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);
}
}
}

View File

@@ -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);
}
}
}

View File

@@ -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);
}
}
}
}
}

View File

@@ -1,26 +1,21 @@
using Gameboard.ShogiUI.Sockets.Models;
using Gameboard.ShogiUI.Sockets.Extensions;
using Gameboard.ShogiUI.Sockets.Models;
using Gameboard.ShogiUI.Sockets.Repositories;
using System;
using System.Security.Claims;
using System.Threading.Tasks;
namespace Gameboard.ShogiUI.Sockets.Managers
{
public interface IGameboardManager
{
Task<string> CreateGuestUser(Guid webSessionId);
Task<bool> IsPlayer1(string sessionName, string playerName);
Task<bool> CreateSession(SessionMetadata session);
Task<Session?> ReadSession(string gameName);
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);
Task<User?> ReadUser(ClaimsPrincipal user);
}
public class GameboardManager : IGameboardManager
{
private const int MaxTries = 3;
private readonly IGameboardRepository repository;
public GameboardManager(IGameboardRepository repository)
@@ -28,30 +23,21 @@ namespace Gameboard.ShogiUI.Sockets.Managers
this.repository = repository;
}
public async Task<string> CreateGuestUser(Guid webSessionId)
public Task<User?> ReadUser(ClaimsPrincipal user)
{
var count = 0;
while (count < MaxTries)
var userId = user.UserId();
if (user.IsGuest() && Guid.TryParse(userId, out var webSessionId))
{
count++;
var userName = $"Guest-{Guid.NewGuid()}";
var isCreated = await repository.CreateUser(new User(userName, webSessionId));
if (isCreated)
{
return userName;
}
return repository.ReadGuestUser(webSessionId);
}
throw new OperationCanceledException($"Failed to create guest user after {count} tries.");
else if (!string.IsNullOrEmpty(userId))
{
return repository.ReadUser(userId);
}
return Task.FromResult<User?>(null);
}
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);
@@ -69,31 +55,6 @@ namespace Gameboard.ShogiUI.Sockets.Managers
return string.Empty;
}
public Task<bool> CreateSession(SessionMetadata session)
{
return repository.CreateSession(session);
}
public Task<Session?> ReadSession(string sessionName)
{
return repository.ReadSession(sessionName);
}
/// <summary>
/// Saves the session to storage.
/// </summary>
/// <param name="session">The session to save.</param>
/// <returns>True if the session was saved successfully.</returns>
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;

View File

@@ -6,20 +6,20 @@ using System.Threading.Tasks;
namespace Gameboard.ShogiUI.Sockets.Managers
{
public interface ISocketTokenManager
public interface ISocketTokenCache
{
Guid GenerateToken(string s);
string GetUsername(Guid g);
string? GetUsername(Guid g);
}
public class SocketTokenManager : ISocketTokenManager
public class SocketTokenCache : ISocketTokenCache
{
/// <summary>
/// Key is userName or webSessionId
/// </summary>
private readonly ConcurrentDictionary<string, Guid> Tokens;
public SocketTokenManager()
public SocketTokenCache()
{
Tokens = new ConcurrentDictionary<string, Guid>();
}
@@ -41,7 +41,7 @@ namespace Gameboard.ShogiUI.Sockets.Managers
}
/// <returns>User name associated to the guid or null.</returns>
public string GetUsername(Guid guid)
public string? GetUsername(Guid guid)
{
var userName = Tokens.FirstOrDefault(kvp => kvp.Value == guid).Key;
if (userName != null)