All the code
This commit is contained in:
@@ -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; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user