Fixed accidentally building the board from player2 perspective.

This commit is contained in:
2021-04-06 19:52:02 -05:00
parent 2d5c6b20b9
commit 05a9c71499
45 changed files with 441 additions and 276 deletions

View File

@@ -1,4 +1,4 @@
using Gameboard.ShogiUI.BoardState;
using Gameboard.ShogiUI.Rules;
using System.Collections.Concurrent;
namespace Gameboard.ShogiUI.Sockets.Managers
@@ -26,10 +26,5 @@ namespace Gameboard.ShogiUI.Sockets.Managers
return board;
return null;
}
public string GetBoardState()
{
return string.Empty;
}
}
}

View File

@@ -12,16 +12,13 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
// It can be an API route and still tell socket connections about the new session.
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;
}
@@ -29,7 +26,7 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
public async Task Handle(string json, string userName)
{
var request = JsonConvert.DeserializeObject<CreateGameRequest>(json);
var postSessionResponse = await repository.PostSession(new PostSession
var sessionName = await repository.PostSession(new PostSession
{
SessionName = request.GameName,
PlayerName = userName,
@@ -41,12 +38,12 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
PlayerName = userName,
Game = new Game
{
GameName = postSessionResponse.SessionName,
GameName = sessionName,
Players = new[] { userName }
}
};
if (string.IsNullOrWhiteSpace(postSessionResponse.SessionName))
if (string.IsNullOrWhiteSpace(sessionName))
{
response.Error = "Game already exists.";
}

View File

@@ -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.Threading.Tasks;
@@ -10,16 +9,13 @@ namespace Gameboard.ShogiUI.Sockets.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;
}
@@ -27,38 +23,38 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
public async Task Handle(string json, string userName)
{
var request = JsonConvert.DeserializeObject<JoinByCode>(json);
var joinGameResponse = await repository.PostJoinPrivateSession(new PostJoinPrivateSession
var sessionName = await repository.PostJoinPrivateSession(new PostJoinPrivateSession
{
PlayerName = userName,
JoinCode = request.JoinCode
});
if (joinGameResponse.JoinSucceeded)
if (sessionName == null)
{
// Other members of the game see a regular JoinGame occur.
var response = new JoinGameResponse(ClientAction.JoinGame)
var response = new JoinGameResponse(ClientAction.JoinByCode)
{
PlayerName = userName,
GameName = joinGameResponse.SessionName
};
// At this time, userName hasn't subscribed and won't receive this message.
await communicationManager.BroadcastToGame(joinGameResponse.SessionName, response);
// The player joining sees the JoinByCode occur.
response = new JoinGameResponse(ClientAction.JoinByCode)
{
PlayerName = userName,
GameName = joinGameResponse.SessionName
GameName = sessionName,
Error = "Error joining game."
};
await communicationManager.BroadcastToPlayers(response, userName);
}
else
{
var response = new JoinGameResponse(ClientAction.JoinByCode)
// Other members of the game see a regular JoinGame occur.
var response = new JoinGameResponse(ClientAction.JoinGame)
{
PlayerName = userName,
GameName = joinGameResponse.SessionName,
Error = "Error joining game."
GameName = sessionName
};
// At this time, userName hasn't subscribed and won't receive this message.
await communicationManager.BroadcastToGame(sessionName, response);
// The player joining sees the JoinByCode occur.
response = new JoinGameResponse(ClientAction.JoinByCode)
{
PlayerName = userName,
GameName = sessionName
};
await communicationManager.BroadcastToPlayers(response, userName);
}

View File

@@ -23,7 +23,7 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
{
var request = JsonConvert.DeserializeObject<JoinGameRequest>(json);
var joinGameResponse = await gameboardRepository.PutJoinPublicSession(new PutJoinPublicSession
var joinSucceeded = await gameboardRepository.PutJoinPublicSession(new PutJoinPublicSession
{
PlayerName = userName,
SessionName = request.GameName
@@ -34,7 +34,7 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
PlayerName = userName,
GameName = request.GameName
};
if (joinGameResponse.JoinSucceeded)
if (joinSucceeded)
{
await communicationManager.BroadcastToAll(response);
}

View File

@@ -1,4 +1,4 @@
using Gameboard.ShogiUI.BoardState;
using Gameboard.ShogiUI.Rules;
using Gameboard.ShogiUI.Sockets.Repositories;
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Messages;
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
@@ -37,9 +37,8 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
var gameTask = gameboardRepository.GetGame(request.GameName);
var moveTask = gameboardRepository.GetMoves(request.GameName);
var getGameResponse = await gameTask;
var getMovesResponse = await moveTask;
if (getGameResponse == null || getMovesResponse == null)
var sessionModel = await gameTask;
if (sessionModel == null)
{
logger.LogWarning("{action} - {user} was unable to load session named {session}.", ClientAction.LoadGame, userName, request.GameName);
var response = new LoadGameResponse(ClientAction.LoadGame) { Error = "Game not found." };
@@ -47,17 +46,17 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
}
else
{
var sessionModel = new Models.Session(getGameResponse.Session);
var moveModels = getMovesResponse.Moves.Select(_ => new Models.Move(_)).ToList();
var moveModels = await moveTask;
communicationManager.SubscribeToGame(sessionModel, userName);
var boardMoves = moveModels.Select(_ => _.ToBoardModel()).ToList();
boardManager.Add(getGameResponse.Session.Name, new ShogiBoard(boardMoves));
var shogiBoard = new ShogiBoard(boardMoves);
boardManager.Add(sessionModel.Name, shogiBoard);
var response = new LoadGameResponse(ClientAction.LoadGame)
{
Game = sessionModel.ToServiceModel(),
Moves = moveModels.Select(_ => _.ToServiceModel()).ToList(),
BoardState = new Models.BoardState(shogiBoard).ToServiceModel()
};
await communicationManager.BroadcastToPlayers(response, userName);
}

View File

@@ -5,6 +5,7 @@ using Newtonsoft.Json;
using System.Threading.Tasks;
using Service = Gameboard.ShogiUI.Sockets.ServiceModels.Socket;
namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
{
public class MoveHandler : IActionHandler
@@ -25,31 +26,40 @@ namespace Gameboard.ShogiUI.Sockets.Managers.ClientActionHandlers
public async Task Handle(string json, string userName)
{
var request = JsonConvert.DeserializeObject<Service.Messages.MoveRequest>(json);
// Basic move validation
if (request.Move.To.Equals(request.Move.From))
{
var error = new Service.Messages.ErrorResponse(Service.Types.ClientAction.Move)
{
Error = "Error: moving piece from tile to the same tile."
};
await communicationManager.BroadcastToPlayers(error, userName);
return;
}
var moveModel = new Move(request.Move);
var board = boardManager.Get(request.GameName);
var boardMove = moveModel.ToBoardModel();
//board.Move()
await gameboardRepository.PostMove(request.GameName, new PostMove(moveModel.ToApiModel()));
var response = new Service.Messages.MoveResponse(Service.Types.ClientAction.Move)
if (board == null)
{
GameName = request.GameName,
PlayerName = userName,
Move = moveModel.ToServiceModel()
};
await communicationManager.BroadcastToGame(request.GameName, response);
// TODO: Find a flow for this
var response = new Service.Messages.MoveResponse(Service.Types.ClientAction.Move)
{
Error = $"Game isn't loaded. Send a message with the {Service.Types.ClientAction.LoadGame} action first."
};
await communicationManager.BroadcastToPlayers(response, userName);
}
var boardMove = moveModel.ToBoardModel();
var moveSuccess = board.Move(boardMove);
if (moveSuccess)
{
await gameboardRepository.PostMove(request.GameName, new PostMove(moveModel.ToApiModel()));
var boardState = new BoardState(board);
var response = new Service.Messages.MoveResponse(Service.Types.ClientAction.Move)
{
GameName = request.GameName,
PlayerName = userName,
BoardState = boardState.ToServiceModel()
};
await communicationManager.BroadcastToGame(request.GameName, response);
}
else
{
var response = new Service.Messages.MoveResponse(Service.Types.ClientAction.Move)
{
Error = "Invalid move."
};
await communicationManager.BroadcastToPlayers(response, userName);
}
}
}
}