More organized communication strategy.
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<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="Microsoft.AspNetCore.Authentication.AzureAD.UI" 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" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Models\" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -28,7 +28,6 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
|
||||
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 postSessionResponse = await repository.PostSession(new PostSession
|
||||
{
|
||||
@@ -43,7 +42,7 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
Game = new Game
|
||||
{
|
||||
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.";
|
||||
}
|
||||
|
||||
var serialized = JsonConvert.SerializeObject(response);
|
||||
logger.LogInformation("Socket Response \n{0}\n", new[] { serialized });
|
||||
if (request.IsPrivate)
|
||||
{
|
||||
var serialized = JsonConvert.SerializeObject(response);
|
||||
logger.LogInformation("Response to {0} \n{1}\n", userName, serialized);
|
||||
await socket.SendTextAsync(serialized);
|
||||
}
|
||||
else
|
||||
{
|
||||
await communicationManager.BroadcastToAll(serialized);
|
||||
await communicationManager.BroadcastToAll(response);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,6 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
|
||||
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.PostJoinPrivateSession(new PostJoinPrivateSession
|
||||
{
|
||||
@@ -46,9 +45,8 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
PlayerName = userName,
|
||||
GameName = gameName
|
||||
};
|
||||
var serialized = JsonConvert.SerializeObject(response);
|
||||
await communicationManager.BroadcastToGame(gameName, serialized);
|
||||
communicationManager.SubscribeToGame(socket, gameName, userName);
|
||||
// At this time, userName hasn't subscribed and won't receive this broadcasted messages.
|
||||
await communicationManager.BroadcastToGame(gameName, response);
|
||||
|
||||
// But the player joining sees the JoinByCode occur.
|
||||
response = new JoinGameResponse(ClientAction.JoinByCode)
|
||||
@@ -56,7 +54,8 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
PlayerName = userName,
|
||||
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);
|
||||
}
|
||||
else
|
||||
@@ -67,7 +66,7 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
Error = "Error joining game."
|
||||
};
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
using Gameboard.ShogiUI.Sockets.Repositories;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using System.Net.WebSockets;
|
||||
using System.Threading.Tasks;
|
||||
@@ -11,31 +10,28 @@ namespace Gameboard.ShogiUI.Sockets.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.PutJoinPublicSession(request.GameName, new PutJoinPublicSession
|
||||
var joinGameResponse = await gameboardRepository.PutJoinPublicSession(new PutJoinPublicSession
|
||||
{
|
||||
PlayerName = userName
|
||||
PlayerName = userName,
|
||||
SessionName = request.GameName
|
||||
});
|
||||
|
||||
if (joinGameResponse.JoinSucceeded)
|
||||
@@ -44,11 +40,9 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
}
|
||||
else
|
||||
{
|
||||
response.Error = "Game is full or code is incorrect.";
|
||||
response.Error = "Game is full.";
|
||||
}
|
||||
var serialized = JsonConvert.SerializeObject(response);
|
||||
logger.LogInformation("Socket Response \n{0}\n", new[] { serialized });
|
||||
await communicationManager.BroadcastToAll(serialized);
|
||||
await communicationManager.BroadcastToAll(response);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
using Gameboard.ShogiUI.Sockets.Extensions;
|
||||
using Gameboard.ShogiUI.Sockets.Models;
|
||||
using Gameboard.ShogiUI.Sockets.Repositories;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Net.WebSockets;
|
||||
using System.Threading.Tasks;
|
||||
@@ -13,20 +12,16 @@ namespace Gameboard.ShogiUI.Sockets.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()
|
||||
@@ -34,20 +29,14 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
|
||||
var games = getGamesResponse.Sessions
|
||||
.OrderBy(s => s.Player1 == userName || s.Player2 == userName)
|
||||
.Select(s =>
|
||||
{
|
||||
var players = new[] { s.Player1, s.Player2 }
|
||||
.Where(p => !string.IsNullOrWhiteSpace(p))
|
||||
.ToArray();
|
||||
return new Game { GameName = s.Name, Players = players };
|
||||
});
|
||||
.Select(s => new Session(s).ToServiceModel()); // yuck
|
||||
|
||||
var response = new ListGamesResponse(ClientAction.ListGames)
|
||||
{
|
||||
Games = games ?? Array.Empty<Game>()
|
||||
Games = games
|
||||
};
|
||||
|
||||
var serialized = JsonConvert.SerializeObject(response);
|
||||
logger.LogInformation("Socket Response \n{0}\n", new[] { serialized });
|
||||
await socket.SendTextAsync(serialized);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using Gameboard.ShogiUI.Sockets.Extensions;
|
||||
using Gameboard.ShogiUI.Sockets.Managers.Utility;
|
||||
using Gameboard.ShogiUI.Sockets.Models;
|
||||
using Gameboard.ShogiUI.Sockets.Repositories;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages;
|
||||
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)
|
||||
{
|
||||
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);
|
||||
|
||||
var response = new LoadGameResponse(ClientAction.LoadGame);
|
||||
if (getGameResponse == null || getMovesResponse == null)
|
||||
{
|
||||
response.Error = $"Could not find game.";
|
||||
}
|
||||
else
|
||||
{
|
||||
var session = getGameResponse.Session;
|
||||
var players = new[] { session.Player1, session.Player2 }
|
||||
.Where(p => !string.IsNullOrWhiteSpace(p))
|
||||
.ToArray();
|
||||
response.Game = new Game { GameName = session.Name, Players = players };
|
||||
var session = new Session(getGameResponse.Session);
|
||||
communicationManager.SubscribeToGame(socket, session, userName);
|
||||
|
||||
response.Game = session.ToServiceModel();
|
||||
response.Moves = userName.Equals(session.Player1)
|
||||
? getMovesResponse.Moves.Select(_ => Mapper.Map(_))
|
||||
: getMovesResponse.Moves.Select(_ => Move.ConvertPerspective(Mapper.Map(_)));
|
||||
|
||||
communicationManager.SubscribeToGame(socket, session.Name, userName);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ using Gameboard.ShogiUI.Sockets.Managers.Utility;
|
||||
using Gameboard.ShogiUI.Sockets.Repositories;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using System.Net.WebSockets;
|
||||
using System.Threading.Tasks;
|
||||
@@ -13,26 +12,21 @@ namespace Gameboard.ShogiUI.Sockets.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))
|
||||
if (request.Move.To.Equals(request.Move.From))
|
||||
{
|
||||
var serialized = JsonConvert.SerializeObject(
|
||||
new ErrorResponse(ClientAction.Move)
|
||||
@@ -43,33 +37,25 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
|
||||
return;
|
||||
}
|
||||
|
||||
var getSessionResponse = await gameboardRepository.GetGame(request.GameName);
|
||||
var isPlayer1 = userName == getSessionResponse.Session.Player1;
|
||||
if (!isPlayer1)
|
||||
{
|
||||
// Convert the move coords to player1 perspective.
|
||||
move = Move.ConvertPerspective(move);
|
||||
}
|
||||
|
||||
var session = (await gameboardRepository.GetGame(request.GameName)).Session;
|
||||
var isPlayer2 = userName == session.Player2;
|
||||
// Shogi.Api expects the move coordinates from the perspective of player 1.
|
||||
var move = isPlayer2 ? Move.ConvertPerspective(request.Move) : request.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,
|
||||
PlayerName = userName
|
||||
PlayerName = userName,
|
||||
Move = isPlayer2 ? Move.ConvertPerspective(request.Move) : request.Move
|
||||
};
|
||||
await communicationManager.BroadcastToGame(
|
||||
request.GameName,
|
||||
(playerName, sslStream) =>
|
||||
var responseForPlayer2 = new MoveResponse(ClientAction.Move)
|
||||
{
|
||||
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;
|
||||
}
|
||||
);
|
||||
GameName = request.GameName,
|
||||
PlayerName = userName,
|
||||
Move = isPlayer2 ? request.Move : Move.ConvertPerspective(request.Move)
|
||||
};
|
||||
await communicationManager.BroadcastToGame(session.Name, responseForPlayer1, responseForPlayer2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
using Gameboard.ShogiUI.Sockets.Extensions;
|
||||
using Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers;
|
||||
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 Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
@@ -16,10 +18,10 @@ namespace Gameboard.ShogiUI.Sockets.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);
|
||||
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 SubscribeToBroadcast(WebSocket socket, string playerName);
|
||||
void UnsubscribeFromBroadcastAndGames(string playerName);
|
||||
void UnsubscribeFromGame(string gameName, string playerName);
|
||||
@@ -27,8 +29,10 @@ namespace Gameboard.ShogiUI.Sockets.Managers
|
||||
|
||||
public class SocketCommunicationManager : ISocketCommunicationManager
|
||||
{
|
||||
/// <summary>Dictionary key is player name.</summary>
|
||||
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 ActionHandlerResolver handlerResolver;
|
||||
|
||||
@@ -39,7 +43,7 @@ namespace Gameboard.ShogiUI.Sockets.Managers
|
||||
this.logger = logger;
|
||||
this.handlerResolver = handlerResolver;
|
||||
connections = new ConcurrentDictionary<string, WebSocket>();
|
||||
gameSeats = new ConcurrentDictionary<string, List<string>>();
|
||||
sessions = new ConcurrentDictionary<string, Session>();
|
||||
}
|
||||
|
||||
public async Task CommunicateWith(WebSocket socket, string userName)
|
||||
@@ -52,7 +56,7 @@ namespace Gameboard.ShogiUI.Sockets.Managers
|
||||
{
|
||||
var message = await socket.ReceiveTextAsync();
|
||||
if (string.IsNullOrWhiteSpace(message)) continue;
|
||||
|
||||
logger.LogInformation("Request \n{0}\n", message);
|
||||
var request = JsonConvert.DeserializeObject<Request>(message);
|
||||
if (!Enum.IsDefined(typeof(ClientAction), request.Action))
|
||||
{
|
||||
@@ -80,93 +84,79 @@ namespace Gameboard.ShogiUI.Sockets.Managers
|
||||
|
||||
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)
|
||||
foreach (var kvp in sessions)
|
||||
{
|
||||
game.Value.Remove(playerName);
|
||||
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(WebSocket socket, string gameName, string playerName)
|
||||
public void SubscribeToGame(WebSocket socket, Session session, string playerName)
|
||||
{
|
||||
// Unsubscribe from any other games
|
||||
foreach (var kvp in gameSeats)
|
||||
foreach (var kvp in sessions)
|
||||
{
|
||||
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);
|
||||
}
|
||||
var s = sessions.GetOrAdd(session.Name, session);
|
||||
s.Subscriptions.TryAdd(playerName, socket);
|
||||
}
|
||||
|
||||
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);
|
||||
gameSeats[gameName].Remove(playerName);
|
||||
if (gameSeats[gameName].Count == 0) gameSeats.TryRemove(gameName, out _);
|
||||
s.Subscriptions.TryRemove(playerName, out _);
|
||||
if (s.Subscriptions.IsEmpty) sessions.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;
|
||||
logger.LogInformation("Broadcasting to player [{0}] \n{1}\n", new[] { player, msg });
|
||||
return socket.SendTextAsync(msg);
|
||||
});
|
||||
await Task.WhenAll(tasks);
|
||||
tasks.Add(socket.SendTextAsync(message));
|
||||
}
|
||||
return 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]
|
||||
.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);
|
||||
var serialized1 = JsonConvert.SerializeObject(forPlayer1);
|
||||
var serialized2 = JsonConvert.SerializeObject(forPlayer2);
|
||||
return Task.WhenAll(
|
||||
session.SendToPlayer1(serialized1),
|
||||
session.SendToPlayer2(serialized2));
|
||||
}
|
||||
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]
|
||||
.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);
|
||||
}
|
||||
var serialized = JsonConvert.SerializeObject(messageForAllPlayers);
|
||||
return session.Broadcast(serialized);
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ namespace Gameboard.ShogiUI.Sockets.Managers
|
||||
var oneTimeToken = context.Request.Query["token"][0];
|
||||
var tokenAsGuid = Guid.Parse(oneTimeToken);
|
||||
var userName = tokenManager.GetUsername(tokenAsGuid);
|
||||
if (!string.IsNullOrEmpty(userName))
|
||||
if (userName != null)
|
||||
{
|
||||
var socket = await context.WebSockets.AcceptWebSocketAsync();
|
||||
await communicationManager.CommunicateWith(socket, userName);
|
||||
|
||||
@@ -1,38 +1,38 @@
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
|
||||
using Microsoft.FSharp.Core;
|
||||
using GameboardTypes = Gameboard.Shogi.Api.ServiceModels.Types;
|
||||
using ShogiApi = Gameboard.Shogi.Api.ServiceModels.Types;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Managers.Utility
|
||||
{
|
||||
public static class Mapper
|
||||
{
|
||||
public static GameboardTypes.Move Map(Move source)
|
||||
public static ShogiApi.Move Map(Move source)
|
||||
{
|
||||
var from = source.From;
|
||||
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),
|
||||
"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),
|
||||
"B" => new FSharpOption<ShogiApi.WhichPieceName>(ShogiApi.WhichPieceName.Bishop),
|
||||
"G" => new FSharpOption<ShogiApi.WhichPieceName>(ShogiApi.WhichPieceName.GoldenGeneral),
|
||||
"K" => new FSharpOption<ShogiApi.WhichPieceName>(ShogiApi.WhichPieceName.King),
|
||||
"k" => new FSharpOption<ShogiApi.WhichPieceName>(ShogiApi.WhichPieceName.Knight),
|
||||
"L" => new FSharpOption<ShogiApi.WhichPieceName>(ShogiApi.WhichPieceName.Lance),
|
||||
"P" => new FSharpOption<ShogiApi.WhichPieceName>(ShogiApi.WhichPieceName.Pawn),
|
||||
"R" => new FSharpOption<ShogiApi.WhichPieceName>(ShogiApi.WhichPieceName.Rook),
|
||||
"S" => new FSharpOption<ShogiApi.WhichPieceName>(ShogiApi.WhichPieceName.SilverGeneral),
|
||||
_ => null
|
||||
};
|
||||
var target = new GameboardTypes.Move
|
||||
var target = new ShogiApi.Move
|
||||
{
|
||||
Origin = new GameboardTypes.BoardLocation { X = from.X, Y = from.Y },
|
||||
Destination = new GameboardTypes.BoardLocation { X = to.X, Y = to.Y },
|
||||
Origin = new ShogiApi.BoardLocation { X = from.X, Y = from.Y },
|
||||
Destination = new ShogiApi.BoardLocation { X = to.X, Y = to.Y },
|
||||
IsPromotion = source.IsPromotion,
|
||||
PieceFromCaptured = pieceFromCaptured
|
||||
};
|
||||
return target;
|
||||
}
|
||||
|
||||
public static Move Map(GameboardTypes.Move source)
|
||||
public static Move Map(ShogiApi.Move source)
|
||||
{
|
||||
var origin = source.Origin;
|
||||
var destination = source.Destination;
|
||||
@@ -41,14 +41,14 @@ namespace Gameboard.ShogiUI.Sockets.Managers.Utility
|
||||
{
|
||||
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",
|
||||
ShogiApi.WhichPieceName.Bishop => "B",
|
||||
ShogiApi.WhichPieceName.GoldenGeneral => "G",
|
||||
ShogiApi.WhichPieceName.King => "K",
|
||||
ShogiApi.WhichPieceName.Knight => "k",
|
||||
ShogiApi.WhichPieceName.Lance => "L",
|
||||
ShogiApi.WhichPieceName.Pawn => "P",
|
||||
ShogiApi.WhichPieceName.Rook => "R",
|
||||
ShogiApi.WhichPieceName.SilverGeneral => "S",
|
||||
_ => ""
|
||||
};
|
||||
}
|
||||
|
||||
68
Gameboard.ShogiUI.Sockets/Models/Session.cs
Normal file
68
Gameboard.ShogiUI.Sockets/Models/Session.cs
Normal 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()
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
using Gameboard.Shogi.Api.ServiceModels.Messages;
|
||||
using Gameboard.ShogiUI.Sockets.Repositories.Utility;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Gameboard.ShogiUI.Sockets.Repositories.Utility;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Repositories
|
||||
{
|
||||
@@ -17,7 +17,7 @@ namespace Gameboard.ShogiUI.Sockets.Repositories
|
||||
Task<GetMovesResponse> GetMoves(string gameName);
|
||||
Task<PostSessionResponse> PostSession(PostSession 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<PostJoinCodeResponse> PostJoinCode(string gameName, string userName);
|
||||
Task<GetPlayerResponse> GetPlayer(string userName);
|
||||
@@ -26,6 +26,11 @@ namespace Gameboard.ShogiUI.Sockets.Repositories
|
||||
|
||||
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;
|
||||
public GameboardRepository(IAuthenticatedHttpClient client)
|
||||
{
|
||||
@@ -34,7 +39,7 @@ namespace Gameboard.ShogiUI.Sockets.Repositories
|
||||
|
||||
public async Task<GetSessionsResponse> GetGames()
|
||||
{
|
||||
var response = await client.GetAsync("Sessions");
|
||||
var response = await client.GetAsync(GetSessionsRoute);
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
return JsonConvert.DeserializeObject<GetSessionsResponse>(json);
|
||||
}
|
||||
@@ -63,25 +68,24 @@ namespace Gameboard.ShogiUI.Sockets.Repositories
|
||||
|
||||
public async Task<PostSessionResponse> PostSession(PostSession request)
|
||||
{
|
||||
var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json");
|
||||
var response = await client.PostAsync("Session", content);
|
||||
var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, MediaType);
|
||||
var response = await client.PostAsync(PostSessionRoute, content);
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
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, "application/json");
|
||||
var response = await client.PostAsync(Uri.EscapeUriString(uri), content);
|
||||
var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, MediaType);
|
||||
var response = await client.PutAsync(JoinSessionRoute, content);
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
return JsonConvert.DeserializeObject<PutJoinPublicSessionResponse>(json);
|
||||
}
|
||||
|
||||
public async Task<PostJoinPrivateSessionResponse> PostJoinPrivateSession(PostJoinPrivateSession request)
|
||||
{
|
||||
var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json");
|
||||
var response = await client.PostAsync("Session/Join", content);
|
||||
var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, MediaType);
|
||||
var response = await client.PostAsync(JoinSessionRoute, content);
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
return JsonConvert.DeserializeObject<PostJoinPrivateSessionResponse>(json);
|
||||
}
|
||||
@@ -97,7 +101,7 @@ namespace Gameboard.ShogiUI.Sockets.Repositories
|
||||
public async Task PostMove(string gameName, PostMove request)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -105,7 +109,7 @@ namespace Gameboard.ShogiUI.Sockets.Repositories
|
||||
{
|
||||
var uri = $"JoinCode/{gameName}";
|
||||
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();
|
||||
return JsonConvert.DeserializeObject<PostJoinCodeResponse>(json);
|
||||
}
|
||||
@@ -120,8 +124,8 @@ namespace Gameboard.ShogiUI.Sockets.Repositories
|
||||
|
||||
public async Task<HttpResponseMessage> PostPlayer(PostPlayer request)
|
||||
{
|
||||
var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json");
|
||||
return await client.PostAsync("Player", content);
|
||||
var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, MediaType);
|
||||
return await client.PostAsync(PlayerRoute, content);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ namespace Gameboard.ShogiUI.Sockets.Repositories.Utility
|
||||
Task<HttpResponseMessage> DeleteAsync(string requestUri);
|
||||
Task<HttpResponseMessage> GetAsync(string requestUri);
|
||||
Task<HttpResponseMessage> PostAsync(string requestUri, HttpContent content);
|
||||
Task<HttpResponseMessage> PutAsync(string requestUri, HttpContent content);
|
||||
}
|
||||
|
||||
public class AuthenticatedHttpClient : HttpClient, IAuthenticatedHttpClient
|
||||
@@ -89,6 +90,23 @@ namespace Gameboard.ShogiUI.Sockets.Repositories.Utility
|
||||
await response.Content.ReadAsStringAsync());
|
||||
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)
|
||||
{
|
||||
var response = await base.DeleteAsync(requestUri);
|
||||
|
||||
Reference in New Issue
Block a user