More organized communication strategy.

This commit is contained in:
2021-02-13 19:14:43 -06:00
parent 1826c07601
commit d76e4f7a8b
13 changed files with 212 additions and 173 deletions

View File

@@ -5,7 +5,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Gameboard.Shogi.Api.ServiceModels" Version="2.10.0" /> <PackageReference Include="Gameboard.Shogi.Api.ServiceModels" Version="2.13.0" />
<PackageReference Include="IdentityModel" Version="5.0.0" /> <PackageReference Include="IdentityModel" Version="5.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.AzureAD.UI" Version="5.0.2" /> <PackageReference Include="Microsoft.AspNetCore.Authentication.AzureAD.UI" Version="5.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.2" /> <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.2" />
@@ -18,9 +18,5 @@
<ProjectReference Include="..\Gameboard.ShogiUI.Sockets.ServiceModels\Gameboard.ShogiUI.Sockets.ServiceModels.csproj" /> <ProjectReference Include="..\Gameboard.ShogiUI.Sockets.ServiceModels\Gameboard.ShogiUI.Sockets.ServiceModels.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Models\" />
</ItemGroup>
</Project> </Project>

View File

@@ -28,7 +28,6 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
public async Task Handle(WebSocket socket, string json, string userName) 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 request = JsonConvert.DeserializeObject<CreateGameRequest>(json);
var postSessionResponse = await repository.PostSession(new PostSession var postSessionResponse = await repository.PostSession(new PostSession
{ {
@@ -43,7 +42,7 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
Game = new Game Game = new Game
{ {
GameName = postSessionResponse.SessionName, GameName = postSessionResponse.SessionName,
Players = new string[] { userName } Players = new[] { userName }
} }
}; };
@@ -52,15 +51,15 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
response.Error = "Game already exists."; response.Error = "Game already exists.";
} }
var serialized = JsonConvert.SerializeObject(response);
logger.LogInformation("Socket Response \n{0}\n", new[] { serialized });
if (request.IsPrivate) if (request.IsPrivate)
{ {
var serialized = JsonConvert.SerializeObject(response);
logger.LogInformation("Response to {0} \n{1}\n", userName, serialized);
await socket.SendTextAsync(serialized); await socket.SendTextAsync(serialized);
} }
else else
{ {
await communicationManager.BroadcastToAll(serialized); await communicationManager.BroadcastToAll(response);
} }
} }
} }

View File

@@ -28,7 +28,6 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
public async Task Handle(WebSocket socket, string json, string userName) 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 request = JsonConvert.DeserializeObject<JoinByCode>(json);
var joinGameResponse = await repository.PostJoinPrivateSession(new PostJoinPrivateSession var joinGameResponse = await repository.PostJoinPrivateSession(new PostJoinPrivateSession
{ {
@@ -46,9 +45,8 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
PlayerName = userName, PlayerName = userName,
GameName = gameName GameName = gameName
}; };
var serialized = JsonConvert.SerializeObject(response); // At this time, userName hasn't subscribed and won't receive this broadcasted messages.
await communicationManager.BroadcastToGame(gameName, serialized); await communicationManager.BroadcastToGame(gameName, response);
communicationManager.SubscribeToGame(socket, gameName, userName);
// But the player joining sees the JoinByCode occur. // But the player joining sees the JoinByCode occur.
response = new JoinGameResponse(ClientAction.JoinByCode) response = new JoinGameResponse(ClientAction.JoinByCode)
@@ -56,7 +54,8 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
PlayerName = userName, PlayerName = userName,
GameName = gameName GameName = gameName
}; };
serialized = JsonConvert.SerializeObject(response); var serialized = JsonConvert.SerializeObject(response);
logger.LogInformation("Response to {0} \n{1}\n", userName, serialized);
await socket.SendTextAsync(serialized); await socket.SendTextAsync(serialized);
} }
else else
@@ -67,7 +66,7 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
Error = "Error joining game." Error = "Error joining game."
}; };
var serialized = JsonConvert.SerializeObject(response); var serialized = JsonConvert.SerializeObject(response);
logger.LogInformation("Socket Response \n{0}\n", new[] { serialized }); logger.LogInformation("Response to {0} \n{1}\n", userName, serialized);
await socket.SendTextAsync(serialized); await socket.SendTextAsync(serialized);
} }
} }

View File

