create, read, playercount

This commit is contained in:
2022-11-09 16:08:04 -06:00
parent a1f996e508
commit da76917490
37 changed files with 999 additions and 814 deletions

View File

@@ -1,32 +1,32 @@
using Shogi.Api.Managers; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Shogi.Api.Extensions;
using Shogi.Api.Managers;
using Shogi.Api.Repositories; using Shogi.Api.Repositories;
using Shogi.Contracts.Api; using Shogi.Contracts.Api;
using Shogi.Contracts.Socket; using Shogi.Contracts.Socket;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Data.SqlClient;
using Shogi.Contracts.Types; using Shogi.Contracts.Types;
using Shogi.Api.Extensions; using System.Data.SqlClient;
namespace Shogi.Api.Controllers; namespace Shogi.Api.Controllers;
[ApiController] [ApiController]
[Route("[controller]")] [Route("[controller]")]
[Authorize] [Authorize]
public class SessionController : ControllerBase public class SessionsController : ControllerBase
{ {
private readonly ISocketConnectionManager communicationManager; private readonly ISocketConnectionManager communicationManager;
private readonly IModelMapper mapper; private readonly IModelMapper mapper;
private readonly ISessionRepository sessionRepository; private readonly ISessionRepository sessionRepository;
private readonly IQueryRespository queryRespository; private readonly IQueryRespository queryRespository;
private readonly ILogger<SessionController> logger; private readonly ILogger<SessionsController> logger;
public SessionController( public SessionsController(
ISocketConnectionManager communicationManager, ISocketConnectionManager communicationManager,
IModelMapper mapper, IModelMapper mapper,
ISessionRepository sessionRepository, ISessionRepository sessionRepository,
IQueryRespository queryRespository, IQueryRespository queryRespository,
ILogger<SessionController> logger) ILogger<SessionsController> logger)
{ {
this.communicationManager = communicationManager; this.communicationManager = communicationManager;
this.mapper = mapper; this.mapper = mapper;
@@ -39,14 +39,10 @@ public class SessionController : ControllerBase
public async Task<IActionResult> CreateSession([FromBody] CreateSessionCommand request) public async Task<IActionResult> CreateSession([FromBody] CreateSessionCommand request)
{ {
var userId = User.GetShogiUserId(); var userId = User.GetShogiUserId();
if (string.IsNullOrWhiteSpace(userId)) return this.Unauthorized(); var session = new Domain.Aggregates.Session(request.Name, userId);
var session = new Domain.Aggregates.Session(
request.Name,
userId,
new Domain.ValueObjects.ShogiBoard(Domain.BoardState.StandardStarting));
try try
{ {
await sessionRepository.CreateShogiBoard(board, request.Name, userId); await sessionRepository.CreateSession(session);
} }
catch (SqlException e) catch (SqlException e)
{ {
@@ -58,6 +54,23 @@ public class SessionController : ControllerBase
return CreatedAtAction(nameof(CreateSession), new { sessionName = request.Name }, null); return CreatedAtAction(nameof(CreateSession), new { sessionName = request.Name }, null);
} }
[HttpDelete("{name}")]
public async Task<IActionResult> DeleteSession(string name)
{
var userId = User.GetShogiUserId();
var session = await sessionRepository.ReadSession(name);
if (session == null) return this.NoContent();
if (session.Player1 == userId)
{
await sessionRepository.DeleteSession(name);
return this.NoContent();
}
return this.Unauthorized("Cannot delete sessions created by others.");
}
//[HttpPost("{sessionName}/Move")] //[HttpPost("{sessionName}/Move")]
//public async Task<IActionResult> MovePiece([FromRoute] string sessionName, [FromBody] MovePieceCommand request) //public async Task<IActionResult> MovePiece([FromRoute] string sessionName, [FromBody] MovePieceCommand request)
//{ //{
@@ -156,12 +169,12 @@ public class SessionController : ControllerBase
// return Ok(response); // return Ok(response);
//} //}
[HttpGet] [HttpGet("PlayerCount")]
public async Task<ActionResult<ReadAllSessionsResponse>> GetSessions() public async Task<ActionResult<ReadSessionsPlayerCountResponse>> GetSessionsPlayerCount()
{ {
var sessions = await this.queryRespository.ReadAllSessionsMetadata(); var sessions = await this.queryRespository.ReadSessionPlayerCount();
return Ok(new ReadAllSessionsResponse return Ok(new ReadSessionsPlayerCountResponse
{ {
PlayerHasJoinedSessions = Array.Empty<SessionMetadata>(), PlayerHasJoinedSessions = Array.Empty<SessionMetadata>(),
AllOtherSessions = sessions.ToList() AllOtherSessions = sessions.ToList()
@@ -171,8 +184,26 @@ public class SessionController : ControllerBase
[HttpGet("{name}")] [HttpGet("{name}")]
public async Task<ActionResult<ReadSessionResponse>> GetSession(string name) public async Task<ActionResult<ReadSessionResponse>> GetSession(string name)
{ {
await Task.CompletedTask; var session = await sessionRepository.ReadSession(name);
return new ReadSessionResponse(); if (session == null) return this.NotFound();
return new ReadSessionResponse
{
Session = new Session
{
BoardState = new BoardState
{
Board = session.Board.BoardState.State.ToContract(),
Player1Hand = session.Board.BoardState.Player1Hand.ToContract(),
Player2Hand = session.Board.BoardState.Player2Hand.ToContract(),
PlayerInCheck = session.Board.BoardState.InCheck?.ToContract(),
WhoseTurn = session.Board.BoardState.WhoseTurn.ToContract()
},
Player1 = session.Player1,
Player2 = session.Player2,
SessionName = session.Name
}
};
} }
//[HttpPut("{sessionName}")] //[HttpPut("{sessionName}")]

View File

@@ -1,14 +1,11 @@
using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Shogi.Contracts.Api;
using Shogi.Api.Extensions; using Shogi.Api.Extensions;
using Shogi.Api.Managers; using Shogi.Api.Managers;
using Shogi.Api.Models;
using Shogi.Api.Repositories; using Shogi.Api.Repositories;
using System.Security.Claims; using Shogi.Contracts.Api;
namespace Shogi.Api.Controllers; namespace Shogi.Api.Controllers;

View File

@@ -2,7 +2,7 @@
namespace Shogi.Api.Extensions; namespace Shogi.Api.Extensions;
public static class Extensions public static class ClaimsExtensions
{ {
private static readonly string MsalUsernameClaim = "preferred_username"; private static readonly string MsalUsernameClaim = "preferred_username";
@@ -26,5 +26,17 @@ public static class Extensions
return self.Claims.FirstOrDefault(c => c.Type == MsalUsernameClaim)?.Value; return self.Claims.FirstOrDefault(c => c.Type == MsalUsernameClaim)?.Value;
} }
public static string? GetShogiUserId(this ClaimsPrincipal self) => self.IsMicrosoft() ? self.GetMicrosoftUserId() : self.GetGuestUserId(); /// <summary>
} /// Reads the userId from claims after claims transformation has occurred.
/// Throws if a shogi userid is not found.
/// </summary>
/// <exception cref="InvalidOperationException"></exception>
public static string GetShogiUserId(this ClaimsPrincipal self)
{
var id = self.IsMicrosoft() ? self.GetMicrosoftUserId() : self.GetGuestUserId();
if (string.IsNullOrEmpty(id)) throw new InvalidOperationException("Shogi UserId not found in claims.");
return id;
}
}

View File

@@ -0,0 +1,52 @@
using Shogi.Contracts.Types;
using System.Collections.ObjectModel;
using System.Linq;
namespace Shogi.Api.Extensions;
public static class ContractsExtensions
{
public static WhichPlayer ToContract(this Domain.WhichPlayer player)
{
return player switch
{
Domain.WhichPlayer.Player1 => WhichPlayer.Player1,
Domain.WhichPlayer.Player2 => WhichPlayer.Player2,
_ => throw new NotImplementedException(),
};
}
public static WhichPiece ToContract(this Domain.WhichPiece piece)
{
return piece switch
{
Domain.WhichPiece.King => WhichPiece.King,
Domain.WhichPiece.GoldGeneral => WhichPiece.GoldGeneral,
Domain.WhichPiece.SilverGeneral => WhichPiece.SilverGeneral,
Domain.WhichPiece.Bishop => WhichPiece.Bishop,
Domain.WhichPiece.Rook => WhichPiece.Rook,
Domain.WhichPiece.Knight => WhichPiece.Knight,
Domain.WhichPiece.Lance => WhichPiece.Lance,
Domain.WhichPiece.Pawn => WhichPiece.Pawn,
_ => throw new NotImplementedException(),
};
}
public static Piece ToContract(this Domain.ValueObjects.Piece piece) => new()
{
IsPromoted = piece.IsPromoted,
Owner = piece.Owner.ToContract(),
WhichPiece = piece.WhichPiece.ToContract()
};
public static IReadOnlyList<Piece> ToContract(this List<Domain.ValueObjects.Piece> pieces)
{
return pieces
.Select(ToContract)
.ToList()
.AsReadOnly();
}
public static Dictionary<string, Piece?> ToContract(this ReadOnlyDictionary<string, Domain.ValueObjects.Piece?> boardState) =>
boardState.ToDictionary(kvp => kvp.Key, kvp => kvp.Value?.ToContract());
}

View File

@@ -2,12 +2,11 @@ using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Http.Extensions; using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Http.Json;
using Microsoft.AspNetCore.HttpLogging; using Microsoft.AspNetCore.HttpLogging;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Identity.Web; using Microsoft.Identity.Web;
using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using Shogi.Api.Managers; using Shogi.Api.Managers;
using Shogi.Api.Repositories; using Shogi.Api.Repositories;
using Shogi.Api.Services; using Shogi.Api.Services;
@@ -38,7 +37,7 @@ namespace Shogi.Api
}); });
ConfigureAuthentication(builder); ConfigureAuthentication(builder);
ConfigureControllersWithNewtonsoft(builder); ConfigureControllers(builder);
ConfigureSwagger(builder); ConfigureSwagger(builder);
ConfigureDependencyInjection(builder); ConfigureDependencyInjection(builder);
ConfigureLogging(builder); ConfigureLogging(builder);
@@ -165,39 +164,13 @@ namespace Shogi.Api
} }
} }
private static void ConfigureControllersWithNewtonsoft(WebApplicationBuilder builder) private static void ConfigureControllers(WebApplicationBuilder builder)
{ {
builder.Services builder.Services.AddControllers();
.AddControllers() builder.Services.Configure<JsonOptions>(options =>
//.AddJsonOptions(options =>
//{
// options.AllowInputFormatterExceptionMessages = true;
// options.JsonSerializerOptions.WriteIndented = true;
//});
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.Formatting = Formatting.Indented;
options.SerializerSettings.ContractResolver = new DefaultContractResolver
{
NamingStrategy = new CamelCaseNamingStrategy { ProcessDictionaryKeys = false }
};
options.SerializerSettings.Converters = new[] { new StringEnumConverter() };
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
});
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{ {
Formatting = Formatting.Indented, options.SerializerOptions.WriteIndented = true;
ContractResolver = new DefaultContractResolver });
{
NamingStrategy = new CamelCaseNamingStrategy
{
ProcessDictionaryKeys = false
}
},
Converters = new[] { new StringEnumConverter() },
NullValueHandling = NullValueHandling.Ignore,
};
} }
private static void ConfigureDependencyInjection(WebApplicationBuilder builder) private static void ConfigureDependencyInjection(WebApplicationBuilder builder)

