From 9cd1ad88834c7d7846e507071c64e3c9ef07f5a2 Mon Sep 17 00:00:00 2001 From: Lucas Morgan Date: Thu, 31 Oct 2024 19:04:55 -0500 Subject: [PATCH] cleanup --- Shogi.Api/Application/ShogiApplication.cs | 2 +- Shogi.Api/Controllers/SessionsController.cs | 2 +- Shogi.Api/Extensions/ContractsExtensions.cs | 1 - Shogi.Api/Extensions/WebSocketExtensions.cs | 21 ---------- Shogi.Api/Repositories/SessionRepository.cs | 2 +- .../Api/Commands/MovePieceCommand.cs | 39 ++++++++++--------- Shogi.Contracts/Types/Session.cs | 2 +- Shogi.Contracts/Types/SessionMetadata.cs | 13 +++---- Shogi.Contracts/Types/WhichPerspective.cs | 11 +++--- Shogi.Contracts/Types/WhichPiece.cs | 23 ++++++----- Shogi.UI/Shared/ShogiApi.cs | 2 +- 11 files changed, 48 insertions(+), 70 deletions(-) delete mode 100644 Shogi.Api/Extensions/WebSocketExtensions.cs diff --git a/Shogi.Api/Application/ShogiApplication.cs b/Shogi.Api/Application/ShogiApplication.cs index 0851b50..fc38692 100644 --- a/Shogi.Api/Application/ShogiApplication.cs +++ b/Shogi.Api/Application/ShogiApplication.cs @@ -5,7 +5,7 @@ using Shogi.Api.Extensions; using Shogi.Api.Identity; using Shogi.Api.Repositories; using Shogi.Api.Repositories.Dto; -using Shogi.Contracts.Api; +using Shogi.Contracts.Api.Commands; using Shogi.Domain.Aggregates; using System.Data.SqlClient; diff --git a/Shogi.Api/Controllers/SessionsController.cs b/Shogi.Api/Controllers/SessionsController.cs index 82aa771..4df8378 100644 --- a/Shogi.Api/Controllers/SessionsController.cs +++ b/Shogi.Api/Controllers/SessionsController.cs @@ -3,7 +3,7 @@ using Microsoft.AspNetCore.Mvc; using Shogi.Api.Application; using Shogi.Api.Extensions; using Shogi.Api.Repositories; -using Shogi.Contracts.Api; +using Shogi.Contracts.Api.Commands; using Shogi.Contracts.Types; namespace Shogi.Api.Controllers; diff --git a/Shogi.Api/Extensions/ContractsExtensions.cs b/Shogi.Api/Extensions/ContractsExtensions.cs index b645b77..71321e0 100644 --- a/Shogi.Api/Extensions/ContractsExtensions.cs +++ b/Shogi.Api/Extensions/ContractsExtensions.cs @@ -49,7 +49,6 @@ public static class ContractsExtensions public static Dictionary ToContract(this ReadOnlyDictionary boardState) => boardState.ToDictionary(kvp => kvp.Key, kvp => kvp.Value?.ToContract()); - public static Domain.ValueObjects.WhichPiece? ToDomain(this WhichPiece? piece) => piece.HasValue ? piece.Value.ToDomain() : null; public static Domain.ValueObjects.WhichPiece ToDomain(this WhichPiece piece) { return piece switch diff --git a/Shogi.Api/Extensions/WebSocketExtensions.cs b/Shogi.Api/Extensions/WebSocketExtensions.cs deleted file mode 100644 index 5b22b67..0000000 --- a/Shogi.Api/Extensions/WebSocketExtensions.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System.Net.WebSockets; -using System.Text; - -namespace Shogi.Api.Extensions -{ - public static class WebSocketExtensions - { - public static async Task SendTextAsync(this WebSocket self, string message) - { - await self.SendAsync(Encoding.UTF8.GetBytes(message), WebSocketMessageType.Text, true, CancellationToken.None); - } - - public static async Task ReceiveTextAsync(this WebSocket self) - { - var buffer = new ArraySegment(new byte[2048]); - var receive = await self.ReceiveAsync(buffer, CancellationToken.None); - return Encoding.UTF8.GetString(buffer.Slice(0, receive.Count)); - // TODO: Make this robust to multi-frame messages. - } - } -} diff --git a/Shogi.Api/Repositories/SessionRepository.cs b/Shogi.Api/Repositories/SessionRepository.cs index 0c6eaa3..7ccc6e5 100644 --- a/Shogi.Api/Repositories/SessionRepository.cs +++ b/Shogi.Api/Repositories/SessionRepository.cs @@ -1,6 +1,6 @@ using Dapper; using Shogi.Api.Repositories.Dto; -using Shogi.Contracts.Api; +using Shogi.Contracts.Api.Commands; using Shogi.Domain.Aggregates; using System.Data; using System.Data.SqlClient; diff --git a/Shogi.Contracts/Api/Commands/MovePieceCommand.cs b/Shogi.Contracts/Api/Commands/MovePieceCommand.cs index 7e77549..741fd69 100644 --- a/Shogi.Contracts/Api/Commands/MovePieceCommand.cs +++ b/Shogi.Contracts/Api/Commands/MovePieceCommand.cs @@ -3,16 +3,16 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Text.RegularExpressions; -namespace Shogi.Contracts.Api; +namespace Shogi.Contracts.Api.Commands; -public class MovePieceCommand : IValidatableObject +public partial class MovePieceCommand : IValidatableObject { /// /// For serialization. /// public MovePieceCommand() { - this.To = string.Empty; + To = string.Empty; } /// @@ -20,9 +20,9 @@ public class MovePieceCommand : IValidatableObject /// public MovePieceCommand(string from, string to, bool isPromotion) { - this.From = from; - this.To = to; - this.IsPromotion = isPromotion; + From = from; + To = to; + IsPromotion = isPromotion; } /// @@ -30,8 +30,8 @@ public class MovePieceCommand : IValidatableObject /// public MovePieceCommand(WhichPiece pieceFromHand, string to) { - this.PieceFromHand = pieceFromHand; - this.To = to; + PieceFromHand = pieceFromHand; + To = to; } /// @@ -39,7 +39,7 @@ public class MovePieceCommand : IValidatableObject /// Set this property to indicate moving a piece from the hand onto the board. /// public WhichPiece? PieceFromHand { get; set; } - + /// /// Board position notation, like A3 or G1 /// Mutually exclusive with . @@ -52,26 +52,29 @@ public class MovePieceCommand : IValidatableObject /// [Required] public string To { get; set; } - + public bool? IsPromotion { get; set; } public IEnumerable Validate(ValidationContext validationContext) { - if (this.PieceFromHand.HasValue && !string.IsNullOrWhiteSpace(this.From)) + if (PieceFromHand.HasValue && !string.IsNullOrWhiteSpace(From)) { - yield return new ValidationResult($"{nameof(this.PieceFromHand)} and {nameof(this.From)} are mutually exclusive properties."); + yield return new ValidationResult($"{nameof(PieceFromHand)} and {nameof(From)} are mutually exclusive properties."); } - if (this.PieceFromHand.HasValue && this.IsPromotion.HasValue) + if (PieceFromHand.HasValue && IsPromotion.HasValue) { - yield return new ValidationResult($"{nameof(this.PieceFromHand)} and {nameof(this.IsPromotion)} are mutually exclusive properties."); + yield return new ValidationResult($"{nameof(PieceFromHand)} and {nameof(IsPromotion)} are mutually exclusive properties."); } - if (!Regex.IsMatch(this.To, "[A-I][1-9]")) + if (!BoardNotationRegex().IsMatch(To)) { - yield return new ValidationResult($"{nameof(this.To)} must be a valid board position, between A1 and I9"); + yield return new ValidationResult($"{nameof(To)} must be a valid board position, between A1 and I9"); } - if (!string.IsNullOrEmpty(this.From) && !Regex.IsMatch(this.From, "[A-I][1-9]")) + if (!string.IsNullOrEmpty(From) && !BoardNotationRegex().IsMatch(From)) { - yield return new ValidationResult($"{nameof(this.From)} must be a valid board position, between A1 and I9"); + yield return new ValidationResult($"{nameof(From)} must be a valid board position, between A1 and I9"); } } + + [GeneratedRegex("[A-I][1-9]")] + private static partial Regex BoardNotationRegex(); } diff --git a/Shogi.Contracts/Types/Session.cs b/Shogi.Contracts/Types/Session.cs index 8f83cd5..bc67642 100644 --- a/Shogi.Contracts/Types/Session.cs +++ b/Shogi.Contracts/Types/Session.cs @@ -7,7 +7,7 @@ public class Session /// /// Email /// - public string Player1 { get; set; } + public string Player1 { get; set; } = string.Empty; /// /// Email. Null if no second player exists. diff --git a/Shogi.Contracts/Types/SessionMetadata.cs b/Shogi.Contracts/Types/SessionMetadata.cs index 14563f9..8b3c2b3 100644 --- a/Shogi.Contracts/Types/SessionMetadata.cs +++ b/Shogi.Contracts/Types/SessionMetadata.cs @@ -1,11 +1,10 @@ using System; -namespace Shogi.Contracts.Types +namespace Shogi.Contracts.Types; + +public class SessionMetadata { - public class SessionMetadata - { - public Guid SessionId { get; set; } - public string Player1 { get; set; } = string.Empty; - public string Player2 { get; set; } = string.Empty; - } + public Guid SessionId { get; set; } + public string Player1 { get; set; } = string.Empty; + public string Player2 { get; set; } = string.Empty; } diff --git a/Shogi.Contracts/Types/WhichPerspective.cs b/Shogi.Contracts/Types/WhichPerspective.cs index c29574f..6016326 100644 --- a/Shogi.Contracts/Types/WhichPerspective.cs +++ b/Shogi.Contracts/Types/WhichPerspective.cs @@ -1,8 +1,7 @@ -namespace Shogi.Contracts.Types +namespace Shogi.Contracts.Types; + +public enum WhichPlayer { - public enum WhichPlayer - { - Player1, - Player2 - } + Player1, + Player2 } diff --git a/Shogi.Contracts/Types/WhichPiece.cs b/Shogi.Contracts/Types/WhichPiece.cs index 6e07e3c..2a13589 100644 --- a/Shogi.Contracts/Types/WhichPiece.cs +++ b/Shogi.Contracts/Types/WhichPiece.cs @@ -1,14 +1,13 @@ -namespace Shogi.Contracts.Types +namespace Shogi.Contracts.Types; + +public enum WhichPiece { - public enum WhichPiece - { - King, - GoldGeneral, - SilverGeneral, - Bishop, - Rook, - Knight, - Lance, - Pawn - } + King, + GoldGeneral, + SilverGeneral, + Bishop, + Rook, + Knight, + Lance, + Pawn } diff --git a/Shogi.UI/Shared/ShogiApi.cs b/Shogi.UI/Shared/ShogiApi.cs index abd86d7..34cb91e 100644 --- a/Shogi.UI/Shared/ShogiApi.cs +++ b/Shogi.UI/Shared/ShogiApi.cs @@ -1,4 +1,4 @@ -using Shogi.Contracts.Api; +using Shogi.Contracts.Api.Commands; using Shogi.Contracts.Types; using System.Net; using System.Net.Http.Json;