checkpoint

This commit is contained in:
2021-02-23 18:03:23 -06:00
parent 8d79c75616
commit f644795cd3
38 changed files with 1451 additions and 177 deletions

View File

@@ -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);