All the code

This commit is contained in:
2020-12-13 14:27:36 -06:00
parent 9c3d67a07e
commit 9e6a7bca2c
49 changed files with 1878 additions and 0 deletions

View File

@@ -0,0 +1,67 @@
using AspShogiSockets.Extensions;
using Gameboard.Shogi.Api.ServiceModels.Messages;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Net.WebSockets;
using System.Threading.Tasks;
using Websockets.Repositories;
using Websockets.ServiceModels.Messages;
using Websockets.ServiceModels.Types;
namespace Websockets.Managers.ClientActionHandlers
{
public class CreateGameHandler : IActionHandler
{
private readonly ILogger<CreateGameHandler> logger;
private readonly IGameboardRepository repository;
private readonly ISocketCommunicationManager communicationManager;
public CreateGameHandler(
ILogger<CreateGameHandler> logger,
ISocketCommunicationManager communicationManager,
IGameboardRepository repository)
{
this.logger = logger;
this.repository = repository;
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<CreateGameRequest>(json);
var postGameResponse = await repository.PostGame(new PostGame
{
GameName = request.GameName,
PlayerName = userName, // TODO : Investigate if needed by UI
IsPrivate = request.IsPrivate
});
var response = new CreateGameResponse(request.Action)
{
PlayerName = userName,
Game = new Game
{
GameName = postGameResponse.GameName,
Players = new string[] { userName }
}
};
if (string.IsNullOrWhiteSpace(postGameResponse.GameName))
{
response.Error = "Game already exists.";
}
var serialized = JsonConvert.SerializeObject(response);
logger.LogInformation("Socket Response \n{0}\n", new[] { serialized });
if (request.IsPrivate)
{
await socket.SendTextAsync(serialized);
}
else
{
await communicationManager.BroadcastToAll(serialized);
}
}
}
}

View File

@@ -0,0 +1,16 @@
using System.Net.WebSockets;
using System.Threading.Tasks;
using Websockets.ServiceModels.Types;
namespace Websockets.Managers.ClientActionHandlers
{
public interface IActionHandler
{
/// <summary>
/// Responsible for parsing json and handling the request.
/// </summary>
Task Handle(WebSocket socket, string json, string userName);
}
public delegate IActionHandler ActionHandlerResolver(ClientAction action);
}

View File

@@ -0,0 +1,75 @@
using AspShogiSockets.Extensions;
using Gameboard.Shogi.Api.ServiceModels.Messages;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Net.WebSockets;
using System.Threading.Tasks;
using Websockets.Repositories;
using Websockets.ServiceModels.Messages;
using Websockets.ServiceModels.Types;
namespace Websockets.Managers.ClientActionHandlers
{
public class JoinByCodeHandler : IActionHandler
{
private readonly ILogger<JoinByCodeHandler> logger;
private readonly IGameboardRepository repository;
private readonly ISocketCommunicationManager communicationManager;
public JoinByCodeHandler(
ILogger<JoinByCodeHandler> logger,
ISocketCommunicationManager communicationManager,
IGameboardRepository repository)
{
this.logger = logger;
this.repository = repository;
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<JoinByCode>(json);
var joinGameResponse = await repository.PostJoinByCode(new PostJoinByCode
{
PlayerName = userName,
JoinCode = request.JoinCode
});
if (joinGameResponse.JoinSucceeded)
{
var gameName = (await repository.GetGame(joinGameResponse.GameName)).GameName;
// Other members of the game see a regular JoinGame occur.
var response = new JoinGameResponse(ClientAction.JoinGame)
{
PlayerName = userName,
GameName = gameName
};
var serialized = JsonConvert.SerializeObject(response);
await communicationManager.BroadcastToGame(gameName, serialized);
communicationManager.SubscribeToGame(socket, gameName, userName);
// But the player joining sees the JoinByCode occur.
response = new JoinGameResponse(ClientAction.JoinByCode)
{
PlayerName = userName,
GameName = gameName
};
serialized = JsonConvert.SerializeObject(response);
await socket.SendTextAsync(serialized);
}
else
{
var response = new JoinGameResponse(ClientAction.JoinByCode)
{
PlayerName = userName,
Error = "Error joining game."
};
var serialized = JsonConvert.SerializeObject(response);
logger.LogInformation("Socket Response \n{0}\n", new[] { serialized });
await socket.SendTextAsync(serialized);
}
}
}
}

