Merged in development (pull request #43)

Development
This commit is contained in:
2021-01-26 23:25:09 +00:00
38 changed files with 268 additions and 208 deletions

View File

@@ -1,6 +1,6 @@
using System; using System;
namespace AspShogiSockets.ServiceModels.Api.Messages namespace Gameboard.ShogiUI.Sockets.ServiceModels.Api.Messages
{ {
public class GetGuestToken public class GetGuestToken
{ {

View File

@@ -1,6 +1,6 @@
using System; using System;
namespace Websockets.ServiceModels namespace Gameboard.ShogiUI.Sockets.ServiceModels.Api.Messages
{ {
public class GetTokenResponse public class GetTokenResponse
{ {

View File

@@ -1,9 +1,14 @@
namespace AspShogiSockets.ServiceModels.Api.Messages namespace Gameboard.ShogiUI.Sockets.ServiceModels.Api.Messages
{ {
public class PostGameInvitation public class PostGameInvitation
{ {
public string SessionName { get; set; } public string SessionName { get; set; }
} }
public class PostGuestGameInvitation
{
public string GuestId { get; set; }
public string SessionName { get; set; }
}
public class PostGameInvitationResponse public class PostGameInvitationResponse
{ {
public string Code { get; } public string Code { get; }

View File

@@ -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 public interface IRequest
{ {

View File

@@ -1,4 +1,4 @@
namespace Websockets.ServiceModels.Interfaces namespace Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Interfaces
{ {
public interface IResponse public interface IResponse
{ {

View File

@@ -1,7 +1,7 @@
using Websockets.ServiceModels.Interfaces; using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Interfaces;
using Websockets.ServiceModels.Types; using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
namespace Websockets.ServiceModels.Messages namespace Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages
{ {
public class CreateGameRequest : IRequest public class CreateGameRequest : IRequest
{ {

View File

@@ -1,16 +1,16 @@
using Websockets.ServiceModels.Interfaces; using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Interfaces;
using Websockets.ServiceModels.Types; using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
namespace Websockets.ServiceModels.Messages namespace Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages
{ {
public class ErrorResponse : IResponse public class ErrorResponse : IResponse
{ {
public string Action { get; private set; } public string Action { get; private set; }
public string Error { get; set; } public string Error { get; set; }
public ErrorResponse(ClientAction action) public ErrorResponse(ClientAction action)
{ {
Action = action.ToString(); Action = action.ToString();
}
} }
}
} }

View File

@@ -1,7 +1,7 @@
using Websockets.ServiceModels.Interfaces; using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Interfaces;
using Websockets.ServiceModels.Types; using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
namespace Websockets.ServiceModels.Messages namespace Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages
{ {
public class JoinByCode : IRequest public class JoinByCode : IRequest
{ {

View File

@@ -1,7 +1,7 @@
using Websockets.ServiceModels.Interfaces; using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Interfaces;
using Websockets.ServiceModels.Types; using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
namespace Websockets.ServiceModels.Messages namespace Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages
{ {
public class JoinGameRequest : IRequest public class JoinGameRequest : IRequest
{ {

View File

@@ -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; namespace Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages
using Websockets.ServiceModels.Types;
namespace Websockets.ServiceModels.Messages
{ {
public class ListGamesRequest : IRequest public class ListGamesRequest : IRequest
{ {
public ClientAction Action { get; set; } public ClientAction Action { get; set; }
} }
public class ListGamesResponse : IResponse public class ListGamesResponse : IResponse
{ {
public string Action { get; private set; } public string Action { get; private set; }
public string Error { get; set; } public string Error { get; set; }
public IEnumerable<Game> Games { get; set; } public IEnumerable<Game> Games { get; set; }
public ListGamesResponse(ClientAction action) public ListGamesResponse(ClientAction action)
{ {
Action = action.ToString(); Action = action.ToString();
}
} }
}
} }

View File

@@ -1,8 +1,8 @@
using System.Collections.Generic; using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Interfaces;
using Websockets.ServiceModels.Interfaces; using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
using Websockets.ServiceModels.Types; using System.Collections.Generic;
namespace Websockets.ServiceModels.Messages namespace Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages
{ {
public class LoadGameRequest : IRequest public class LoadGameRequest : IRequest
{ {

View File

@@ -1,7 +1,7 @@
using Websockets.ServiceModels.Interfaces; using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Interfaces;
using Websockets.ServiceModels.Types; using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
namespace Websockets.ServiceModels.Messages namespace Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages
{ {
public class MoveRequest : IRequest public class MoveRequest : IRequest
{ {

View File

@@ -1,4 +1,4 @@
namespace Websockets.ServiceModels.Types namespace Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types
{ {
public enum ClientAction public enum ClientAction
{ {

View File

@@ -1,4 +1,4 @@
namespace Websockets.ServiceModels.Types namespace Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types
{ {
public class Coords public class Coords
{ {

View File

@@ -1,4 +1,4 @@
namespace Websockets.ServiceModels.Types namespace Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types
{ {
public class Game public class Game
{ {

View File

@@ -1,4 +1,4 @@
namespace Websockets.ServiceModels.Types namespace Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types
{ {
public class Move public class Move
{ {

View File

@@ -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.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Websockets.Repositories;
namespace AspShogiSockets.Controllers namespace Gameboard.ShogiUI.Sockets.Controllers
{ {
[Authorize] [Authorize]
[ApiController] [ApiController]
[Route("[controller]")] [Route("[controller]")]
public class GameController : ControllerBase 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")] [Route("JoinCode")]
public async Task<IActionResult> PostGameInvitation([FromBody] PostGameInvitation request) public async Task<IActionResult> PostGameInvitation([FromBody] PostGameInvitation request)
{ {
var userName = HttpContext.User.Claims.First(c => c.Type == "preferred_username").Value; var userName = HttpContext.User.Claims.First(c => c.Type == "preferred_username").Value;
var code = (await gameboardRepository.PostJoinCode(request.SessionName, userName)).JoinCode; var isPlayer1 = await manager.IsPlayer1(request.SessionName, userName);
return new CreatedResult("", new PostGameInvitationResponse(code)); 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<IActionResult> 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();
}
} }
} }
} }

View File

@@ -1,15 +1,13 @@
using AspShogiSockets.Repositories.RepositoryManagers; using Gameboard.ShogiUI.Sockets.Managers;
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.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Websockets.Managers;
using Websockets.Repositories;
using Websockets.ServiceModels;
namespace Websockets.Controllers namespace Gameboard.ShogiUI.Sockets.Controllers
{ {
[Authorize] [Authorize]
[Route("[controller]")] [Route("[controller]")]

View File

@@ -4,7 +4,7 @@ using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace AspShogiSockets.Extensions namespace Gameboard.ShogiUI.Sockets.Extensions
{ {
public static class WebSocketExtensions public static class WebSocketExtensions
{ {

View File

@@ -4,6 +4,7 @@
<Controller_SelectedScaffolderID>ApiControllerEmptyScaffolder</Controller_SelectedScaffolderID> <Controller_SelectedScaffolderID>ApiControllerEmptyScaffolder</Controller_SelectedScaffolderID>
<Controller_SelectedScaffolderCategoryPath>root/Controller</Controller_SelectedScaffolderCategoryPath> <Controller_SelectedScaffolderCategoryPath>root/Controller</Controller_SelectedScaffolderCategoryPath>
<ActiveDebugProfile>AspShogiSockets</ActiveDebugProfile> <ActiveDebugProfile>AspShogiSockets</ActiveDebugProfile>
<ShowAllFiles>false</ShowAllFiles>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor> <DebuggerFlavor>ProjectDebugger</DebuggerFlavor>

View File

@@ -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 Microsoft.Extensions.Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
using System.Net.WebSockets; using System.Net.WebSockets;
using System.Threading.Tasks; 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 public class CreateGameHandler : IActionHandler
{ {

View File

@@ -1,8 +1,8 @@
using System.Net.WebSockets; using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
using System.Net.WebSockets;
using System.Threading.Tasks; using System.Threading.Tasks;
using Websockets.ServiceModels.Types;
namespace Websockets.Managers.ClientActionHandlers namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
{ {
public interface IActionHandler public interface IActionHandler
{ {

View File

@@ -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 Microsoft.Extensions.Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
using System.Net.WebSockets; using System.Net.WebSockets;
using System.Threading.Tasks; 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 public class JoinByCodeHandler : IActionHandler
{ {

View File

@@ -1,13 +1,13 @@
using Gameboard.Shogi.Api.ServiceModels.Messages; 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 Microsoft.Extensions.Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
using System.Net.WebSockets; using System.Net.WebSockets;
using System.Threading.Tasks; 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 public class JoinGameHandler : IActionHandler
{ {

View File

@@ -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 Microsoft.Extensions.Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
using System; using System;
using System.Linq; using System.Linq;
using System.Net.WebSockets; using System.Net.WebSockets;
using System.Threading.Tasks; 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 public class ListGamesHandler : IActionHandler
{ {

View File

@@ -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 Microsoft.Extensions.Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
using System.Linq; using System.Linq;
using System.Net.WebSockets; using System.Net.WebSockets;
using System.Threading.Tasks; 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 public class LoadGameHandler : IActionHandler
{ {

View File

@@ -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 Microsoft.Extensions.Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
using System.Net.WebSockets; using System.Net.WebSockets;
using System.Threading.Tasks; 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 public class MoveHandler : IActionHandler
{ {

View File

@@ -1,5 +1,7 @@
using AspShogiSockets.Extensions; using Gameboard.ShogiUI.Sockets.Extensions;
using AspShogiSockets.Managers.Utility; using Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers;
using Gameboard.ShogiUI.Sockets.Managers.Utility;
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
using System; using System;
@@ -8,10 +10,8 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net.WebSockets; using System.Net.WebSockets;
using System.Threading.Tasks; using System.Threading.Tasks;
using Websockets.Managers.ClientActionHandlers;
using Websockets.ServiceModels.Types;
namespace Websockets.Managers namespace Gameboard.ShogiUI.Sockets.Managers
{ {
public interface ISocketCommunicationManager public interface ISocketCommunicationManager
{ {
@@ -68,6 +68,12 @@ namespace Websockets.Managers
{ {
logger.LogError(ex.Message); 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); UnsubscribeFromBroadcastAndGames(userName);
} }

View File

@@ -3,7 +3,7 @@ using System;
using System.Net; using System.Net;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Websockets.Managers namespace Gameboard.ShogiUI.Sockets.Managers
{ {
public interface ISocketConnectionManager public interface ISocketConnectionManager
{ {

View File

@@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Websockets.Managers namespace Gameboard.ShogiUI.Sockets.Managers
{ {
public interface ISocketTokenManager public interface ISocketTokenManager
{ {

View File

@@ -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; } Request = request;
public string Json { get; private set; } Json = json;
public JsonRequest(IRequest request, string json)
{
Request = request;
Json = json;
}
} }
}
} }

View File

@@ -1,67 +1,67 @@
using Microsoft.FSharp.Core; using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
using Websockets.ServiceModels.Types; using Microsoft.FSharp.Core;
using GameboardTypes = Gameboard.Shogi.Api.ServiceModels.Types; 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;
var from = source.From; FSharpOption<GameboardTypes.PieceName> pieceFromCaptured = source.PieceFromCaptured switch
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),
"B" => new FSharpOption<GameboardTypes.PieceName>(GameboardTypes.PieceName.Bishop), "K" => new FSharpOption<GameboardTypes.PieceName>(GameboardTypes.PieceName.King),
"G" => new FSharpOption<GameboardTypes.PieceName>(GameboardTypes.PieceName.GoldenGeneral), "k" => new FSharpOption<GameboardTypes.PieceName>(GameboardTypes.PieceName.Knight),
"K" => new FSharpOption<GameboardTypes.PieceName>(GameboardTypes.PieceName.King), "L" => new FSharpOption<GameboardTypes.PieceName>(GameboardTypes.PieceName.Lance),
"k" => new FSharpOption<GameboardTypes.PieceName>(GameboardTypes.PieceName.Knight), "P" => new FSharpOption<GameboardTypes.PieceName>(GameboardTypes.PieceName.Pawn),
"L" => new FSharpOption<GameboardTypes.PieceName>(GameboardTypes.PieceName.Lance), "R" => new FSharpOption<GameboardTypes.PieceName>(GameboardTypes.PieceName.Rook),
"P" => new FSharpOption<GameboardTypes.PieceName>(GameboardTypes.PieceName.Pawn), "S" => new FSharpOption<GameboardTypes.PieceName>(GameboardTypes.PieceName.SilverGeneral),
"R" => new FSharpOption<GameboardTypes.PieceName>(GameboardTypes.PieceName.Rook), _ => null
"S" => new FSharpOption<GameboardTypes.PieceName>(GameboardTypes.PieceName.SilverGeneral), };
_ => null var target = new GameboardTypes.Move
}; {
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 },
Origin = new GameboardTypes.BoardLocation { X = from.X, Y = from.Y }, IsPromotion = source.IsPromotion,
Destination = new GameboardTypes.BoardLocation { X = to.X, Y = to.Y }, PieceFromCaptured = pieceFromCaptured
IsPromotion = source.IsPromotion, };
PieceFromCaptured = pieceFromCaptured return target;
};
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;
}
} }
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;
}
}
} }

View File

@@ -1,11 +1,11 @@
using Websockets.ServiceModels.Interfaces; using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Interfaces;
using Websockets.ServiceModels.Types; using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
namespace AspShogiSockets.Managers.Utility namespace Gameboard.ShogiUI.Sockets.Managers.Utility
{ {
public class Request : IRequest public class Request : IRequest
{ {
public ClientAction Action { get; set; } public ClientAction Action { get; set; }
public string PlayerName { get; set; } public string PlayerName { get; set; }
} }
} }

View File

@@ -1,7 +1,7 @@
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
namespace Websockets namespace Gameboard.ShogiUI.Sockets
{ {
public class Program public class Program
{ {

View File

@@ -4,9 +4,9 @@ using System;
using System.Net.Http; using System.Net.Http;
using System.Text; using System.Text;
using System.Threading.Tasks; 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 public interface IGameboardRepository
{ {

View File

@@ -1,19 +1,21 @@
using Gameboard.Shogi.Api.ServiceModels.Messages; using Gameboard.Shogi.Api.ServiceModels.Messages;
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Websockets.Repositories;
namespace AspShogiSockets.Repositories.RepositoryManagers namespace Gameboard.ShogiUI.Sockets.Repositories.RepositoryManagers
{ {
public interface IGameboardRepositoryManager public interface IGameboardRepositoryManager
{ {
Task<string> CreateGuestUser(); Task<string> CreateGuestUser();
Task<bool> IsPlayer1(string sessionName, string playerName);
bool IsGuest(string playerName);
} }
public class GameboardRepositoryManager : IGameboardRepositoryManager public class GameboardRepositoryManager : IGameboardRepositoryManager
{ {
private readonly IGameboardRepository repository;
private const int MaxTries = 3; private const int MaxTries = 3;
private const string GuestPrefix = "Guest-";
private readonly IGameboardRepository repository;
public GameboardRepositoryManager(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."); throw new OperationCanceledException($"Failed to create guest user after {MaxTries} tries.");
} }
public async Task<bool> IsPlayer1(string sessionName, string playerName)
{
var session = await repository.GetGame(sessionName);
return session?.Session.Player1 == playerName;
}
public async Task<string> 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);
} }
} }

View File

@@ -6,7 +6,7 @@ using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Websockets.Repositories.Utility namespace Gameboard.ShogiUI.Sockets.Repositories.Utility
{ {
public interface IAuthenticatedHttpClient public interface IAuthenticatedHttpClient
{ {

View File

@@ -1,4 +1,4 @@
using AspShogiSockets.Repositories.RepositoryManagers; using Gameboard.ShogiUI.Sockets.Repositories.RepositoryManagers;
using Gameboard.ShogiUI.Sockets.Extensions; using Gameboard.ShogiUI.Sockets.Extensions;
using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
@@ -12,13 +12,13 @@ using Newtonsoft.Json.Serialization;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Websockets.Managers; using Gameboard.ShogiUI.Sockets.Managers;
using Websockets.Managers.ClientActionHandlers; using Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers;
using Websockets.Repositories; using Gameboard.ShogiUI.Sockets.Repositories;
using Websockets.Repositories.Utility; using Gameboard.ShogiUI.Sockets.Repositories.Utility;
using Websockets.ServiceModels.Types; using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
namespace Websockets namespace Gameboard.ShogiUI.Sockets
{ {
public class Startup public class Startup
{ {