From 4b921bdc97df163bf9c446f11d57f82c1386e6cc Mon Sep 17 00:00:00 2001 From: Fayti1703 Date: Sat, 27 Apr 2024 00:13:02 +0200 Subject: [PATCH 1/2] Engine: Add lane validation to ActionSet --- BinaryMatrixEngine/ActionSet.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/BinaryMatrixEngine/ActionSet.cs b/BinaryMatrixEngine/ActionSet.cs index 72a20ef..c28134d 100644 --- a/BinaryMatrixEngine/ActionSet.cs +++ b/BinaryMatrixEngine/ActionSet.cs @@ -34,7 +34,7 @@ public ActionSet(ActionType type, int lane) { if(type != ActionType.DRAW && type != ActionType.COMBAT) throw new ArgumentException("Invalid overload called for this type.", nameof(type)); this.type = type; - this.lane = lane; + this.lane = ValidateLane(lane, allowPseudo: type == ActionType.DRAW); this.card = default; } @@ -42,7 +42,15 @@ public ActionSet(ActionType type, CardSpecification card, int lane) { if(type != ActionType.PLAY && type != ActionType.FACEUP_PLAY && type != ActionType.DISCARD) throw new ArgumentException("Invalid overload called for this type.", nameof(type)); this.type = type; - this.lane = lane; + this.lane = ValidateLane(lane, allowPseudo: type == ActionType.DISCARD); this.card = card; } + + private static int ValidateLane(int lane, bool allowPseudo = false) { + return lane switch { + < 0 or > LANE_A => throw new ArgumentOutOfRangeException(nameof(lane), lane, "Lane must be within the range [0; " + LANE_A + "]."), + LANE_A when !allowPseudo => throw new ArgumentOutOfRangeException(nameof(lane), lane, "Lane must name a real lane, not the `a` pseudolane for this action."), + _ => lane + }; + } } From 0db89bdb484f3238d6a396f09881cfe09442eb19 Mon Sep 17 00:00:00 2001 From: Fayti1703 Date: Sat, 27 Apr 2024 00:13:14 +0200 Subject: [PATCH 2/2] Engine: Mark ActionSet readonly --- BinaryMatrixEngine/ActionSet.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BinaryMatrixEngine/ActionSet.cs b/BinaryMatrixEngine/ActionSet.cs index c28134d..7e87b13 100644 --- a/BinaryMatrixEngine/ActionSet.cs +++ b/BinaryMatrixEngine/ActionSet.cs @@ -13,7 +13,7 @@ public interface CardSpecification { public int? ResolveForPlayer(Player player); } -public struct ActionSet { +public readonly struct ActionSet { public readonly ActionType type; public readonly int lane; public readonly CardSpecification? card;