Files
Shogi/Shogi.Contracts/Api/Commands/MovePieceCommand.cs
Lucas Morgan 51d234d871 Replace custom socket implementation with SignalR.
Replace MSAL and custom cookie auth with Microsoft.Identity.EntityFramework
Also some UI redesign to accommodate different login experience.
2024-08-25 03:46:44 +00:00

78 lines
2.2 KiB
C#

using Shogi.Contracts.Types;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;
namespace Shogi.Contracts.Api;
public class MovePieceCommand : IValidatableObject
{
/// <summary>
/// For serialization.
/// </summary>
public MovePieceCommand()
{
this.To = string.Empty;
}
/// <summary>
/// Move a piece on the board.
/// </summary>
public MovePieceCommand(string from, string to, bool isPromotion)
{
this.From = from;
this.To = to;
this.IsPromotion = isPromotion;
}
/// <summary>
/// Add a piece to the board from the hand.
/// </summary>
public MovePieceCommand(WhichPiece pieceFromHand, string to)
{
this.PieceFromHand = pieceFromHand;
this.To = to;
}
/// <summary>
/// Mutually exclusive with <see cref="From"/>.
/// Set this property to indicate moving a piece from the hand onto the board.
/// </summary>
public WhichPiece? PieceFromHand { get; set; }
/// <summary>
/// Board position notation, like A3 or G1
/// Mutually exclusive with <see cref="PieceFromHand"/>.
/// Set this property to indicate moving a piece from the board to another position on the board.
/// </summary>
public string? From { get; set; }
/// <summary>
/// Board position notation, like A3 or G1
/// </summary>
[Required]
public string To { get; set; }
public bool? IsPromotion { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (this.PieceFromHand.HasValue && !string.IsNullOrWhiteSpace(this.From))
{
yield return new ValidationResult($"{nameof(this.PieceFromHand)} and {nameof(this.From)} are mutually exclusive properties.");
}
if (this.PieceFromHand.HasValue && this.IsPromotion.HasValue)
{
yield return new ValidationResult($"{nameof(this.PieceFromHand)} and {nameof(this.IsPromotion)} are mutually exclusive properties.");
}
if (!Regex.IsMatch(this.To, "[A-I][1-9]"))
{
yield return new ValidationResult($"{nameof(this.To)} must be a valid board position, between A1 and I9");
}
if (!string.IsNullOrEmpty(this.From) && !Regex.IsMatch(this.From, "[A-I][1-9]"))
{
yield return new ValidationResult($"{nameof(this.From)} must be a valid board position, between A1 and I9");
}
}
}