@@ -2,7 +2,6 @@
using Gameboard.ShogiUI.Sockets.Repositories; using Gameboard.ShogiUI.Sockets.Repositories;
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages; using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages;
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types; using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
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;
@@ -11,31 +10,28 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
{ {
public class JoinGameHandler : IActionHandler public class JoinGameHandler : IActionHandler
{ {
private readonly ILogger<JoinGameHandler> logger;
private readonly IGameboardRepository gameboardRepository; private readonly IGameboardRepository gameboardRepository;
private readonly ISocketCommunicationManager communicationManager; private readonly ISocketCommunicationManager communicationManager;
public JoinGameHandler( public JoinGameHandler(
ILogger<JoinGameHandler> logger,
ISocketCommunicationManager communicationManager, ISocketCommunicationManager communicationManager,
IGameboardRepository gameboardRepository) IGameboardRepository gameboardRepository)
{ {
this.logger = logger;
this.gameboardRepository = gameboardRepository; this.gameboardRepository = gameboardRepository;
this.communicationManager = communicationManager; this.communicationManager = communicationManager;
} }
public async Task Handle(WebSocket socket, string json, string userName) 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 request = JsonConvert.DeserializeObject<JoinGameRequest>(json);
var response = new JoinGameResponse(ClientAction.JoinGame) var response = new JoinGameResponse(ClientAction.JoinGame)
{ {
PlayerName = userName PlayerName = userName
}; };
var joinGameResponse = await gameboardRepository.PutJoinPublicSession(request.GameName, new PutJoinPublicSession var joinGameResponse = await gameboardRepository.PutJoinPublicSession(new PutJoinPublicSession
{ {
PlayerName = userName PlayerName = userName,
SessionName = request.GameName
}); });
if (joinGameResponse.JoinSucceeded) if (joinGameResponse.JoinSucceeded)
@@ -44,11 +40,9 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
} }
else else
{ {
response.Error = "Game is full or code is incorrect."; response.Error = "Game is full.";
} }
var serialized = JsonConvert.SerializeObject(response); await communicationManager.BroadcastToAll(response);
logger.LogInformation("Socket Response \n{0}\n", new[] { serialized });
await communicationManager.BroadcastToAll(serialized);
} }
} }
} }

View File

@@ -1,10 +1,9 @@
using Gameboard.ShogiUI.Sockets.Extensions; using Gameboard.ShogiUI.Sockets.Extensions;
using Gameboard.ShogiUI.Sockets.Models;
using Gameboard.ShogiUI.Sockets.Repositories; using Gameboard.ShogiUI.Sockets.Repositories;
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages; using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages;
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types; using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
using System;
using System.Linq; using System.Linq;
using System.Net.WebSockets; using System.Net.WebSockets;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -13,20 +12,16 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
{ {
public class ListGamesHandler : IActionHandler public class ListGamesHandler : IActionHandler
{ {
private readonly ILogger<ListGamesHandler> logger;
private readonly IGameboardRepository repository; private readonly IGameboardRepository repository;
public ListGamesHandler( public ListGamesHandler(
ILogger<ListGamesHandler> logger,
IGameboardRepository repository) IGameboardRepository repository)
{ {
this.logger = logger;
this.repository = repository; this.repository = repository;
} }
public async Task Handle(WebSocket socket, string json, string userName) 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 request = JsonConvert.DeserializeObject<ListGamesRequest>(json);
var getGamesResponse = string.IsNullOrWhiteSpace(userName) var getGamesResponse = string.IsNullOrWhiteSpace(userName)
? await repository.GetGames() ? await repository.GetGames()
@@ -34,20 +29,14 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
var games = getGamesResponse.Sessions var games = getGamesResponse.Sessions
.OrderBy(s => s.Player1 == userName || s.Player2 == userName) .OrderBy(s => s.Player1 == userName || s.Player2 == userName)
.Select(s => .Select(s => new Session(s).ToServiceModel()); // yuck
{
var players = new[] { s.Player1, s.Player2 }
.Where(p => !string.IsNullOrWhiteSpace(p))
.ToArray();
return new Game { GameName = s.Name, Players = players };
});
var response = new ListGamesResponse(ClientAction.ListGames) var response = new ListGamesResponse(ClientAction.ListGames)
{ {
Games = games ?? Array.Empty<Game>() Games = games
}; };
var serialized = JsonConvert.SerializeObject(response); var serialized = JsonConvert.SerializeObject(response);
logger.LogInformation("Socket Response \n{0}\n", new[] { serialized });
await socket.SendTextAsync(serialized); await socket.SendTextAsync(serialized);
} }
} }

