mapper class and delete old stuff

This commit is contained in:
2022-06-12 21:45:46 -05:00
parent 4ca0b63564
commit ab8d0c4c7c
39 changed files with 325 additions and 818 deletions

View File

@@ -21,15 +21,18 @@ namespace Gameboard.ShogiUI.Sockets.Controllers
private readonly IGameboardManager gameboardManager;
private readonly IGameboardRepository gameboardRepository;
private readonly ISocketConnectionManager communicationManager;
private readonly IModelMapper mapper;
public GameController(
IGameboardRepository repository,
IGameboardManager manager,
ISocketConnectionManager communicationManager)
ISocketConnectionManager communicationManager,
IModelMapper mapper)
{
gameboardManager = manager;
gameboardRepository = repository;
this.communicationManager = communicationManager;
this.mapper = mapper;
}
[HttpPost("JoinCode")]
@@ -75,32 +78,35 @@ namespace Gameboard.ShogiUI.Sockets.Controllers
{
return NotFound();
}
if (user == null || (session.Player1.Id != user.Id && session.Player2?.Id != user.Id))
if (user == null || (session.Player1Name != user.Id && session.Player2Name != user.Id))
{
return Forbid("User is not seated at this game.");
}
var move = request.Move;
var moveModel = move.PieceFromCaptured.HasValue
? new Models.Move(move.PieceFromCaptured.Value, move.To, move.IsPromotion)
: new Models.Move(move.From!, move.To, move.IsPromotion);
var moveSuccess = session.Shogi.Move(moveModel);
if (moveSuccess)
try
{
var createSuccess = await gameboardRepository.CreateBoardState(session);
if (!createSuccess)
{
throw new ApplicationException("Unable to persist board state.");
}
await communicationManager.BroadcastToPlayers(new MoveResponse
{
GameName = session.Name,
PlayerName = user.Id
}, session.Player1.Id, session.Player2?.Id);
var move = request.Move;
if (move.PieceFromCaptured.HasValue)
session.Move(mapper.Map(move.PieceFromCaptured.Value), move.To);
else if (!string.IsNullOrWhiteSpace(move.From))
session.Move(move.From, move.To, move.IsPromotion);
await gameboardRepository.CreateBoardState(session);
await communicationManager.BroadcastToPlayers(
new MoveResponse
{
GameName = session.Name,
PlayerName = user.Id
},
session.Player1Name,
session.Player2Name);
return Ok();
}
return Conflict("Illegal move.");
catch (InvalidOperationException ex)
{
return Conflict(ex.Message);
}
}
// TODO: Use JWT tokens for guests so they can authenticate and use API routes, too.
@@ -128,28 +134,15 @@ namespace Gameboard.ShogiUI.Sockets.Controllers
public async Task<IActionResult> PostSession([FromBody] PostSession request)
{
var user = await ReadUserOrThrow();
var session = new Models.SessionMetadata(request.Name, request.IsPrivate, user!);
var success = await gameboardRepository.CreateSession(session);
if (success)
var session = new Shogi.Domain.SessionMetadata(request.Name, request.IsPrivate, user.Id);
await gameboardRepository.CreateSession(session);
await communicationManager.BroadcastToAll(new CreateGameResponse
{
try
{
Game = mapper.Map(session),
PlayerName = user.Id
});
await communicationManager.BroadcastToAll(new CreateGameResponse
{
Game = session.ToServiceModel(),
PlayerName = user.Id
});
}
catch (Exception e)
{
Console.Error.WriteLine("Error broadcasting during PostSession");
}
return Ok();
}
return Conflict();
return Ok();
}
@@ -174,40 +167,16 @@ namespace Gameboard.ShogiUI.Sockets.Controllers
{
BoardState = new BoardState
{
Board = null,
Player1Hand = session.Player1Hand.Select(MapPiece).ToList(),
Player2Hand = session.Player2Hand.Select(MapPiece).ToList(),
PlayerInCheck = session.InCheck.HasValue ? Map(session.InCheck.Value) : null
Board = mapper.Map(session.BoardState),
Player1Hand = session.Player1Hand.Select(mapper.Map).ToList(),
Player2Hand = session.Player2Hand.Select(mapper.Map).ToList(),
PlayerInCheck = mapper.Map(session.InCheck)
},
GameName = session.Name,
Player1 = session.Player1Name,
Player2 = session.Player2Name
};
return this.Ok(response);
static WhichPlayer Map(Shogi.Domain.WhichPlayer whichPlayer)
{
return whichPlayer == Shogi.Domain.WhichPlayer.Player1
? WhichPlayer.Player1
: WhichPlayer.Player2;
}
static Piece MapPiece(Shogi.Domain.Pieces.Piece piece)
{
var owner = Map(piece.Owner);
var whichPiece = piece.WhichPiece switch
{
Shogi.Domain.WhichPiece.King => WhichPiece.King,
Shogi.Domain.WhichPiece.GoldGeneral => WhichPiece.GoldGeneral,
Shogi.Domain.WhichPiece.SilverGeneral => WhichPiece.SilverGeneral,
Shogi.Domain.WhichPiece.Bishop => WhichPiece.Bishop,
Shogi.Domain.WhichPiece.Rook => WhichPiece.Rook,
Shogi.Domain.WhichPiece.Knight => WhichPiece.Knight,
Shogi.Domain.WhichPiece.Lance => WhichPiece.Lance,
Shogi.Domain.WhichPiece.Pawn => WhichPiece.Pawn,
_ => throw new ArgumentException($"Unknown value for {nameof(WhichPiece)}")
};
return new Piece { IsPromoted = piece.IsPromoted, Owner = owner, WhichPiece = whichPiece };
}
return Ok(response);
}
[HttpGet]
@@ -217,18 +186,18 @@ namespace Gameboard.ShogiUI.Sockets.Controllers
var sessions = await gameboardRepository.ReadSessionMetadatas();
var sessionsJoinedByUser = sessions
.Where(s => s.IsSeated(user))
.Select(s => s.ToServiceModel())
.Where(s => s.IsSeated(user.Id))
.Select(s => mapper.Map(s))
.ToList();
var sessionsNotJoinedByUser = sessions
.Where(s => !s.IsSeated(user))
.Select(s => s.ToServiceModel())
.Where(s => !s.IsSeated(user.Id))
.Select(s => mapper.Map(s))
.ToList();
return new GetSessionsResponse
{
PlayerHasJoinedSessions = new Collection<Session>(sessionsJoinedByUser),
AllOtherSessions = new Collection<Session>(sessionsNotJoinedByUser)
PlayerHasJoinedSessions = sessionsJoinedByUser,
AllOtherSessions = sessionsNotJoinedByUser
};
}
@@ -246,13 +215,12 @@ namespace Gameboard.ShogiUI.Sockets.Controllers
return this.Conflict("This session already has two seated players and is full.");
}
session.SetPlayer2(user);
var success = await gameboardRepository.UpdateSession(session);
if (!success) return this.Problem(detail: "Unable to update session.");
session.SetPlayer2(user.Id);
await gameboardRepository.UpdateSession(session);
var opponentName = user.Id == session.Player1.Id
? session.Player2!.Id
: session.Player1.Id;
var opponentName = user.Id == session.Player1
? session.Player2!
: session.Player1;
await communicationManager.BroadcastToPlayers(new JoinGameResponse
{
GameName = session.Name,

View File

@@ -51,13 +51,13 @@ namespace Gameboard.ShogiUI.Sockets.Controllers
public async Task<IActionResult> GuestLogout()
{
var signoutTask = HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
var userId = User?.UserId();
if (!string.IsNullOrEmpty(userId))
{
connectionManager.Unsubscribe(userId);
}
await signoutTask;
return Ok();
}
@@ -68,10 +68,8 @@ namespace Gameboard.ShogiUI.Sockets.Controllers
var user = await gameboardManager.ReadUser(User);
if (user == null)
{
if (await gameboardManager.CreateUser(User))
{
user = await gameboardManager.ReadUser(User);
}
await gameboardManager.CreateUser(User);
user = await gameboardManager.ReadUser(User);
}
if (user == null)
@@ -92,11 +90,7 @@ namespace Gameboard.ShogiUI.Sockets.Controllers
{
// Create a guest user.
var newUser = Models.User.CreateGuestUser(Guid.NewGuid().ToString());
var success = await gameboardRepository.CreateUser(newUser);
if (!success)
{
return Conflict();
}
await gameboardRepository.CreateUser(newUser);
var identity = newUser.CreateClaimsIdentity();
await HttpContext.SignInAsync(