From 1352d75c6a3aeba96f97b7ff097b2c007d8d9834 Mon Sep 17 00:00:00 2001 From: Lucas Morgan Date: Mon, 25 Jan 2021 20:41:14 -0600 Subject: [PATCH 1/2] namespaces and guest game invitations --- .../Api/Messages/GetGuestToken.cs | 2 +- .../Api/Messages/GetToken.cs | 2 +- .../Api/Messages/PostGameInvitation.cs | 7 +- .../Socket/Interfaces/IRequest.cs | 4 +- .../Socket/Interfaces/IResponse.cs | 2 +- .../Socket/Messages/CreateGame.cs | 6 +- .../Socket/Messages/ErrorResponse.cs | 22 ++-- .../Socket/Messages/JoinByCode.cs | 6 +- .../Socket/Messages/JoinGame.cs | 6 +- .../Socket/Messages/ListGames.cs | 35 +++-- .../Socket/Messages/LoadGame.cs | 8 +- .../Socket/Messages/Move.cs | 6 +- .../Socket/Types/ClientActionEnum.cs | 2 +- .../Socket/Types/Coords.cs | 2 +- .../Socket/Types/Game.cs | 2 +- .../Socket/Types/Move.cs | 2 +- .../Controllers/GameController.cs | 47 +++++-- .../Controllers/SocketController.cs | 12 +- .../Extensions/WebSocketExtensions.cs | 2 +- .../Gameboard.ShogiUI.Sockets.csproj.user | 1 + .../ClientActionHandlers/CreateGameHandler.cs | 12 +- .../ClientActionHandlers/IActionHandler.cs | 6 +- .../ClientActionHandlers/JoinByCodeHandler.cs | 12 +- .../ClientActionHandlers/JoinGameHandler.cs | 8 +- .../ClientActionHandlers/ListGamesHandler.cs | 10 +- .../ClientActionHandlers/LoadGameHandler.cs | 12 +- .../ClientActionHandlers/MoveHandler.cs | 14 +- .../Managers/SocketCommunicationManager.cs | 10 +- .../Managers/SocketConnectionManager.cs | 2 +- .../Managers/SocketTokenManager.cs | 2 +- .../Managers/Utility/JsonRequest.cs | 20 +-- .../Managers/Utility/Mapper.cs | 122 +++++++++--------- .../Managers/Utility/Request.cs | 16 +-- Gameboard.ShogiUI.Sockets/Program.cs | 2 +- .../Repositories/GameboardRepository.cs | 4 +- .../GameboardRepositoryManager.cs | 26 +++- .../Utility/AuthenticatedHttpClient.cs | 2 +- Gameboard.ShogiUI.Sockets/Startup.cs | 14 +- 38 files changed, 262 insertions(+), 208 deletions(-) diff --git a/Gameboard.ShogiUI.Sockets.ServiceModels/Api/Messages/GetGuestToken.cs b/Gameboard.ShogiUI.Sockets.ServiceModels/Api/Messages/GetGuestToken.cs index 81d3dcd..6f6a751 100644 --- a/Gameboard.ShogiUI.Sockets.ServiceModels/Api/Messages/GetGuestToken.cs +++ b/Gameboard.ShogiUI.Sockets.ServiceModels/Api/Messages/GetGuestToken.cs @@ -1,6 +1,6 @@ using System; -namespace AspShogiSockets.ServiceModels.Api.Messages +namespace Gameboard.ShogiUI.Sockets.ServiceModels.Api.Messages { public class GetGuestToken { diff --git a/Gameboard.ShogiUI.Sockets.ServiceModels/Api/Messages/GetToken.cs b/Gameboard.ShogiUI.Sockets.ServiceModels/Api/Messages/GetToken.cs index bfcc716..e2f3c98 100644 --- a/Gameboard.ShogiUI.Sockets.ServiceModels/Api/Messages/GetToken.cs +++ b/Gameboard.ShogiUI.Sockets.ServiceModels/Api/Messages/GetToken.cs @@ -1,6 +1,6 @@ using System; -namespace Websockets.ServiceModels +namespace Gameboard.ShogiUI.Sockets.ServiceModels.Api.Messages { public class GetTokenResponse { diff --git a/Gameboard.ShogiUI.Sockets.ServiceModels/Api/Messages/PostGameInvitation.cs b/Gameboard.ShogiUI.Sockets.ServiceModels/Api/Messages/PostGameInvitation.cs index 6cec25a..10850f6 100644 --- a/Gameboard.ShogiUI.Sockets.ServiceModels/Api/Messages/PostGameInvitation.cs +++ b/Gameboard.ShogiUI.Sockets.ServiceModels/Api/Messages/PostGameInvitation.cs @@ -1,9 +1,14 @@ -namespace AspShogiSockets.ServiceModels.Api.Messages +namespace Gameboard.ShogiUI.Sockets.ServiceModels.Api.Messages { public class PostGameInvitation { public string SessionName { get; set; } } + public class PostGuestGameInvitation + { + public string GuestId { get; set; } + public string SessionName { get; set; } + } public class PostGameInvitationResponse { public string Code { get; } diff --git a/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Interfaces/IRequest.cs b/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Interfaces/IRequest.cs index dc174c7..ce8c0a4 100644 --- a/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Interfaces/IRequest.cs +++ b/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Interfaces/IRequest.cs @@ -1,6 +1,6 @@ -using Websockets.ServiceModels.Types; +using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types; -namespace Websockets.ServiceModels.Interfaces +namespace Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Interfaces { public interface IRequest { diff --git a/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Interfaces/IResponse.cs b/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Interfaces/IResponse.cs index 3712e66..8c1bed8 100644 --- a/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Interfaces/IResponse.cs +++ b/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Interfaces/IResponse.cs @@ -1,4 +1,4 @@ -namespace Websockets.ServiceModels.Interfaces +namespace Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Interfaces { public interface IResponse { diff --git a/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Messages/CreateGame.cs b/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Messages/CreateGame.cs index 43603d7..09d7455 100644 --- a/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Messages/CreateGame.cs +++ b/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Messages/CreateGame.cs @@ -1,7 +1,7 @@ -using Websockets.ServiceModels.Interfaces; -using Websockets.ServiceModels.Types; +using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Interfaces; +using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types; -namespace Websockets.ServiceModels.Messages +namespace Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages { public class CreateGameRequest : IRequest { diff --git a/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Messages/ErrorResponse.cs b/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Messages/ErrorResponse.cs index 9875612..376ffc8 100644 --- a/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Messages/ErrorResponse.cs +++ b/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Messages/ErrorResponse.cs @@ -1,16 +1,16 @@ -using Websockets.ServiceModels.Interfaces; -using Websockets.ServiceModels.Types; +using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Interfaces; +using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types; -namespace Websockets.ServiceModels.Messages +namespace Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages { - public class ErrorResponse : IResponse - { - public string Action { get; private set; } - public string Error { get; set; } + public class ErrorResponse : IResponse + { + public string Action { get; private set; } + public string Error { get; set; } - public ErrorResponse(ClientAction action) - { - Action = action.ToString(); - } + public ErrorResponse(ClientAction action) + { + Action = action.ToString(); } + } } diff --git a/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Messages/JoinByCode.cs b/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Messages/JoinByCode.cs index b0d20b8..51f2f8d 100644 --- a/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Messages/JoinByCode.cs +++ b/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Messages/JoinByCode.cs @@ -1,7 +1,7 @@ -using Websockets.ServiceModels.Interfaces; -using Websockets.ServiceModels.Types; +using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Interfaces; +using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types; -namespace Websockets.ServiceModels.Messages +namespace Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages { public class JoinByCode : IRequest { diff --git a/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Messages/JoinGame.cs b/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Messages/JoinGame.cs index 8a2cd08..ef8842d 100644 --- a/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Messages/JoinGame.cs +++ b/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Messages/JoinGame.cs @@ -1,7 +1,7 @@ -using Websockets.ServiceModels.Interfaces; -using Websockets.ServiceModels.Types; +using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Interfaces; +using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types; -namespace Websockets.ServiceModels.Messages +namespace Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages { public class JoinGameRequest : IRequest { diff --git a/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Messages/ListGames.cs b/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Messages/ListGames.cs index ebb3691..1f6541f 100644 --- a/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Messages/ListGames.cs +++ b/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Messages/ListGames.cs @@ -1,24 +1,23 @@ -using System.Collections.Generic; +using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Interfaces; +using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types; +using System.Collections.Generic; -using Websockets.ServiceModels.Interfaces; -using Websockets.ServiceModels.Types; - -namespace Websockets.ServiceModels.Messages +namespace Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages { - public class ListGamesRequest : IRequest - { - public ClientAction Action { get; set; } - } + public class ListGamesRequest : IRequest + { + public ClientAction Action { get; set; } + } - public class ListGamesResponse : IResponse - { - public string Action { get; private set; } - public string Error { get; set; } - public IEnumerable Games { get; set; } + public class ListGamesResponse : IResponse + { + public string Action { get; private set; } + public string Error { get; set; } + public IEnumerable Games { get; set; } - public ListGamesResponse(ClientAction action) - { - Action = action.ToString(); - } + public ListGamesResponse(ClientAction action) + { + Action = action.ToString(); } + } } diff --git a/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Messages/LoadGame.cs b/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Messages/LoadGame.cs index e9b1ee7..19e6c08 100644 --- a/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Messages/LoadGame.cs +++ b/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Messages/LoadGame.cs @@ -1,8 +1,8 @@ -using System.Collections.Generic; -using Websockets.ServiceModels.Interfaces; -using Websockets.ServiceModels.Types; +using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Interfaces; +using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types; +using System.Collections.Generic; -namespace Websockets.ServiceModels.Messages +namespace Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages { public class LoadGameRequest : IRequest { diff --git a/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Messages/Move.cs b/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Messages/Move.cs index eb5d8eb..5039196 100644 --- a/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Messages/Move.cs +++ b/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Messages/Move.cs @@ -1,7 +1,7 @@ -using Websockets.ServiceModels.Interfaces; -using Websockets.ServiceModels.Types; +using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Interfaces; +using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types; -namespace Websockets.ServiceModels.Messages +namespace Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages { public class MoveRequest : IRequest { diff --git a/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Types/ClientActionEnum.cs b/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Types/ClientActionEnum.cs index f921e73..9e0952e 100644 --- a/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Types/ClientActionEnum.cs +++ b/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Types/ClientActionEnum.cs @@ -1,4 +1,4 @@ -namespace Websockets.ServiceModels.Types +namespace Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types { public enum ClientAction { diff --git a/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Types/Coords.cs b/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Types/Coords.cs index 1588e7c..d64dae4 100644 --- a/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Types/Coords.cs +++ b/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Types/Coords.cs @@ -1,4 +1,4 @@ -namespace Websockets.ServiceModels.Types +namespace Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types { public class Coords { diff --git a/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Types/Game.cs b/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Types/Game.cs index 4da5eac..a4a2ebe 100644 --- a/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Types/Game.cs +++ b/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Types/Game.cs @@ -1,4 +1,4 @@ -namespace Websockets.ServiceModels.Types +namespace Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types { public class Game { diff --git a/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Types/Move.cs b/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Types/Move.cs index 17693d4..f815b00 100644 --- a/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Types/Move.cs +++ b/Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Types/Move.cs @@ -1,4 +1,4 @@ -namespace Websockets.ServiceModels.Types +namespace Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types { public class Move { diff --git a/Gameboard.ShogiUI.Sockets/Controllers/GameController.cs b/Gameboard.ShogiUI.Sockets/Controllers/GameController.cs index 414be02..5d0becd 100644 --- a/Gameboard.ShogiUI.Sockets/Controllers/GameController.cs +++ b/Gameboard.ShogiUI.Sockets/Controllers/GameController.cs @@ -1,30 +1,61 @@ -using AspShogiSockets.ServiceModels.Api.Messages; +using Gameboard.ShogiUI.Sockets.Repositories; +using Gameboard.ShogiUI.Sockets.Repositories.RepositoryManagers; +using Gameboard.ShogiUI.Sockets.ServiceModels.Api.Messages; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System.Linq; using System.Threading.Tasks; -using Websockets.Repositories; -namespace AspShogiSockets.Controllers +namespace Gameboard.ShogiUI.Sockets.Controllers { [Authorize] [ApiController] [Route("[controller]")] public class GameController : ControllerBase { - private readonly IGameboardRepository gameboardRepository; + private readonly IGameboardRepositoryManager manager; + private readonly IGameboardRepository repository; - public GameController(IGameboardRepository gameboardRepository) + public GameController( + IGameboardRepository repository, + IGameboardRepositoryManager manager) { - this.gameboardRepository = gameboardRepository; + this.manager = manager; + this.repository = repository; } [Route("JoinCode")] public async Task PostGameInvitation([FromBody] PostGameInvitation request) { var userName = HttpContext.User.Claims.First(c => c.Type == "preferred_username").Value; - var code = (await gameboardRepository.PostJoinCode(request.SessionName, userName)).JoinCode; - return new CreatedResult("", new PostGameInvitationResponse(code)); + var isPlayer1 = await manager.IsPlayer1(request.SessionName, userName); + if (isPlayer1) + { + var code = (await repository.PostJoinCode(request.SessionName, userName)).JoinCode; + return new CreatedResult("", new PostGameInvitationResponse(code)); + } + else + { + return new UnauthorizedResult(); + } + } + + [AllowAnonymous] + [Route("GuestJoinCode")] + public async Task PostGuestGameInvitation([FromBody] PostGuestGameInvitation request) + { + + var isGuest = manager.IsGuest(request.GuestId); + var isPlayer1 = manager.IsPlayer1(request.SessionName, request.GuestId); + if (isGuest && await isPlayer1) + { + var code = (await repository.PostJoinCode(request.SessionName, request.GuestId)).JoinCode; + return new CreatedResult("", new PostGameInvitationResponse(code)); + } + else + { + return new UnauthorizedResult(); + } } } } diff --git a/Gameboard.ShogiUI.Sockets/Controllers/SocketController.cs b/Gameboard.ShogiUI.Sockets/Controllers/SocketController.cs index 51058e6..a729cdb 100644 --- a/Gameboard.ShogiUI.Sockets/Controllers/SocketController.cs +++ b/Gameboard.ShogiUI.Sockets/Controllers/SocketController.cs @@ -1,15 +1,13 @@ -using AspShogiSockets.Repositories.RepositoryManagers; -using AspShogiSockets.ServiceModels.Api.Messages; +using Gameboard.ShogiUI.Sockets.Managers; +using Gameboard.ShogiUI.Sockets.Repositories; +using Gameboard.ShogiUI.Sockets.Repositories.RepositoryManagers; +using Gameboard.ShogiUI.Sockets.ServiceModels.Api.Messages; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; -using System; using System.Linq; using System.Threading.Tasks; -using Websockets.Managers; -using Websockets.Repositories; -using Websockets.ServiceModels; -namespace Websockets.Controllers +namespace Gameboard.ShogiUI.Sockets.Controllers { [Authorize] [Route("[controller]")] diff --git a/Gameboard.ShogiUI.Sockets/Extensions/WebSocketExtensions.cs b/Gameboard.ShogiUI.Sockets/Extensions/WebSocketExtensions.cs index a97f75b..fc53a38 100644 --- a/Gameboard.ShogiUI.Sockets/Extensions/WebSocketExtensions.cs +++ b/Gameboard.ShogiUI.Sockets/Extensions/WebSocketExtensions.cs @@ -4,7 +4,7 @@ using System.Text; using System.Threading; using System.Threading.Tasks; -namespace AspShogiSockets.Extensions +namespace Gameboard.ShogiUI.Sockets.Extensions { public static class WebSocketExtensions { diff --git a/Gameboard.ShogiUI.Sockets/Gameboard.ShogiUI.Sockets.csproj.user b/Gameboard.ShogiUI.Sockets/Gameboard.ShogiUI.Sockets.csproj.user index d757c8c..2adf92b 100644 --- a/Gameboard.ShogiUI.Sockets/Gameboard.ShogiUI.Sockets.csproj.user +++ b/Gameboard.ShogiUI.Sockets/Gameboard.ShogiUI.Sockets.csproj.user @@ -4,6 +4,7 @@ ApiControllerEmptyScaffolder root/Controller AspShogiSockets + false ProjectDebugger diff --git a/Gameboard.ShogiUI.Sockets/Managers/ClientActionHandlers/CreateGameHandler.cs b/Gameboard.ShogiUI.Sockets/Managers/ClientActionHandlers/CreateGameHandler.cs index 07e29dd..b7e3773 100644 --- a/Gameboard.ShogiUI.Sockets/Managers/ClientActionHandlers/CreateGameHandler.cs +++ b/Gameboard.ShogiUI.Sockets/Managers/ClientActionHandlers/CreateGameHandler.cs @@ -1,14 +1,14 @@ -using AspShogiSockets.Extensions; -using Gameboard.Shogi.Api.ServiceModels.Messages; +using Gameboard.Shogi.Api.ServiceModels.Messages; +using Gameboard.ShogiUI.Sockets.Extensions; +using Gameboard.ShogiUI.Sockets.Repositories; +using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages; +using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types; 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 +namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers { public class CreateGameHandler : IActionHandler { diff --git a/Gameboard.ShogiUI.Sockets/Managers/ClientActionHandlers/IActionHandler.cs b/Gameboard.ShogiUI.Sockets/Managers/ClientActionHandlers/IActionHandler.cs index 13cd3db..5168598 100644 --- a/Gameboard.ShogiUI.Sockets/Managers/ClientActionHandlers/IActionHandler.cs +++ b/Gameboard.ShogiUI.Sockets/Managers/ClientActionHandlers/IActionHandler.cs @@ -1,8 +1,8 @@ -using System.Net.WebSockets; +using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types; +using System.Net.WebSockets; using System.Threading.Tasks; -using Websockets.ServiceModels.Types; -namespace Websockets.Managers.ClientActionHandlers +namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers { public interface IActionHandler { diff --git a/Gameboard.ShogiUI.Sockets/Managers/ClientActionHandlers/JoinByCodeHandler.cs b/Gameboard.ShogiUI.Sockets/Managers/ClientActionHandlers/JoinByCodeHandler.cs index 4880007..102564a 100644 --- a/Gameboard.ShogiUI.Sockets/Managers/ClientActionHandlers/JoinByCodeHandler.cs +++ b/Gameboard.ShogiUI.Sockets/Managers/ClientActionHandlers/JoinByCodeHandler.cs @@ -1,14 +1,14 @@ -using AspShogiSockets.Extensions; -using Gameboard.Shogi.Api.ServiceModels.Messages; +using Gameboard.Shogi.Api.ServiceModels.Messages; +using Gameboard.ShogiUI.Sockets.Extensions; +using Gameboard.ShogiUI.Sockets.Repositories; +using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages; +using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types; 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 +namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers { public class JoinByCodeHandler : IActionHandler { diff --git a/Gameboard.ShogiUI.Sockets/Managers/ClientActionHandlers/JoinGameHandler.cs b/Gameboard.ShogiUI.Sockets/Managers/ClientActionHandlers/JoinGameHandler.cs index 45b6e64..0a45cca 100644 --- a/Gameboard.ShogiUI.Sockets/Managers/ClientActionHandlers/JoinGameHandler.cs +++ b/Gameboard.ShogiUI.Sockets/Managers/ClientActionHandlers/JoinGameHandler.cs @@ -1,13 +1,13 @@ using Gameboard.Shogi.Api.ServiceModels.Messages; +using Gameboard.ShogiUI.Sockets.Repositories; +using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages; +using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types; 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 +namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers { public class JoinGameHandler : IActionHandler { diff --git a/Gameboard.ShogiUI.Sockets/Managers/ClientActionHandlers/ListGamesHandler.cs b/Gameboard.ShogiUI.Sockets/Managers/ClientActionHandlers/ListGamesHandler.cs index cb09fc0..72050fc 100644 --- a/Gameboard.ShogiUI.Sockets/Managers/ClientActionHandlers/ListGamesHandler.cs +++ b/Gameboard.ShogiUI.Sockets/Managers/ClientActionHandlers/ListGamesHandler.cs @@ -1,15 +1,15 @@ -using AspShogiSockets.Extensions; +using Gameboard.ShogiUI.Sockets.Extensions; +using Gameboard.ShogiUI.Sockets.Repositories; +using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages; +using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using System; using System.Linq; using System.Net.WebSockets; using System.Threading.Tasks; -using Websockets.Repositories; -using Websockets.ServiceModels.Messages; -using Websockets.ServiceModels.Types; -namespace Websockets.Managers.ClientActionHandlers +namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers { public class ListGamesHandler : IActionHandler { diff --git a/Gameboard.ShogiUI.Sockets/Managers/ClientActionHandlers/LoadGameHandler.cs b/Gameboard.ShogiUI.Sockets/Managers/ClientActionHandlers/LoadGameHandler.cs index 5c8d1ba..97e7c32 100644 --- a/Gameboard.ShogiUI.Sockets/Managers/ClientActionHandlers/LoadGameHandler.cs +++ b/Gameboard.ShogiUI.Sockets/Managers/ClientActionHandlers/LoadGameHandler.cs @@ -1,15 +1,15 @@ -using AspShogiSockets.Extensions; +using Gameboard.ShogiUI.Sockets.Extensions; +using Gameboard.ShogiUI.Sockets.Managers.Utility; +using Gameboard.ShogiUI.Sockets.Repositories; +using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages; +using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using System.Linq; using System.Net.WebSockets; using System.Threading.Tasks; -using Websockets.Managers.Utility; -using Websockets.Repositories; -using Websockets.ServiceModels.Messages; -using Websockets.ServiceModels.Types; -namespace Websockets.Managers.ClientActionHandlers +namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers { public class LoadGameHandler : IActionHandler { diff --git a/Gameboard.ShogiUI.Sockets/Managers/ClientActionHandlers/MoveHandler.cs b/Gameboard.ShogiUI.Sockets/Managers/ClientActionHandlers/MoveHandler.cs index a5f8a78..bbce0c5 100644 --- a/Gameboard.ShogiUI.Sockets/Managers/ClientActionHandlers/MoveHandler.cs +++ b/Gameboard.ShogiUI.Sockets/Managers/ClientActionHandlers/MoveHandler.cs @@ -1,15 +1,15 @@ -using AspShogiSockets.Extensions; -using Gameboard.Shogi.Api.ServiceModels.Messages; +using Gameboard.Shogi.Api.ServiceModels.Messages; +using Gameboard.ShogiUI.Sockets.Extensions; +using Gameboard.ShogiUI.Sockets.Managers.Utility; +using Gameboard.ShogiUI.Sockets.Repositories; +using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages; +using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using System.Net.WebSockets; using System.Threading.Tasks; -using Websockets.Managers.Utility; -using Websockets.Repositories; -using Websockets.ServiceModels.Messages; -using Websockets.ServiceModels.Types; -namespace Websockets.Managers.ClientActionHandlers +namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers { public class MoveHandler : IActionHandler { diff --git a/Gameboard.ShogiUI.Sockets/Managers/SocketCommunicationManager.cs b/Gameboard.ShogiUI.Sockets/Managers/SocketCommunicationManager.cs index 88a13f2..2a0b452 100644 --- a/Gameboard.ShogiUI.Sockets/Managers/SocketCommunicationManager.cs +++ b/Gameboard.ShogiUI.Sockets/Managers/SocketCommunicationManager.cs @@ -1,5 +1,7 @@ -using AspShogiSockets.Extensions; -using AspShogiSockets.Managers.Utility; +using Gameboard.ShogiUI.Sockets.Extensions; +using Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers; +using Gameboard.ShogiUI.Sockets.Managers.Utility; +using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using System; @@ -8,10 +10,8 @@ using System.Collections.Generic; using System.Linq; using System.Net.WebSockets; using System.Threading.Tasks; -using Websockets.Managers.ClientActionHandlers; -using Websockets.ServiceModels.Types; -namespace Websockets.Managers +namespace Gameboard.ShogiUI.Sockets.Managers { public interface ISocketCommunicationManager { diff --git a/Gameboard.ShogiUI.Sockets/Managers/SocketConnectionManager.cs b/Gameboard.ShogiUI.Sockets/Managers/SocketConnectionManager.cs index 9c4c579..0e5d729 100644 --- a/Gameboard.ShogiUI.Sockets/Managers/SocketConnectionManager.cs +++ b/Gameboard.ShogiUI.Sockets/Managers/SocketConnectionManager.cs @@ -3,7 +3,7 @@ using System; using System.Net; using System.Threading.Tasks; -namespace Websockets.Managers +namespace Gameboard.ShogiUI.Sockets.Managers { public interface ISocketConnectionManager { diff --git a/Gameboard.ShogiUI.Sockets/Managers/SocketTokenManager.cs b/Gameboard.ShogiUI.Sockets/Managers/SocketTokenManager.cs index 9a7eb2a..971e438 100644 --- a/Gameboard.ShogiUI.Sockets/Managers/SocketTokenManager.cs +++ b/Gameboard.ShogiUI.Sockets/Managers/SocketTokenManager.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -namespace Websockets.Managers +namespace Gameboard.ShogiUI.Sockets.Managers { public interface ISocketTokenManager { diff --git a/Gameboard.ShogiUI.Sockets/Managers/Utility/JsonRequest.cs b/Gameboard.ShogiUI.Sockets/Managers/Utility/JsonRequest.cs index 0a2719a..9ac96f6 100644 --- a/Gameboard.ShogiUI.Sockets/Managers/Utility/JsonRequest.cs +++ b/Gameboard.ShogiUI.Sockets/Managers/Utility/JsonRequest.cs @@ -1,15 +1,15 @@ -using Websockets.ServiceModels.Interfaces; +using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Interfaces; -namespace Websockets.Managers.Utility +namespace Gameboard.ShogiUI.Sockets.Managers.Utility { - public class JsonRequest + public class JsonRequest + { + public IRequest Request { get; private set; } + public string Json { get; private set; } + public JsonRequest(IRequest request, string json) { - public IRequest Request { get; private set; } - public string Json { get; private set; } - public JsonRequest(IRequest request, string json) - { - Request = request; - Json = json; - } + Request = request; + Json = json; } + } } diff --git a/Gameboard.ShogiUI.Sockets/Managers/Utility/Mapper.cs b/Gameboard.ShogiUI.Sockets/Managers/Utility/Mapper.cs index 37662b7..9d84fac 100644 --- a/Gameboard.ShogiUI.Sockets/Managers/Utility/Mapper.cs +++ b/Gameboard.ShogiUI.Sockets/Managers/Utility/Mapper.cs @@ -1,67 +1,67 @@ -using Microsoft.FSharp.Core; -using Websockets.ServiceModels.Types; +using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types; +using Microsoft.FSharp.Core; using GameboardTypes = Gameboard.Shogi.Api.ServiceModels.Types; -namespace Websockets.Managers.Utility +namespace Gameboard.ShogiUI.Sockets.Managers.Utility { - public static class Mapper + public static class Mapper + { + public static GameboardTypes.Move Map(Move source) { - public static GameboardTypes.Move Map(Move source) - { - var from = source.From; - var to = source.To; - FSharpOption pieceFromCaptured = source.PieceFromCaptured switch - { - "B" => new FSharpOption(GameboardTypes.PieceName.Bishop), - "G" => new FSharpOption(GameboardTypes.PieceName.GoldenGeneral), - "K" => new FSharpOption(GameboardTypes.PieceName.King), - "k" => new FSharpOption(GameboardTypes.PieceName.Knight), - "L" => new FSharpOption(GameboardTypes.PieceName.Lance), - "P" => new FSharpOption(GameboardTypes.PieceName.Pawn), - "R" => new FSharpOption(GameboardTypes.PieceName.Rook), - "S" => new FSharpOption(GameboardTypes.PieceName.SilverGeneral), - _ => null - }; - var target = new GameboardTypes.Move - { - Origin = new GameboardTypes.BoardLocation { X = from.X, Y = from.Y }, - Destination = new GameboardTypes.BoardLocation { X = to.X, Y = to.Y }, - IsPromotion = source.IsPromotion, - PieceFromCaptured = pieceFromCaptured - }; - return target; - } - - public static Move Map(GameboardTypes.Move source) - { - var origin = source.Origin; - var destination = source.Destination; - string pieceFromCaptured = null; - if (source.PieceFromCaptured != null) - { - pieceFromCaptured = source.PieceFromCaptured.Value switch - { - GameboardTypes.PieceName.Bishop => "B", - GameboardTypes.PieceName.GoldenGeneral => "G", - GameboardTypes.PieceName.King => "K", - GameboardTypes.PieceName.Knight => "k", - GameboardTypes.PieceName.Lance => "L", - GameboardTypes.PieceName.Pawn => "P", - GameboardTypes.PieceName.Rook => "R", - GameboardTypes.PieceName.SilverGeneral => "S", - _ => "" - }; - } - - var target = new Move - { - From = new Coords { X = origin.X, Y = origin.Y }, - To = new Coords { X = destination.X, Y = destination.Y }, - IsPromotion = source.IsPromotion, - PieceFromCaptured = pieceFromCaptured - }; - - return target; - } + var from = source.From; + var to = source.To; + FSharpOption pieceFromCaptured = source.PieceFromCaptured switch + { + "B" => new FSharpOption(GameboardTypes.PieceName.Bishop), + "G" => new FSharpOption(GameboardTypes.PieceName.GoldenGeneral), + "K" => new FSharpOption(GameboardTypes.PieceName.King), + "k" => new FSharpOption(GameboardTypes.PieceName.Knight), + "L" => new FSharpOption(GameboardTypes.PieceName.Lance), + "P" => new FSharpOption(GameboardTypes.PieceName.Pawn), + "R" => new FSharpOption(GameboardTypes.PieceName.Rook), + "S" => new FSharpOption(GameboardTypes.PieceName.SilverGeneral), + _ => null + }; + var target = new GameboardTypes.Move + { + Origin = new GameboardTypes.BoardLocation { X = from.X, Y = from.Y }, + Destination = new GameboardTypes.BoardLocation { X = to.X, Y = to.Y }, + IsPromotion = source.IsPromotion, + PieceFromCaptured = pieceFromCaptured + }; + return target; } + + public static Move Map(GameboardTypes.Move source) + { + var origin = source.Origin; + var destination = source.Destination; + string pieceFromCaptured = null; + if (source.PieceFromCaptured != null) + { + pieceFromCaptured = source.PieceFromCaptured.Value switch + { + GameboardTypes.PieceName.Bishop => "B", + GameboardTypes.PieceName.GoldenGeneral => "G", + GameboardTypes.PieceName.King => "K", + GameboardTypes.PieceName.Knight => "k", + GameboardTypes.PieceName.Lance => "L", + GameboardTypes.PieceName.Pawn => "P", + GameboardTypes.PieceName.Rook => "R", + GameboardTypes.PieceName.SilverGeneral => "S", + _ => "" + }; + } + + var target = new Move + { + From = new Coords { X = origin.X, Y = origin.Y }, + To = new Coords { X = destination.X, Y = destination.Y }, + IsPromotion = source.IsPromotion, + PieceFromCaptured = pieceFromCaptured + }; + + return target; + } + } } diff --git a/Gameboard.ShogiUI.Sockets/Managers/Utility/Request.cs b/Gameboard.ShogiUI.Sockets/Managers/Utility/Request.cs index b0c4e95..df3f245 100644 --- a/Gameboard.ShogiUI.Sockets/Managers/Utility/Request.cs +++ b/Gameboard.ShogiUI.Sockets/Managers/Utility/Request.cs @@ -1,11 +1,11 @@ -using Websockets.ServiceModels.Interfaces; -using Websockets.ServiceModels.Types; +using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Interfaces; +using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types; -namespace AspShogiSockets.Managers.Utility +namespace Gameboard.ShogiUI.Sockets.Managers.Utility { - public class Request : IRequest - { - public ClientAction Action { get; set; } - public string PlayerName { get; set; } - } + public class Request : IRequest + { + public ClientAction Action { get; set; } + public string PlayerName { get; set; } + } } diff --git a/Gameboard.ShogiUI.Sockets/Program.cs b/Gameboard.ShogiUI.Sockets/Program.cs index af5f468..2cafbbf 100644 --- a/Gameboard.ShogiUI.Sockets/Program.cs +++ b/Gameboard.ShogiUI.Sockets/Program.cs @@ -1,7 +1,7 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Hosting; -namespace Websockets +namespace Gameboard.ShogiUI.Sockets { public class Program { diff --git a/Gameboard.ShogiUI.Sockets/Repositories/GameboardRepository.cs b/Gameboard.ShogiUI.Sockets/Repositories/GameboardRepository.cs index a31e313..96c3e44 100644 --- a/Gameboard.ShogiUI.Sockets/Repositories/GameboardRepository.cs +++ b/Gameboard.ShogiUI.Sockets/Repositories/GameboardRepository.cs @@ -4,9 +4,9 @@ using System; using System.Net.Http; using System.Text; using System.Threading.Tasks; -using Websockets.Repositories.Utility; +using Gameboard.ShogiUI.Sockets.Repositories.Utility; -namespace Websockets.Repositories +namespace Gameboard.ShogiUI.Sockets.Repositories { public interface IGameboardRepository { diff --git a/Gameboard.ShogiUI.Sockets/Repositories/RepositoryManagers/GameboardRepositoryManager.cs b/Gameboard.ShogiUI.Sockets/Repositories/RepositoryManagers/GameboardRepositoryManager.cs index 646b467..55ff15a 100644 --- a/Gameboard.ShogiUI.Sockets/Repositories/RepositoryManagers/GameboardRepositoryManager.cs +++ b/Gameboard.ShogiUI.Sockets/Repositories/RepositoryManagers/GameboardRepositoryManager.cs @@ -1,19 +1,21 @@ using Gameboard.Shogi.Api.ServiceModels.Messages; using System; using System.Threading.Tasks; -using Websockets.Repositories; -namespace AspShogiSockets.Repositories.RepositoryManagers +namespace Gameboard.ShogiUI.Sockets.Repositories.RepositoryManagers { public interface IGameboardRepositoryManager { Task CreateGuestUser(); + Task IsPlayer1(string sessionName, string playerName); + bool IsGuest(string playerName); } public class GameboardRepositoryManager : IGameboardRepositoryManager { - private readonly IGameboardRepository repository; private const int MaxTries = 3; + private const string GuestPrefix = "Guest-"; + private readonly IGameboardRepository repository; public GameboardRepositoryManager(IGameboardRepository repository) { @@ -39,5 +41,23 @@ namespace AspShogiSockets.Repositories.RepositoryManagers } throw new OperationCanceledException($"Failed to create guest user after {MaxTries} tries."); } + + public async Task IsPlayer1(string sessionName, string playerName) + { + var session = await repository.GetGame(sessionName); + return session?.Session.Player1 == playerName; + } + + public async Task CreateJoinCode(string sessionName, string playerName) + { + var getGameResponse = await repository.GetGame(sessionName); + if (playerName == getGameResponse?.Session.Player1) + { + return (await repository.PostJoinCode(sessionName, playerName)).JoinCode; + } + return null; + } + + public bool IsGuest(string playerName) => playerName.StartsWith(GuestPrefix); } } diff --git a/Gameboard.ShogiUI.Sockets/Repositories/Utility/AuthenticatedHttpClient.cs b/Gameboard.ShogiUI.Sockets/Repositories/Utility/AuthenticatedHttpClient.cs index cd50a1a..a25c7d7 100644 --- a/Gameboard.ShogiUI.Sockets/Repositories/Utility/AuthenticatedHttpClient.cs +++ b/Gameboard.ShogiUI.Sockets/Repositories/Utility/AuthenticatedHttpClient.cs @@ -6,7 +6,7 @@ using System.Net; using System.Net.Http; using System.Threading.Tasks; -namespace Websockets.Repositories.Utility +namespace Gameboard.ShogiUI.Sockets.Repositories.Utility { public interface IAuthenticatedHttpClient { diff --git a/Gameboard.ShogiUI.Sockets/Startup.cs b/Gameboard.ShogiUI.Sockets/Startup.cs index ecb5f8d..7d32044 100644 --- a/Gameboard.ShogiUI.Sockets/Startup.cs +++ b/Gameboard.ShogiUI.Sockets/Startup.cs @@ -1,4 +1,4 @@ -using AspShogiSockets.Repositories.RepositoryManagers; +using Gameboard.ShogiUI.Sockets.Repositories.RepositoryManagers; using Gameboard.ShogiUI.Sockets.Extensions; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Builder; @@ -12,13 +12,13 @@ using Newtonsoft.Json.Serialization; using System; using System.Collections.Generic; using System.Linq; -using Websockets.Managers; -using Websockets.Managers.ClientActionHandlers; -using Websockets.Repositories; -using Websockets.Repositories.Utility; -using Websockets.ServiceModels.Types; +using Gameboard.ShogiUI.Sockets.Managers; +using Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers; +using Gameboard.ShogiUI.Sockets.Repositories; +using Gameboard.ShogiUI.Sockets.Repositories.Utility; +using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types; -namespace Websockets +namespace Gameboard.ShogiUI.Sockets { public class Startup { From 1826c0760151cf9d1e8e604db92db3d8fb550ec5 Mon Sep 17 00:00:00 2001 From: Lucas Morgan Date: Mon, 25 Jan 2021 21:02:29 -0600 Subject: [PATCH 2/2] Handle socket disonnects --- .../Managers/SocketCommunicationManager.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Gameboard.ShogiUI.Sockets/Managers/SocketCommunicationManager.cs b/Gameboard.ShogiUI.Sockets/Managers/SocketCommunicationManager.cs index 2a0b452..8e68a7a 100644 --- a/Gameboard.ShogiUI.Sockets/Managers/SocketCommunicationManager.cs +++ b/Gameboard.ShogiUI.Sockets/Managers/SocketCommunicationManager.cs @@ -68,6 +68,12 @@ namespace Gameboard.ShogiUI.Sockets.Managers { logger.LogError(ex.Message); } + catch(WebSocketException ex) + { + logger.LogInformation($"{nameof(WebSocketException)} in {nameof(SocketCommunicationManager)}."); + logger.LogInformation("Probably tried writing to a closed socket."); + logger.LogError(ex.Message); + } } UnsubscribeFromBroadcastAndGames(userName); }