View File

@@ -1,5 +1,6 @@
using Gameboard.ShogiUI.Sockets.Extensions; using Gameboard.ShogiUI.Sockets.Extensions;
using Gameboard.ShogiUI.Sockets.Managers.Utility; using Gameboard.ShogiUI.Sockets.Managers.Utility;
using Gameboard.ShogiUI.Sockets.Models;
using Gameboard.ShogiUI.Sockets.Repositories; using Gameboard.ShogiUI.Sockets.Repositories;
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages; using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages;
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types; using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
@@ -29,33 +30,28 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
public async Task Handle(WebSocket socket, string json, string userName) 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 request = JsonConvert.DeserializeObject<LoadGameRequest>(json);
var response = new LoadGameResponse(ClientAction.LoadGame);
var getGameResponse = await gameboardRepository.GetGame(request.GameName); var getGameResponse = await gameboardRepository.GetGame(request.GameName);
var getMovesResponse = await gameboardRepository.GetMoves(request.GameName); var getMovesResponse = await gameboardRepository.GetMoves(request.GameName);
var response = new LoadGameResponse(ClientAction.LoadGame);
if (getGameResponse == null || getMovesResponse == null) if (getGameResponse == null || getMovesResponse == null)
{ {
response.Error = $"Could not find game."; response.Error = $"Could not find game.";
} }
else else
{ {
var session = getGameResponse.Session; var session = new Session(getGameResponse.Session);
var players = new[] { session.Player1, session.Player2 } communicationManager.SubscribeToGame(socket, session, userName);
.Where(p => !string.IsNullOrWhiteSpace(p))
.ToArray();
response.Game = new Game { GameName = session.Name, Players = players };
response.Game = session.ToServiceModel();
response.Moves = userName.Equals(session.Player1) response.Moves = userName.Equals(session.Player1)
? getMovesResponse.Moves.Select(_ => Mapper.Map(_)) ? getMovesResponse.Moves.Select(_ => Mapper.Map(_))
: getMovesResponse.Moves.Select(_ => Move.ConvertPerspective(Mapper.Map(_))); : getMovesResponse.Moves.Select(_ => Move.ConvertPerspective(Mapper.Map(_)));
communicationManager.SubscribeToGame(socket, session.Name, userName);
} }
var serialized = JsonConvert.SerializeObject(response); var serialized = JsonConvert.SerializeObject(response);
logger.LogInformation("Socket Response \n{0}\n", serialized); logger.LogInformation("Response to {0} \n{1}\n", userName, serialized);
await socket.SendTextAsync(serialized); await socket.SendTextAsync(serialized);
} }
} }

View File

