This commit is contained in:
2022-11-11 18:42:27 -06:00
parent b89760af8e
commit 79b70d6fa5
13 changed files with 656 additions and 364 deletions

View File

@@ -68,7 +68,7 @@ public class SessionsController : ControllerBase
return this.NoContent();
}
return this.Forbid("Cannot delete sessions created by others.");
return this.StatusCode(StatusCodes.Status403Forbidden, "Cannot delete sessions created by others.");
}
[HttpGet("PlayerCount")]
@@ -108,15 +108,15 @@ public class SessionsController : ControllerBase
};
}
[HttpPatch("{name}/Move")]
public async Task<IActionResult> Move([FromRoute] string name, [FromBody] MovePieceCommand command)
[HttpPatch("{sessionName}/Move")]
public async Task<IActionResult> Move([FromRoute] string sessionName, [FromBody] MovePieceCommand command)
{
var userId = User.GetShogiUserId();
var session = await sessionRepository.ReadSession(name);
var session = await sessionRepository.ReadSession(sessionName);
if (session == null) return this.NotFound("Shogi session does not exist.");
if (!session.IsSeated(userId)) return this.Forbid("Player is not a member of the Shogi session.");
if (!session.IsSeated(userId)) return this.StatusCode(StatusCodes.Status403Forbidden, "Player is not a member of the Shogi session.");
try
{
@@ -126,14 +126,14 @@ public class SessionsController : ControllerBase
}
else
{
session.Board.Move(command.From!, command.To, command.IsPromotion!.Value);
session.Board.Move(command.From!, command.To, command.IsPromotion ?? false);
}
}
catch (InvalidOperationException)
catch (InvalidOperationException e)
{
return this.Conflict("Move is illegal.");
return this.Conflict(e.Message);
}
// TODO: sessionRespository.SaveMove();
await sessionRepository.CreateMove(sessionName, command);
await communicationManager.BroadcastToPlayers(
new PlayerHasMovedMessage
{
@@ -145,73 +145,4 @@ public class SessionsController : ControllerBase
return this.NoContent();
}
//[HttpPost("{sessionName}/Move")]
//public async Task<IActionResult> MovePiece([FromRoute] string sessionName, [FromBody] MovePieceCommand request)
//{
// var user = await gameboardManager.ReadUser(User);
// var session = await gameboardRepository.ReadSession(sessionName);
// if (session == null)
// {
// return NotFound();
// }
// if (user == null || (session.Player1 != user.Id && session.Player2 != user.Id))
// {
// return Forbid("User is not seated at this game.");
// }
// try
// {
// 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
// {
// SessionName = session.Name,
// PlayerName = user.Id
// },
// session.Player1,
// session.Player2);
// return Ok();
// }
// catch (InvalidOperationException ex)
// {
// return Conflict(ex.Message);
// }
//}
//[HttpPut("{sessionName}")]
//public async Task<IActionResult> PutJoinSession([FromRoute] string sessionName)
//{
// var user = await ReadUserOrThrow();
// var session = await gameboardRepository.ReadSessionMetaData(sessionName);
// if (session == null)
// {
// return NotFound();
// }
// if (session.Player2 != null)
// {
// return this.Conflict("This session already has two seated players and is full.");
// }
// session.SetPlayer2(user.Id);
// await gameboardRepository.UpdateSession(session);
// var opponentName = user.Id == session.Player1
// ? session.Player2!
// : session.Player1;
// await communicationManager.BroadcastToPlayers(new JoinSessionResponse
// {
// SessionName = session.Name,
// PlayerName = user.Id
// }, opponentName);
// return Ok();
//}
}

View File

@@ -5,10 +5,11 @@ namespace Shogi.Api.Models;
public class User
{
public static readonly ReadOnlyCollection<string> Adjectives = new(new[] {
"Fortuitous", "Retractable", "Happy", "Habbitable", "Creative", "Fluffy", "Impervious", "Kingly"
"Fortuitous", "Retractable", "Happy", "Habbitable", "Creative", "Fluffy", "Impervious", "Kingly", "Queenly", "Blushing", "Brave",
"Brainy", "Eager", "Itchy", "Fierce"
});
public static readonly ReadOnlyCollection<string> Subjects = new(new[] {
"Hippo", "Basil", "Mouse", "Walnut", "Prince", "Lima Bean", "Coala", "Potato", "Penguin"
"Hippo", "Basil", "Mouse", "Walnut", "Minstrel", "Lima Bean", "Koala", "Potato", "Penguin", "Cola", "Banana", "Egg", "Fish", "Yak"
});
public static User CreateMsalUser(string id) => new(id, id, WhichLoginPlatform.Microsoft);
public static User CreateGuestUser(string id)

View File

@@ -1,6 +1,6 @@
using Dapper;
using Shogi.Api.Extensions;
using Shogi.Api.Repositories.Dto;
using Shogi.Contracts.Api;
using Shogi.Domain;
using System.Data;
using System.Data.SqlClient;
@@ -74,9 +74,11 @@ public class SessionRepository : ISessionRepository
"session.CreateMove",
new
{
To = command.To,
From = command.From,
IsPromotion = command.IsPromotion
command.To,
command.From,
command.IsPromotion,
command.PieceFromHand,
SessionName = sessionName
},
commandType: CommandType.StoredProcedure);
}
@@ -84,6 +86,7 @@ public class SessionRepository : ISessionRepository
public interface ISessionRepository
{
Task CreateMove(string sessionName, MovePieceCommand command);
Task CreateSession(Session session);
Task DeleteSession(string name);
Task<Session?> ReadSession(string name);