View File

@@ -1,22 +0,0 @@
using Shogi.Domain;
using Shogi.Domain.ValueObjects;
using System.Collections.ObjectModel;
namespace Shogi.Api.Repositories.Dto;
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
public class BoardStateDto
{
public ReadOnlyDictionary<string, Piece?> State { get; set; }
public List<Piece> Player1Hand { get; set; }
public List<Piece> Player2Hand { get; set; }
public Move PreviousMove { get; }
public WhichPlayer WhoseTurn { get; set; }
public WhichPlayer? InCheck { get; set; }
public bool IsCheckmate { get; set; }
}
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.

View File

@@ -0,0 +1,10 @@
using Shogi.Domain;
namespace Shogi.Api.Repositories.Dto;
/// <summary>
/// Useful with Dapper to read from database.
/// </summary>
public readonly record struct MoveDto(string From, string To, bool IsPromotion, WhichPiece? PieceFromHand)
{
}

View File

@@ -1,14 +1,5 @@
namespace Shogi.Api.Repositories.Dto; namespace Shogi.Api.Repositories.Dto;
/// <summary> public readonly record struct SessionDto(string Name, string Player1, string Player2)
/// Useful with Dapper to read from database.
/// </summary>
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
public class SessionDto
{ {
public string Name { get; set; }
public string Player1 { get; set; }
public string Player2 { get; set; }
public BoardStateDto BoardState { get; set; }
} }
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.

View File

@@ -1,5 +1,4 @@
using Dapper; using Dapper;
using Shogi.Api.Repositories.Dto;
using Shogi.Contracts.Types; using Shogi.Contracts.Types;
using System.Data.SqlClient; using System.Data.SqlClient;
@@ -14,16 +13,16 @@ public class QueryRepository : IQueryRespository
connectionString = configuration.GetConnectionString("ShogiDatabase"); connectionString = configuration.GetConnectionString("ShogiDatabase");
} }
public async Task<IEnumerable<SessionMetadata>> ReadAllSessionsMetadata() public async Task<IEnumerable<SessionMetadata>> ReadSessionPlayerCount()
{ {
using var connection = new SqlConnection(connectionString); using var connection = new SqlConnection(connectionString);
return await connection.QueryAsync<SessionMetadata>( return await connection.QueryAsync<SessionMetadata>(
"session.ReadAllSessionsMetadata", "session.ReadSessionPlayerCount",
commandType: System.Data.CommandType.StoredProcedure); commandType: System.Data.CommandType.StoredProcedure);
} }
} }
public interface IQueryRespository public interface IQueryRespository
{ {
Task<IEnumerable<SessionMetadata>> ReadAllSessionsMetadata(); Task<IEnumerable<SessionMetadata>> ReadSessionPlayerCount();
} }

View File

@@ -1,11 +1,8 @@
using Dapper; using Dapper;
using Shogi.Api.Repositories.Dto; using Shogi.Api.Repositories.Dto;
using Shogi.Domain;
using Shogi.Domain.Aggregates; using Shogi.Domain.Aggregates;
using Shogi.Domain.ValueObjects;
using System.Data; using System.Data;
using System.Data.SqlClient; using System.Data.SqlClient;
using System.Text.Json;
namespace Shogi.Api.Repositories; namespace Shogi.Api.Repositories;
@@ -20,45 +17,52 @@ public class SessionRepository : ISessionRepository
public async Task CreateSession(Session session) public async Task CreateSession(Session session)
{ {
var boardStateDto = new BoardStateDto
{
InCheck = session.BoardState.InCheck,
IsCheckmate = session.BoardState.IsCheckmate,
Player1Hand = session.BoardState.Player1Hand,
Player2Hand = session.BoardState.Player2Hand,
State = session.BoardState.State,
WhoseTurn = session.BoardState.WhoseTurn,
};
using var connection = new SqlConnection(connectionString); using var connection = new SqlConnection(connectionString);
await connection.ExecuteAsync( await connection.ExecuteAsync(
"session.CreateSession", "session.CreateSession",
new new
{ {
Name = sessionName, session.Name,
InitialBoardStateDocument = JsonSerializer.Serialize(boardStateDto), Player1Name = session.Player1,
Player1Name = player1,
}, },
commandType: CommandType.StoredProcedure); commandType: CommandType.StoredProcedure);
} }
public async Task<ShogiBoard?> ReadShogiBoard(string name) public async Task DeleteSession(string name)
{ {
using var connection = new SqlConnection(connectionString); using var connection = new SqlConnection(connectionString);
var results = await connection.QueryAsync<SessionDto>( await connection.ExecuteAsync(
"session.ReadSession", "session.DeleteSession",
new { Name = name },
commandType: CommandType.StoredProcedure); commandType: CommandType.StoredProcedure);
var dto = results.SingleOrDefault(); }
if (dto == null) return null;
var boardState = new BoardState( public async Task<Session?> ReadSession(string name)
state: new(dto.BoardState.State), {
player1Hand: dto.BoardState.Player1Hand, using var connection = new SqlConnection(connectionString);
player2Hand: dto.BoardState.Player2Hand, var results = await connection.QueryMultipleAsync(
whoseTurn: dto.BoardState.WhoseTurn, "session.ReadSession",
playerInCheck: dto.BoardState.InCheck, new { Name = name },
previousMove: dto.BoardState.PreviousMove); commandType: CommandType.StoredProcedure);
var session = new ShogiBoard(boardState);
var sessionDtos = await results.ReadAsync<SessionDto>();
if (!sessionDtos.Any()) return null;
var dto = sessionDtos.First();
var session = new Session(dto.Name, dto.Player1);
if (!string.IsNullOrWhiteSpace(dto.Player2)) session.AddPlayer2(dto.Player2);
var moveDtos = await results.ReadAsync<MoveDto>();
foreach (var move in moveDtos)
{
if (move.PieceFromHand.HasValue)
{
session.Board.Move(move.PieceFromHand.Value, move.To);
}
else
{
session.Board.Move(move.From, move.To, false);
}
}
return session; return session;
} }
} }
@@ -66,5 +70,6 @@ public class SessionRepository : ISessionRepository
public interface ISessionRepository public interface ISessionRepository
{ {
Task CreateSession(Session session); Task CreateSession(Session session);
Task<ShogiBoard?> ReadShogiBoard(string name); Task DeleteSession(string name);
Task<Session?> ReadSession(string name);
} }

View File

