checkpoint

This commit is contained in:
2022-06-12 12:37:32 -05:00
parent 2dcc6ca417
commit 4ca0b63564
43 changed files with 563 additions and 2128 deletions

View File

@@ -1,5 +1,4 @@
using Gameboard.ShogiUI.Sockets.Extensions;
using Gameboard.ShogiUI.Sockets.Models;
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
@@ -14,12 +13,8 @@ namespace Gameboard.ShogiUI.Sockets.Managers
public interface ISocketConnectionManager
{
Task BroadcastToAll(IResponse response);
//Task BroadcastToGame(string gameName, IResponse response);
//Task BroadcastToGame(string gameName, IResponse forPlayer1, IResponse forPlayer2);
void SubscribeToGame(Session session, string playerName);
void SubscribeToBroadcast(WebSocket socket, string playerName);
void UnsubscribeFromBroadcastAndGames(string playerName);
void UnsubscribeFromGame(string gameName, string playerName);
void Subscribe(WebSocket socket, string playerName);
void Unsubscribe(string playerName);
Task BroadcastToPlayers(IResponse response, params string?[] playerNames);
}
@@ -31,59 +26,23 @@ namespace Gameboard.ShogiUI.Sockets.Managers
/// <summary>Dictionary key is player name.</summary>
private readonly ConcurrentDictionary<string, WebSocket> connections;
/// <summary>Dictionary key is game name.</summary>
private readonly ConcurrentDictionary<string, Session> sessions;
private readonly ILogger<SocketConnectionManager> logger;
public SocketConnectionManager(ILogger<SocketConnectionManager> logger)
{
this.logger = logger;
connections = new ConcurrentDictionary<string, WebSocket>();
sessions = new ConcurrentDictionary<string, Session>();
}
public void SubscribeToBroadcast(WebSocket socket, string playerName)
public void Subscribe(WebSocket socket, string playerName)
{
connections.TryRemove(playerName, out var _);
connections.TryAdd(playerName, socket);
}
public void UnsubscribeFromBroadcastAndGames(string playerName)
public void Unsubscribe(string playerName)
{
connections.TryRemove(playerName, out _);
foreach (var kvp in sessions)
{
var sessionName = kvp.Key;
UnsubscribeFromGame(sessionName, playerName);
}
}
/// <summary>
/// Unsubscribes the player from their current game, then subscribes to the new game.
/// </summary>
public void SubscribeToGame(Session session, string playerName)
{
// Unsubscribe from any other games
foreach (var kvp in sessions)
{
var gameNameKey = kvp.Key;
UnsubscribeFromGame(gameNameKey, playerName);
}
// Subscribe
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)
{
if (sessions.TryGetValue(gameName, out var s))
{
s.Subscriptions.TryRemove(playerName, out _);
if (s.Subscriptions.IsEmpty) sessions.TryRemove(gameName, out _);
}
}
public async Task BroadcastToPlayers(IResponse response, params string?[] playerNames)
@@ -113,15 +72,15 @@ namespace Gameboard.ShogiUI.Sockets.Managers
tasks.Add(socket.SendTextAsync(message));
}
catch (WebSocketException webSocketException)
catch (WebSocketException)
{
logger.LogInformation("Tried sending a message to socket connection for user [{user}], but found the connection has closed.", kvp.Key);
UnsubscribeFromBroadcastAndGames(kvp.Key);
Unsubscribe(kvp.Key);
}
catch (Exception exception)
catch
{
logger.LogInformation("Tried sending a message to socket connection for user [{user}], but found the connection has closed.", kvp.Key);
UnsubscribeFromBroadcastAndGames(kvp.Key);
Unsubscribe(kvp.Key);
}
}
try
@@ -129,34 +88,11 @@ namespace Gameboard.ShogiUI.Sockets.Managers
var task = Task.WhenAll(tasks);
return task;
}
catch (Exception e)
catch
{
Console.WriteLine("Yo");
}
return Task.FromResult(0);
}
//public Task BroadcastToGame(string gameName, IResponse forPlayer1, IResponse forPlayer2)
//{
// if (sessions.TryGetValue(gameName, out var session))
// {
// var serialized1 = JsonConvert.SerializeObject(forPlayer1);
// var serialized2 = JsonConvert.SerializeObject(forPlayer2);
// return Task.WhenAll(
// session.SendToPlayer1(serialized1),
// session.SendToPlayer2(serialized2));
// }
// return Task.CompletedTask;
//}
//public Task BroadcastToGame(string gameName, IResponse messageForAllPlayers)
//{
// if (sessions.TryGetValue(gameName, out var session))
// {
// var serialized = JsonConvert.SerializeObject(messageForAllPlayers);
// return session.Broadcast(serialized);
// }
// return Task.CompletedTask;
//}
}
}