checkpoint
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Gameboard.ShogiUI.BoardState\Gameboard.ShogiUI.BoardState.csproj" />
|
||||
<ProjectReference Include="..\Gameboard.ShogiUI.Sockets.ServiceModels\Gameboard.ShogiUI.Sockets.ServiceModels.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
<?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>
|
||||
<ShowAllFiles>false</ShowAllFiles>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
@@ -1,6 +1,35 @@
|
||||
namespace Gameboard.ShogiUI.Sockets.Managers
|
||||
using Gameboard.ShogiUI.BoardState;
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Managers
|
||||
{
|
||||
public class BoardManager
|
||||
public interface IBoardManager
|
||||
{
|
||||
void Add(string sessionName, ShogiBoard board);
|
||||
ShogiBoard Get(string sessionName);
|
||||
}
|
||||
|
||||
public class BoardManager : IBoardManager
|
||||
{
|
||||
private readonly ConcurrentDictionary<string, ShogiBoard> Boards;
|
||||
|
||||
public BoardManager()
|
||||
{
|
||||
Boards = new ConcurrentDictionary<string, ShogiBoard>();
|
||||
}
|
||||
|
||||
public void Add(string sessionName, ShogiBoard board) => Boards.TryAdd(sessionName, board);
|
||||
|
||||
public ShogiBoard Get(string sessionName)
|
||||
{
|
||||
if (Boards.TryGetValue(sessionName, out var board))
|
||||
return board;
|
||||
return null;
|
||||
}
|
||||
|
||||
public string GetBoardState()
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
using Gameboard.Shogi.Api.ServiceModels.Messages;
|
||||
using Gameboard.ShogiUI.Sockets.Extensions;
|
||||
using Gameboard.ShogiUI.Sockets.Repositories;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using System.Net.WebSockets;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
{
|
||||
// TODO: This doesn't need to be a socket action.
|
||||
// It can be an API route and still tell socket connections about the new session.
|
||||
public class CreateGameHandler : IActionHandler
|
||||
{
|
||||
private readonly ILogger<CreateGameHandler> logger;
|
||||
@@ -26,13 +26,13 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
this.communicationManager = communicationManager;
|
||||
}
|
||||
|
||||
public async Task Handle(WebSocket socket, string json, string userName)
|
||||
public async Task Handle(string json, string userName)
|
||||
{
|
||||
var request = JsonConvert.DeserializeObject<CreateGameRequest>(json);
|
||||
var postSessionResponse = await repository.PostSession(new PostSession
|
||||
{
|
||||
SessionName = request.GameName,
|
||||
PlayerName = userName, // TODO : Investigate if needed by UI
|
||||
PlayerName = userName,
|
||||
IsPrivate = request.IsPrivate
|
||||
});
|
||||
|
||||
@@ -53,9 +53,7 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
|
||||
if (request.IsPrivate)
|
||||
{
|
||||
var serialized = JsonConvert.SerializeObject(response);
|
||||
logger.LogInformation("Response to {0} \n{1}\n", userName, serialized);
|
||||
await socket.SendTextAsync(serialized);
|
||||
await communicationManager.BroadcastToPlayers(response, userName);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
/// <summary>
|
||||
/// Responsible for parsing json and handling the request.
|
||||
/// </summary>
|
||||
Task Handle(WebSocket socket, string json, string userName);
|
||||
Task Handle(string json, string userName);
|
||||
}
|
||||
|
||||
public delegate IActionHandler ActionHandlerResolver(ClientAction action);
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
using Gameboard.Shogi.Api.ServiceModels.Messages;
|
||||
using Gameboard.ShogiUI.Sockets.Extensions;
|
||||
using Gameboard.ShogiUI.Sockets.Repositories;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using System.Net.WebSockets;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
@@ -26,7 +24,7 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
this.communicationManager = communicationManager;
|
||||
}
|
||||
|
||||
public async Task Handle(WebSocket socket, string json, string userName)
|
||||
public async Task Handle(string json, string userName)
|
||||
{
|
||||
var request = JsonConvert.DeserializeObject<JoinByCode>(json);
|
||||
var joinGameResponse = await repository.PostJoinPrivateSession(new PostJoinPrivateSession
|
||||
@@ -37,37 +35,32 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
|
||||
if (joinGameResponse.JoinSucceeded)
|
||||
{
|
||||
var gameName = (await repository.GetGame(joinGameResponse.SessionName)).Session.Name;
|
||||
|
||||
// Other members of the game see a regular JoinGame occur.
|
||||
var response = new JoinGameResponse(ClientAction.JoinGame)
|
||||
{
|
||||
PlayerName = userName,
|
||||
GameName = gameName
|
||||
GameName = joinGameResponse.SessionName
|
||||
};
|
||||
// At this time, userName hasn't subscribed and won't receive this broadcasted messages.
|
||||
await communicationManager.BroadcastToGame(gameName, response);
|
||||
// At this time, userName hasn't subscribed and won't receive this message.
|
||||
await communicationManager.BroadcastToGame(joinGameResponse.SessionName, response);
|
||||
|
||||
// But the player joining sees the JoinByCode occur.
|
||||
// The player joining sees the JoinByCode occur.
|
||||
response = new JoinGameResponse(ClientAction.JoinByCode)
|
||||
{
|
||||
PlayerName = userName,
|
||||
GameName = gameName
|
||||
GameName = joinGameResponse.SessionName
|
||||
};
|
||||
var serialized = JsonConvert.SerializeObject(response);
|
||||
logger.LogInformation("Response to {0} \n{1}\n", userName, serialized);
|
||||
await socket.SendTextAsync(serialized);
|
||||
await communicationManager.BroadcastToPlayers(response, userName);
|
||||
}
|
||||
else
|
||||
{
|
||||
var response = new JoinGameResponse(ClientAction.JoinByCode)
|
||||
{
|
||||
PlayerName = userName,
|
||||
GameName = joinGameResponse.SessionName,
|
||||
Error = "Error joining game."
|
||||
};
|
||||
var serialized = JsonConvert.SerializeObject(response);
|
||||
logger.LogInformation("Response to {0} \n{1}\n", userName, serialized);
|
||||
await socket.SendTextAsync(serialized);
|
||||
await communicationManager.BroadcastToPlayers(response, userName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ using Gameboard.ShogiUI.Sockets.Repositories;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
|
||||
using Newtonsoft.Json;
|
||||
using System.Net.WebSockets;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
@@ -20,13 +19,9 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
this.communicationManager = communicationManager;
|
||||
}
|
||||
|
||||
public async Task Handle(WebSocket socket, string json, string userName)
|
||||
public async Task Handle(string json, string userName)
|
||||
{
|
||||
var request = JsonConvert.DeserializeObject<JoinGameRequest>(json);
|
||||
var response = new JoinGameResponse(ClientAction.JoinGame)
|
||||
{
|
||||
PlayerName = userName
|
||||
};
|
||||
|
||||
var joinGameResponse = await gameboardRepository.PutJoinPublicSession(new PutJoinPublicSession
|
||||
{
|
||||
@@ -34,15 +29,20 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
SessionName = request.GameName
|
||||
});
|
||||
|
||||
var response = new JoinGameResponse(ClientAction.JoinGame)
|
||||
{
|
||||
PlayerName = userName,
|
||||
GameName = request.GameName
|
||||
};
|
||||
if (joinGameResponse.JoinSucceeded)
|
||||
{
|
||||
response.GameName = request.GameName;
|
||||
await communicationManager.BroadcastToAll(response);
|
||||
}
|
||||
else
|
||||
{
|
||||
response.Error = "Game is full.";
|
||||
await communicationManager.BroadcastToPlayers(response, userName);
|
||||
}
|
||||
await communicationManager.BroadcastToAll(response);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,26 +1,29 @@
|
||||
using Gameboard.ShogiUI.Sockets.Extensions;
|
||||
using Gameboard.ShogiUI.Sockets.Models;
|
||||
using Gameboard.ShogiUI.Sockets.Models;
|
||||
using Gameboard.ShogiUI.Sockets.Repositories;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
|
||||
using Newtonsoft.Json;
|
||||
using System.Linq;
|
||||
using System.Net.WebSockets;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
{
|
||||
// TODO: This doesn't need to be a socket action.
|
||||
// It can be an HTTP route.
|
||||
public class ListGamesHandler : IActionHandler
|
||||
{
|
||||
private readonly ISocketCommunicationManager communicationManager;
|
||||
private readonly IGameboardRepository repository;
|
||||
|
||||
public ListGamesHandler(
|
||||
ISocketCommunicationManager communicationManager,
|
||||
IGameboardRepository repository)
|
||||
{
|
||||
this.communicationManager = communicationManager;
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
public async Task Handle(WebSocket socket, string json, string userName)
|
||||
public async Task Handle(string json, string userName)
|
||||
{
|
||||
var request = JsonConvert.DeserializeObject<ListGamesRequest>(json);
|
||||
var getGamesResponse = string.IsNullOrWhiteSpace(userName)
|
||||
@@ -33,11 +36,10 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
|
||||
var response = new ListGamesResponse(ClientAction.ListGames)
|
||||
{
|
||||
Games = games
|
||||
Games = games.ToList()
|
||||
};
|
||||
|
||||
var serialized = JsonConvert.SerializeObject(response);
|
||||
await socket.SendTextAsync(serialized);
|
||||
await communicationManager.BroadcastToPlayers(response, userName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,56 +1,66 @@
|
||||
using Gameboard.ShogiUI.Sockets.Extensions;
|
||||
using Gameboard.ShogiUI.Sockets.Managers.Utility;
|
||||
using Gameboard.ShogiUI.Sockets.Models;
|
||||
using Gameboard.ShogiUI.BoardState;
|
||||
using Gameboard.ShogiUI.Sockets.Repositories;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using System.Linq;
|
||||
using System.Net.WebSockets;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
{
|
||||
/// <summary>
|
||||
/// Subscribes a user to messages for a session and loads that session into the BoardManager for playing.
|
||||
/// </summary>
|
||||
public class LoadGameHandler : IActionHandler
|
||||
{
|
||||
private readonly ILogger<LoadGameHandler> logger;
|
||||
private readonly IGameboardRepository gameboardRepository;
|
||||
private readonly ISocketCommunicationManager communicationManager;
|
||||
private readonly IBoardManager boardManager;
|
||||
|
||||
public LoadGameHandler(
|
||||
ILogger<LoadGameHandler> logger,
|
||||
ISocketCommunicationManager communicationManager,
|
||||
IGameboardRepository gameboardRepository)
|
||||
IGameboardRepository gameboardRepository,
|
||||
IBoardManager boardManager)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.gameboardRepository = gameboardRepository;
|
||||
this.communicationManager = communicationManager;
|
||||
this.boardManager = boardManager;
|
||||
}
|
||||
|
||||
public async Task Handle(WebSocket socket, string json, string userName)
|
||||
public async Task Handle(string json, string userName)
|
||||
{
|
||||
var request = JsonConvert.DeserializeObject<LoadGameRequest>(json);
|
||||
var getGameResponse = await gameboardRepository.GetGame(request.GameName);
|
||||
var getMovesResponse = await gameboardRepository.GetMoves(request.GameName);
|
||||
var gameTask = gameboardRepository.GetGame(request.GameName);
|
||||
var moveTask = gameboardRepository.GetMoves(request.GameName);
|
||||
|
||||
var response = new LoadGameResponse(ClientAction.LoadGame);
|
||||
var getGameResponse = await gameTask;
|
||||
var getMovesResponse = await moveTask;
|
||||
if (getGameResponse == null || getMovesResponse == null)
|
||||
{
|
||||
response.Error = $"Could not find game.";
|
||||
logger.LogWarning("{action} - {user} was unable to load session named {session}.", ClientAction.LoadGame, userName, request.GameName);
|
||||
var response = new LoadGameResponse(ClientAction.LoadGame) { Error = "Game not found." };
|
||||
await communicationManager.BroadcastToPlayers(response, userName);
|
||||
}
|
||||
else
|
||||
{
|
||||
var sessionModel = new Session(getGameResponse.Session);
|
||||
communicationManager.SubscribeToGame(socket, sessionModel, userName);
|
||||
var sessionModel = new Models.Session(getGameResponse.Session);
|
||||
var moveModels = getMovesResponse.Moves.Select(_ => new Models.Move(_)).ToList();
|
||||
|
||||
response.Game = sessionModel.ToServiceModel();
|
||||
response.Moves = getMovesResponse.Moves.Select(_ => Mapper.Map(_).ToServiceModel());
|
||||
communicationManager.SubscribeToGame(sessionModel, userName);
|
||||
var boardMoves = moveModels.Select(_ => _.ToBoardModel()).ToList();
|
||||
boardManager.Add(getGameResponse.Session.Name, new ShogiBoard(boardMoves));
|
||||
|
||||
var response = new LoadGameResponse(ClientAction.LoadGame)
|
||||
{
|
||||
Game = sessionModel.ToServiceModel(),
|
||||
Moves = moveModels.Select(_ => _.ToServiceModel()).ToList(),
|
||||
};
|
||||
await communicationManager.BroadcastToPlayers(response, userName);
|
||||
}
|
||||
|
||||
var serialized = JsonConvert.SerializeObject(response);
|
||||
logger.LogInformation("Response to {0} \n{1}\n", userName, serialized);
|
||||
await socket.SendTextAsync(serialized);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,45 +1,47 @@
|
||||
using Gameboard.Shogi.Api.ServiceModels.Messages;
|
||||
using Gameboard.ShogiUI.Sockets.Extensions;
|
||||
using Gameboard.ShogiUI.Sockets.Managers.Utility;
|
||||
using Gameboard.ShogiUI.Sockets.Repositories;
|
||||
using Service = Gameboard.ShogiUI.Sockets.ServiceModels.Socket;
|
||||
using Newtonsoft.Json;
|
||||
using System.Net.WebSockets;
|
||||
using System.Threading.Tasks;
|
||||
using Gameboard.ShogiUI.Sockets.Models;
|
||||
using Gameboard.ShogiUI.Sockets.Repositories;
|
||||
using Newtonsoft.Json;
|
||||
using System.Threading.Tasks;
|
||||
using Service = Gameboard.ShogiUI.Sockets.ServiceModels.Socket;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
{
|
||||
public class MoveHandler : IActionHandler
|
||||
{
|
||||
private readonly IBoardManager boardManager;
|
||||
private readonly IGameboardRepository gameboardRepository;
|
||||
private readonly ISocketCommunicationManager communicationManager;
|
||||
public MoveHandler(
|
||||
IBoardManager boardManager,
|
||||
ISocketCommunicationManager communicationManager,
|
||||
IGameboardRepository gameboardRepository)
|
||||
{
|
||||
this.boardManager = boardManager;
|
||||
this.gameboardRepository = gameboardRepository;
|
||||
this.communicationManager = communicationManager;
|
||||
}
|
||||
|
||||
public async Task Handle(WebSocket socket, string json, string userName)
|
||||
public async Task Handle(string json, string userName)
|
||||
{
|
||||
var request = JsonConvert.DeserializeObject<Service.Messages.MoveRequest>(json);
|
||||
// Basic move validation
|
||||
if (request.Move.To.Equals(request.Move.From))
|
||||
{
|
||||
var serialized = JsonConvert.SerializeObject(
|
||||
new Service.Messages.ErrorResponse(Service.Types.ClientAction.Move)
|
||||
{
|
||||
Error = "Error: moving piece from tile to the same tile."
|
||||
});
|
||||
await socket.SendTextAsync(serialized);
|
||||
var error = new Service.Messages.ErrorResponse(Service.Types.ClientAction.Move)
|
||||
{
|
||||
Error = "Error: moving piece from tile to the same tile."
|
||||
};
|
||||
await communicationManager.BroadcastToPlayers(error, userName);
|
||||
return;
|
||||
}
|
||||
|
||||
var moveModel = new Move(request.Move);
|
||||
var session = (await gameboardRepository.GetGame(request.GameName)).Session;
|
||||
await gameboardRepository.PostMove(request.GameName, new PostMove(Mapper.Map(moveModel)));
|
||||
var board = boardManager.Get(request.GameName);
|
||||
var boardMove = moveModel.ToBoardModel();
|
||||
//board.Move()
|
||||
await gameboardRepository.PostMove(request.GameName, new PostMove(moveModel.ToApiModel()));
|
||||
|
||||
|
||||
var response = new Service.Messages.MoveResponse(Service.Types.ClientAction.Move)
|
||||
{
|
||||
@@ -47,7 +49,7 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
PlayerName = userName,
|
||||
Move = moveModel.ToServiceModel()
|
||||
};
|
||||
await communicationManager.BroadcastToGame(session.Name, response);
|
||||
await communicationManager.BroadcastToGame(request.GameName, response);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,10 +21,11 @@ namespace Gameboard.ShogiUI.Sockets.Managers
|
||||
Task BroadcastToAll(IResponse response);
|
||||
Task BroadcastToGame(string gameName, IResponse response);
|
||||
Task BroadcastToGame(string gameName, IResponse forPlayer1, IResponse forPlayer2);
|
||||
void SubscribeToGame(WebSocket socket, Session session, string playerName);
|
||||
void SubscribeToGame(Session session, string playerName);
|
||||
void SubscribeToBroadcast(WebSocket socket, string playerName);
|
||||
void UnsubscribeFromBroadcastAndGames(string playerName);
|
||||
void UnsubscribeFromGame(string gameName, string playerName);
|
||||
Task BroadcastToPlayers(IResponse response, params string[] playerNames);
|
||||
}
|
||||
|
||||
public class SocketCommunicationManager : ISocketCommunicationManager
|
||||
@@ -65,7 +66,7 @@ namespace Gameboard.ShogiUI.Sockets.Managers
|
||||
else
|
||||
{
|
||||
var handler = handlerResolver(request.Action);
|
||||
await handler.Handle(socket, message, userName);
|
||||
await handler.Handle(message, userName);
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException ex)
|
||||
@@ -100,7 +101,7 @@ namespace Gameboard.ShogiUI.Sockets.Managers
|
||||
/// <summary>
|
||||
/// Unsubscribes the player from their current game, then subscribes to the new game.
|
||||
/// </summary>
|
||||
public void SubscribeToGame(WebSocket socket, Session session, string playerName)
|
||||
public void SubscribeToGame(Session session, string playerName)
|
||||
{
|
||||
// Unsubscribe from any other games
|
||||
foreach (var kvp in sessions)
|
||||
@@ -110,8 +111,11 @@ namespace Gameboard.ShogiUI.Sockets.Managers
|
||||
}
|
||||
|
||||
// Subscribe
|
||||
var s = sessions.GetOrAdd(session.Name, session);
|
||||
s.Subscriptions.TryAdd(playerName, socket);
|
||||
if (connections.TryGetValue(playerName, out var socket))
|
||||
{
|
||||
var s = sessions.GetOrAdd(session.Name, session);
|
||||
s.Subscriptions.TryAdd(playerName, socket);
|
||||
}
|
||||
}
|
||||
|
||||
public void UnsubscribeFromGame(string gameName, string playerName)
|
||||
@@ -123,6 +127,21 @@ namespace Gameboard.ShogiUI.Sockets.Managers
|
||||
}
|
||||
}
|
||||
|
||||
public async Task BroadcastToPlayers(IResponse response, params string[] playerNames)
|
||||
{
|
||||
var tasks = new List<Task>(playerNames.Length);
|
||||
foreach (var name in playerNames)
|
||||
{
|
||||
if (connections.TryGetValue(name, out var socket))
|
||||
{
|
||||
var serialized = JsonConvert.SerializeObject(response);
|
||||
logger.LogInformation("Response to {0} \n{1}\n", name, serialized);
|
||||
tasks.Add(socket.SendTextAsync(serialized));
|
||||
|
||||
}
|
||||
}
|
||||
await Task.WhenAll(tasks);
|
||||
}
|
||||
public Task BroadcastToAll(IResponse response)
|
||||
{
|
||||
var message = JsonConvert.SerializeObject(response);
|
||||
|
||||
@@ -1,67 +0,0 @@
|
||||
using Gameboard.ShogiUI.Sockets.Models;
|
||||
using Microsoft.FSharp.Core;
|
||||
using ShogiApi = Gameboard.Shogi.Api.ServiceModels.Types;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Managers.Utility
|
||||
{
|
||||
public static class Mapper
|
||||
{
|
||||
public static ShogiApi.Move Map(Move source)
|
||||
{
|
||||
var from = source.From;
|
||||
var to = source.To;
|
||||
FSharpOption<ShogiApi.WhichPieceName> pieceFromCaptured = source.PieceFromCaptured switch
|
||||
{
|
||||
"B" => new FSharpOption<ShogiApi.WhichPieceName>(ShogiApi.WhichPieceName.Bishop),
|
||||
"G" => new FSharpOption<ShogiApi.WhichPieceName>(ShogiApi.WhichPieceName.GoldenGeneral),
|
||||
"K" => new FSharpOption<ShogiApi.WhichPieceName>(ShogiApi.WhichPieceName.King),
|
||||
"k" => new FSharpOption<ShogiApi.WhichPieceName>(ShogiApi.WhichPieceName.Knight),
|
||||
"L" => new FSharpOption<ShogiApi.WhichPieceName>(ShogiApi.WhichPieceName.Lance),
|
||||
"P" => new FSharpOption<ShogiApi.WhichPieceName>(ShogiApi.WhichPieceName.Pawn),
|
||||
"R" => new FSharpOption<ShogiApi.WhichPieceName>(ShogiApi.WhichPieceName.Rook),
|
||||
"S" => new FSharpOption<ShogiApi.WhichPieceName>(ShogiApi.WhichPieceName.SilverGeneral),
|
||||
_ => null
|
||||
};
|
||||
var target = new ShogiApi.Move
|
||||
{
|
||||
Origin = new ShogiApi.BoardLocation { X = from.X, Y = from.Y },
|
||||
Destination = new ShogiApi.BoardLocation { X = to.X, Y = to.Y },
|
||||
IsPromotion = source.IsPromotion,
|
||||
PieceFromCaptured = pieceFromCaptured
|
||||
};
|
||||
return target;
|
||||
}
|
||||
|
||||
public static Move Map(ShogiApi.Move source)
|
||||
{
|
||||
var origin = source.Origin;
|
||||
var destination = source.Destination;
|
||||
string pieceFromCaptured = null;
|
||||
if (source.PieceFromCaptured != null)
|
||||
{
|
||||
pieceFromCaptured = source.PieceFromCaptured.Value switch
|
||||
{
|
||||
ShogiApi.WhichPieceName.Bishop => "B",
|
||||
ShogiApi.WhichPieceName.GoldenGeneral => "G",
|
||||
ShogiApi.WhichPieceName.King => "K",
|
||||
ShogiApi.WhichPieceName.Knight => "k",
|
||||
ShogiApi.WhichPieceName.Lance => "L",
|
||||
ShogiApi.WhichPieceName.Pawn => "P",
|
||||
ShogiApi.WhichPieceName.Rook => "R",
|
||||
ShogiApi.WhichPieceName.SilverGeneral => "S",
|
||||
_ => ""
|
||||
};
|
||||
}
|
||||
|
||||
var target = new Move
|
||||
{
|
||||
From = new Coords(origin.X, origin.Y),
|
||||
To = new Coords(destination.X, destination.Y),
|
||||
IsPromotion = source.IsPromotion,
|
||||
PieceFromCaptured = pieceFromCaptured
|
||||
};
|
||||
|
||||
return target;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,9 @@
|
||||
namespace Gameboard.ShogiUI.Sockets.Models
|
||||
using Gameboard.ShogiUI.BoardState;
|
||||
using Microsoft.FSharp.Core;
|
||||
using System;
|
||||
using ShogiApi = Gameboard.Shogi.Api.ServiceModels.Types;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Models
|
||||
{
|
||||
public class Move
|
||||
{
|
||||
@@ -15,7 +20,29 @@
|
||||
PieceFromCaptured = move.PieceFromCaptured;
|
||||
IsPromotion = move.IsPromotion;
|
||||
}
|
||||
|
||||
public Move(ShogiApi.Move move)
|
||||
{
|
||||
string pieceFromCaptured = null;
|
||||
if (move.PieceFromCaptured != null)
|
||||
{
|
||||
pieceFromCaptured = move.PieceFromCaptured.Value switch
|
||||
{
|
||||
ShogiApi.WhichPieceName.Bishop => "",
|
||||
ShogiApi.WhichPieceName.GoldenGeneral => "G",
|
||||
ShogiApi.WhichPieceName.King => "K",
|
||||
ShogiApi.WhichPieceName.Knight => "k",
|
||||
ShogiApi.WhichPieceName.Lance => "L",
|
||||
ShogiApi.WhichPieceName.Pawn => "P",
|
||||
ShogiApi.WhichPieceName.Rook => "R",
|
||||
ShogiApi.WhichPieceName.SilverGeneral => "S",
|
||||
_ => ""
|
||||
};
|
||||
}
|
||||
From = new Coords(move.Origin.X, move.Origin.Y);
|
||||
To = new Coords(move.Destination.X, move.Destination.Y);
|
||||
IsPromotion = move.IsPromotion;
|
||||
PieceFromCaptured = pieceFromCaptured;
|
||||
}
|
||||
public ServiceModels.Socket.Types.Move ToServiceModel() => new ServiceModels.Socket.Types.Move
|
||||
{
|
||||
From = From.ToBoardNotation(),
|
||||
@@ -23,5 +50,38 @@
|
||||
PieceFromCaptured = PieceFromCaptured,
|
||||
To = To.ToBoardNotation()
|
||||
};
|
||||
public ShogiApi.Move ToApiModel()
|
||||
{
|
||||
var pieceFromCaptured = PieceFromCaptured switch
|
||||
{
|
||||
"B" => new FSharpOption<ShogiApi.WhichPieceName>(ShogiApi.WhichPieceName.Bishop),
|
||||
"G" => new FSharpOption<ShogiApi.WhichPieceName>(ShogiApi.WhichPieceName.GoldenGeneral),
|
||||
"K" => new FSharpOption<ShogiApi.WhichPieceName>(ShogiApi.WhichPieceName.King),
|
||||
"k" => new FSharpOption<ShogiApi.WhichPieceName>(ShogiApi.WhichPieceName.Knight),
|
||||
"L" => new FSharpOption<ShogiApi.WhichPieceName>(ShogiApi.WhichPieceName.Lance),
|
||||
"P" => new FSharpOption<ShogiApi.WhichPieceName>(ShogiApi.WhichPieceName.Pawn),
|
||||
"R" => new FSharpOption<ShogiApi.WhichPieceName>(ShogiApi.WhichPieceName.Rook),
|
||||
"S" => new FSharpOption<ShogiApi.WhichPieceName>(ShogiApi.WhichPieceName.SilverGeneral),
|
||||
_ => null
|
||||
};
|
||||
var target = new ShogiApi.Move
|
||||
{
|
||||
Origin = new ShogiApi.BoardLocation { X = From.X, Y = From.Y },
|
||||
Destination = new ShogiApi.BoardLocation { X = To.X, Y = To.Y },
|
||||
IsPromotion = IsPromotion,
|
||||
PieceFromCaptured = pieceFromCaptured
|
||||
};
|
||||
return target;
|
||||
}
|
||||
public BoardState.Move ToBoardModel()
|
||||
{
|
||||
return new BoardState.Move
|
||||
{
|
||||
From = new BoardVector(From.X, From.Y),
|
||||
IsPromotion = IsPromotion,
|
||||
PieceFromCaptured = Enum.TryParse<WhichPiece>(PieceFromCaptured, out var whichPiece) ? whichPiece : null,
|
||||
To = new BoardVector(To.X, To.Y)
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,6 +45,7 @@ namespace Gameboard.ShogiUI.Sockets
|
||||
services.AddSingleton<ISocketTokenManager, SocketTokenManager>();
|
||||
services.AddSingleton<ISocketConnectionManager, SocketConnectionManager>();
|
||||
services.AddScoped<IGameboardRepositoryManager, GameboardRepositoryManager>();
|
||||
services.AddSingleton<IBoardManager, BoardManager>();
|
||||
services.AddSingleton<ActionHandlerResolver>(sp => action =>
|
||||
{
|
||||
return action switch
|
||||
|
||||
Reference in New Issue
Block a user