All the code
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -39,6 +39,7 @@ Thumbs.db
|
||||
*.app
|
||||
*.exe
|
||||
*.war
|
||||
*.vs
|
||||
|
||||
# Large media files
|
||||
*.mp4
|
||||
@@ -48,3 +49,6 @@ Thumbs.db
|
||||
*.mov
|
||||
*.wmv
|
||||
|
||||
#Luke
|
||||
bin
|
||||
obj
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
|
||||
namespace AspShogiSockets.ServiceModels.Api.Messages
|
||||
{
|
||||
public class GetGuestToken
|
||||
{
|
||||
public string ClientId { get; set; }
|
||||
}
|
||||
|
||||
public class GetGuestTokenResponse
|
||||
{
|
||||
public string ClientId { get; }
|
||||
public Guid OneTimeToken { get; }
|
||||
|
||||
public GetGuestTokenResponse(string clientId, Guid token)
|
||||
{
|
||||
ClientId = clientId;
|
||||
OneTimeToken = token;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
|
||||
namespace Websockets.ServiceModels
|
||||
{
|
||||
public class GetTokenResponse
|
||||
{
|
||||
public Guid OneTimeToken { get; }
|
||||
|
||||
public GetTokenResponse(Guid token)
|
||||
{
|
||||
OneTimeToken = token;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
namespace AspShogiSockets.ServiceModels.Api.Messages
|
||||
{
|
||||
public class PostGameInvitation
|
||||
{
|
||||
public string SessionName { get; set; }
|
||||
}
|
||||
public class PostGameInvitationResponse
|
||||
{
|
||||
public string Code { get; }
|
||||
public PostGameInvitationResponse(string code)
|
||||
{
|
||||
Code = code;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,9 @@
|
||||
using Websockets.ServiceModels.Types;
|
||||
|
||||
namespace Websockets.ServiceModels.Interfaces
|
||||
{
|
||||
public interface IRequest
|
||||
{
|
||||
ClientAction Action { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Websockets.ServiceModels.Interfaces
|
||||
{
|
||||
public interface IResponse
|
||||
{
|
||||
string Action { get; }
|
||||
string Error { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using Websockets.ServiceModels.Interfaces;
|
||||
using Websockets.ServiceModels.Types;
|
||||
|
||||
namespace Websockets.ServiceModels.Messages
|
||||
{
|
||||
public class CreateGameRequest : IRequest
|
||||
{
|
||||
public ClientAction Action { get; set; }
|
||||
public string GameName { get; set; }
|
||||
public bool IsPrivate { get; set; }
|
||||
}
|
||||
|
||||
public class CreateGameResponse : IResponse
|
||||
{
|
||||
public string Action { get; private set; }
|
||||
public string Error { get; set; }
|
||||
public Game Game { get; set; }
|
||||
public string PlayerName { get; set; }
|
||||
|
||||
public CreateGameResponse(ClientAction action)
|
||||
{
|
||||
Action = action.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using Websockets.ServiceModels.Interfaces;
|
||||
using Websockets.ServiceModels.Types;
|
||||
|
||||
namespace Websockets.ServiceModels.Messages
|
||||
{
|
||||
public class ErrorResponse : IResponse
|
||||
{
|
||||
public string Action { get; private set; }
|
||||
public string Error { get; set; }
|
||||
|
||||
public ErrorResponse(ClientAction action)
|
||||
{
|
||||
Action = action.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using Websockets.ServiceModels.Interfaces;
|
||||
using Websockets.ServiceModels.Types;
|
||||
|
||||
namespace Websockets.ServiceModels.Messages
|
||||
{
|
||||
public class JoinByCode : IRequest
|
||||
{
|
||||
public ClientAction Action { get; set; }
|
||||
public string JoinCode { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using Websockets.ServiceModels.Interfaces;
|
||||
using Websockets.ServiceModels.Types;
|
||||
|
||||
namespace Websockets.ServiceModels.Messages
|
||||
{
|
||||
public class JoinGameRequest : IRequest
|
||||
{
|
||||
public ClientAction Action { get; set; }
|
||||
public string GameName { get; set; }
|
||||
}
|
||||
|
||||
public class JoinGameResponse : IResponse
|
||||
{
|
||||
public string Action { get; private set; }
|
||||
public string Error { get; set; }
|
||||
public string GameName { get; set; }
|
||||
public string PlayerName { get; set; }
|
||||
|
||||
public JoinGameResponse(ClientAction action)
|
||||
{
|
||||
Action = action.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
using Websockets.ServiceModels.Interfaces;
|
||||
using Websockets.ServiceModels.Types;
|
||||
|
||||
namespace Websockets.ServiceModels.Messages
|
||||
{
|
||||
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<Game> Games { get; set; }
|
||||
|
||||
public ListGamesResponse(ClientAction action)
|
||||
{
|
||||
Action = action.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using System.Collections.Generic;
|
||||
using Websockets.ServiceModels.Interfaces;
|
||||
using Websockets.ServiceModels.Types;
|
||||
|
||||
namespace Websockets.ServiceModels.Messages
|
||||
{
|
||||
public class LoadGameRequest : IRequest
|
||||
{
|
||||
public ClientAction Action { get; set; }
|
||||
public string GameName { get; set; }
|
||||
}
|
||||
|
||||
public class LoadGameResponse : IResponse
|
||||
{
|
||||
public string Action { get; private set; }
|
||||
public Game Game { get; set; }
|
||||
public IEnumerable<Move> Moves { get; set; }
|
||||
public string Error { get; set; }
|
||||
|
||||
public LoadGameResponse(ClientAction action)
|
||||
{
|
||||
Action = action.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using Websockets.ServiceModels.Interfaces;
|
||||
using Websockets.ServiceModels.Types;
|
||||
|
||||
namespace Websockets.ServiceModels.Messages
|
||||
{
|
||||
public class MoveRequest : IRequest
|
||||
{
|
||||
public ClientAction Action { get; set; }
|
||||
public string GameName { get; set; }
|
||||
public Move Move { get; set; }
|
||||
}
|
||||
|
||||
public class MoveResponse : IResponse
|
||||
{
|
||||
public string Action { get; }
|
||||
public string Error { get; set; }
|
||||
public string GameName { get; set; }
|
||||
public Move Move { get; set; }
|
||||
public string PlayerName { get; set; }
|
||||
|
||||
public MoveResponse(ClientAction action)
|
||||
{
|
||||
Action = action.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
namespace Websockets.ServiceModels.Types
|
||||
{
|
||||
public enum ClientAction
|
||||
{
|
||||
ListGames,
|
||||
CreateGame,
|
||||
JoinGame,
|
||||
JoinByCode,
|
||||
LoadGame,
|
||||
Move,
|
||||
KeepAlive
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Websockets.ServiceModels.Types
|
||||
{
|
||||
public class Coords
|
||||
{
|
||||
public int X { get; set; }
|
||||
public int Y { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Websockets.ServiceModels.Types
|
||||
{
|
||||
public class Game
|
||||
{
|
||||
public string GameName { get; set; }
|
||||
public string[] Players { get; set; }
|
||||
}
|
||||
}
|
||||
33
Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Types/Move.cs
Normal file
33
Gameboard.ShogiUI.Sockets.ServiceModels/Socket/Types/Move.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
namespace Websockets.ServiceModels.Types
|
||||
{
|
||||
public class Move
|
||||
{
|
||||
public string PieceFromCaptured { get; set; }
|
||||
public Coords From { get; set; }
|
||||
public Coords To { get; set; }
|
||||
public bool IsPromotion { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Toggles perspective of this move. (ie from player 1 to player 2)
|
||||
/// </summary>
|
||||
public static Move ConvertPerspective(Move m)
|
||||
{
|
||||
var convertedMove = new Move
|
||||
{
|
||||
To = new Coords
|
||||
{
|
||||
X = 8 - m.To.X,
|
||||
Y = 8 - m.To.Y
|
||||
},
|
||||
From = new Coords
|
||||
{
|
||||
X = 8 - m.From.X,
|
||||
Y = 8 - m.From.Y
|
||||
},
|
||||
IsPromotion = m.IsPromotion,
|
||||
PieceFromCaptured = m.PieceFromCaptured
|
||||
};
|
||||
return convertedMove;
|
||||
}
|
||||
}
|
||||
}
|
||||
31
Gameboard.ShogiUI.Sockets.sln
Normal file
31
Gameboard.ShogiUI.Sockets.sln
Normal file
@@ -0,0 +1,31 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 16
|
||||
VisualStudioVersion = 16.0.30503.244
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Gameboard.ShogiUI.Sockets", "Gameboard.ShogiUI.Sockets\Gameboard.ShogiUI.Sockets.csproj", "{4FF35F9D-E525-46CF-A8A6-A147FE50AD68}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Gameboard.ShogiUI.Sockets.ServiceModels", "Gameboard.ShogiUI.Sockets.ServiceModels\Gameboard.ShogiUI.Sockets.ServiceModels.csproj", "{FE775DE4-50F0-4C5D-AD2B-01320B1E7086}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{4FF35F9D-E525-46CF-A8A6-A147FE50AD68}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{4FF35F9D-E525-46CF-A8A6-A147FE50AD68}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{4FF35F9D-E525-46CF-A8A6-A147FE50AD68}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{4FF35F9D-E525-46CF-A8A6-A147FE50AD68}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{FE775DE4-50F0-4C5D-AD2B-01320B1E7086}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{FE775DE4-50F0-4C5D-AD2B-01320B1E7086}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{FE775DE4-50F0-4C5D-AD2B-01320B1E7086}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{FE775DE4-50F0-4C5D-AD2B-01320B1E7086}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {1D0B04F2-0DA1-4CB4-A82A-5A1C3B52ACEB}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
30
Gameboard.ShogiUI.Sockets/Controllers/GameController.cs
Normal file
30
Gameboard.ShogiUI.Sockets/Controllers/GameController.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using AspShogiSockets.ServiceModels.Api.Messages;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Websockets.Repositories;
|
||||
|
||||
namespace AspShogiSockets.Controllers
|
||||
{
|
||||
[Authorize]
|
||||
[ApiController]
|
||||
[Route("[controller]")]
|
||||
public class GameController : ControllerBase
|
||||
{
|
||||
private readonly IGameboardRepository gameboardRepository;
|
||||
|
||||
public GameController(IGameboardRepository gameboardRepository)
|
||||
{
|
||||
this.gameboardRepository = gameboardRepository;
|
||||
}
|
||||
|
||||
[Route("JoinCode")]
|
||||
public async Task<IActionResult> 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
63
Gameboard.ShogiUI.Sockets/Controllers/SocketController.cs
Normal file
63
Gameboard.ShogiUI.Sockets/Controllers/SocketController.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using AspShogiSockets.Repositories.RepositoryManagers;
|
||||
using AspShogiSockets.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
|
||||
{
|
||||
[Authorize]
|
||||
[Route("[controller]")]
|
||||
[ApiController]
|
||||
public class SocketController : ControllerBase
|
||||
{
|
||||
private readonly ISocketTokenManager tokenManager;
|
||||
private readonly IGameboardRepository gameboardRepository;
|
||||
private readonly IGameboardRepositoryManager gameboardManager;
|
||||
|
||||
public SocketController(
|
||||
ISocketTokenManager tokenManager,
|
||||
IGameboardRepository gameboardRepository,
|
||||
IGameboardRepositoryManager gameboardManager)
|
||||
{
|
||||
this.tokenManager = tokenManager;
|
||||
this.gameboardRepository = gameboardRepository;
|
||||
this.gameboardManager = gameboardManager;
|
||||
}
|
||||
|
||||
[Route("Token")]
|
||||
public IActionResult GetToken()
|
||||
{
|
||||
var userName = HttpContext.User.Claims.First(c => c.Type == "preferred_username").Value;
|
||||
var token = tokenManager.GenerateToken(userName);
|
||||
return new JsonResult(new GetTokenResponse(token));
|
||||
}
|
||||
|
||||
[AllowAnonymous]
|
||||
[Route("GuestToken")]
|
||||
public async Task<IActionResult> GetGuestToken([FromQuery] GetGuestToken request)
|
||||
{
|
||||
if (request.ClientId == null)
|
||||
{
|
||||
var clientId = await gameboardManager.CreateGuestUser();
|
||||
var token = tokenManager.GenerateToken(clientId);
|
||||
return new JsonResult(new GetGuestTokenResponse(clientId, token));
|
||||
}
|
||||
else
|
||||
{
|
||||
var response = await gameboardRepository.GetPlayer(request.ClientId);
|
||||
if (response != null && response.Player != null)
|
||||
{
|
||||
var token = tokenManager.GenerateToken(response.Player.Name);
|
||||
return new JsonResult(new GetGuestTokenResponse(response.Player.Name, token));
|
||||
}
|
||||
}
|
||||
return new UnauthorizedResult();
|
||||
}
|
||||
}
|
||||
}
|
||||
43
Gameboard.ShogiUI.Sockets/Extensions/LogMiddleware.cs
Normal file
43
Gameboard.ShogiUI.Sockets/Extensions/LogMiddleware.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Extensions
|
||||
{
|
||||
public class LogMiddleware
|
||||
{
|
||||
private readonly RequestDelegate next;
|
||||
private readonly ILogger logger;
|
||||
|
||||
public LogMiddleware(RequestDelegate next, ILoggerFactory factory)
|
||||
{
|
||||
this.next = next;
|
||||
logger = factory.CreateLogger<LogMiddleware>();
|
||||
}
|
||||
|
||||
public async Task Invoke(HttpContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
await next(context);
|
||||
}
|
||||
finally
|
||||
{
|
||||
logger.LogInformation("Request {method} {url} => {statusCode}",
|
||||
context.Request?.Method,
|
||||
context.Request?.Path.Value,
|
||||
context.Response?.StatusCode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class IApplicationBuilderExtensions
|
||||
{
|
||||
public static IApplicationBuilder UseRequestResponseLogging(this IApplicationBuilder builder)
|
||||
{
|
||||
builder.UseMiddleware<LogMiddleware>();
|
||||
return builder;
|
||||
}
|
||||
}
|
||||
}
|
||||
24
Gameboard.ShogiUI.Sockets/Extensions/WebSocketExtensions.cs
Normal file
24
Gameboard.ShogiUI.Sockets/Extensions/WebSocketExtensions.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using System.Net.WebSockets;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AspShogiSockets.Extensions
|
||||
{
|
||||
public static class WebSocketExtensions
|
||||
{
|
||||
public static async Task SendTextAsync(this WebSocket self, string message)
|
||||
{
|
||||
await self.SendAsync(Encoding.UTF8.GetBytes(message), WebSocketMessageType.Text, true, CancellationToken.None);
|
||||
}
|
||||
|
||||
public static async Task<string> ReceiveTextAsync(this WebSocket self)
|
||||
{
|
||||
var buffer = new ArraySegment<byte>(new byte[2048]);
|
||||
var receive = await self.ReceiveAsync(buffer, CancellationToken.None);
|
||||
return Encoding.UTF8.GetString(buffer.Slice(0, receive.Count));
|
||||
// TODO: Make this robust to multi-frame messages.
|
||||
}
|
||||
}
|
||||
}
|
||||
26
Gameboard.ShogiUI.Sockets/Gameboard.ShogiUI.Sockets.csproj
Normal file
26
Gameboard.ShogiUI.Sockets/Gameboard.ShogiUI.Sockets.csproj
Normal file
@@ -0,0 +1,26 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Gameboard.Shogi.Api.ServiceModels" Version="1.0.8" />
|
||||
<PackageReference Include="IdentityModel" Version="4.4.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.AzureAD.UI" Version="3.1.8" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.8" />
|
||||
<PackageReference Include="Microsoft.Identity.Web" Version="1.0.0" />
|
||||
<PackageReference Include="Microsoft.Identity.Web.MicrosoftGraph" Version="1.0.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Gameboard.ShogiUI.Sockets.ServiceModels\Gameboard.ShogiUI.Sockets.ServiceModels.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Models\" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<Controller_SelectedScaffolderID>ApiControllerEmptyScaffolder</Controller_SelectedScaffolderID>
|
||||
<Controller_SelectedScaffolderCategoryPath>root/Controller</Controller_SelectedScaffolderCategoryPath>
|
||||
<ActiveDebugProfile>AspShogiSockets</ActiveDebugProfile>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,67 @@
|
||||
using AspShogiSockets.Extensions;
|
||||
using Gameboard.Shogi.Api.ServiceModels.Messages;
|
||||
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
|
||||
{
|
||||
public class CreateGameHandler : IActionHandler
|
||||
{
|
||||
private readonly ILogger<CreateGameHandler> logger;
|
||||
private readonly IGameboardRepository repository;
|
||||
private readonly ISocketCommunicationManager communicationManager;
|
||||
|
||||
public CreateGameHandler(
|
||||
ILogger<CreateGameHandler> logger,
|
||||
ISocketCommunicationManager communicationManager,
|
||||
IGameboardRepository repository)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.repository = repository;
|
||||
this.communicationManager = communicationManager;
|
||||
}
|
||||
|
||||
public async Task Handle(WebSocket socket, string json, string userName)
|
||||
{
|
||||
logger.LogInformation("Socket Request \n{0}\n", new[] { json });
|
||||
var request = JsonConvert.DeserializeObject<CreateGameRequest>(json);
|
||||
var postGameResponse = await repository.PostGame(new PostGame
|
||||
{
|
||||
GameName = request.GameName,
|
||||
PlayerName = userName, // TODO : Investigate if needed by UI
|
||||
IsPrivate = request.IsPrivate
|
||||
});
|
||||
|
||||
var response = new CreateGameResponse(request.Action)
|
||||
{
|
||||
PlayerName = userName,
|
||||
Game = new Game
|
||||
{
|
||||
GameName = postGameResponse.GameName,
|
||||
Players = new string[] { userName }
|
||||
}
|
||||
};
|
||||
|
||||
if (string.IsNullOrWhiteSpace(postGameResponse.GameName))
|
||||
{
|
||||
response.Error = "Game already exists.";
|
||||
}
|
||||
|
||||
var serialized = JsonConvert.SerializeObject(response);
|
||||
logger.LogInformation("Socket Response \n{0}\n", new[] { serialized });
|
||||
if (request.IsPrivate)
|
||||
{
|
||||
await socket.SendTextAsync(serialized);
|
||||
}
|
||||
else
|
||||
{
|
||||
await communicationManager.BroadcastToAll(serialized);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using System.Net.WebSockets;
|
||||
using System.Threading.Tasks;
|
||||
using Websockets.ServiceModels.Types;
|
||||
|
||||
namespace Websockets.Managers.ClientActionHandlers
|
||||
{
|
||||
public interface IActionHandler
|
||||
{
|
||||
/// <summary>
|
||||
/// Responsible for parsing json and handling the request.
|
||||
/// </summary>
|
||||
Task Handle(WebSocket socket, string json, string userName);
|
||||
}
|
||||
|
||||
public delegate IActionHandler ActionHandlerResolver(ClientAction action);
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
using AspShogiSockets.Extensions;
|
||||
using Gameboard.Shogi.Api.ServiceModels.Messages;
|
||||
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
|
||||
{
|
||||
public class JoinByCodeHandler : IActionHandler
|
||||
{
|
||||
private readonly ILogger<JoinByCodeHandler> logger;
|
||||
private readonly IGameboardRepository repository;
|
||||
private readonly ISocketCommunicationManager communicationManager;
|
||||
|
||||
public JoinByCodeHandler(
|
||||
ILogger<JoinByCodeHandler> logger,
|
||||
ISocketCommunicationManager communicationManager,
|
||||
IGameboardRepository repository)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.repository = repository;
|
||||
this.communicationManager = communicationManager;
|
||||
}
|
||||
|
||||
public async Task Handle(WebSocket socket, string json, string userName)
|
||||
{
|
||||
logger.LogInformation("Socket Request \n{0}\n", new[] { json });
|
||||
var request = JsonConvert.DeserializeObject<JoinByCode>(json);
|
||||
var joinGameResponse = await repository.PostJoinByCode(new PostJoinByCode
|
||||
{
|
||||
PlayerName = userName,
|
||||
JoinCode = request.JoinCode
|
||||
});
|
||||
|
||||
if (joinGameResponse.JoinSucceeded)
|
||||
{
|
||||
var gameName = (await repository.GetGame(joinGameResponse.GameName)).GameName;
|
||||
|
||||
// Other members of the game see a regular JoinGame occur.
|
||||
var response = new JoinGameResponse(ClientAction.JoinGame)
|
||||
{
|
||||
PlayerName = userName,
|
||||
GameName = gameName
|
||||
};
|
||||
var serialized = JsonConvert.SerializeObject(response);
|
||||
await communicationManager.BroadcastToGame(gameName, serialized);
|
||||
communicationManager.SubscribeToGame(socket, gameName, userName);
|
||||
|
||||
// But the player joining sees the JoinByCode occur.
|
||||
response = new JoinGameResponse(ClientAction.JoinByCode)
|
||||
{
|
||||
PlayerName = userName,
|
||||
GameName = gameName
|
||||
};
|
||||
serialized = JsonConvert.SerializeObject(response);
|
||||
await socket.SendTextAsync(serialized);
|
||||
}
|
||||
else
|
||||
{
|
||||
var response = new JoinGameResponse(ClientAction.JoinByCode)
|
||||
{
|
||||
PlayerName = userName,
|
||||
Error = "Error joining game."
|
||||
};
|
||||
var serialized = JsonConvert.SerializeObject(response);
|
||||
logger.LogInformation("Socket Response \n{0}\n", new[] { serialized });
|
||||
await socket.SendTextAsync(serialized);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
using Gameboard.Shogi.Api.ServiceModels.Messages;
|
||||
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
|
||||
{
|
||||
public class JoinGameHandler : IActionHandler
|
||||
{
|
||||
private readonly ILogger<JoinGameHandler> logger;
|
||||
private readonly IGameboardRepository gameboardRepository;
|
||||
private readonly ISocketCommunicationManager communicationManager;
|
||||
public JoinGameHandler(
|
||||
ILogger<JoinGameHandler> logger,
|
||||
ISocketCommunicationManager communicationManager,
|
||||
IGameboardRepository gameboardRepository)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.gameboardRepository = gameboardRepository;
|
||||
this.communicationManager = communicationManager;
|
||||
}
|
||||
|
||||
public async Task Handle(WebSocket socket, string json, string userName)
|
||||
{
|
||||
logger.LogInformation("Socket Request \n{0}\n", new[] { json });
|
||||
var request = JsonConvert.DeserializeObject<JoinGameRequest>(json);
|
||||
var response = new JoinGameResponse(ClientAction.JoinGame)
|
||||
{
|
||||
PlayerName = userName
|
||||
};
|
||||
|
||||
var joinGameResponse = await gameboardRepository.PostJoinGame(request.GameName, new PostJoinGame
|
||||
{
|
||||
PlayerName = userName
|
||||
});
|
||||
|
||||
if (joinGameResponse.JoinSucceeded)
|
||||
{
|
||||
response.GameName = request.GameName;
|
||||
}
|
||||
else
|
||||
{
|
||||
response.Error = "Game is full or code is incorrect.";
|
||||
}
|
||||
var serialized = JsonConvert.SerializeObject(response);
|
||||
logger.LogInformation("Socket Response \n{0}\n", new[] { serialized });
|
||||
await communicationManager.BroadcastToAll(serialized);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
using AspShogiSockets.Extensions;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
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
|
||||
{
|
||||
public class ListGamesHandler : IActionHandler
|
||||
{
|
||||
private readonly ILogger<ListGamesHandler> logger;
|
||||
private readonly IGameboardRepository repository;
|
||||
|
||||
public ListGamesHandler(
|
||||
ILogger<ListGamesHandler> logger,
|
||||
IGameboardRepository repository)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
public async Task Handle(WebSocket socket, string json, string userName)
|
||||
{
|
||||
logger.LogInformation("Socket Request \n{0}\n", new[] { json });
|
||||
var request = JsonConvert.DeserializeObject<ListGamesRequest>(json);
|
||||
var getGamesResponse = string.IsNullOrWhiteSpace(userName)
|
||||
? await repository.GetGames()
|
||||
: await repository.GetGames(userName);
|
||||
|
||||
var games = getGamesResponse.Games
|
||||
.OrderBy(g => g.Players.Contains(userName))
|
||||
.Select(g => new Game
|
||||
{
|
||||
GameName = g.GameName,
|
||||
Players = g.Players
|
||||
});
|
||||
var response = new ListGamesResponse(ClientAction.ListGames)
|
||||
{
|
||||
Games = games ?? new Game[0]
|
||||
};
|
||||
|
||||
var serialized = JsonConvert.SerializeObject(response);
|
||||
logger.LogInformation("Socket Response \n{0}\n", new[] { serialized });
|
||||
await socket.SendTextAsync(serialized);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
using AspShogiSockets.Extensions;
|
||||
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
|
||||
{
|
||||
public class LoadGameHandler : IActionHandler
|
||||
{
|
||||
private readonly ILogger<LoadGameHandler> logger;
|
||||
private readonly IGameboardRepository gameboardRepository;
|
||||
private readonly ISocketCommunicationManager communicationManager;
|
||||
|
||||
public LoadGameHandler(
|
||||
ILogger<LoadGameHandler> logger,
|
||||
ISocketCommunicationManager communicationManager,
|
||||
IGameboardRepository gameboardRepository)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.gameboardRepository = gameboardRepository;
|
||||
this.communicationManager = communicationManager;
|
||||
}
|
||||
|
||||
public async Task Handle(WebSocket socket, string json, string userName)
|
||||
{
|
||||
logger.LogInformation("Socket Request \n{0}\n", json);
|
||||
var request = JsonConvert.DeserializeObject<LoadGameRequest>(json);
|
||||
var response = new LoadGameResponse(ClientAction.LoadGame);
|
||||
var getGameResponse = await gameboardRepository.GetGame(request.GameName);
|
||||
var getMovesResponse = await gameboardRepository.GetMoves(request.GameName);
|
||||
|
||||
if (getGameResponse == null || getMovesResponse == null)
|
||||
{
|
||||
response.Error = $"Could not find game.";
|
||||
}
|
||||
else
|
||||
{
|
||||
var player1 = getGameResponse.Players[0];
|
||||
response.Game = new Game
|
||||
{
|
||||
GameName = getGameResponse.GameName,
|
||||
Players = getGameResponse.Players
|
||||
};
|
||||
|
||||
response.Moves = userName.Equals(player1)
|
||||
? getMovesResponse.Moves.Select(_ => Mapper.Map(_))
|
||||
: getMovesResponse.Moves.Select(_ => Move.ConvertPerspective(Mapper.Map(_)));
|
||||
|
||||
communicationManager.SubscribeToGame(socket, getGameResponse.GameName, userName);
|
||||
}
|
||||
|
||||
var serialized = JsonConvert.SerializeObject(response);
|
||||
logger.LogInformation("Socket Response \n{0}\n", serialized);
|
||||
await socket.SendTextAsync(serialized);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
using AspShogiSockets.Extensions;
|
||||
using Gameboard.Shogi.Api.ServiceModels.Messages;
|
||||
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
|
||||
{
|
||||
public class MoveHandler : IActionHandler
|
||||
{
|
||||
private readonly ILogger<MoveHandler> logger;
|
||||
private readonly IGameboardRepository gameboardRepository;
|
||||
private readonly ISocketCommunicationManager communicationManager;
|
||||
public MoveHandler(
|
||||
ILogger<MoveHandler> logger,
|
||||
ISocketCommunicationManager communicationManager,
|
||||
IGameboardRepository gameboardRepository)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.gameboardRepository = gameboardRepository;
|
||||
this.communicationManager = communicationManager;
|
||||
}
|
||||
|
||||
public async Task Handle(WebSocket socket, string json, string userName)
|
||||
{
|
||||
logger.LogInformation("Socket Request \n{0}\n", new[] { json });
|
||||
var request = JsonConvert.DeserializeObject<MoveRequest>(json);
|
||||
// Basic move validation
|
||||
var move = request.Move;
|
||||
if (move.To.Equals(move.From))
|
||||
{
|
||||
var serialized = JsonConvert.SerializeObject(
|
||||
new ErrorResponse(ClientAction.Move)
|
||||
{
|
||||
Error = "Error: moving piece from tile to the same tile."
|
||||
});
|
||||
await socket.SendTextAsync(serialized);
|
||||
return;
|
||||
}
|
||||
|
||||
var getGameResponse = await gameboardRepository.GetGame(request.GameName);
|
||||
var isPlayer1 = userName.Equals(getGameResponse.Players[0]);
|
||||
if (!isPlayer1)
|
||||
{
|
||||
// Convert the move coords to player1 perspective.
|
||||
move = Move.ConvertPerspective(move);
|
||||
}
|
||||
|
||||
await gameboardRepository.PostMove(
|
||||
request.GameName,
|
||||
new PostMove { Move = Mapper.Map(move) });
|
||||
|
||||
var response = new MoveResponse(ClientAction.Move)
|
||||
{
|
||||
GameName = request.GameName,
|
||||
PlayerName = userName
|
||||
};
|
||||
await communicationManager.BroadcastToGame(
|
||||
request.GameName,
|
||||
(playerName, sslStream) =>
|
||||
{
|
||||
response.Move = playerName.Equals(userName)
|
||||
? request.Move
|
||||
: Move.ConvertPerspective(request.Move);
|
||||
var serialized = JsonConvert.SerializeObject(response);
|
||||
logger.LogInformation("Socket Response \n{0}\n", new[] { serialized });
|
||||
return serialized;
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
166
Gameboard.ShogiUI.Sockets/Managers/SocketCommunicationManager.cs
Normal file
166
Gameboard.ShogiUI.Sockets/Managers/SocketCommunicationManager.cs
Normal file
@@ -0,0 +1,166 @@
|
||||
using AspShogiSockets.Extensions;
|
||||
using AspShogiSockets.Managers.Utility;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
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
|
||||
{
|
||||
public interface ISocketCommunicationManager
|
||||
{
|
||||
Task CommunicateWith(WebSocket w, string s);
|
||||
Task BroadcastToAll(string msg);
|
||||
Task BroadcastToGame(string gameName, Func<string, WebSocket, string> msgBuilder);
|
||||
Task BroadcastToGame(string gameName, string msg);
|
||||
void SubscribeToGame(WebSocket socket, string gameName, string playerName);
|
||||
void SubscribeToBroadcast(WebSocket socket, string playerName);
|
||||
void UnsubscribeFromBroadcastAndGames(string playerName);
|
||||
void UnsubscribeFromGame(string gameName, string playerName);
|
||||
}
|
||||
|
||||
public class SocketCommunicationManager : ISocketCommunicationManager
|
||||
{
|
||||
private readonly ConcurrentDictionary<string, WebSocket> connections;
|
||||
private readonly ConcurrentDictionary<string, List<string>> gameSeats;
|
||||
private readonly ILogger<SocketCommunicationManager> logger;
|
||||
private readonly ActionHandlerResolver handlerResolver;
|
||||
|
||||
public SocketCommunicationManager(
|
||||
ILogger<SocketCommunicationManager> logger,
|
||||
ActionHandlerResolver handlerResolver)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.handlerResolver = handlerResolver;
|
||||
connections = new ConcurrentDictionary<string, WebSocket>();
|
||||
gameSeats = new ConcurrentDictionary<string, List<string>>();
|
||||
}
|
||||
|
||||
public async Task CommunicateWith(WebSocket socket, string userName)
|
||||
{
|
||||
SubscribeToBroadcast(socket, userName);
|
||||
|
||||
while (!socket.CloseStatus.HasValue)
|
||||
{
|
||||
try
|
||||
{
|
||||
var message = await socket.ReceiveTextAsync();
|
||||
if (string.IsNullOrWhiteSpace(message)) continue;
|
||||
|
||||
var request = JsonConvert.DeserializeObject<Request>(message);
|
||||
if (!Enum.IsDefined(typeof(ClientAction), request.Action))
|
||||
{
|
||||
await socket.SendTextAsync("Error: Action not recognized.");
|
||||
}
|
||||
else
|
||||
{
|
||||
var handler = handlerResolver(request.Action);
|
||||
await handler.Handle(socket, message, userName);
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException ex)
|
||||
{
|
||||
logger.LogError(ex.Message);
|
||||
}
|
||||
}
|
||||
UnsubscribeFromBroadcastAndGames(userName);
|
||||
}
|
||||
|
||||
public void SubscribeToBroadcast(WebSocket socket, string playerName)
|
||||
{
|
||||
logger.LogInformation("Subscribing [{0}] to broadcast", playerName);
|
||||
connections.TryAdd(playerName, socket);
|
||||
}
|
||||
|
||||
public void UnsubscribeFromBroadcastAndGames(string playerName)
|
||||
{
|
||||
logger.LogInformation("Unsubscribing [{0}] from broadcast", playerName);
|
||||
connections.TryRemove(playerName, out _);
|
||||
foreach (var game in gameSeats)
|
||||
{
|
||||
game.Value.Remove(playerName);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unsubscribes the player from their current game, then subscribes to the new game.
|
||||
/// </summary>
|
||||
public void SubscribeToGame(WebSocket socket, string gameName, string playerName)
|
||||
{
|
||||
// Unsubscribe from any other games
|
||||
foreach (var kvp in gameSeats)
|
||||
{
|
||||
var gameNameKey = kvp.Key;
|
||||
UnsubscribeFromGame(gameNameKey, playerName);
|
||||
}
|
||||
|
||||
// Subscribe
|
||||
logger.LogInformation("Subscribing player [{0}] to game [{1}]", playerName, gameName);
|
||||
var addSuccess = gameSeats.TryAdd(gameName, new List<string> { playerName });
|
||||
if (!addSuccess && !gameSeats[gameName].Contains(playerName))
|
||||
{
|
||||
gameSeats[gameName].Add(playerName);
|
||||
}
|
||||
}
|
||||
|
||||
public void UnsubscribeFromGame(string gameName, string playerName)
|
||||
{
|
||||
if (gameSeats.ContainsKey(gameName))
|
||||
{
|
||||
logger.LogInformation("Unsubscribing player [{0}] from game [{1}]", playerName, gameName);
|
||||
gameSeats[gameName].Remove(playerName);
|
||||
if (gameSeats[gameName].Count == 0) gameSeats.TryRemove(gameName, out _);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task BroadcastToAll(string msg)
|
||||
{
|
||||
var tasks = connections.Select(kvp =>
|
||||
{
|
||||
var player = kvp.Key;
|
||||
var socket = kvp.Value;
|
||||
logger.LogInformation("Broadcasting to player [{0}] \n{1}\n", new[] { player, msg });
|
||||
return socket.SendTextAsync(msg);
|
||||
});
|
||||
await Task.WhenAll(tasks);
|
||||
}
|
||||
|
||||
public async Task BroadcastToGame(string gameName, string msg)
|
||||
{
|
||||
if (gameSeats.ContainsKey(gameName))
|
||||
{
|
||||
var tasks = gameSeats[gameName]
|
||||
.Select(playerName =>
|
||||
{
|
||||
logger.LogInformation("Broadcasting to game [{0}], player [{0}] \n{1}\n", gameName, playerName, msg);
|
||||
return connections[playerName];
|
||||
})
|
||||
.Where(stream => stream != null)
|
||||
.Select(socket => socket.SendTextAsync(msg));
|
||||
await Task.WhenAll(tasks);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task BroadcastToGame(string gameName, Func<string, WebSocket, string> msgBuilder)
|
||||
{
|
||||
if (gameSeats.ContainsKey(gameName))
|
||||
{
|
||||
var tasks = gameSeats[gameName]
|
||||
.Select(playerName =>
|
||||
{
|
||||
var socket = connections[playerName];
|
||||
var msg = msgBuilder(playerName, socket);
|
||||
logger.LogInformation("Broadcasting to game [{0}], player [{0}] \n{1}\n", gameName, playerName, msg);
|
||||
return socket.SendTextAsync(msg);
|
||||
});
|
||||
await Task.WhenAll(tasks);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Websockets.Managers
|
||||
{
|
||||
public interface ISocketConnectionManager
|
||||
{
|
||||
Task HandleSocketRequest(HttpContext context);
|
||||
}
|
||||
|
||||
public class SocketConnectionManager : ISocketConnectionManager
|
||||
{
|
||||
private readonly ISocketCommunicationManager communicationManager;
|
||||
private readonly ISocketTokenManager tokenManager;
|
||||
|
||||
public SocketConnectionManager(ISocketCommunicationManager communicationManager, ISocketTokenManager tokenManager) : base()
|
||||
{
|
||||
this.communicationManager = communicationManager;
|
||||
this.tokenManager = tokenManager;
|
||||
|
||||
}
|
||||
|
||||
public async Task HandleSocketRequest(HttpContext context)
|
||||
{
|
||||
var hasToken = context.Request.Query.Keys.Contains("token");
|
||||
if (hasToken)
|
||||
{
|
||||
var oneTimeToken = context.Request.Query["token"][0];
|
||||
var tokenAsGuid = Guid.Parse(oneTimeToken);
|
||||
var userName = tokenManager.GetUsername(tokenAsGuid);
|
||||
if (!string.IsNullOrEmpty(userName))
|
||||
{
|
||||
var socket = await context.WebSockets.AcceptWebSocketAsync();
|
||||
await communicationManager.CommunicateWith(socket, userName);
|
||||
return;
|
||||
}
|
||||
}
|
||||
context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
57
Gameboard.ShogiUI.Sockets/Managers/SocketTokenManager.cs
Normal file
57
Gameboard.ShogiUI.Sockets/Managers/SocketTokenManager.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Websockets.Managers
|
||||
{
|
||||
public interface ISocketTokenManager
|
||||
{
|
||||
Guid GenerateToken(string s);
|
||||
string GetUsername(Guid g);
|
||||
}
|
||||
|
||||
public class SocketTokenManager : ISocketTokenManager
|
||||
{
|
||||
/// <summary>
|
||||
/// Key is userName
|
||||
/// </summary>
|
||||
private readonly Dictionary<string, Guid> Tokens;
|
||||
|
||||
public SocketTokenManager()
|
||||
{
|
||||
Tokens = new Dictionary<string, Guid>();
|
||||
}
|
||||
|
||||
public Guid GenerateToken(string userName)
|
||||
{
|
||||
var guid = Guid.NewGuid();
|
||||
|
||||
if (Tokens.ContainsKey(userName))
|
||||
{
|
||||
Tokens.Remove(userName);
|
||||
}
|
||||
Tokens.Add(userName, guid);
|
||||
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
await Task.Delay(TimeSpan.FromMinutes(1));
|
||||
Tokens.Remove(userName);
|
||||
});
|
||||
|
||||
return guid;
|
||||
}
|
||||
|
||||
/// <returns>User name associated to the guid or null.</returns>
|
||||
public string GetUsername(Guid guid)
|
||||
{
|
||||
if (Tokens.ContainsValue(guid))
|
||||
{
|
||||
var username = Tokens.First(kvp => kvp.Value == guid).Key;
|
||||
Tokens.Remove(username);
|
||||
return username;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
15
Gameboard.ShogiUI.Sockets/Managers/Utility/JsonRequest.cs
Normal file
15
Gameboard.ShogiUI.Sockets/Managers/Utility/JsonRequest.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using Websockets.ServiceModels.Interfaces;
|
||||
|
||||
namespace Websockets.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;
|
||||
}
|
||||
}
|
||||
}
|
||||
67
Gameboard.ShogiUI.Sockets/Managers/Utility/Mapper.cs
Normal file
67
Gameboard.ShogiUI.Sockets/Managers/Utility/Mapper.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
using Microsoft.FSharp.Core;
|
||||
using Websockets.ServiceModels.Types;
|
||||
using GameboardTypes = Gameboard.Shogi.Api.ServiceModels.Types;
|
||||
|
||||
namespace Websockets.Managers.Utility
|
||||
{
|
||||
public static class Mapper
|
||||
{
|
||||
public static GameboardTypes.Move Map(Move source)
|
||||
{
|
||||
var from = source.From;
|
||||
var to = source.To;
|
||||
FSharpOption<GameboardTypes.PieceName> pieceFromCaptured = source.PieceFromCaptured switch
|
||||
{
|
||||
"B" => new FSharpOption<GameboardTypes.PieceName>(GameboardTypes.PieceName.Bishop),
|
||||
"G" => new FSharpOption<GameboardTypes.PieceName>(GameboardTypes.PieceName.GoldenGeneral),
|
||||
"K" => new FSharpOption<GameboardTypes.PieceName>(GameboardTypes.PieceName.King),
|
||||
"k" => new FSharpOption<GameboardTypes.PieceName>(GameboardTypes.PieceName.Knight),
|
||||
"L" => new FSharpOption<GameboardTypes.PieceName>(GameboardTypes.PieceName.Lance),
|
||||
"P" => new FSharpOption<GameboardTypes.PieceName>(GameboardTypes.PieceName.Pawn),
|
||||
"R" => new FSharpOption<GameboardTypes.PieceName>(GameboardTypes.PieceName.Rook),
|
||||
"S" => new FSharpOption<GameboardTypes.PieceName>(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;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Gameboard.ShogiUI.Sockets/Managers/Utility/Request.cs
Normal file
11
Gameboard.ShogiUI.Sockets/Managers/Utility/Request.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using Websockets.ServiceModels.Interfaces;
|
||||
using Websockets.ServiceModels.Types;
|
||||
|
||||
namespace AspShogiSockets.Managers.Utility
|
||||
{
|
||||
public class Request : IRequest
|
||||
{
|
||||
public ClientAction Action { get; set; }
|
||||
public string PlayerName { get; set; }
|
||||
}
|
||||
}
|
||||
20
Gameboard.ShogiUI.Sockets/Program.cs
Normal file
20
Gameboard.ShogiUI.Sockets/Program.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace Websockets
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
CreateHostBuilder(args).Build().Run();
|
||||
}
|
||||
|
||||
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
||||
Host.CreateDefaultBuilder(args)
|
||||
.ConfigureWebHostDefaults(webBuilder =>
|
||||
{
|
||||
webBuilder.UseStartup<Startup>();
|
||||
});
|
||||
}
|
||||
}
|
||||
29
Gameboard.ShogiUI.Sockets/Properties/launchSettings.json
Normal file
29
Gameboard.ShogiUI.Sockets/Properties/launchSettings.json
Normal file
@@ -0,0 +1,29 @@
|
||||
{
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:63676",
|
||||
"sslPort": 44396
|
||||
}
|
||||
},
|
||||
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||
"profiles": {
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchUrl": "weatherforecast",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"AspShogiSockets": {
|
||||
"commandName": "Project",
|
||||
"launchBrowser": false,
|
||||
"launchUrl": "Socket/Token",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
},
|
||||
"applicationUrl": "http://127.0.0.1:5101"
|
||||
}
|
||||
}
|
||||
}
|
||||
127
Gameboard.ShogiUI.Sockets/Repositories/GameboardRepository.cs
Normal file
127
Gameboard.ShogiUI.Sockets/Repositories/GameboardRepository.cs
Normal file
@@ -0,0 +1,127 @@
|
||||
using Gameboard.Shogi.Api.ServiceModels.Messages;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Websockets.Repositories.Utility;
|
||||
|
||||
namespace Websockets.Repositories
|
||||
{
|
||||
public interface IGameboardRepository
|
||||
{
|
||||
Task DeleteGame(string gameName);
|
||||
Task<GetGameResponse> GetGame(string gameName);
|
||||
Task<GetGamesResponse> GetGames();
|
||||
Task<GetGamesResponse> GetGames(string playerName);
|
||||
Task<GetMovesResponse> GetMoves(string gameName);
|
||||
Task<PostGameResponse> PostGame(PostGame request);
|
||||
Task<PostJoinByCodeResponse> PostJoinByCode(PostJoinByCode request);
|
||||
Task<PostJoinGameResponse> PostJoinGame(string gameName, PostJoinGame request);
|
||||
Task PostMove(string gameName, PostMove request);
|
||||
Task<PostJoinCodeResponse> PostJoinCode(string gameName, string userName);
|
||||
Task<GetPlayerResponse> GetPlayer(string userName);
|
||||
Task<HttpResponseMessage> PostPlayer(PostPlayer request);
|
||||
}
|
||||
|
||||
public class GameboardRepository : IGameboardRepository
|
||||
{
|
||||
private readonly IAuthenticatedHttpClient client;
|
||||
public GameboardRepository(IAuthenticatedHttpClient client)
|
||||
{
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
public async Task<GetGamesResponse> GetGames()
|
||||
{
|
||||
var response = await client.GetAsync("Games");
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
return JsonConvert.DeserializeObject<GetGamesResponse>(json);
|
||||
}
|
||||
|
||||
public async Task<GetGamesResponse> GetGames(string playerName)
|
||||
{
|
||||
var uri = $"Games/{playerName}";
|
||||
var response = await client.GetAsync(Uri.EscapeUriString(uri));
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
return JsonConvert.DeserializeObject<GetGamesResponse>(json);
|
||||
}
|
||||
|
||||
public async Task<GetGameResponse> GetGame(string gameName)
|
||||
{
|
||||
var uri = $"Game/{gameName}";
|
||||
var response = await client.GetAsync(Uri.EscapeUriString(uri));
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
return JsonConvert.DeserializeObject<GetGameResponse>(json);
|
||||
}
|
||||
|
||||
public async Task DeleteGame(string gameName)
|
||||
{
|
||||
var uri = $"Game/{gameName}";
|
||||
await client.DeleteAsync(Uri.EscapeUriString(uri));
|
||||
}
|
||||
|
||||
public async Task<PostGameResponse> PostGame(PostGame request)
|
||||
{
|
||||
var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json");
|
||||
var response = await client.PostAsync("Game", content);
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
return JsonConvert.DeserializeObject<PostGameResponse>(json);
|
||||
}
|
||||
|
||||
public async Task<PostJoinGameResponse> PostJoinGame(string gameName, PostJoinGame request)
|
||||
{
|
||||
var uri = $"Game/{gameName}/Join";
|
||||
var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json");
|
||||
var response = await client.PostAsync(Uri.EscapeUriString(uri), content);
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
return JsonConvert.DeserializeObject<PostJoinGameResponse>(json);
|
||||
}
|
||||
|
||||
public async Task<PostJoinByCodeResponse> PostJoinByCode(PostJoinByCode request)
|
||||
{
|
||||
var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json");
|
||||
var response = await client.PostAsync("Game/Join", content);
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
return JsonConvert.DeserializeObject<PostJoinByCodeResponse>(json);
|
||||
}
|
||||
|
||||
public async Task<GetMovesResponse> GetMoves(string gameName)
|
||||
{
|
||||
var uri = $"Game/{gameName}/Moves";
|
||||
var response = await client.GetAsync(Uri.EscapeUriString(uri));
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
return JsonConvert.DeserializeObject<GetMovesResponse>(json);
|
||||
}
|
||||
|
||||
public async Task PostMove(string gameName, PostMove request)
|
||||
{
|
||||
var uri = $"Game/{gameName}/Move";
|
||||
var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json");
|
||||
await client.PostAsync(Uri.EscapeUriString(uri), content);
|
||||
}
|
||||
|
||||
public async Task<PostJoinCodeResponse> PostJoinCode(string gameName, string userName)
|
||||
{
|
||||
var uri = $"JoinCode/{gameName}";
|
||||
var serialized = JsonConvert.SerializeObject(new PostJoinCode { PlayerName = userName });
|
||||
var content = new StringContent(serialized, Encoding.UTF8, "application/json");
|
||||
var json = await (await client.PostAsync(Uri.EscapeUriString(uri), content)).Content.ReadAsStringAsync();
|
||||
return JsonConvert.DeserializeObject<PostJoinCodeResponse>(json);
|
||||
}
|
||||
|
||||
public async Task<GetPlayerResponse> GetPlayer(string playerName)
|
||||
{
|
||||
var uri = $"Player/{playerName}";
|
||||
var response = await client.GetAsync(Uri.EscapeUriString(uri));
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
return JsonConvert.DeserializeObject<GetPlayerResponse>(json);
|
||||
}
|
||||
|
||||
public async Task<HttpResponseMessage> PostPlayer(PostPlayer request)
|
||||
{
|
||||
var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json");
|
||||
return await client.PostAsync("Player", content);
|
||||
}
|
||||
}
|
||||
}
|
||||
32
Gameboard.ShogiUI.Sockets/Repositories/PlayerRepository.cs
Normal file
32
Gameboard.ShogiUI.Sockets/Repositories/PlayerRepository.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using Gameboard.Shogi.Api.ServiceModels.Messages;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Websockets.Repositories.Utility;
|
||||
|
||||
namespace Websockets.Repositories
|
||||
{
|
||||
[Obsolete("Use GameboardRepository. Functions from PlayerRepository will be moved.")]
|
||||
public class PlayerRepository
|
||||
{
|
||||
private readonly IAuthenticatedHttpClient client;
|
||||
|
||||
public PlayerRepository(IAuthenticatedHttpClient client)
|
||||
{
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
public async Task<GetPlayerResponse> GetPlayer(string playerName)
|
||||
{
|
||||
var response = await client.GetAsync($"/Player/{playerName}");
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
return JsonConvert.DeserializeObject<GetPlayerResponse>(json);
|
||||
}
|
||||
|
||||
public async Task DeletePlayer(string playerName)
|
||||
{
|
||||
var response = await client.DeleteAsync($"/Player/{playerName}");
|
||||
await response.Content.ReadAsStringAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using Gameboard.Shogi.Api.ServiceModels.Messages;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Websockets.Repositories;
|
||||
|
||||
namespace AspShogiSockets.Repositories.RepositoryManagers
|
||||
{
|
||||
public interface IGameboardRepositoryManager
|
||||
{
|
||||
Task<string> CreateGuestUser();
|
||||
}
|
||||
|
||||
public class GameboardRepositoryManager : IGameboardRepositoryManager
|
||||
{
|
||||
private readonly IGameboardRepository repository;
|
||||
private const int MaxTries = 3;
|
||||
|
||||
public GameboardRepositoryManager(IGameboardRepository repository)
|
||||
{
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
public async Task<string> CreateGuestUser()
|
||||
{
|
||||
var count = 0;
|
||||
while (count < MaxTries)
|
||||
{
|
||||
count++;
|
||||
var clientId = $"Guest-{Guid.NewGuid()}";
|
||||
var request = new PostPlayer
|
||||
{
|
||||
PlayerName = clientId
|
||||
};
|
||||
var response = await repository.PostPlayer(request);
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
return clientId;
|
||||
}
|
||||
}
|
||||
throw new OperationCanceledException($"Failed to create guest user after {MaxTries} tries.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
using IdentityModel.Client;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Websockets.Repositories.Utility
|
||||
{
|
||||
public interface IAuthenticatedHttpClient
|
||||
{
|
||||
Task<HttpResponseMessage> DeleteAsync(string requestUri);
|
||||
Task<HttpResponseMessage> GetAsync(string requestUri);
|
||||
Task<HttpResponseMessage> PostAsync(string requestUri, HttpContent content);
|
||||
}
|
||||
|
||||
public class AuthenticatedHttpClient : HttpClient, IAuthenticatedHttpClient
|
||||
{
|
||||
private readonly ILogger<AuthenticatedHttpClient> logger;
|
||||
private readonly string identityServerUrl;
|
||||
private TokenResponse tokenResponse;
|
||||
private readonly string clientId;
|
||||
private readonly string clientSecret;
|
||||
|
||||
public AuthenticatedHttpClient(ILogger<AuthenticatedHttpClient> logger, IConfiguration configuration) : base()
|
||||
{
|
||||
this.logger = logger;
|
||||
identityServerUrl = configuration["AppSettings:IdentityServer"];
|
||||
clientId = configuration["AppSettings:ClientId"];
|
||||
clientSecret = configuration["AppSettings:ClientSecret"];
|
||||
BaseAddress = new Uri(configuration["AppSettings:GameboardShogiApi"]);
|
||||
}
|
||||
|
||||
private async Task RefreshBearerToken()
|
||||
{
|
||||
var disco = await this.GetDiscoveryDocumentAsync(identityServerUrl);
|
||||
if (disco.IsError)
|
||||
{
|
||||
logger.LogError("{DiscoveryErrorType}", disco.ErrorType);
|
||||
throw new Exception(disco.Error);
|
||||
}
|
||||
|
||||
var request = new ClientCredentialsTokenRequest
|
||||
{
|
||||
Address = disco.TokenEndpoint,
|
||||
ClientId = clientId,
|
||||
ClientSecret = clientSecret
|
||||
};
|
||||
var response = await this.RequestClientCredentialsTokenAsync(request);
|
||||
if (response.IsError)
|
||||
{
|
||||
throw new Exception(response.Error);
|
||||
}
|
||||
tokenResponse = response;
|
||||
logger.LogInformation("Refreshing Bearer Token to {BaseAddress}", BaseAddress);
|
||||
this.SetBearerToken(tokenResponse.AccessToken);
|
||||
}
|
||||
|
||||
public async new Task<HttpResponseMessage> GetAsync(string requestUri)
|
||||
{
|
||||
var response = await base.GetAsync(requestUri);
|
||||
if (response.StatusCode == HttpStatusCode.Unauthorized)
|
||||
{
|
||||
await RefreshBearerToken();
|
||||
response = await base.GetAsync(requestUri);
|
||||
}
|
||||
logger.LogInformation(
|
||||
"Repository GET to {BaseUrl}{RequestUrl} \nResponse: {Response}\n",
|
||||
BaseAddress,
|
||||
requestUri,
|
||||
await response.Content.ReadAsStringAsync());
|
||||
return response;
|
||||
}
|
||||
public async new Task<HttpResponseMessage> PostAsync(string requestUri, HttpContent content)
|
||||
{
|
||||
var response = await base.PostAsync(requestUri, content);
|
||||
if (response.StatusCode == HttpStatusCode.Unauthorized)
|
||||
{
|
||||
await RefreshBearerToken();
|
||||
response = await base.PostAsync(requestUri, content);
|
||||
}
|
||||
logger.LogInformation(
|
||||
"Repository POST to {BaseUrl}{RequestUrl} \nRequest: {Request}\nResponse: {Response}\n",
|
||||
BaseAddress,
|
||||
requestUri,
|
||||
await content.ReadAsStringAsync(),
|
||||
await response.Content.ReadAsStringAsync());
|
||||
return response;
|
||||
}
|
||||
public async new Task<HttpResponseMessage> DeleteAsync(string requestUri)
|
||||
{
|
||||
var response = await base.DeleteAsync(requestUri);
|
||||
if (response.StatusCode == HttpStatusCode.Unauthorized)
|
||||
{
|
||||
await RefreshBearerToken();
|
||||
response = await base.DeleteAsync(requestUri);
|
||||
}
|
||||
logger.LogInformation("Repository DELETE to {BaseUrl}{RequestUrl}", BaseAddress, requestUri);
|
||||
return response;
|
||||
}
|
||||
}
|
||||
}
|
||||
145
Gameboard.ShogiUI.Sockets/Startup.cs
Normal file
145
Gameboard.ShogiUI.Sockets/Startup.cs
Normal file
@@ -0,0 +1,145 @@
|
||||
using AspShogiSockets.Repositories.RepositoryManagers;
|
||||
using Gameboard.ShogiUI.Sockets.Extensions;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
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;
|
||||
|
||||
namespace Websockets
|
||||
{
|
||||
public class Startup
|
||||
{
|
||||
public Startup(IConfiguration configuration)
|
||||
{
|
||||
Configuration = configuration;
|
||||
}
|
||||
|
||||
public IConfiguration Configuration { get; }
|
||||
|
||||
// This method gets called by the runtime. Use this method to add services to the container.
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
// Socket ActionHandlers
|
||||
services.AddSingleton<CreateGameHandler>();
|
||||
services.AddSingleton<JoinByCodeHandler>();
|
||||
services.AddSingleton<JoinGameHandler>();
|
||||
services.AddSingleton<ListGamesHandler>();
|
||||
services.AddSingleton<LoadGameHandler>();
|
||||
services.AddSingleton<MoveHandler>();
|
||||
|
||||
// Managers
|
||||
services.AddSingleton<ISocketCommunicationManager, SocketCommunicationManager>();
|
||||
services.AddSingleton<ISocketTokenManager, SocketTokenManager>();
|
||||
services.AddSingleton<ISocketConnectionManager, SocketConnectionManager>();
|
||||
services.AddScoped<IGameboardRepositoryManager, GameboardRepositoryManager>();
|
||||
services.AddSingleton<ActionHandlerResolver>(sp => action =>
|
||||
{
|
||||
return action switch
|
||||
{
|
||||
ClientAction.ListGames => sp.GetService<ListGamesHandler>(),
|
||||
ClientAction.CreateGame => sp.GetService<CreateGameHandler>(),
|
||||
ClientAction.JoinGame => sp.GetService<JoinGameHandler>(),
|
||||
ClientAction.JoinByCode => sp.GetService<JoinByCodeHandler>(),
|
||||
ClientAction.LoadGame => sp.GetService<LoadGameHandler>(),
|
||||
ClientAction.Move => sp.GetService<MoveHandler>(),
|
||||
_ => throw new KeyNotFoundException($"Unable to resolve {nameof(IActionHandler)} for {nameof(ClientAction)} {action}"),
|
||||
};
|
||||
});
|
||||
|
||||
// Repositories
|
||||
services.AddTransient<IGameboardRepository, GameboardRepository>();
|
||||
services.AddSingleton<IAuthenticatedHttpClient, AuthenticatedHttpClient>();
|
||||
|
||||
services.AddControllers();
|
||||
|
||||
services
|
||||
.AddAuthentication(options =>
|
||||
{
|
||||
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
})
|
||||
.AddJwtBearer(options =>
|
||||
{
|
||||
options.Authority = "https://login.microsoftonline.com/9188040d-6c67-4c5b-b112-36a304b66dad/v2.0";
|
||||
options.Audience = "935df672-efa6-45fa-b2e8-b76dfd65a122";
|
||||
options.TokenValidationParameters.ValidateIssuer = true;
|
||||
options.TokenValidationParameters.ValidateAudience = true;
|
||||
});
|
||||
}
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ISocketConnectionManager socketConnectionManager)
|
||||
{
|
||||
var origins = new[] { "https://localhost:3000", "https://dev.lucaserver.space", "https://lucaserver.space" };
|
||||
var socketOptions = new WebSocketOptions();
|
||||
foreach (var o in origins)
|
||||
socketOptions.AllowedOrigins.Add(o);
|
||||
|
||||
if (env.IsDevelopment())
|
||||
{
|
||||
app.UseDeveloperExceptionPage();
|
||||
}
|
||||
else
|
||||
{
|
||||
app.UseHsts();
|
||||
}
|
||||
app
|
||||
.UseRequestResponseLogging()
|
||||
.UseCors(
|
||||
opt => opt
|
||||
.WithOrigins(origins)
|
||||
.AllowAnyMethod()
|
||||
.AllowAnyHeader()
|
||||
.AllowCredentials()
|
||||
)
|
||||
.UseRouting()
|
||||
.UseAuthentication()
|
||||
.UseAuthorization()
|
||||
.UseWebSockets(socketOptions)
|
||||
.UseEndpoints(endpoints =>
|
||||
{
|
||||
endpoints.MapControllers();
|
||||
})
|
||||
.Use(async (context, next) =>
|
||||
{
|
||||
var isUpgradeHeader = context
|
||||
.Request
|
||||
.Headers
|
||||
.Any(h => h.Key.Contains("upgrade", StringComparison.InvariantCultureIgnoreCase)
|
||||
&& h.Value.ToString().Contains("websocket", StringComparison.InvariantCultureIgnoreCase));
|
||||
if (isUpgradeHeader)
|
||||
{
|
||||
await socketConnectionManager.HandleSocketRequest(context);
|
||||
}
|
||||
else
|
||||
{
|
||||
await next();
|
||||
}
|
||||
});
|
||||
|
||||
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
|
||||
{
|
||||
Formatting = Formatting.Indented,
|
||||
ContractResolver = new DefaultContractResolver
|
||||
{
|
||||
NamingStrategy = new CamelCaseNamingStrategy(),
|
||||
},
|
||||
Converters = new[] { new StringEnumConverter() },
|
||||
NullValueHandling = NullValueHandling.Ignore
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
9
Gameboard.ShogiUI.Sockets/appsettings.Development.json
Normal file
9
Gameboard.ShogiUI.Sockets/appsettings.Development.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft": "Warning",
|
||||
"Microsoft.Hosting.Lifetime": "Information"
|
||||
}
|
||||
}
|
||||
}
|
||||
17
Gameboard.ShogiUI.Sockets/appsettings.json
Normal file
17
Gameboard.ShogiUI.Sockets/appsettings.json
Normal file
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"AppSettings": {
|
||||
"IdentityServer": "https://identity.lucaserver.space/",
|
||||
"GameboardShogiApi": "https://api.lucaserver.space/Gameboard.Shogi.Api/",
|
||||
"ClientId": "DevClientId",
|
||||
"ClientSecret": "DevSecret",
|
||||
"Scope": "DevEnvironment"
|
||||
},
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft": "Warning",
|
||||
"Microsoft.Hosting.Lifetime": "Information"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
64
azure-pipelines.yml
Normal file
64
azure-pipelines.yml
Normal file
@@ -0,0 +1,64 @@
|
||||
# ASP.NET Core
|
||||
# Build and test ASP.NET Core projects targeting .NET Core.
|
||||
# Add steps that run tests, create a NuGet package, deploy, and more:
|
||||
# https://docs.microsoft.com/azure/devops/pipelines/languages/dotnet-core
|
||||
|
||||
trigger:
|
||||
- master
|
||||
|
||||
pr:
|
||||
- none
|
||||
|
||||
pool:
|
||||
vmImage: 'windows-latest'
|
||||
|
||||
variables:
|
||||
solution: '**/*.sln'
|
||||
buildPlatform: 'Any CPU'
|
||||
buildConfiguration: 'Release'
|
||||
projectName: 'Gameboard.ShogiUI.Sockets'
|
||||
|
||||
steps:
|
||||
|
||||
- task: NuGetToolInstaller@1
|
||||
|
||||
- task: NuGetCommand@2
|
||||
inputs:
|
||||
command: 'restore'
|
||||
restoreSolution: '**/*.sln'
|
||||
feedsToUse: 'config'
|
||||
|
||||
- task: DotNetCoreCLI@2
|
||||
displayName: Publish
|
||||
env :
|
||||
DOTNET_CLI_TELEMETRY_OPTOUT: 1
|
||||
inputs:
|
||||
command: 'publish'
|
||||
publishWebProjects: false
|
||||
projects: '**/*.csproj '
|
||||
zipAfterPublish: false
|
||||
|
||||
- task: FileTransform@1
|
||||
inputs:
|
||||
folderPath: '$(System.DefaultWorkingDirectory)'
|
||||
fileType: 'json'
|
||||
targetFiles: '**/appsettings.json'
|
||||
|
||||
- task: CopyFilesOverSSH@0
|
||||
displayName: SSH Copy to 1UB
|
||||
inputs:
|
||||
sshEndpoint: 'LucaServer'
|
||||
sourceFolder: '$(System.DefaultWorkingDirectory)\$(projectName)\bin\Debug\netcoreapp3.1\publish'
|
||||
targetFolder: '/var/www/api/production/$(projectName)'
|
||||
contents: '**'
|
||||
failOnEmptySource: true
|
||||
cleanTargetFolder: true
|
||||
|
||||
- task: SSH@0
|
||||
displayName: Restart Kestrel
|
||||
inputs:
|
||||
sshEndpoint: 'LucaServer'
|
||||
runOptions: 'commands'
|
||||
commands: 'sudo systemctl restart kestrel-gameboard.shogiui.sockets.service'
|
||||
readyTimeout: '20000'
|
||||
|
||||
7
nuget.config
Normal file
7
nuget.config
Normal file
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<configuration>
|
||||
<packageSources>
|
||||
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
|
||||
<add key="Gameboard.Shogi.Api" value="https://pkgs.dev.azure.com/hauth/GameBoard/_packaging/Gameboard.Shogi.Api/nuget/v3/index.json" />
|
||||
</packageSources>
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user