@@ -4,7 +4,6 @@ using Gameboard.ShogiUI.Sockets.Managers.Utility;
using Gameboard.ShogiUI.Sockets.Repositories; using Gameboard.ShogiUI.Sockets.Repositories;
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages; using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages;
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types; using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
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;
@@ -13,26 +12,21 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
{ {
public class MoveHandler : IActionHandler public class MoveHandler : IActionHandler
{ {
private readonly ILogger<MoveHandler> logger;
private readonly IGameboardRepository gameboardRepository; private readonly IGameboardRepository gameboardRepository;
private readonly ISocketCommunicationManager communicationManager; private readonly ISocketCommunicationManager communicationManager;
public MoveHandler( public MoveHandler(
ILogger<MoveHandler> logger,
ISocketCommunicationManager communicationManager, ISocketCommunicationManager communicationManager,
IGameboardRepository gameboardRepository) IGameboardRepository gameboardRepository)
{ {
this.logger = logger;
this.gameboardRepository = gameboardRepository; this.gameboardRepository = gameboardRepository;
this.communicationManager = communicationManager; this.communicationManager = communicationManager;
} }
public async Task Handle(WebSocket socket, string json, string userName) 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); var request = JsonConvert.DeserializeObject<MoveRequest>(json);
// Basic move validation // Basic move validation
var move = request.Move; if (request.Move.To.Equals(request.Move.From))
if (move.To.Equals(move.From))
{ {
var serialized = JsonConvert.SerializeObject( var serialized = JsonConvert.SerializeObject(
new ErrorResponse(ClientAction.Move) new ErrorResponse(ClientAction.Move)
@@ -43,33 +37,25 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
return; return;
} }
var getSessionResponse = await gameboardRepository.GetGame(request.GameName); var session = (await gameboardRepository.GetGame(request.GameName)).Session;
var isPlayer1 = userName == getSessionResponse.Session.Player1; var isPlayer2 = userName == session.Player2;
if (!isPlayer1) // Shogi.Api expects the move coordinates from the perspective of player 1.
{ var move = isPlayer2 ? Move.ConvertPerspective(request.Move) : request.Move;
// Convert the move coords to player1 perspective.
move = Move.ConvertPerspective(move);
}
await gameboardRepository.PostMove(request.GameName, new PostMove(Mapper.Map(move))); await gameboardRepository.PostMove(request.GameName, new PostMove(Mapper.Map(move)));
var response = new MoveResponse(ClientAction.Move) var responseForPlayer1 = new MoveResponse(ClientAction.Move)
{ {
GameName = request.GameName, GameName = request.GameName,
PlayerName = userName PlayerName = userName,
Move = isPlayer2 ? Move.ConvertPerspective(request.Move) : request.Move
}; };
await communicationManager.BroadcastToGame( var responseForPlayer2 = new MoveResponse(ClientAction.Move)
request.GameName,
(playerName, sslStream) =>
{ {
response.Move = playerName.Equals(userName) GameName = request.GameName,
? request.Move PlayerName = userName,
: Move.ConvertPerspective(request.Move); Move = isPlayer2 ? request.Move : Move.ConvertPerspective(request.Move)
var serialized = JsonConvert.SerializeObject(response); };
logger.LogInformation("Socket Response \n{0}\n", new[] { serialized }); await communicationManager.BroadcastToGame(session.Name, responseForPlayer1, responseForPlayer2);
return serialized;
}
);
} }
} }
} }

View File

