squash a bunch of commits
This commit is contained in:
86
Shogi.Sockets/Managers/ModelMapper.cs
Normal file
86
Shogi.Sockets/Managers/ModelMapper.cs
Normal file
@@ -0,0 +1,86 @@
|
||||
using Shogi.Contracts.Types;
|
||||
using DomainWhichPiece = Shogi.Domain.WhichPiece;
|
||||
using DomainWhichPlayer = Shogi.Domain.WhichPlayer;
|
||||
using Piece = Shogi.Contracts.Types.Piece;
|
||||
|
||||
namespace Shogi.Api.Managers
|
||||
{
|
||||
public class ModelMapper : IModelMapper
|
||||
{
|
||||
public WhichPlayer Map(DomainWhichPlayer whichPlayer)
|
||||
{
|
||||
return whichPlayer switch
|
||||
{
|
||||
DomainWhichPlayer.Player1 => WhichPlayer.Player1,
|
||||
DomainWhichPlayer.Player2 => WhichPlayer.Player2,
|
||||
_ => throw new ArgumentException("Unrecognized value for WhichPlayer", nameof(whichPlayer))
|
||||
};
|
||||
}
|
||||
|
||||
public WhichPlayer? Map(DomainWhichPlayer? whichPlayer)
|
||||
{
|
||||
return whichPlayer.HasValue
|
||||
? Map(whichPlayer.Value)
|
||||
: null;
|
||||
}
|
||||
|
||||
public WhichPiece Map(DomainWhichPiece whichPiece)
|
||||
{
|
||||
return whichPiece switch
|
||||
{
|
||||
DomainWhichPiece.King => WhichPiece.King,
|
||||
DomainWhichPiece.GoldGeneral => WhichPiece.GoldGeneral,
|
||||
DomainWhichPiece.SilverGeneral => WhichPiece.SilverGeneral,
|
||||
DomainWhichPiece.Bishop => WhichPiece.Bishop,
|
||||
DomainWhichPiece.Rook => WhichPiece.Rook,
|
||||
DomainWhichPiece.Knight => WhichPiece.Knight,
|
||||
DomainWhichPiece.Lance => WhichPiece.Lance,
|
||||
DomainWhichPiece.Pawn => WhichPiece.Pawn,
|
||||
_ => throw new ArgumentException("Unrecognized value", nameof(whichPiece)),
|
||||
};
|
||||
}
|
||||
|
||||
public DomainWhichPiece Map(WhichPiece whichPiece)
|
||||
{
|
||||
return whichPiece switch
|
||||
{
|
||||
WhichPiece.King => DomainWhichPiece.King,
|
||||
WhichPiece.GoldGeneral => DomainWhichPiece.GoldGeneral,
|
||||
WhichPiece.SilverGeneral => DomainWhichPiece.SilverGeneral,
|
||||
WhichPiece.Bishop => DomainWhichPiece.Bishop,
|
||||
WhichPiece.Rook => DomainWhichPiece.Rook,
|
||||
WhichPiece.Knight => DomainWhichPiece.Knight,
|
||||
WhichPiece.Lance => DomainWhichPiece.Lance,
|
||||
WhichPiece.Pawn => DomainWhichPiece.Pawn,
|
||||
_ => throw new ArgumentException("Unrecognized value", nameof(whichPiece)),
|
||||
};
|
||||
}
|
||||
|
||||
public Piece Map(Domain.ValueObjects.Piece piece)
|
||||
{
|
||||
return new Piece { IsPromoted = piece.IsPromoted, Owner = Map(piece.Owner), WhichPiece = Map(piece.WhichPiece) };
|
||||
}
|
||||
|
||||
public Dictionary<string, Piece?> Map(IDictionary<string, Domain.ValueObjects.Piece?> boardState)
|
||||
{
|
||||
return boardState.ToDictionary(kvp => kvp.Key.ToUpper(), kvp => MapNullable(kvp.Value));
|
||||
}
|
||||
|
||||
public Piece? MapNullable(Domain.ValueObjects.Piece? piece)
|
||||
{
|
||||
if (piece == null) return null;
|
||||
return Map(piece);
|
||||
}
|
||||
}
|
||||
|
||||
public interface IModelMapper
|
||||
{
|
||||
WhichPlayer Map(DomainWhichPlayer whichPlayer);
|
||||
WhichPlayer? Map(DomainWhichPlayer? whichPlayer);
|
||||
WhichPiece Map(DomainWhichPiece whichPiece);
|
||||
DomainWhichPiece Map(WhichPiece value);
|
||||
Piece Map(Domain.ValueObjects.Piece p);
|
||||
Piece? MapNullable(Domain.ValueObjects.Piece? p);
|
||||
Dictionary<string, Piece?> Map(IDictionary<string, Domain.ValueObjects.Piece?> boardState);
|
||||
}
|
||||
}
|
||||
89
Shogi.Sockets/Managers/SocketConnectionManager.cs
Normal file
89
Shogi.Sockets/Managers/SocketConnectionManager.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
using Shogi.Contracts.Socket;
|
||||
using Shogi.Api.Extensions;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Net.WebSockets;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Shogi.Api.Managers;
|
||||
|
||||
public interface ISocketConnectionManager
|
||||
{
|
||||
Task BroadcastToAll(ISocketResponse response);
|
||||
void Subscribe(WebSocket socket, string playerName);
|
||||
void Unsubscribe(string playerName);
|
||||
Task BroadcastToPlayers(ISocketResponse response, params string?[] playerNames);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retains all active socket connections and provides convenient methods for sending messages to clients.
|
||||
/// </summary>
|
||||
public class SocketConnectionManager : ISocketConnectionManager
|
||||
{
|
||||
/// <summary>Dictionary key is player name.</summary>
|
||||
private readonly ConcurrentDictionary<string, WebSocket> connections;
|
||||
private readonly JsonSerializerOptions serializeOptions;
|
||||
|
||||
/// <summary>Dictionary key is game name.</summary>
|
||||
private readonly ILogger<SocketConnectionManager> logger;
|
||||
|
||||
public SocketConnectionManager(ILogger<SocketConnectionManager> logger)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.connections = new ConcurrentDictionary<string, WebSocket>();
|
||||
this.serializeOptions = new JsonSerializerOptions(JsonSerializerDefaults.General);
|
||||
|
||||
}
|
||||
|
||||
public void Subscribe(WebSocket socket, string playerName)
|
||||
{
|
||||
connections.TryRemove(playerName, out var _);
|
||||
connections.TryAdd(playerName, socket);
|
||||
}
|
||||
|
||||
public void Unsubscribe(string playerName)
|
||||
{
|
||||
connections.TryRemove(playerName, out _);
|
||||
}
|
||||
|
||||
public async Task BroadcastToPlayers(ISocketResponse response, params string?[] playerNames)
|
||||
{
|
||||
var tasks = new List<Task>(playerNames.Length);
|
||||
foreach (var name in playerNames)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(name) && connections.TryGetValue(name, out var socket))
|
||||
{
|
||||
var serialized = Serialize(response);
|
||||
logger.LogInformation("Response to {0} \n{1}\n", name, serialized);
|
||||
tasks.Add(socket.SendTextAsync(serialized));
|
||||
}
|
||||
}
|
||||
await Task.WhenAll(tasks);
|
||||
}
|
||||
public Task BroadcastToAll(ISocketResponse response)
|
||||
{
|
||||
var message = Serialize(response);
|
||||
logger.LogInformation("Broadcasting:\n{0}\nDone Broadcasting.", message);
|
||||
var tasks = new List<Task>(connections.Count);
|
||||
foreach (var kvp in connections)
|
||||
{
|
||||
var socket = kvp.Value;
|
||||
try
|
||||
{
|
||||
tasks.Add(socket.SendTextAsync(message));
|
||||
}
|
||||
catch (WebSocketException)
|
||||
{
|
||||
logger.LogInformation("Tried sending a message to socket connection for user [{user}], but found the connection has closed.", kvp.Key);
|
||||
Unsubscribe(kvp.Key);
|
||||
}
|
||||
catch
|
||||
{
|
||||
logger.LogInformation("Tried sending a message to socket connection for user [{user}], but found the connection has closed.", kvp.Key);
|
||||
Unsubscribe(kvp.Key);
|
||||
}
|
||||
}
|
||||
return Task.WhenAll(tasks);
|
||||
}
|
||||
|
||||
private string Serialize(object o) => JsonSerializer.Serialize(o, this.serializeOptions);
|
||||
}
|
||||
54
Shogi.Sockets/Managers/SocketTokenCache.cs
Normal file
54
Shogi.Sockets/Managers/SocketTokenCache.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Shogi.Api.Managers
|
||||
{
|
||||
public interface ISocketTokenCache
|
||||
{
|
||||
Guid GenerateToken(string s);
|
||||
string? GetUsername(Guid g);
|
||||
}
|
||||
|
||||
public class SocketTokenCache : ISocketTokenCache
|
||||
{
|
||||
/// <summary>
|
||||
/// Key is userName or webSessionId
|
||||
/// </summary>
|
||||
private readonly ConcurrentDictionary<string, Guid> Tokens;
|
||||
|
||||
public SocketTokenCache()
|
||||
{
|
||||
Tokens = new ConcurrentDictionary<string, Guid>();
|
||||
}
|
||||
|
||||
public Guid GenerateToken(string userName)
|
||||
{
|
||||
Tokens.Remove(userName, out _);
|
||||
|
||||
var guid = Guid.NewGuid();
|
||||
Tokens.TryAdd(userName, guid);
|
||||
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
await Task.Delay(TimeSpan.FromMinutes(1));
|
||||
Tokens.Remove(userName, out _);
|
||||
}).ConfigureAwait(false);
|
||||
|
||||
return guid;
|
||||
}
|
||||
|
||||
/// <returns>User name associated to the guid or null.</returns>
|
||||
public string? GetUsername(Guid guid)
|
||||
{
|
||||
var userName = Tokens.FirstOrDefault(kvp => kvp.Value == guid).Key;
|
||||
if (userName != null)
|
||||
{
|
||||
Tokens.Remove(userName, out _);
|
||||
}
|
||||
return userName;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user