View File

@@ -0,0 +1,54 @@
using Gameboard.Shogi.Api.ServiceModels.Messages;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Net.WebSockets;
using System.Threading.Tasks;
using Websockets.Repositories;
using Websockets.ServiceModels.Messages;
using Websockets.ServiceModels.Types;
namespace Websockets.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.PostJoinGame(request.GameName, new PostJoinGame
{
PlayerName = userName
});
if (joinGameResponse.JoinSucceeded)
{
response.GameName = request.GameName;
}
else
{
response.Error = "Game is full or code is incorrect.";
}
var serialized = JsonConvert.SerializeObject(response);
logger.LogInformation("Socket Response \n{0}\n", new[] { serialized });
await communicationManager.BroadcastToAll(serialized);
}
}
}

View File

@@ -0,0 +1,51 @@
using AspShogiSockets.Extensions;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Linq;
using System.Net.WebSockets;
using System.Threading.Tasks;
using Websockets.Repositories;
using Websockets.ServiceModels.Messages;
using Websockets.ServiceModels.Types;
namespace Websockets.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()
: await repository.GetGames(userName);
var games = getGamesResponse.Games
.OrderBy(g => g.Players.Contains(userName))
.Select(g => new Game
{
GameName = g.GameName,
Players = g.Players
});
var response = new ListGamesResponse(ClientAction.ListGames)
{
Games = games ?? new Game[0]
};
var serialized = JsonConvert.SerializeObject(response);
logger.LogInformation("Socket Response \n{0}\n", new[] { serialized });
await socket.SendTextAsync(serialized);
}
}
}

View File

@@ -0,0 +1,63 @@
using AspShogiSockets.Extensions;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Linq;
using System.Net.WebSockets;
using System.Threading.Tasks;
using Websockets.Managers.Utility;
using Websockets.Repositories;
using Websockets.ServiceModels.Messages;
using Websockets.ServiceModels.Types;
namespace Websockets.Managers.ClientActionHandlers
{
public class LoadGameHandler : IActionHandler
{
private readonly ILogger<LoadGameHandler> logger;
private readonly IGameboardRepository gameboardRepository;
private readonly ISocketCommunicationManager communicationManager;
public LoadGameHandler(
ILogger<LoadGameHandler> 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", 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);
if (getGameResponse == null || getMovesResponse == null)
{
response.Error = $"Could not find game.";
}
else
{
var player1 = getGameResponse.Players[0];
response.Game = new Game
{
GameName = getGameResponse.GameName,
Players = getGameResponse.Players
};
response.Moves = userName.Equals(player1)
? getMovesResponse.Moves.Select(_ => Mapper.Map(_))
: getMovesResponse.Moves.Select(_ => Move.ConvertPerspective(Mapper.Map(_)));
communicationManager.SubscribeToGame(socket, getGameResponse.GameName, userName);
}
var serialized = JsonConvert.SerializeObject(response);
logger.LogInformation("Socket Response \n{0}\n", serialized);
await socket.SendTextAsync(serialized);
}
}
}

View File

@@ -0,0 +1,77 @@
using AspShogiSockets.Extensions;
using Gameboard.Shogi.Api.ServiceModels.Messages;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Net.WebSockets;
using System.Threading.Tasks;
using Websockets.Managers.Utility;
using Websockets.Repositories;
using Websockets.ServiceModels.Messages;
using Websockets.ServiceModels.Types;
namespace Websockets.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))
{
var serialized = JsonConvert.SerializeObject(
new ErrorResponse(ClientAction.Move)
{
Error = "Error: moving piece from tile to the same tile."
});
await socket.SendTextAsync(serialized);
return;
}
var getGameResponse = await gameboardRepository.GetGame(request.GameName);
var isPlayer1 = userName.Equals(getGameResponse.Players[0]);
if (!isPlayer1)
{
// Convert the move coords to player1 perspective.
move = Move.ConvertPerspective(move);
}
await gameboardRepository.PostMove(
request.GameName,
new PostMove { Move = Mapper.Map(move) });
var response = new MoveResponse(ClientAction.Move)
{
GameName = request.GameName,
PlayerName = userName
};
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;
}
);
}
}
}