@@ -1,6 +1,8 @@
using Gameboard.ShogiUI.Sockets.Extensions; using Gameboard.ShogiUI.Sockets.Extensions;
using Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers; using Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers;
using Gameboard.ShogiUI.Sockets.Managers.Utility; using Gameboard.ShogiUI.Sockets.Managers.Utility;
using Gameboard.ShogiUI.Sockets.Models;
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Interfaces;
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types; using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
@@ -16,10 +18,10 @@ namespace Gameboard.ShogiUI.Sockets.Managers
public interface ISocketCommunicationManager public interface ISocketCommunicationManager
{ {
Task CommunicateWith(WebSocket w, string s); Task CommunicateWith(WebSocket w, string s);
Task BroadcastToAll(string msg); Task BroadcastToAll(IResponse response);
Task BroadcastToGame(string gameName, Func<string, WebSocket, string> msgBuilder); Task BroadcastToGame(string gameName, IResponse response);
Task BroadcastToGame(string gameName, string msg); Task BroadcastToGame(string gameName, IResponse forPlayer1, IResponse forPlayer2);
void SubscribeToGame(WebSocket socket, string gameName, string playerName); void SubscribeToGame(WebSocket socket, Session session, string playerName);
void SubscribeToBroadcast(WebSocket socket, string playerName); void SubscribeToBroadcast(WebSocket socket, string playerName);
void UnsubscribeFromBroadcastAndGames(string playerName); void UnsubscribeFromBroadcastAndGames(string playerName);
void UnsubscribeFromGame(string gameName, string playerName); void UnsubscribeFromGame(string gameName, string playerName);
@@ -27,8 +29,10 @@ namespace Gameboard.ShogiUI.Sockets.Managers
public class SocketCommunicationManager : ISocketCommunicationManager public class SocketCommunicationManager : ISocketCommunicationManager
{ {
/// <summary>Dictionary key is player name.</summary>
private readonly ConcurrentDictionary<string, WebSocket> connections; private readonly ConcurrentDictionary<string, WebSocket> connections;
private readonly ConcurrentDictionary<string, List<string>> gameSeats; /// <summary>Dictionary key is game name.</summary>
private readonly ConcurrentDictionary<string, Session> sessions;
private readonly ILogger<SocketCommunicationManager> logger; private readonly ILogger<SocketCommunicationManager> logger;
private readonly ActionHandlerResolver handlerResolver; private readonly ActionHandlerResolver handlerResolver;
@@ -39,7 +43,7 @@ namespace Gameboard.ShogiUI.Sockets.Managers
this.logger = logger; this.logger = logger;
this.handlerResolver = handlerResolver; this.handlerResolver = handlerResolver;
connections = new ConcurrentDictionary<string, WebSocket>(); connections = new ConcurrentDictionary<string, WebSocket>();
gameSeats = new ConcurrentDictionary<string, List<string>>(); sessions = new ConcurrentDictionary<string, Session>();
} }
public async Task CommunicateWith(WebSocket socket, string userName) public async Task CommunicateWith(WebSocket socket, string userName)
@@ -52,7 +56,7 @@ namespace Gameboard.ShogiUI.Sockets.Managers
{ {
var message = await socket.ReceiveTextAsync(); var message = await socket.ReceiveTextAsync();
if (string.IsNullOrWhiteSpace(message)) continue; if (string.IsNullOrWhiteSpace(message)) continue;
logger.LogInformation("Request \n{0}\n", message);
var request = JsonConvert.DeserializeObject<Request>(message); var request = JsonConvert.DeserializeObject<Request>(message);
if (!Enum.IsDefined(typeof(ClientAction), request.Action)) if (!Enum.IsDefined(typeof(ClientAction), request.Action))
{ {
@@ -68,7 +72,7 @@ namespace Gameboard.ShogiUI.Sockets.Managers
{ {
logger.LogError(ex.Message); logger.LogError(ex.Message);
} }
catch(WebSocketException ex) catch (WebSocketException ex)
{ {
logger.LogInformation($"{nameof(WebSocketException)} in {nameof(SocketCommunicationManager)}."); logger.LogInformation($"{nameof(WebSocketException)} in {nameof(SocketCommunicationManager)}.");
logger.LogInformation("Probably tried writing to a closed socket."); logger.LogInformation("Probably tried writing to a closed socket.");
@@ -80,93 +84,79 @@ namespace Gameboard.ShogiUI.Sockets.Managers
public void SubscribeToBroadcast(WebSocket socket, string playerName) public void SubscribeToBroadcast(WebSocket socket, string playerName)
{ {
logger.LogInformation("Subscribing [{0}] to broadcast", playerName);
connections.TryAdd(playerName, socket); connections.TryAdd(playerName, socket);
} }
public void UnsubscribeFromBroadcastAndGames(string playerName) public void UnsubscribeFromBroadcastAndGames(string playerName)
{ {
logger.LogInformation("Unsubscribing [{0}] from broadcast", playerName);
connections.TryRemove(playerName, out _); connections.TryRemove(playerName, out _);
foreach (var game in gameSeats) foreach (var kvp in sessions)
{ {
game.Value.Remove(playerName); var sessionName = kvp.Key;
UnsubscribeFromGame(sessionName, playerName);
} }
} }
/// <summary> /// <summary>
/// Unsubscribes the player from their current game, then subscribes to the new game. /// Unsubscribes the player from their current game, then subscribes to the new game.
/// </summary> /// </summary>
public void SubscribeToGame(WebSocket socket, string gameName, string playerName) public void SubscribeToGame(WebSocket socket, Session session, string playerName)
{ {
// Unsubscribe from any other games // Unsubscribe from any other games
foreach (var kvp in gameSeats) foreach (var kvp in sessions)
{ {
var gameNameKey = kvp.Key; var gameNameKey = kvp.Key;
UnsubscribeFromGame(gameNameKey, playerName); UnsubscribeFromGame(gameNameKey, playerName);
} }
// Subscribe // Subscribe
logger.LogInformation("Subscribing player [{0}] to game [{1}]", playerName, gameName); var s = sessions.GetOrAdd(session.Name, session);
var addSuccess = gameSeats.TryAdd(gameName, new List<string> { playerName }); s.Subscriptions.TryAdd(playerName, socket);
if (!addSuccess && !gameSeats[gameName].Contains(playerName))
{
gameSeats[gameName].Add(playerName);
}
} }
public void UnsubscribeFromGame(string gameName, string playerName) public void UnsubscribeFromGame(string gameName, string playerName)
{ {
if (gameSeats.ContainsKey(gameName)) if (sessions.TryGetValue(gameName, out var s))
{ {
logger.LogInformation("Unsubscribing player [{0}] from game [{1}]", playerName, gameName); s.Subscriptions.TryRemove(playerName, out _);
gameSeats[gameName].Remove(playerName); if (s.Subscriptions.IsEmpty) sessions.TryRemove(gameName, out _);
if (gameSeats[gameName].Count == 0) gameSeats.TryRemove(gameName, out _);
} }
} }
public async Task BroadcastToAll(string msg) public Task BroadcastToAll(IResponse response)
{ {
var tasks = connections.Select(kvp => var message = JsonConvert.SerializeObject(response);
logger.LogInformation($"Broadcasting\n{0}", message);
var tasks = new List<Task>(connections.Count);
foreach (var kvp in connections)
{ {
var player = kvp.Key;
var socket = kvp.Value; var socket = kvp.Value;
logger.LogInformation("Broadcasting to player [{0}] \n{1}\n", new[] { player, msg }); tasks.Add(socket.SendTextAsync(message));
return socket.SendTextAsync(msg); }
}); return Task.WhenAll(tasks);
await Task.WhenAll(tasks);
} }
public async Task BroadcastToGame(string gameName, string msg) public Task BroadcastToGame(string gameName, IResponse forPlayer1, IResponse forPlayer2)
{ {
if (gameSeats.ContainsKey(gameName)) if (sessions.TryGetValue(gameName, out var session))
{ {
var tasks = gameSeats[gameName] var serialized1 = JsonConvert.SerializeObject(forPlayer1);
.Select(playerName => var serialized2 = JsonConvert.SerializeObject(forPlayer2);
{ return Task.WhenAll(
logger.LogInformation("Broadcasting to game [{0}], player [{0}] \n{1}\n", gameName, playerName, msg); session.SendToPlayer1(serialized1),
return connections[playerName]; session.SendToPlayer2(serialized2));
})
.Where(stream => stream != null)
.Select(socket => socket.SendTextAsync(msg));
await Task.WhenAll(tasks);
} }
return Task.CompletedTask;
} }
public async Task BroadcastToGame(string gameName, Func<string, WebSocket, string> msgBuilder) public Task BroadcastToGame(string gameName, IResponse messageForAllPlayers)
{ {
if (gameSeats.ContainsKey(gameName)) if (sessions.TryGetValue(gameName, out var session))
{ {
var tasks = gameSeats[gameName] var serialized = JsonConvert.SerializeObject(messageForAllPlayers);
.Select(playerName => return session.Broadcast(serialized);
{
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);
} }
return Task.CompletedTask;
} }
} }
} }

View File

@@ -30,7 +30,7 @@ namespace Gameboard.ShogiUI.Sockets.Managers
var oneTimeToken = context.Request.Query["token"][0]; var oneTimeToken = context.Request.Query["token"][0];
var tokenAsGuid = Guid.Parse(oneTimeToken); var tokenAsGuid = Guid.Parse(oneTimeToken);
var userName = tokenManager.GetUsername(tokenAsGuid); var userName = tokenManager.GetUsername(tokenAsGuid);
if (!string.IsNullOrEmpty(userName)) if (userName != null)
{ {
var socket = await context.WebSockets.AcceptWebSocketAsync(); var socket = await context.WebSockets.AcceptWebSocketAsync();
await communicationManager.CommunicateWith(socket, userName); await communicationManager.CommunicateWith(socket, userName);

View File

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

View File

@@ -0,0 +1,68 @@
using Gameboard.ShogiUI.Sockets.Extensions;
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Net.WebSockets;
using System.Threading.Tasks;
namespace Gameboard.ShogiUI.Sockets.Models
{
public class Session
{
public string Name { get; }
public string Player1 { get; }
public string Player2 { get; }
public ConcurrentDictionary<string, WebSocket> Subscriptions { get; }
public Session(Shogi.Api.ServiceModels.Types.Session session)
{
Name = session.Name;
Player1 = session.Player1;
Player2 = session.Player2;
Subscriptions = new ConcurrentDictionary<string, WebSocket>();
}
public bool Subscribe(string playerName, WebSocket socket) => Subscriptions.TryAdd(playerName, socket);
public Task Broadcast(string message)
{
var tasks = new List<Task>(Subscriptions.Count);
foreach (var kvp in Subscriptions)
{
var socket = kvp.Value;
tasks.Add(socket.SendTextAsync(message));
}
return Task.WhenAll(tasks);
}
public Task SendToPlayer1(string message)
{
if (Subscriptions.TryGetValue(Player1, out var socket))
{
return socket.SendTextAsync(message);
}
return Task.CompletedTask;
}
public Task SendToPlayer2(string message)
{
if (Subscriptions.TryGetValue(Player2, out var socket))
{
return socket.SendTextAsync(message);
}
return Task.CompletedTask;
}
public Game ToServiceModel()
{
var players = new List<string>(2) { Player1 };
if (!string.IsNullOrWhiteSpace(Player2)) players.Add(Player2);
return new Game
{
GameName = Name,
Players = players.ToArray()
};
}
}
}

View File

@@ -1,10 +1,10 @@
using Gameboard.Shogi.Api.ServiceModels.Messages; using Gameboard.Shogi.Api.ServiceModels.Messages;
using Gameboard.ShogiUI.Sockets.Repositories.Utility;
using Newtonsoft.Json; using Newtonsoft.Json;
using System; 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 Gameboard.ShogiUI.Sockets.Repositories.Utility;
namespace Gameboard.ShogiUI.Sockets.Repositories namespace Gameboard.ShogiUI.Sockets.Repositories
{ {
@@ -17,7 +17,7 @@ namespace Gameboard.ShogiUI.Sockets.Repositories
Task<GetMovesResponse> GetMoves(string gameName); Task<GetMovesResponse> GetMoves(string gameName);
Task<PostSessionResponse> PostSession(PostSession request); Task<PostSessionResponse> PostSession(PostSession request);
Task<PostJoinPrivateSessionResponse> PostJoinPrivateSession(PostJoinPrivateSession request); Task<PostJoinPrivateSessionResponse> PostJoinPrivateSession(PostJoinPrivateSession request);
Task<PutJoinPublicSessionResponse> PutJoinPublicSession(string gameName, PutJoinPublicSession request); Task<PutJoinPublicSessionResponse> PutJoinPublicSession(PutJoinPublicSession request);
Task PostMove(string gameName, PostMove request); Task PostMove(string gameName, PostMove request);
Task<PostJoinCodeResponse> PostJoinCode(string gameName, string userName); Task<PostJoinCodeResponse> PostJoinCode(string gameName, string userName);
Task<GetPlayerResponse> GetPlayer(string userName); Task<GetPlayerResponse> GetPlayer(string userName);
@@ -26,6 +26,11 @@ namespace Gameboard.ShogiUI.Sockets.Repositories
public class GameboardRepository : IGameboardRepository public class GameboardRepository : IGameboardRepository
{ {
private const string GetSessionsRoute = "Sessions";
private const string PostSessionRoute = "Session";
private const string JoinSessionRoute = "Session/Join";
private const string PlayerRoute = "Player";
private const string MediaType = "application/json";
private readonly IAuthenticatedHttpClient client; private readonly IAuthenticatedHttpClient client;
public GameboardRepository(IAuthenticatedHttpClient client) public GameboardRepository(IAuthenticatedHttpClient client)
{ {
@@ -34,7 +39,7 @@ namespace Gameboard.ShogiUI.Sockets.Repositories
public async Task<GetSessionsResponse> GetGames() public async Task<GetSessionsResponse> GetGames()
{ {
var response = await client.GetAsync("Sessions"); var response = await client.GetAsync(GetSessionsRoute);
var json = await response.Content.ReadAsStringAsync(); var json = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<GetSessionsResponse>(json); return JsonConvert.DeserializeObject<GetSessionsResponse>(json);
} }
@@ -63,25 +68,24 @@ namespace Gameboard.ShogiUI.Sockets.Repositories
public async Task<PostSessionResponse> PostSession(PostSession request) public async Task<PostSessionResponse> PostSession(PostSession request)
{ {
var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json"); var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, MediaType);
var response = await client.PostAsync("Session", content); var response = await client.PostAsync(PostSessionRoute, content);
var json = await response.Content.ReadAsStringAsync(); var json = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<PostSessionResponse>(json); return JsonConvert.DeserializeObject<PostSessionResponse>(json);
} }
public async Task<PutJoinPublicSessionResponse> PutJoinPublicSession(string gameName, PutJoinPublicSession request) public async Task<PutJoinPublicSessionResponse> PutJoinPublicSession(PutJoinPublicSession request)
{ {
var uri = $"Session/{gameName}/Join"; var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, MediaType);
var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json"); var response = await client.PutAsync(JoinSessionRoute, content);
var response = await client.PostAsync(Uri.EscapeUriString(uri), content);
var json = await response.Content.ReadAsStringAsync(); var json = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<PutJoinPublicSessionResponse>(json); return JsonConvert.DeserializeObject<PutJoinPublicSessionResponse>(json);
} }
public async Task<PostJoinPrivateSessionResponse> PostJoinPrivateSession(PostJoinPrivateSession request) public async Task<PostJoinPrivateSessionResponse> PostJoinPrivateSession(PostJoinPrivateSession request)
{ {
var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json"); var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, MediaType);
var response = await client.PostAsync("Session/Join", content); var response = await client.PostAsync(JoinSessionRoute, content);
var json = await response.Content.ReadAsStringAsync(); var json = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<PostJoinPrivateSessionResponse>(json); return JsonConvert.DeserializeObject<PostJoinPrivateSessionResponse>(json);
} }
@@ -97,7 +101,7 @@ namespace Gameboard.ShogiUI.Sockets.Repositories
public async Task PostMove(string gameName, PostMove request) public async Task PostMove(string gameName, PostMove request)
{ {
var uri = $"Session/{gameName}/Move"; var uri = $"Session/{gameName}/Move";
var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json"); var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, MediaType);
await client.PostAsync(Uri.EscapeUriString(uri), content); await client.PostAsync(Uri.EscapeUriString(uri), content);
} }
@@ -105,7 +109,7 @@ namespace Gameboard.ShogiUI.Sockets.Repositories
{ {
var uri = $"JoinCode/{gameName}"; var uri = $"JoinCode/{gameName}";
var serialized = JsonConvert.SerializeObject(new PostJoinCode { PlayerName = userName }); var serialized = JsonConvert.SerializeObject(new PostJoinCode { PlayerName = userName });
var content = new StringContent(serialized, Encoding.UTF8, "application/json"); var content = new StringContent(serialized, Encoding.UTF8, MediaType);
var json = await (await client.PostAsync(Uri.EscapeUriString(uri), content)).Content.ReadAsStringAsync(); var json = await (await client.PostAsync(Uri.EscapeUriString(uri), content)).Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<PostJoinCodeResponse>(json); return JsonConvert.DeserializeObject<PostJoinCodeResponse>(json);
} }
@@ -120,8 +124,8 @@ namespace Gameboard.ShogiUI.Sockets.Repositories
public async Task<HttpResponseMessage> PostPlayer(PostPlayer request) public async Task<HttpResponseMessage> PostPlayer(PostPlayer request)
{ {
var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json"); var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, MediaType);
return await client.PostAsync("Player", content); return await client.PostAsync(PlayerRoute, content);
} }
} }
} }

View File

@@ -13,6 +13,7 @@ namespace Gameboard.ShogiUI.Sockets.Repositories.Utility
Task<HttpResponseMessage> DeleteAsync(string requestUri); Task<HttpResponseMessage> DeleteAsync(string requestUri);
Task<HttpResponseMessage> GetAsync(string requestUri); Task<HttpResponseMessage> GetAsync(string requestUri);
Task<HttpResponseMessage> PostAsync(string requestUri, HttpContent content); Task<HttpResponseMessage> PostAsync(string requestUri, HttpContent content);
Task<HttpResponseMessage> PutAsync(string requestUri, HttpContent content);
} }
public class AuthenticatedHttpClient : HttpClient, IAuthenticatedHttpClient public class AuthenticatedHttpClient : HttpClient, IAuthenticatedHttpClient
@@ -89,6 +90,23 @@ namespace Gameboard.ShogiUI.Sockets.Repositories.Utility
await response.Content.ReadAsStringAsync()); await response.Content.ReadAsStringAsync());
return response; return response;
} }
public async new Task<HttpResponseMessage> PutAsync(string requestUri, HttpContent content)
{
var response = await base.PutAsync(requestUri, content);
if (response.StatusCode == HttpStatusCode.Unauthorized)
{
await RefreshBearerToken();
response = await base.PutAsync(requestUri, content);
}
logger.LogInformation(
"Repository PUT to {BaseUrl}{RequestUrl} \n\tRespCode: {RespCode} \n\tRequest: {Request}\n\tResponse: {Response}\n",
BaseAddress,
requestUri,
response.StatusCode,
await content.ReadAsStringAsync(),
await response.Content.ReadAsStringAsync());
return response;
}
public async new Task<HttpResponseMessage> DeleteAsync(string requestUri) public async new Task<HttpResponseMessage> DeleteAsync(string requestUri)
{ {
var response = await base.DeleteAsync(requestUri); var response = await base.DeleteAsync(requestUri);