Skip to content

Commit

Permalink
Add lane validation to ActionSet (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fayti1703 authored Apr 26, 2024
2 parents de912da + 0db89bd commit 7abf0b9
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions BinaryMatrixEngine/ActionSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,15 +34,23 @@ 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;
}

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
};
}
}

0 comments on commit 7abf0b9

Please sign in to comment.