@@ -1,12 +1,11 @@
using FluentValidation; using FluentValidation;
using Newtonsoft.Json;
using Shogi.Contracts.Socket; using Shogi.Contracts.Socket;
using Shogi.Contracts.Types; using Shogi.Contracts.Types;
using Shogi.Api.Extensions; using Shogi.Api.Extensions;
using Shogi.Api.Managers; using Shogi.Api.Managers;
using Shogi.Api.Repositories;
using System.Net; using System.Net;
using System.Net.WebSockets; using System.Net.WebSockets;
using System.Text.Json;
namespace Shogi.Api.Services namespace Shogi.Api.Services
{ {
@@ -59,7 +58,7 @@ namespace Shogi.Api.Services
var message = await socket.ReceiveTextAsync(); var message = await socket.ReceiveTextAsync();
if (string.IsNullOrWhiteSpace(message)) continue; if (string.IsNullOrWhiteSpace(message)) continue;
logger.LogInformation("Request \n{0}\n", message); logger.LogInformation("Request \n{0}\n", message);
var request = JsonConvert.DeserializeObject<ISocketRequest>(message); var request = JsonSerializer.Deserialize<ISocketRequest>(message);
if (request == null || !Enum.IsDefined(typeof(SocketAction), request.Action)) if (request == null || !Enum.IsDefined(typeof(SocketAction), request.Action))
{ {
await socket.SendTextAsync("Error: Action not recognized."); await socket.SendTextAsync("Error: Action not recognized.");

View File

@@ -29,12 +29,10 @@
<PackageReference Include="FluentValidation" Version="11.2.0" /> <PackageReference Include="FluentValidation" Version="11.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.8" /> <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.8" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="6.0.8" /> <PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="6.0.8" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.8" />
<PackageReference Include="Microsoft.Identity.Web" Version="1.25.2" /> <PackageReference Include="Microsoft.Identity.Web" Version="1.25.2" />
<PackageReference Include="Microsoft.Identity.Web.UI" Version="1.25.2" /> <PackageReference Include="Microsoft.Identity.Web.UI" Version="1.25.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.4" /> <PackageReference Include="System.Data.SqlClient" Version="4.8.5" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@@ -5,6 +5,5 @@ namespace Shogi.Contracts.Api;
public class CreateSessionCommand public class CreateSessionCommand
{ {
[Required] [Required]
public string Name { get; set; } = string.Empty; public string Name { get; set; }
public bool IsPrivate { get; set; }
} }

View File

@@ -3,7 +3,7 @@ using System.Collections.Generic;
namespace Shogi.Contracts.Api; namespace Shogi.Contracts.Api;
public class ReadAllSessionsResponse public class ReadSessionsPlayerCountResponse
{ {
public IList<SessionMetadata> PlayerHasJoinedSessions { get; set; } public IList<SessionMetadata> PlayerHasJoinedSessions { get; set; }
public IList<SessionMetadata> AllOtherSessions { get; set; } public IList<SessionMetadata> AllOtherSessions { get; set; }

View File

@@ -0,0 +1,11 @@
using System.Text.Json;
namespace Shogi.Contracts;
public class ShogiApiJsonSerializerSettings
{
public readonly static JsonSerializerOptions SystemTextJsonSerializerOptions = new(JsonSerializerDefaults.Web)
{
WriteIndented = true,
};
}

View File

@@ -2,9 +2,8 @@
public class Session public class Session
{ {
public string Player1 { get; set; } = string.Empty; public string Player1 { get; set; }
public string? Player2 { get; set; } public string? Player2 { get; set; }
public string SessionName { get; set; } = string.Empty; public string SessionName { get; set; }
public bool GameOver { get; set; }
public BoardState BoardState { get; set; } public BoardState BoardState { get; set; }
} }

View File

@@ -10,4 +10,5 @@ Post-Deployment Script Template
-------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------
*/ */
:r .\Scripts\PopulateLoginPlatforms.sql :r .\Scripts\PopulateLoginPlatforms.sql
:r .\Scripts\PopulatePieces.sql

View File

@@ -0,0 +1,2 @@
ALTER DATABASE Shogi
SET ALLOW_SNAPSHOT_ISOLATION ON

View File

@@ -0,0 +1,21 @@
DECLARE @Pieces TABLE(
[Name] NVARCHAR(13)
)
INSERT INTO @Pieces ([Name])
VALUES
('King'),
('GoldGeneral'),
('SilverGeneral'),
('Bishop'),
('Rook'),
('Knight'),
('Lance'),
('Pawn');
MERGE [session].[Piece] as t
USING @Pieces as s
ON t.[Name] = s.[Name]
WHEN NOT MATCHED THEN
INSERT ([Name])
VALUES (s.[Name]);

View File

@@ -1,15 +1,12 @@
CREATE PROCEDURE [session].[CreateSession] CREATE PROCEDURE [session].[CreateSession]
@Name [session].[SessionName], @Name [session].[SessionName],
@Player1Name [user].[UserName], @Player1Name [user].[UserName]
@InitialBoardStateDocument [session].[JsonDocument]
AS AS
BEGIN BEGIN
SET NOCOUNT ON SET NOCOUNT ON
INSERT INTO [session].[Session] (BoardState, Player1Id) INSERT INTO [session].[Session] ([Name], Player1Id)
SELECT SELECT @Name, Id
@InitialBoardStateDocument,
Id
FROM [user].[User] FROM [user].[User]
WHERE [Name] = @Player1Name WHERE [Name] = @Player1Name
END END

View File

@@ -0,0 +1,5 @@
CREATE PROCEDURE [session].[DeleteSession]
@Name [session].[SessionName]
AS
DELETE FROM [session].[Session] WHERE [Name] = @Name;

View File

@@ -2,15 +2,32 @@
@Name [session].[SessionName] @Name [session].[SessionName]
AS AS
BEGIN BEGIN
SET NOCOUNT ON SET NOCOUNT ON -- Performance boost
SET XACT_ABORT ON -- Rollback transaction on error
SET TRANSACTION ISOLATION LEVEL SNAPSHOT -- Ignores data changes that happen after the transaction begins.
SELECT BEGIN TRANSACTION
sess.[Name],
BoardState, -- Session
p1.[Name] as Player1, SELECT
p2.[Name] as Player2 sess.[Name],
FROM [session].[Session] sess p1.[Name] as Player1,
INNER JOIN [user].[User] p1 on sess.Player1Id = p1.Id p2.[Name] as Player2
LEFT JOIN [user].[User] p2 on sess.Player2Id = p2.Id FROM [session].[Session] sess
WHERE sess.[Name] = @Name; INNER JOIN [user].[User] p1 on sess.Player1Id = p1.Id
LEFT JOIN [user].[User] p2 on sess.Player2Id = p2.Id
WHERE sess.[Name] = @Name;
-- Player moves
SELECT
mv.[From],
mv.[To],
mv.IsPromotion,
piece.[Name] as PieceFromHand
FROM [session].[Move] mv
INNER JOIN [session].[Session] sess ON sess.Id = mv.SessionId
RIGHT JOIN [session].Piece piece on piece.Id = mv.PieceIdFromHand
WHERE sess.[Name] = @Name;
COMMIT
END END

View File

@@ -1,4 +1,4 @@
CREATE PROCEDURE [session].[ReadAllSessionsMetadata] CREATE PROCEDURE [session].[ReadSessionPlayerCount]
AS AS
BEGIN BEGIN
SET NOCOUNT ON; SET NOCOUNT ON;

View File

@@ -0,0 +1,20 @@
CREATE TABLE [session].[Move]
(
[Id] INT NOT NULL PRIMARY KEY IDENTITY,
[SessionId] BIGINT NOT NULL,
[From] VARCHAR(2) NOT NULL,
[To] VARCHAR(2) NOT NULL,
[IsPromotion] BIT NOT NULL,
[PieceIdFromHand] INT NULL
CONSTRAINT [Cannot end where you start]
CHECK ([From] <> [To]),
CONSTRAINT FK_Move_Session FOREIGN KEY (SessionId) REFERENCES [session].[Session] (Id)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT FK_Move_Piece FOREIGN KEY (PieceIdFromHand) REFERENCES [session].[Piece] (Id)
ON DELETE NO ACTION
ON UPDATE NO ACTION
)

View File

@@ -0,0 +1,5 @@
CREATE TABLE [session].[Piece]
(
[Id] INT NOT NULL PRIMARY KEY IDENTITY,
[Name] NVARCHAR(13) NOT NULL UNIQUE
)

View File

@@ -4,13 +4,12 @@
[Name] [session].[SessionName] UNIQUE, [Name] [session].[SessionName] UNIQUE,
Player1Id BIGINT NOT NULL, Player1Id BIGINT NOT NULL,
Player2Id BIGINT NULL, Player2Id BIGINT NULL,
BoardState [session].[JsonDocument] NOT NULL,
Created DATETIMEOFFSET NOT NULL DEFAULT SYSDATETIMEOFFSET(), Created DATETIMEOFFSET NOT NULL DEFAULT SYSDATETIMEOFFSET(),
CONSTRAINT [BoardState must be json] CHECK (isjson(BoardState)=1),
CONSTRAINT FK_Player1_User FOREIGN KEY (Player1Id) REFERENCES [user].[User] (Id) CONSTRAINT FK_Player1_User FOREIGN KEY (Player1Id) REFERENCES [user].[User] (Id)
ON DELETE CASCADE ON DELETE CASCADE
ON UPDATE CASCADE, ON UPDATE CASCADE,
CONSTRAINT FK_Player2_User FOREIGN KEY (Player2Id) REFERENCES [user].[User] (Id) CONSTRAINT FK_Player2_User FOREIGN KEY (Player2Id) REFERENCES [user].[User] (Id)
ON DELETE NO ACTION ON DELETE NO ACTION
ON UPDATE NO ACTION ON UPDATE NO ACTION

View File

@@ -22,6 +22,7 @@
<SqlServerVerification>False</SqlServerVerification> <SqlServerVerification>False</SqlServerVerification>
<IncludeCompositeObjects>True</IncludeCompositeObjects> <IncludeCompositeObjects>True</IncludeCompositeObjects>
<TargetDatabaseSet>True</TargetDatabaseSet> <TargetDatabaseSet>True</TargetDatabaseSet>
<DefaultSchema>session</DefaultSchema>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<OutputPath>bin\Release\</OutputPath> <OutputPath>bin\Release\</OutputPath>
@@ -77,14 +78,21 @@
<Build Include="User\Types\UserName.sql" /> <Build Include="User\Types\UserName.sql" />
<Build Include="Session\Types\JsonDocument.sql" /> <Build Include="Session\Types\JsonDocument.sql" />
<Build Include="User\StoredProcedures\CreateUser.sql" /> <Build Include="User\StoredProcedures\CreateUser.sql" />
<Build Include="Session\Stored Procedures\ReadAllSessionsMetadata.sql" /> <Build Include="Session\Stored Procedures\ReadSessionPlayerCount.sql" />
<Build Include="User\StoredProcedures\ReadUser.sql" /> <Build Include="User\StoredProcedures\ReadUser.sql" />
<Build Include="User\Tables\LoginPlatform.sql" /> <Build Include="User\Tables\LoginPlatform.sql" />
<None Include="Post Deployment\Scripts\PopulateLoginPlatforms.sql" /> <None Include="Post Deployment\Scripts\PopulateLoginPlatforms.sql" />
<Build Include="Session\Stored Procedures\UpdateSession.sql" /> <Build Include="Session\Stored Procedures\UpdateSession.sql" />
<Build Include="Session\Stored Procedures\ReadSession.sql" /> <Build Include="Session\Stored Procedures\ReadSession.sql" />
<Build Include="Session\Tables\Move.sql" />
<Build Include="Session\Tables\Piece.sql" />
<Build Include="Session\Stored Procedures\DeleteSession.sql" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PostDeploy Include="Post Deployment\Script.PostDeployment.sql" /> <PostDeploy Include="Post Deployment\Script.PostDeployment.sql" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Include="Post Deployment\Scripts\PopulatePieces.sql" />
<None Include="Post Deployment\Scripts\EnableSnapshotIsolationLevel.sql" />
</ItemGroup>
</Project> </Project>

View File

@@ -6,12 +6,11 @@ public class Session
{ {
public Session( public Session(
string name, string name,
string player1Name, string player1Name)
ShogiBoard board)
{ {
Name = name; Name = name;
Player1 = player1Name; Player1 = player1Name;
Board = board; Board = new(BoardState.StandardStarting);
} }
public string Name { get; } public string Name { get; }

View File

@@ -9,7 +9,7 @@ public class BoardState
/// <summary> /// <summary>
/// Board state before any moves have been made, using standard setup and rules. /// Board state before any moves have been made, using standard setup and rules.
/// </summary> /// </summary>
public static readonly BoardState StandardStarting = new( public static BoardState StandardStarting => new(
state: BuildStandardStartingBoardState(), state: BuildStandardStartingBoardState(),
player1Hand: new(), player1Hand: new(),
player2Hand: new(), player2Hand: new(),

View File

@@ -8,7 +8,7 @@ namespace Shogi.UI.Pages.Home.Api
{ {
Task<CreateGuestTokenResponse?> GetGuestToken(); Task<CreateGuestTokenResponse?> GetGuestToken();
Task<Session?> GetSession(string name); Task<Session?> GetSession(string name);
Task<ReadAllSessionsResponse?> GetSessions(); Task<ReadSessionsPlayerCountResponse?> GetSessions();
Task<Guid?> GetToken(); Task<Guid?> GetToken();
Task GuestLogout(); Task GuestLogout();
Task PostMove(string sessionName, Move move); Task PostMove(string sessionName, Move move);

View File

@@ -63,12 +63,12 @@ namespace Shogi.UI.Pages.Home.Api
return null; return null;
} }
public async Task<ReadAllSessionsResponse?> GetSessions() public async Task<ReadSessionsPlayerCountResponse?> GetSessions()
{ {
var response = await HttpClient.GetAsync(new Uri("Session", UriKind.Relative)); var response = await HttpClient.GetAsync(new Uri("Session", UriKind.Relative));
if (response.IsSuccessStatusCode) if (response.IsSuccessStatusCode)
{ {
return await response.Content.ReadFromJsonAsync<ReadAllSessionsResponse>(serializerOptions); return await response.Content.ReadFromJsonAsync<ReadSessionsPlayerCountResponse>(serializerOptions);
} }
return null; return null;
} }
@@ -90,7 +90,6 @@ namespace Shogi.UI.Pages.Home.Api
var response = await HttpClient.PostAsJsonAsync(new Uri("Session", UriKind.Relative), new CreateSessionCommand var response = await HttpClient.PostAsJsonAsync(new Uri("Session", UriKind.Relative), new CreateSessionCommand
{ {
Name = name, Name = name,
IsPrivate = isPrivate
}); });
return response.StatusCode; return response.StatusCode;
} }

View File

@@ -1,5 +1,6 @@
using Shogi.AcceptanceTests.TestSetup; using Shogi.AcceptanceTests.TestSetup;
using Shogi.Contracts.Api; using Shogi.Contracts.Api;
using Shogi.Contracts.Types;
using System.Net; using System.Net;
using System.Net.Http.Json; using System.Net.Http.Json;
using Xunit.Abstractions; using Xunit.Abstractions;
@@ -21,16 +22,78 @@ public class AcceptanceTests : IClassFixture<GuestTestFixture>
private HttpClient Service => fixture.Service; private HttpClient Service => fixture.Service;
[Fact]
public async Task ReadSessionsPlayerCount()
{
try
{
// Arrange
await CreateSession();
// Act
var readAllResponse = await Service
.GetFromJsonAsync<ReadSessionsPlayerCountResponse>(new Uri("Sessions/PlayerCount", UriKind.Relative),
Contracts.ShogiApiJsonSerializerSettings.SystemTextJsonSerializerOptions);
// Assert
readAllResponse.Should().NotBeNull();
readAllResponse!
.AllOtherSessions
.Should()
.ContainSingle(session => session.Name == "Acceptance Tests" && session.PlayerCount == 1);
}
finally
{
// Annul
await DeleteSession();
}
}
[Fact] [Fact]
public async Task CreateAndReadSession() public async Task CreateAndReadSession()
{ {
var createResponse = await Service.PostAsJsonAsync(new Uri("Session", UriKind.Relative), new CreateSessionCommand { Name = "Acceptance Tests" }); try
createResponse.StatusCode.Should().Be(HttpStatusCode.Created); {
var yep = await createResponse.Content.ReadAsStringAsync(); // Arrange
console.WriteLine(yep); await CreateSession();
// Act
var response = await Service.GetFromJsonAsync<ReadSessionResponse>(
new Uri("Sessions/Acceptance Tests", UriKind.Relative),
Contracts.ShogiApiJsonSerializerSettings.SystemTextJsonSerializerOptions);
// Assert
response.Should().NotBeNull();
response!.Session.Should().NotBeNull();
response.Session.BoardState.Board.Should().NotBeEmpty();
response.Session.BoardState.Player1Hand.Should().BeEmpty();
response.Session.BoardState.Player2Hand.Should().BeEmpty();
response.Session.BoardState.PlayerInCheck.Should().BeNull();
response.Session.BoardState.WhoseTurn.Should().Be(WhichPlayer.Player1);
response.Session.Player1.Should().NotBeNullOrEmpty();
response.Session.Player2.Should().BeNull();
response.Session.SessionName.Should().Be("Acceptance Tests");
}
finally
{
// Annul
await DeleteSession();
}
}
private async Task CreateSession()
{
var createResponse = await Service.PostAsJsonAsync(
new Uri("Sessions", UriKind.Relative),
new CreateSessionCommand { Name = "Acceptance Tests" },
Contracts.ShogiApiJsonSerializerSettings.SystemTextJsonSerializerOptions);
createResponse.StatusCode.Should().Be(HttpStatusCode.Created);
}
private async Task DeleteSession()
{
var response = await Service.DeleteAsync(new Uri("Sessions/Acceptance Tests", UriKind.Relative));
response.StatusCode.Should().Be(HttpStatusCode.NoContent, because: "Test cleanup should succeed");
var readAllResponse = await Service.GetFromJsonAsync<ReadAllSessionsResponse>(new Uri("Session", UriKind.Relative));
readAllResponse.Should().NotBeNull();
readAllResponse!.AllOtherSessions.Should().ContainSingle(session => session.Name == "Acceptance Tests" && session.PlayerCount == 1);
} }
} }

View File

@@ -21,21 +21,21 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.7.0" /> <PackageReference Include="FluentAssertions" Version="6.8.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" /> <PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" /> <PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="6.0.1" /> <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" /> <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="6.0.1" /> <PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="7.0.0" />
<PackageReference Include="Microsoft.Identity.Client" Version="4.46.1" /> <PackageReference Include="Microsoft.Identity.Client" Version="4.48.0" />
<PackageReference Include="Microsoft.Net.Http.Headers" Version="2.2.8" /> <PackageReference Include="Microsoft.Net.Http.Headers" Version="2.2.8" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.0" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
<PackageReference Include="xunit" Version="2.4.2" /> <PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5"> <PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
</PackageReference> </PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2"> <PackageReference Include="coverlet.collector" Version="3.2.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
</PackageReference> </PackageReference>

View File

@@ -1,62 +1,60 @@
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
namespace Shogi.AcceptanceTests.TestSetup namespace Shogi.AcceptanceTests.TestSetup;
/// <summary>
/// Acceptance Test fixture for tests which assert features for Microsoft accounts.
/// </summary>
public class GuestTestFixture : IAsyncLifetime, IDisposable
{ {
/// <summary> private bool disposedValue;
/// Acceptance Test fixture for tests which assert features for Microsoft accounts.
/// </summary> public GuestTestFixture()
public class GuestTestFixture : IAsyncLifetime, IDisposable
{ {
private bool disposedValue; Configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
public GuestTestFixture() Service = new HttpClient
{ {
Configuration = new ConfigurationBuilder() BaseAddress = new Uri(Configuration["ServiceUrl"], UriKind.Absolute)
.AddJsonFile("appsettings.json") };
//.AddEnvironmentVariables() }
.Build();
Service = new HttpClient public IConfiguration Configuration { get; private set; }
public HttpClient Service { get; }
public async Task InitializeAsync()
{
// Log in as a guest account and retain the session cookie for future requests.
var loginResponse = await Service.GetAsync(new Uri("User/LoginAsGuest", UriKind.Relative));
loginResponse.IsSuccessStatusCode.Should().BeTrue(because: "Guest accounts should work");
var guestSessionCookie = loginResponse.Headers.GetValues("Set-Cookie").SingleOrDefault();
Service.DefaultRequestHeaders.Add("Set-Cookie", guestSessionCookie);
}
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{ {
BaseAddress = new Uri(Configuration["ServiceUrl"], UriKind.Absolute) Service.Dispose();
};
}
public IConfiguration Configuration { get; private set; }
public HttpClient Service { get; }
public async Task InitializeAsync()
{
// Log in as a guest account.
var loginResponse = await Service.GetAsync(new Uri("User/LoginAsGuest", UriKind.Relative));
loginResponse.EnsureSuccessStatusCode();
}
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
Service.Dispose();
}
disposedValue = true;
} }
}
public Task DisposeAsync() disposedValue = true;
{
Dispose(true);
return Task.CompletedTask;
}
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
} }
} }
public Task DisposeAsync()
{
Dispose(true);
return Task.CompletedTask;
}
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
} }

View File

@@ -1,186 +1,182 @@
using FluentAssertions; namespace Shogi.Domain.UnitTests;
using Xunit;
namespace Shogi.Domain.UnitTests public class ShogiBoardStateShould
{ {
public class ShogiBoardStateShould [Fact]
public void InitializeBoardState()
{ {
[Fact] // Act
public void InitializeBoardState() var board = BoardState.StandardStarting;
{
// Act
var board = new BoardState();
// Assert // Assert
board["A1"]?.WhichPiece.Should().Be(WhichPiece.Lance); board["A1"]?.WhichPiece.Should().Be(WhichPiece.Lance);
board["A1"]?.Owner.Should().Be(WhichPlayer.Player1); board["A1"]?.Owner.Should().Be(WhichPlayer.Player1);
board["A1"]?.IsPromoted.Should().Be(false); board["A1"]?.IsPromoted.Should().Be(false);
board["B1"]?.WhichPiece.Should().Be(WhichPiece.Knight); board["B1"]?.WhichPiece.Should().Be(WhichPiece.Knight);
board["B1"]?.Owner.Should().Be(WhichPlayer.Player1); board["B1"]?.Owner.Should().Be(WhichPlayer.Player1);
board["B1"]?.IsPromoted.Should().Be(false); board["B1"]?.IsPromoted.Should().Be(false);
board["C1"]?.WhichPiece.Should().Be(WhichPiece.SilverGeneral); board["C1"]?.WhichPiece.Should().Be(WhichPiece.SilverGeneral);
board["C1"]?.Owner.Should().Be(WhichPlayer.Player1); board["C1"]?.Owner.Should().Be(WhichPlayer.Player1);
board["C1"]?.IsPromoted.Should().Be(false); board["C1"]?.IsPromoted.Should().Be(false);
board["D1"]?.WhichPiece.Should().Be(WhichPiece.GoldGeneral); board["D1"]?.WhichPiece.Should().Be(WhichPiece.GoldGeneral);
board["D1"]?.Owner.Should().Be(WhichPlayer.Player1); board["D1"]?.Owner.Should().Be(WhichPlayer.Player1);
board["D1"]?.IsPromoted.Should().Be(false); board["D1"]?.IsPromoted.Should().Be(false);
board["E1"]?.WhichPiece.Should().Be(WhichPiece.King); board["E1"]?.WhichPiece.Should().Be(WhichPiece.King);
board["E1"]?.Owner.Should().Be(WhichPlayer.Player1); board["E1"]?.Owner.Should().Be(WhichPlayer.Player1);
board["E1"]?.IsPromoted.Should().Be(false); board["E1"]?.IsPromoted.Should().Be(false);
board["F1"]?.WhichPiece.Should().Be(WhichPiece.GoldGeneral); board["F1"]?.WhichPiece.Should().Be(WhichPiece.GoldGeneral);
board["F1"]?.Owner.Should().Be(WhichPlayer.Player1); board["F1"]?.Owner.Should().Be(WhichPlayer.Player1);
board["F1"]?.IsPromoted.Should().Be(false); board["F1"]?.IsPromoted.Should().Be(false);
board["G1"]?.WhichPiece.Should().Be(WhichPiece.SilverGeneral); board["G1"]?.WhichPiece.Should().Be(WhichPiece.SilverGeneral);
board["G1"]?.Owner.Should().Be(WhichPlayer.Player1); board["G1"]?.Owner.Should().Be(WhichPlayer.Player1);
board["G1"]?.IsPromoted.Should().Be(false); board["G1"]?.IsPromoted.Should().Be(false);
board["H1"]?.WhichPiece.Should().Be(WhichPiece.Knight); board["H1"]?.WhichPiece.Should().Be(WhichPiece.Knight);
board["H1"]?.Owner.Should().Be(WhichPlayer.Player1); board["H1"]?.Owner.Should().Be(WhichPlayer.Player1);
board["H1"]?.IsPromoted.Should().Be(false); board["H1"]?.IsPromoted.Should().Be(false);
board["I1"]?.WhichPiece.Should().Be(WhichPiece.Lance); board["I1"]?.WhichPiece.Should().Be(WhichPiece.Lance);
board["I1"]?.Owner.Should().Be(WhichPlayer.Player1); board["I1"]?.Owner.Should().Be(WhichPlayer.Player1);
board["I1"]?.IsPromoted.Should().Be(false); board["I1"]?.IsPromoted.Should().Be(false);
board["A2"].Should().BeNull(); board["A2"].Should().BeNull();
board["B2"]?.WhichPiece.Should().Be(WhichPiece.Bishop); board["B2"]?.WhichPiece.Should().Be(WhichPiece.Bishop);
board["B2"]?.Owner.Should().Be(WhichPlayer.Player1); board["B2"]?.Owner.Should().Be(WhichPlayer.Player1);
board["B2"]?.IsPromoted.Should().Be(false); board["B2"]?.IsPromoted.Should().Be(false);
board["C2"].Should().BeNull(); board["C2"].Should().BeNull();
board["D2"].Should().BeNull(); board["D2"].Should().BeNull();
board["E2"].Should().BeNull(); board["E2"].Should().BeNull();
board["F2"].Should().BeNull(); board["F2"].Should().BeNull();
board["G2"].Should().BeNull(); board["G2"].Should().BeNull();
board["H2"]?.WhichPiece.Should().Be(WhichPiece.Rook); board["H2"]?.WhichPiece.Should().Be(WhichPiece.Rook);
board["H2"]?.Owner.Should().Be(WhichPlayer.Player1); board["H2"]?.Owner.Should().Be(WhichPlayer.Player1);
board["H2"]?.IsPromoted.Should().Be(false); board["H2"]?.IsPromoted.Should().Be(false);
board["I2"].Should().BeNull(); board["I2"].Should().BeNull();
board["A3"]?.WhichPiece.Should().Be(WhichPiece.Pawn); board["A3"]?.WhichPiece.Should().Be(WhichPiece.Pawn);
board["A3"]?.Owner.Should().Be(WhichPlayer.Player1); board["A3"]?.Owner.Should().Be(WhichPlayer.Player1);
board["A3"]?.IsPromoted.Should().Be(false); board["A3"]?.IsPromoted.Should().Be(false);
board["B3"]?.WhichPiece.Should().Be(WhichPiece.Pawn); board["B3"]?.WhichPiece.Should().Be(WhichPiece.Pawn);
board["B3"]?.Owner.Should().Be(WhichPlayer.Player1); board["B3"]?.Owner.Should().Be(WhichPlayer.Player1);
board["B3"]?.IsPromoted.Should().Be(false); board["B3"]?.IsPromoted.Should().Be(false);
board["C3"]?.WhichPiece.Should().Be(WhichPiece.Pawn); board["C3"]?.WhichPiece.Should().Be(WhichPiece.Pawn);
board["C3"]?.Owner.Should().Be(WhichPlayer.Player1); board["C3"]?.Owner.Should().Be(WhichPlayer.Player1);
board["C3"]?.IsPromoted.Should().Be(false); board["C3"]?.IsPromoted.Should().Be(false);
board["D3"]?.WhichPiece.Should().Be(WhichPiece.Pawn); board["D3"]?.WhichPiece.Should().Be(WhichPiece.Pawn);
board["D3"]?.Owner.Should().Be(WhichPlayer.Player1); board["D3"]?.Owner.Should().Be(WhichPlayer.Player1);
board["D3"]?.IsPromoted.Should().Be(false); board["D3"]?.IsPromoted.Should().Be(false);
board["E3"]?.WhichPiece.Should().Be(WhichPiece.Pawn); board["E3"]?.WhichPiece.Should().Be(WhichPiece.Pawn);
board["E3"]?.Owner.Should().Be(WhichPlayer.Player1); board["E3"]?.Owner.Should().Be(WhichPlayer.Player1);
board["E3"]?.IsPromoted.Should().Be(false); board["E3"]?.IsPromoted.Should().Be(false);
board["F3"]?.WhichPiece.Should().Be(WhichPiece.Pawn); board["F3"]?.WhichPiece.Should().Be(WhichPiece.Pawn);
board["F3"]?.Owner.Should().Be(WhichPlayer.Player1); board["F3"]?.Owner.Should().Be(WhichPlayer.Player1);
board["F3"]?.IsPromoted.Should().Be(false); board["F3"]?.IsPromoted.Should().Be(false);
board["G3"]?.WhichPiece.Should().Be(WhichPiece.Pawn); board["G3"]?.WhichPiece.Should().Be(WhichPiece.Pawn);
board["G3"]?.Owner.Should().Be(WhichPlayer.Player1); board["G3"]?.Owner.Should().Be(WhichPlayer.Player1);
board["G3"]?.IsPromoted.Should().Be(false); board["G3"]?.IsPromoted.Should().Be(false);
board["H3"]?.WhichPiece.Should().Be(WhichPiece.Pawn); board["H3"]?.WhichPiece.Should().Be(WhichPiece.Pawn);
board["H3"]?.Owner.Should().Be(WhichPlayer.Player1); board["H3"]?.Owner.Should().Be(WhichPlayer.Player1);
board["H3"]?.IsPromoted.Should().Be(false); board["H3"]?.IsPromoted.Should().Be(false);
board["I3"]?.WhichPiece.Should().Be(WhichPiece.Pawn); board["I3"]?.WhichPiece.Should().Be(WhichPiece.Pawn);
board["I3"]?.Owner.Should().Be(WhichPlayer.Player1); board["I3"]?.Owner.Should().Be(WhichPlayer.Player1);
board["I3"]?.IsPromoted.Should().Be(false); board["I3"]?.IsPromoted.Should().Be(false);
board["A4"].Should().BeNull(); board["A4"].Should().BeNull();
board["B4"].Should().BeNull(); board["B4"].Should().BeNull();
board["C4"].Should().BeNull(); board["C4"].Should().BeNull();
board["D4"].Should().BeNull(); board["D4"].Should().BeNull();
board["E4"].Should().BeNull(); board["E4"].Should().BeNull();
board["F4"].Should().BeNull(); board["F4"].Should().BeNull();
board["G4"].Should().BeNull(); board["G4"].Should().BeNull();
board["H4"].Should().BeNull(); board["H4"].Should().BeNull();
board["I4"].Should().BeNull(); board["I4"].Should().BeNull();
board["A5"].Should().BeNull(); board["A5"].Should().BeNull();
board["B5"].Should().BeNull(); board["B5"].Should().BeNull();
board["C5"].Should().BeNull(); board["C5"].Should().BeNull();
board["D5"].Should().BeNull(); board["D5"].Should().BeNull();
board["E5"].Should().BeNull(); board["E5"].Should().BeNull();
board["F5"].Should().BeNull(); board["F5"].Should().BeNull();
board["G5"].Should().BeNull(); board["G5"].Should().BeNull();
board["H5"].Should().BeNull(); board["H5"].Should().BeNull();
board["I5"].Should().BeNull(); board["I5"].Should().BeNull();
board["A6"].Should().BeNull(); board["A6"].Should().BeNull();
board["B6"].Should().BeNull(); board["B6"].Should().BeNull();
board["C6"].Should().BeNull(); board["C6"].Should().BeNull();
board["D6"].Should().BeNull(); board["D6"].Should().BeNull();
board["E6"].Should().BeNull(); board["E6"].Should().BeNull();
board["F6"].Should().BeNull(); board["F6"].Should().BeNull();
board["G6"].Should().BeNull(); board["G6"].Should().BeNull();
board["H6"].Should().BeNull(); board["H6"].Should().BeNull();
board["I6"].Should().BeNull(); board["I6"].Should().BeNull();
board["A7"]?.WhichPiece.Should().Be(WhichPiece.Pawn); board["A7"]?.WhichPiece.Should().Be(WhichPiece.Pawn);
board["A7"]?.Owner.Should().Be(WhichPlayer.Player2); board["A7"]?.Owner.Should().Be(WhichPlayer.Player2);
board["A7"]?.IsPromoted.Should().Be(false); board["A7"]?.IsPromoted.Should().Be(false);
board["B7"]?.WhichPiece.Should().Be(WhichPiece.Pawn); board["B7"]?.WhichPiece.Should().Be(WhichPiece.Pawn);
board["B7"]?.Owner.Should().Be(WhichPlayer.Player2); board["B7"]?.Owner.Should().Be(WhichPlayer.Player2);
board["B7"]?.IsPromoted.Should().Be(false); board["B7"]?.IsPromoted.Should().Be(false);
board["C7"]?.WhichPiece.Should().Be(WhichPiece.Pawn); board["C7"]?.WhichPiece.Should().Be(WhichPiece.Pawn);
board["C7"]?.Owner.Should().Be(WhichPlayer.Player2); board["C7"]?.Owner.Should().Be(WhichPlayer.Player2);
board["C7"]?.IsPromoted.Should().Be(false); board["C7"]?.IsPromoted.Should().Be(false);
board["D7"]?.WhichPiece.Should().Be(WhichPiece.Pawn); board["D7"]?.WhichPiece.Should().Be(WhichPiece.Pawn);
board["D7"]?.Owner.Should().Be(WhichPlayer.Player2); board["D7"]?.Owner.Should().Be(WhichPlayer.Player2);
board["D7"]?.IsPromoted.Should().Be(false); board["D7"]?.IsPromoted.Should().Be(false);
board["E7"]?.WhichPiece.Should().Be(WhichPiece.Pawn); board["E7"]?.WhichPiece.Should().Be(WhichPiece.Pawn);
board["E7"]?.Owner.Should().Be(WhichPlayer.Player2); board["E7"]?.Owner.Should().Be(WhichPlayer.Player2);
board["E7"]?.IsPromoted.Should().Be(false); board["E7"]?.IsPromoted.Should().Be(false);
board["F7"]?.WhichPiece.Should().Be(WhichPiece.Pawn); board["F7"]?.WhichPiece.Should().Be(WhichPiece.Pawn);
board["F7"]?.Owner.Should().Be(WhichPlayer.Player2); board["F7"]?.Owner.Should().Be(WhichPlayer.Player2);
board["F7"]?.IsPromoted.Should().Be(false); board["F7"]?.IsPromoted.Should().Be(false);
board["G7"]?.WhichPiece.Should().Be(WhichPiece.Pawn); board["G7"]?.WhichPiece.Should().Be(WhichPiece.Pawn);
board["G7"]?.Owner.Should().Be(WhichPlayer.Player2); board["G7"]?.Owner.Should().Be(WhichPlayer.Player2);
board["G7"]?.IsPromoted.Should().Be(false); board["G7"]?.IsPromoted.Should().Be(false);
board["H7"]?.WhichPiece.Should().Be(WhichPiece.Pawn); board["H7"]?.WhichPiece.Should().Be(WhichPiece.Pawn);
board["H7"]?.Owner.Should().Be(WhichPlayer.Player2); board["H7"]?.Owner.Should().Be(WhichPlayer.Player2);
board["H7"]?.IsPromoted.Should().Be(false); board["H7"]?.IsPromoted.Should().Be(false);
board["I7"]?.WhichPiece.Should().Be(WhichPiece.Pawn); board["I7"]?.WhichPiece.Should().Be(WhichPiece.Pawn);
board["I7"]?.Owner.Should().Be(WhichPlayer.Player2); board["I7"]?.Owner.Should().Be(WhichPlayer.Player2);
board["I7"]?.IsPromoted.Should().Be(false); board["I7"]?.IsPromoted.Should().Be(false);
board["A8"].Should().BeNull(); board["A8"].Should().BeNull();
board["B8"]?.WhichPiece.Should().Be(WhichPiece.Rook); board["B8"]?.WhichPiece.Should().Be(WhichPiece.Rook);
board["B8"]?.Owner.Should().Be(WhichPlayer.Player2); board["B8"]?.Owner.Should().Be(WhichPlayer.Player2);
board["B8"]?.IsPromoted.Should().Be(false); board["B8"]?.IsPromoted.Should().Be(false);
board["C8"].Should().BeNull(); board["C8"].Should().BeNull();
board["D8"].Should().BeNull(); board["D8"].Should().BeNull();
board["E8"].Should().BeNull(); board["E8"].Should().BeNull();
board["F8"].Should().BeNull(); board["F8"].Should().BeNull();
board["G8"].Should().BeNull(); board["G8"].Should().BeNull();
board["H8"]?.WhichPiece.Should().Be(WhichPiece.Bishop); board["H8"]?.WhichPiece.Should().Be(WhichPiece.Bishop);
board["H8"]?.Owner.Should().Be(WhichPlayer.Player2); board["H8"]?.Owner.Should().Be(WhichPlayer.Player2);
board["H8"]?.IsPromoted.Should().Be(false); board["H8"]?.IsPromoted.Should().Be(false);
board["I8"].Should().BeNull(); board["I8"].Should().BeNull();
board["A9"]?.WhichPiece.Should().Be(WhichPiece.Lance); board["A9"]?.WhichPiece.Should().Be(WhichPiece.Lance);
board["A9"]?.Owner.Should().Be(WhichPlayer.Player2); board["A9"]?.Owner.Should().Be(WhichPlayer.Player2);
board["A9"]?.IsPromoted.Should().Be(false); board["A9"]?.IsPromoted.Should().Be(false);
board["B9"]?.WhichPiece.Should().Be(WhichPiece.Knight); board["B9"]?.WhichPiece.Should().Be(WhichPiece.Knight);
board["B9"]?.Owner.Should().Be(WhichPlayer.Player2); board["B9"]?.Owner.Should().Be(WhichPlayer.Player2);
board["B9"]?.IsPromoted.Should().Be(false); board["B9"]?.IsPromoted.Should().Be(false);
board["C9"]?.WhichPiece.Should().Be(WhichPiece.SilverGeneral); board["C9"]?.WhichPiece.Should().Be(WhichPiece.SilverGeneral);
board["C9"]?.Owner.Should().Be(WhichPlayer.Player2); board["C9"]?.Owner.Should().Be(WhichPlayer.Player2);
board["C9"]?.IsPromoted.Should().Be(false); board["C9"]?.IsPromoted.Should().Be(false);
board["D9"]?.WhichPiece.Should().Be(WhichPiece.GoldGeneral); board["D9"]?.WhichPiece.Should().Be(WhichPiece.GoldGeneral);
board["D9"]?.Owner.Should().Be(WhichPlayer.Player2); board["D9"]?.Owner.Should().Be(WhichPlayer.Player2);
board["D9"]?.IsPromoted.Should().Be(false); board["D9"]?.IsPromoted.Should().Be(false);
board["E9"]?.WhichPiece.Should().Be(WhichPiece.King); board["E9"]?.WhichPiece.Should().Be(WhichPiece.King);
board["E9"]?.Owner.Should().Be(WhichPlayer.Player2); board["E9"]?.Owner.Should().Be(WhichPlayer.Player2);
board["E9"]?.IsPromoted.Should().Be(false); board["E9"]?.IsPromoted.Should().Be(false);
board["F9"]?.WhichPiece.Should().Be(WhichPiece.GoldGeneral); board["F9"]?.WhichPiece.Should().Be(WhichPiece.GoldGeneral);
board["F9"]?.Owner.Should().Be(WhichPlayer.Player2); board["F9"]?.Owner.Should().Be(WhichPlayer.Player2);
board["F9"]?.IsPromoted.Should().Be(false); board["F9"]?.IsPromoted.Should().Be(false);
board["G9"]?.WhichPiece.Should().Be(WhichPiece.SilverGeneral); board["G9"]?.WhichPiece.Should().Be(WhichPiece.SilverGeneral);
board["G9"]?.Owner.Should().Be(WhichPlayer.Player2); board["G9"]?.Owner.Should().Be(WhichPlayer.Player2);
board["G9"]?.IsPromoted.Should().Be(false); board["G9"]?.IsPromoted.Should().Be(false);
board["H9"]?.WhichPiece.Should().Be(WhichPiece.Knight); board["H9"]?.WhichPiece.Should().Be(WhichPiece.Knight);
board["H9"]?.Owner.Should().Be(WhichPlayer.Player2); board["H9"]?.Owner.Should().Be(WhichPlayer.Player2);
board["H9"]?.IsPromoted.Should().Be(false); board["H9"]?.IsPromoted.Should().Be(false);
board["I9"]?.WhichPiece.Should().Be(WhichPiece.Lance); board["I9"]?.WhichPiece.Should().Be(WhichPiece.Lance);
board["I9"]?.Owner.Should().Be(WhichPlayer.Player2); board["I9"]?.Owner.Should().Be(WhichPlayer.Player2);
board["I9"]?.IsPromoted.Should().Be(false); board["I9"]?.IsPromoted.Should().Be(false);
}
} }
} }

View File

@@ -1,462 +1,463 @@
using System; using Shogi.Domain.ValueObjects;
using System;
namespace Shogi.Domain.UnitTests namespace Shogi.Domain.UnitTests
{ {
public class ShogiShould public class ShogiShould
{ {
private readonly ITestOutputHelper console; private readonly ITestOutputHelper console;
public ShogiShould(ITestOutputHelper console) public ShogiShould(ITestOutputHelper console)
{ {
this.console = console; this.console = console;
} }
[Fact] [Fact]
public void MoveAPieceToAnEmptyPosition() public void MoveAPieceToAnEmptyPosition()
{ {
// Arrange // Arrange
var shogi = MockShogiBoard(); var shogi = MockShogiBoard();
var board = shogi.BoardState; var board = shogi.BoardState;
board["A4"].Should().BeNull(); board["A4"].Should().BeNull();
var expectedPiece = board["A3"]; var expectedPiece = board["A3"];
expectedPiece.Should().NotBeNull(); expectedPiece.Should().NotBeNull();
// Act // Act
shogi.Move("A3", "A4", false); shogi.Move("A3", "A4", false);
// Assert // Assert
board["A3"].Should().BeNull(); board["A3"].Should().BeNull();
board["A4"].Should().Be(expectedPiece); board["A4"].Should().Be(expectedPiece);
} }
[Fact] [Fact]
public void AllowValidMoves_AfterCheck() public void AllowValidMoves_AfterCheck()
{ {
// Arrange // Arrange
var shogi = MockShogiBoard(); var shogi = MockShogiBoard();
var board = shogi.BoardState; var board = shogi.BoardState;
// P1 Pawn // P1 Pawn
shogi.Move("C3", "C4", false); shogi.Move("C3", "C4", false);
// P2 Pawn // P2 Pawn
shogi.Move("G7", "G6", false); shogi.Move("G7", "G6", false);
// P1 Bishop puts P2 in check // P1 Bishop puts P2 in check
shogi.Move("B2", "G7", false); shogi.Move("B2", "G7", false);
board.InCheck.Should().Be(WhichPlayer.Player2); board.InCheck.Should().Be(WhichPlayer.Player2);
// Act - P2 is able to un-check theirself. // Act - P2 is able to un-check theirself.
/// P2 King moves out of check /// P2 King moves out of check
shogi.Move("E9", "E8", false); shogi.Move("E9", "E8", false);
// Assert // Assert
using (new AssertionScope()) using (new AssertionScope())
{ {
board.InCheck.Should().BeNull(); board.InCheck.Should().BeNull();
} }
} }
[Fact] [Fact]
public void PreventInvalidMoves_MoveFromEmptyPosition() public void PreventInvalidMoves_MoveFromEmptyPosition()
{ {
// Arrange // Arrange
var shogi = MockShogiBoard(); var shogi = MockShogiBoard();
var board = shogi.BoardState; var board = shogi.BoardState;
board["D5"].Should().BeNull(); board["D5"].Should().BeNull();
// Act // Act
var act = () => shogi.Move("D5", "D6", false); var act = () => shogi.Move("D5", "D6", false);
// Assert // Assert
act.Should().Throw<InvalidOperationException>(); act.Should().Throw<InvalidOperationException>();
board["D5"].Should().BeNull(); board["D5"].Should().BeNull();
board["D6"].Should().BeNull(); board["D6"].Should().BeNull();
board.Player1Hand.Should().BeEmpty(); board.Player1Hand.Should().BeEmpty();
board.Player2Hand.Should().BeEmpty(); board.Player2Hand.Should().BeEmpty();
} }
[Fact] [Fact]
public void PreventInvalidMoves_MoveToCurrentPosition() public void PreventInvalidMoves_MoveToCurrentPosition()
{ {
// Arrange // Arrange
var shogi = MockShogiBoard(); var shogi = MockShogiBoard();
var board = shogi.BoardState; var board = shogi.BoardState;
var expectedPiece = board["A3"]; var expectedPiece = board["A3"];
// Act - P1 "moves" pawn to the position it already exists at. // Act - P1 "moves" pawn to the position it already exists at.
var act = () => shogi.Move("A3", "A3", false); var act = () => shogi.Move("A3", "A3", false);
// Assert // Assert
using (new AssertionScope()) using (new AssertionScope())
{ {
act.Should().Throw<InvalidOperationException>(); act.Should().Throw<InvalidOperationException>();
board["A3"].Should().Be(expectedPiece); board["A3"].Should().Be(expectedPiece);
board.Player1Hand.Should().BeEmpty(); board.Player1Hand.Should().BeEmpty();
board.Player2Hand.Should().BeEmpty(); board.Player2Hand.Should().BeEmpty();
} }
} }
[Fact] [Fact]
public void PreventInvalidMoves_MoveSet() public void PreventInvalidMoves_MoveSet()
{ {
// Arrange // Arrange
var shogi = MockShogiBoard(); var shogi = MockShogiBoard();
var board = shogi.BoardState; var board = shogi.BoardState;
var expectedPiece = board["A1"]; var expectedPiece = board["A1"];
expectedPiece!.WhichPiece.Should().Be(WhichPiece.Lance); expectedPiece!.WhichPiece.Should().Be(WhichPiece.Lance);
// Act - Move Lance illegally // Act - Move Lance illegally
var act = () => shogi.Move("A1", "D5", false); var act = () => shogi.Move("A1", "D5", false);
// Assert // Assert
using (new AssertionScope()) using (new AssertionScope())
{ {
act.Should().Throw<InvalidOperationException>(); act.Should().Throw<InvalidOperationException>();
board["A1"].Should().Be(expectedPiece); board["A1"].Should().Be(expectedPiece);
board["A5"].Should().BeNull(); board["A5"].Should().BeNull();
board.Player1Hand.Should().BeEmpty(); board.Player1Hand.Should().BeEmpty();
board.Player2Hand.Should().BeEmpty(); board.Player2Hand.Should().BeEmpty();
} }
} }
[Fact] [Fact]
public void PreventInvalidMoves_Ownership() public void PreventInvalidMoves_Ownership()
{ {
// Arrange // Arrange
var shogi = MockShogiBoard(); var shogi = MockShogiBoard();
var board = shogi.BoardState; var board = shogi.BoardState;
var expectedPiece = board["A7"]; var expectedPiece = board["A7"];
expectedPiece!.Owner.Should().Be(WhichPlayer.Player2); expectedPiece!.Owner.Should().Be(WhichPlayer.Player2);
board.WhoseTurn.Should().Be(WhichPlayer.Player1); board.WhoseTurn.Should().Be(WhichPlayer.Player1);
// Act - Move Player2 Pawn when it is Player1 turn. // Act - Move Player2 Pawn when it is Player1 turn.
var act = () => shogi.Move("A7", "A6", false); var act = () => shogi.Move("A7", "A6", false);
// Assert // Assert
using (new AssertionScope()) using (new AssertionScope())
{ {
act.Should().Throw<InvalidOperationException>(); act.Should().Throw<InvalidOperationException>();
board["A7"].Should().Be(expectedPiece); board["A7"].Should().Be(expectedPiece);
board["A6"].Should().BeNull(); board["A6"].Should().BeNull();
} }
} }
[Fact] [Fact]
public void PreventInvalidMoves_MoveThroughAllies() public void PreventInvalidMoves_MoveThroughAllies()
{ {
// Arrange // Arrange
var shogi = MockShogiBoard(); var shogi = MockShogiBoard();
var board = shogi.BoardState; var board = shogi.BoardState;
var lance = board["A1"]; var lance = board["A1"];
var pawn = board["A3"]; var pawn = board["A3"];
lance!.Owner.Should().Be(pawn!.Owner); lance!.Owner.Should().Be(pawn!.Owner);
// Act - Move P1 Lance through P1 Pawn. // Act - Move P1 Lance through P1 Pawn.
var act = () => shogi.Move("A1", "A5", false); var act = () => shogi.Move("A1", "A5", false);
// Assert // Assert
using (new AssertionScope()) using (new AssertionScope())
{ {
act.Should().Throw<InvalidOperationException>(); act.Should().Throw<InvalidOperationException>();
board["A1"].Should().Be(lance); board["A1"].Should().Be(lance);
board["A3"].Should().Be(pawn); board["A3"].Should().Be(pawn);
board["A5"].Should().BeNull(); board["A5"].Should().BeNull();
} }
} }
[Fact] [Fact]
public void PreventInvalidMoves_CaptureAlly() public void PreventInvalidMoves_CaptureAlly()
{ {
// Arrange // Arrange
var shogi = MockShogiBoard(); var shogi = MockShogiBoard();
var board = shogi.BoardState; var board = shogi.BoardState;
var knight = board["B1"]; var knight = board["B1"];
var pawn = board["C3"]; var pawn = board["C3"];
knight!.Owner.Should().Be(pawn!.Owner); knight!.Owner.Should().Be(pawn!.Owner);
// Act - P1 Knight tries to capture P1 Pawn. // Act - P1 Knight tries to capture P1 Pawn.
var act = () => shogi.Move("B1", "C3", false); var act = () => shogi.Move("B1", "C3", false);
// Arrange // Arrange
using (new AssertionScope()) using (new AssertionScope())
{ {
act.Should().Throw<InvalidOperationException>(); act.Should().Throw<InvalidOperationException>();
board["B1"].Should().Be(knight); board["B1"].Should().Be(knight);
board["C3"].Should().Be(pawn); board["C3"].Should().Be(pawn);
board.Player1Hand.Should().BeEmpty(); board.Player1Hand.Should().BeEmpty();
board.Player2Hand.Should().BeEmpty(); board.Player2Hand.Should().BeEmpty();
} }
} }
[Fact] [Fact]
public void PreventInvalidMoves_Check() public void PreventInvalidMoves_Check()
{ {
// Arrange // Arrange
var shogi = MockShogiBoard(); var shogi = MockShogiBoard();
var board = shogi.BoardState; var board = shogi.BoardState;
// P1 Pawn // P1 Pawn
shogi.Move("C3", "C4", false); shogi.Move("C3", "C4", false);
// P2 Pawn // P2 Pawn
shogi.Move("G7", "G6", false); shogi.Move("G7", "G6", false);
// P1 Bishop puts P2 in check // P1 Bishop puts P2 in check
shogi.Move("B2", "G7", false); shogi.Move("B2", "G7", false);
board.InCheck.Should().Be(WhichPlayer.Player2); board.InCheck.Should().Be(WhichPlayer.Player2);
var lance = board["I9"]; var lance = board["I9"];
// Act - P2 moves Lance while in check. // Act - P2 moves Lance while in check.
var act = () => shogi.Move("I9", "I8", false); var act = () => shogi.Move("I9", "I8", false);
// Assert // Assert
using (new AssertionScope()) using (new AssertionScope())
{ {
act.Should().Throw<InvalidOperationException>(); act.Should().Throw<InvalidOperationException>();
board.InCheck.Should().Be(WhichPlayer.Player2); board.InCheck.Should().Be(WhichPlayer.Player2);
board["I9"].Should().Be(lance); board["I9"].Should().Be(lance);
board["I8"].Should().BeNull(); board["I8"].Should().BeNull();
} }
} }
[Fact] [Fact]
// TODO: Consider nesting classes to share this setup in a constructor but have act and assert as separate facts. // TODO: Consider nesting classes to share this setup in a constructor but have act and assert as separate facts.
public void PreventInvalidDrops_MoveSet() public void PreventInvalidDrops_MoveSet()
{ {
// Arrange // Arrange
var shogi = MockShogiBoard(); var shogi = MockShogiBoard();
var board = shogi.BoardState; var board = shogi.BoardState;
// P1 Pawn // P1 Pawn
shogi.Move("C3", "C4", false); shogi.Move("C3", "C4", false);
// P2 Pawn // P2 Pawn
shogi.Move("I7", "I6", false); shogi.Move("I7", "I6", false);
// P1 Bishop takes P2 Pawn. // P1 Bishop takes P2 Pawn.
shogi.Move("B2", "G7", false); shogi.Move("B2", "G7", false);
// P2 Gold, block check from P1 Bishop. // P2 Gold, block check from P1 Bishop.
shogi.Move("F9", "F8", false); shogi.Move("F9", "F8", false);
// P1 Bishop takes P2 Bishop, promotes so it can capture P2 Knight and P2 Lance // P1 Bishop takes P2 Bishop, promotes so it can capture P2 Knight and P2 Lance
shogi.Move("G7", "H8", true); shogi.Move("G7", "H8", true);
// P2 Pawn again // P2 Pawn again
shogi.Move("I6", "I5", false); shogi.Move("I6", "I5", false);
// P1 Bishop takes P2 Knight // P1 Bishop takes P2 Knight
shogi.Move("H8", "H9", false); shogi.Move("H8", "H9", false);
// P2 Pawn again // P2 Pawn again
shogi.Move("I5", "I4", false); shogi.Move("I5", "I4", false);
// P1 Bishop takes P2 Lance // P1 Bishop takes P2 Lance
shogi.Move("H9", "I9", false); shogi.Move("H9", "I9", false);
// P2 Pawn captures P1 Pawn // P2 Pawn captures P1 Pawn
shogi.Move("I4", "I3", false); shogi.Move("I4", "I3", false);
board.Player1Hand.Count.Should().Be(4); board.Player1Hand.Count.Should().Be(4);
board.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Knight); board.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Knight);
board.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Lance); board.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Lance);
board.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Pawn); board.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Pawn);
board.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop); board.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop);
board.WhoseTurn.Should().Be(WhichPlayer.Player1); board.WhoseTurn.Should().Be(WhichPlayer.Player1);
// Act | Assert - Illegally placing Knight from the hand in farthest rank. // Act | Assert - Illegally placing Knight from the hand in farthest rank.
board["H9"].Should().BeNull(); board["H9"].Should().BeNull();
var act = () => shogi.Move(WhichPiece.Knight, "H9"); var act = () => shogi.Move(WhichPiece.Knight, "H9");
act.Should().Throw<InvalidOperationException>(); act.Should().Throw<InvalidOperationException>();
board["H9"].Should().BeNull(); board["H9"].Should().BeNull();
board.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Knight); board.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Knight);
// Act | Assert - Illegally placing Knight from the hand in second farthest row. // Act | Assert - Illegally placing Knight from the hand in second farthest row.
board["H8"].Should().BeNull(); board["H8"].Should().BeNull();
act = () => shogi.Move(WhichPiece.Knight, "H8"); act = () => shogi.Move(WhichPiece.Knight, "H8");
act.Should().Throw<InvalidOperationException>(); act.Should().Throw<InvalidOperationException>();
board["H8"].Should().BeNull(); board["H8"].Should().BeNull();
board.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Knight); board.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Knight);
// Act | Assert - Illegally place Lance from the hand. // Act | Assert - Illegally place Lance from the hand.
board["H9"].Should().BeNull(); board["H9"].Should().BeNull();
act = () => shogi.Move(WhichPiece.Knight, "H9"); act = () => shogi.Move(WhichPiece.Knight, "H9");
act.Should().Throw<InvalidOperationException>(); act.Should().Throw<InvalidOperationException>();
board["H9"].Should().BeNull(); board["H9"].Should().BeNull();
board.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Lance); board.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Lance);
// Act | Assert - Illegally place Pawn from the hand. // Act | Assert - Illegally place Pawn from the hand.
board["H9"].Should().BeNull(); board["H9"].Should().BeNull();
act = () => shogi.Move(WhichPiece.Pawn, "H9"); act = () => shogi.Move(WhichPiece.Pawn, "H9");
act.Should().Throw<InvalidOperationException>(); act.Should().Throw<InvalidOperationException>();
board["H9"].Should().BeNull(); board["H9"].Should().BeNull();
board.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Pawn); board.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Pawn);
// // Act | Assert - Illegally place Pawn from the hand in a row which already has an unpromoted Pawn. // // Act | Assert - Illegally place Pawn from the hand in a row which already has an unpromoted Pawn.
// // TODO // // TODO
} }
//[Fact] //[Fact]
//public void PreventInvalidDrop_Check() //public void PreventInvalidDrop_Check()
//{ //{
// // Arrange // // Arrange
// var moves = new[] // var moves = new[]
// { // {
// // P1 Pawn // // P1 Pawn
// new Move("C3", "C4"), // new Move("C3", "C4"),
// // P2 Pawn // // P2 Pawn
// new Move("G7", "G6"), // new Move("G7", "G6"),
// // P1 Pawn, arbitrary move. // // P1 Pawn, arbitrary move.
// new Move("A3", "A4"), // new Move("A3", "A4"),
// // P2 Bishop takes P1 Bishop // // P2 Bishop takes P1 Bishop
// new Move("H8", "B2"), // new Move("H8", "B2"),
// // P1 Silver takes P2 Bishop // // P1 Silver takes P2 Bishop
// new Move("C1", "B2"), // new Move("C1", "B2"),
// // P2 Pawn, arbtrary move // // P2 Pawn, arbtrary move
// new Move("A7", "A6"), // new Move("A7", "A6"),
// // P1 drop Bishop, place P2 in check // // P1 drop Bishop, place P2 in check
// new Move(WhichPiece.Bishop, "G7") // new Move(WhichPiece.Bishop, "G7")
// }; // };
// var shogi = new Shogi(moves); // var shogi = new Shogi(moves);
// shogi.InCheck.Should().Be(WhichPlayer.Player2); // shogi.InCheck.Should().Be(WhichPlayer.Player2);
// shogi.Player2Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop); // shogi.Player2Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop);
// board["E5"].Should().BeNull(); // board["E5"].Should().BeNull();
// // Act - P2 places a Bishop while in check. // // Act - P2 places a Bishop while in check.
// var dropSuccess = shogi.Move(new Move(WhichPiece.Bishop, "E5")); // var dropSuccess = shogi.Move(new Move(WhichPiece.Bishop, "E5"));
// // Assert // // Assert
// dropSuccess.Should().BeFalse(); // dropSuccess.Should().BeFalse();
// board["E5"].Should().BeNull(); // board["E5"].Should().BeNull();
// shogi.InCheck.Should().Be(WhichPlayer.Player2); // shogi.InCheck.Should().Be(WhichPlayer.Player2);
// shogi.Player2Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop); // shogi.Player2Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop);
//} //}
//[Fact] //[Fact]
//public void PreventInvalidDrop_Capture() //public void PreventInvalidDrop_Capture()
//{ //{
// // Arrange // // Arrange
// var moves = new[] // var moves = new[]
// { // {
// // P1 Pawn // // P1 Pawn
// new Move("C3", "C4"), // new Move("C3", "C4"),
// // P2 Pawn // // P2 Pawn
// new Move("G7", "G6"), // new Move("G7", "G6"),
// // P1 Bishop capture P2 Bishop // // P1 Bishop capture P2 Bishop
// new Move("B2", "H8"), // new Move("B2", "H8"),
// // P2 Pawn // // P2 Pawn
// new Move("G6", "G5") // new Move("G6", "G5")
// }; // };
// var shogi = new Shogi(moves); // var shogi = new Shogi(moves);
// using (new AssertionScope()) // using (new AssertionScope())
// { // {
// shogi.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop); // shogi.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop);
// board["I9"].Should().NotBeNull(); // board["I9"].Should().NotBeNull();
// board["I9"].WhichPiece.Should().Be(WhichPiece.Lance); // board["I9"].WhichPiece.Should().Be(WhichPiece.Lance);
// board["I9"].Owner.Should().Be(WhichPlayer.Player2); // board["I9"].Owner.Should().Be(WhichPlayer.Player2);
// } // }
// // Act - P1 tries to place a piece where an opponent's piece resides. // // Act - P1 tries to place a piece where an opponent's piece resides.
// var dropSuccess = shogi.Move(new Move(WhichPiece.Bishop, "I9")); // var dropSuccess = shogi.Move(new Move(WhichPiece.Bishop, "I9"));
// // Assert // // Assert
// using (new AssertionScope()) // using (new AssertionScope())
// { // {
// dropSuccess.Should().BeFalse(); // dropSuccess.Should().BeFalse();
// shogi.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop); // shogi.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop);
// board["I9"].Should().NotBeNull(); // board["I9"].Should().NotBeNull();
// board["I9"].WhichPiece.Should().Be(WhichPiece.Lance); // board["I9"].WhichPiece.Should().Be(WhichPiece.Lance);
// board["I9"].Owner.Should().Be(WhichPlayer.Player2); // board["I9"].Owner.Should().Be(WhichPlayer.Player2);
// } // }
//} //}
[Fact] [Fact]
public void Check() public void Check()
{ {
// Arrange // Arrange
var shogi = MockShogiBoard(); var shogi = MockShogiBoard();
var board = shogi.BoardState; var board = shogi.BoardState;
// P1 Pawn // P1 Pawn
shogi.Move("C3", "C4", false); shogi.Move("C3", "C4", false);
// P2 Pawn // P2 Pawn
shogi.Move("G7", "G6", false); shogi.Move("G7", "G6", false);
// Act - P1 Bishop, check // Act - P1 Bishop, check
shogi.Move("B2", "G7", false); shogi.Move("B2", "G7", false);
// Assert // Assert
board.InCheck.Should().Be(WhichPlayer.Player2); board.InCheck.Should().Be(WhichPlayer.Player2);
} }
[Fact] [Fact]
public void Promote() public void Promote()
{ {
// Arrange // Arrange
var shogi = MockShogiBoard(); var shogi = MockShogiBoard();
var board = shogi.BoardState; var board = shogi.BoardState;
// P1 Pawn // P1 Pawn
shogi.Move("C3", "C4", false); shogi.Move("C3", "C4", false);
// P2 Pawn // P2 Pawn
shogi.Move("G7", "G6", false); shogi.Move("G7", "G6", false);
// Act - P1 moves across promote threshold. // Act - P1 moves across promote threshold.
shogi.Move("B2", "G7", true); shogi.Move("B2", "G7", true);
// Assert // Assert
using (new AssertionScope()) using (new AssertionScope())
{ {
board["B2"].Should().BeNull(); board["B2"].Should().BeNull();
board["G7"].Should().NotBeNull(); board["G7"].Should().NotBeNull();
board["G7"]!.WhichPiece.Should().Be(WhichPiece.Bishop); board["G7"]!.WhichPiece.Should().Be(WhichPiece.Bishop);
board["G7"]!.Owner.Should().Be(WhichPlayer.Player1); board["G7"]!.Owner.Should().Be(WhichPlayer.Player1);
board["G7"]!.IsPromoted.Should().BeTrue(); board["G7"]!.IsPromoted.Should().BeTrue();
} }
} }
[Fact] [Fact]
public void Capture() public void Capture()
{ {
// Arrange // Arrange
var shogi = MockShogiBoard(); var shogi = MockShogiBoard();
var board = shogi.BoardState; var board = shogi.BoardState;
var p1Bishop = board["B2"]; var p1Bishop = board["B2"];
p1Bishop!.WhichPiece.Should().Be(WhichPiece.Bishop); p1Bishop!.WhichPiece.Should().Be(WhichPiece.Bishop);
shogi.Move("C3", "C4", false); shogi.Move("C3", "C4", false);
shogi.Move("G7", "G6", false); shogi.Move("G7", "G6", false);
// Act - P1 Bishop captures P2 Bishop // Act - P1 Bishop captures P2 Bishop
shogi.Move("B2", "H8", false); shogi.Move("B2", "H8", false);
// Assert // Assert
board["B2"].Should().BeNull(); board["B2"].Should().BeNull();
board["H8"].Should().Be(p1Bishop); board["H8"].Should().Be(p1Bishop);
board board
.Player1Hand .Player1Hand
.Should() .Should()
.ContainSingle(p => p.WhichPiece == WhichPiece.Bishop && p.Owner == WhichPlayer.Player1); .ContainSingle(p => p.WhichPiece == WhichPiece.Bishop && p.Owner == WhichPlayer.Player1);
} }
[Fact] [Fact]
public void CheckMate() public void CheckMate()
{ {
// Arrange // Arrange
var shogi = MockShogiBoard(); var shogi = MockShogiBoard();
var board = shogi.BoardState; var board = shogi.BoardState;
// P1 Rook // P1 Rook
shogi.Move("H2", "E2", false); shogi.Move("H2", "E2", false);
// P2 Gold // P2 Gold
shogi.Move("F9", "G8", false); shogi.Move("F9", "G8", false);
// P1 Pawn // P1 Pawn
shogi.Move("E3", "E4", false); shogi.Move("E3", "E4", false);
// P2 other Gold // P2 other Gold
shogi.Move("D9", "C8", false); shogi.Move("D9", "C8", false);
// P1 same Pawn // P1 same Pawn
shogi.Move("E4", "E5", false); shogi.Move("E4", "E5", false);
// P2 Pawn // P2 Pawn
shogi.Move("E7", "E6", false); shogi.Move("E7", "E6", false);
// P1 Pawn takes P2 Pawn // P1 Pawn takes P2 Pawn
shogi.Move("E5", "E6", false); shogi.Move("E5", "E6", false);
// P2 King // P2 King
shogi.Move("E9", "E8", false); shogi.Move("E9", "E8", false);
// P1 Pawn promotes; threatens P2 King // P1 Pawn promotes; threatens P2 King
shogi.Move("E6", "E7", true); shogi.Move("E6", "E7", true);
// P2 King retreat // P2 King retreat
shogi.Move("E8", "E9", false); shogi.Move("E8", "E9", false);
// Act - P1 Pawn wins by checkmate. // Act - P1 Pawn wins by checkmate.
shogi.Move("E7", "E8", false); shogi.Move("E7", "E8", false);
// Assert - checkmate // Assert - checkmate
console.WriteLine(shogi.ToStringStateAsAscii()); console.WriteLine(shogi.ToStringStateAsAscii());
board.IsCheckmate.Should().BeTrue(); board.IsCheckmate.Should().BeTrue();
board.InCheck.Should().Be(WhichPlayer.Player2); board.InCheck.Should().Be(WhichPlayer.Player2);
} }
private static ShogiBoard MockShogiBoard() => new ShogiBoard("Test Session", BoardState.StandardStarting); private static ShogiBoard MockShogiBoard() => new(BoardState.StandardStarting);
} }
} }

View File

@@ -8,14 +8,14 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.7.0" /> <PackageReference Include="FluentAssertions" Version="6.8.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.0" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
<PackageReference Include="xunit" Version="2.4.2" /> <PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5"> <PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
</PackageReference> </PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2"> <PackageReference Include="coverlet.collector" Version="3.2.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
</PackageReference> </PackageReference>