More organized communication strategy.
This commit is contained in:
@@ -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) =>
|
||||
{
|
||||
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;
|
||||
}
|
||||
);
|
||||
var responseForPlayer2 = new MoveResponse(ClientAction.Move)
|
||||
{
|
||||
GameName = request.GameName,
|
||||
PlayerName = userName,
|
||||
Move = isPlayer2 ? request.Move : Move.ConvertPerspective(request.Move)
|
||||
};
|
||||
await communicationManager.BroadcastToGame(session.Name, responseForPlayer1, responseForPlayer2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user