diff --git a/Src/Giants.Core/Src/Commands/PrepareMatchCommand.cs b/Src/Giants.Core/Src/Commands/PrepareMatchCommand.cs index e789a62..968c21d 100644 --- a/Src/Giants.Core/Src/Commands/PrepareMatchCommand.cs +++ b/Src/Giants.Core/Src/Commands/PrepareMatchCommand.cs @@ -14,23 +14,42 @@ public class PrepareMatchCommand : BaseMatchCommand //Gestion des assets des joueurs - for (int i = 0; i < PlayerIDs.Count; i++) + for (int itePlayer = 0; itePlayer < PlayerIDs.Count; itePlayer++) { - match.SetPlayer(i, PlayerIDs[i]); - //marqueurs tribaux - int nbMarker = NbMarkerPerPlayer(PlayerIDs.Count); - for (int t = 0; t < Match.MaxTribalMarker; t++) + match.SetPlayer(itePlayer, PlayerIDs[itePlayer]); + Player? player = match.GetPlayer(itePlayer); + if (null == player) + throw new ArgumentException($"Cannot get player {itePlayer} in PrepareMatchCommand"); + + //socles + int nbSocle = NbSoclePerPlayer(PlayerIDs.Count); + foreach (var socle in player.Bases) { - if (t < nbMarker) + if (nbSocle > 0) { - match.AddTribalMarkerToPlayer(i); + player.PutPieceInVisibleArea(socle); + nbSocle--; } else { - var piece = match.GetPlayerTribalTokenFromBox(i); - match.AssignPiece(piece, PiecePosition.Urne); + break; } + } + + //marqueurs tribaux (on ajoute 2 par joueurs et le reste dans l'urne) + int i = 0; + foreach (var marker in player.Markers) + { + if (i < 2) + { + player.PutPieceInVisibleArea(marker); + } + else + { + match.AssignPiece(marker, PiecePosition.Urne); + } + i++; } } return new PrepareMatchResult() @@ -40,7 +59,7 @@ public class PrepareMatchCommand : BaseMatchCommand }; } - private static int NbMarkerPerPlayer(int nbTotalPlayer) + private static int NbSoclePerPlayer(int nbTotalPlayer) { return nbTotalPlayer switch { diff --git a/Src/Giants.Core/Src/Entities/Match.cs b/Src/Giants.Core/Src/Entities/Match.cs index 5f23610..40bc533 100644 --- a/Src/Giants.Core/Src/Entities/Match.cs +++ b/Src/Giants.Core/Src/Entities/Match.cs @@ -39,12 +39,13 @@ public class Match /// /// un tableau indiquant ou sont placé les pieces, l'index est l'id de la piece et la valeur la position de la piece /// /// - PiecePosition[] pieces = new PiecePosition[(int)Enum.GetValues(typeof(PieceIndex)).Cast().Max()]; + PiecePosition[] pieces = new PiecePosition[(int)Enum.GetValues(typeof(PieceIndex)).Cast().Max() + 1]; #endregion #region Getters public static int MaxTribalMarker { get => 6; } + public static int MaxSocle { get => 7; } public int NbPlayer { get => players.Count(p => p != -1); } @@ -58,29 +59,6 @@ public class Match return null; } - - public PieceIndex GetPlayerTribalTokenFromBox(int playerIndex) - { - PieceIndex firstMarker = playerIndex switch - { - 0 => PieceIndex.player1TribalMarker1, - 1 => PieceIndex.player1TribalMarker2, - 2 => PieceIndex.player1TribalMarker3, - 3 => PieceIndex.player1TribalMarker4, - 4 => PieceIndex.player1TribalMarker5, - _ => throw new ArgumentOutOfRangeException($"{playerIndex} is invalid player Index") - }; - for (int i = 0; i < MaxTribalMarker; i++) - { - PieceIndex piece = firstMarker + i; - if (GetPiece(piece) == PiecePosition.boite) - { - return piece; - } - } - - return PieceIndex.Unknown; - } #endregion #region Setters for commands @@ -99,28 +77,6 @@ public class Match return pieces[(int)PieceIndex]; } - public void AddTribalMarkerToPlayer(int playerId) - { - PieceIndex firstElem = PieceIndex.StartPlayer; - PiecePosition target = PiecePosition.boite; - - switch (playerId) - { - case 0: firstElem = PieceIndex.player1TribalMarker1; target = PiecePosition.player1Visible; break; - case 1: firstElem = PieceIndex.player2TribalMarker1; target = PiecePosition.player2Visible; break; - case 2: firstElem = PieceIndex.player3TribalMarker1; target = PiecePosition.player3Visible; break; - case 3: firstElem = PieceIndex.player4TribalMarker1; target = PiecePosition.player4Visible; break; - case 4: firstElem = PieceIndex.player5TribalMarker1; target = PiecePosition.player5Visible; break; - } - for (int i = 0; i < 6; i++) - { - if (GetPiece(firstElem + i) == PiecePosition.boite) - { - AssignPiece(firstElem + i, target); - break; - } - } - } #endregion /// diff --git a/Src/Giants.Core/Src/Entities/Player.cs b/Src/Giants.Core/Src/Entities/Player.cs index 1dbf045..4515bdf 100644 --- a/Src/Giants.Core/Src/Entities/Player.cs +++ b/Src/Giants.Core/Src/Entities/Player.cs @@ -1,3 +1,4 @@ +using System.Collections.ObjectModel; using System.Reflection.Metadata.Ecma335; using Giants.Core.Enums; @@ -11,67 +12,50 @@ public class Player private Match _match; public required int Index { get; init; } + static Player() + { + // preparation des données statiques + return; + } + public Player(Match match) { _match = match; } - public int NbVisibleTribalTokenCount + #region Collections + public static readonly ICollection[] PlayerPieces ={ + [.. Enum.GetValues().Where(p => p.ToString().Contains("player1"))], + [.. Enum.GetValues().Where(p => p.ToString().Contains("player2"))], + [.. Enum.GetValues().Where(p => p.ToString().Contains("player3"))], + [.. Enum.GetValues().Where(p => p.ToString().Contains("player4"))], + [.. Enum.GetValues().Where(p => p.ToString().Contains("player5"))] + }; + public static readonly PiecePosition[] PlayerVisibileAreas = { PiecePosition.player1Visible, PiecePosition.player2Visible, PiecePosition.player3Visible, PiecePosition.player4Visible, PiecePosition.player5Visible }; + public static readonly PiecePosition[] PlayerHiddenAreas = { PiecePosition.player1Hidden, PiecePosition.player2Hidden, PiecePosition.player3Hidden, PiecePosition.player4Hidden, PiecePosition.player5Hidden }; + + #endregion Collections + + #region Generics + public void PutPieceInVisibleArea(PieceIndex piece) { - get - { - int result = 0; - var fisrtIndex = FirstTribalMarker; - for (int i = 0; i < Match.MaxTribalMarker; i++) - { - var pos = _match.GetPiece(fisrtIndex + i); - if (pos.ToString().Contains("Visible")) - { - result++; - } - } - return result; - } + _match.AssignPiece(piece, PlayerVisibileAreas[Index]); } - - public int NbHiddenTribalTokenCount + public void PutPieceInHiddenArea(PieceIndex piece) { - get - { - { - int result = 0; - var fisrtIndex = FirstTribalMarker; - for (int i = 0; i < Match.MaxTribalMarker; i++) - { - var pos = _match.GetPiece(fisrtIndex + i); - if (pos.ToString().Contains("hidden")) - { - result++; - } - } - return result; - } - } + _match.AssignPiece(piece, PlayerHiddenAreas[Index]); } + #endregion - public int NbUrnTribalTokenCount - { - get - { - int result = 0; - var fisrtIndex = FirstTribalMarker; - for (int i = 0; i < Match.MaxTribalMarker; i++) - { - var pos = _match.GetPiece(fisrtIndex + i); - if (pos == PiecePosition.Urne) - { - result++; - } - } + #region socles + public ICollection Bases => PlayerPieces[Index]?.Where(p => p.ToString().Contains("Base")).ToList() ?? []; + public int NbVisibleBase => Bases.Count(p => _match.GetPiece(p) == PlayerVisibileAreas[Index]); - return result; - } - } - - private PieceIndex FirstTribalMarker { get => (PieceIndex)Enum.Parse(typeof(PieceIndex), $"player1TribalMarker{Index + 1}"); } + #endregion + #region Tribal markers + public ICollection Markers => PlayerPieces[Index]?.Where(p => p.ToString().Contains("TribalMarker")).ToList() ?? []; + public int NbVisibleTribalTokenCount => Markers.Count(p => _match.GetPiece(p) == PlayerVisibileAreas[Index]); + public int NbHiddenTribalTokenCount => Markers.Count(p => _match.GetPiece(p) == PlayerHiddenAreas[Index]); + public int NbUrnTribalTokenCount => Markers.Count(p => _match.GetPiece(p) == PiecePosition.Urne); + #endregion } \ No newline at end of file diff --git a/Tests/Giants.Core.Tests/Commands/PrepareMatchCommandTest.cs b/Tests/Giants.Core.Tests/Commands/PrepareMatchCommandTest.cs index 135e61c..11f34fe 100644 --- a/Tests/Giants.Core.Tests/Commands/PrepareMatchCommandTest.cs +++ b/Tests/Giants.Core.Tests/Commands/PrepareMatchCommandTest.cs @@ -17,7 +17,7 @@ public class PrepareMatchCommandTest } [Fact] - void SimpleInit() + void SimpleInit5Players() { NewMatchCommand command = new NewMatchCommand(_repo); NewMatchResult result = command.Execute(); @@ -29,12 +29,65 @@ public class PrepareMatchCommandTest Assert.True(resultPrep.Success); Assert.NotNull(resultPrep?.Match); - Player? p1 = resultPrep.Match.GetPlayer(0); - Assert.NotNull(p1); + for (int i = 0; i < 5; i++) + { + Player? p = resultPrep.Match.GetPlayer(i); + Assert.NotNull(p); - Assert.Equal(5, p1.NbVisibleTribalTokenCount); - Assert.Equal(0, p1.NbHiddenTribalTokenCount); - Assert.Equal(1, p1.NbUrnTribalTokenCount); + Assert.Equal(2, p.NbVisibleTribalTokenCount); + Assert.Equal(0, p.NbHiddenTribalTokenCount); + Assert.Equal(4, p.NbUrnTribalTokenCount); + Assert.Equal(5, p.NbVisibleBase); + } + } + [Fact] + void SimpleInit4Players() + { + NewMatchCommand command = new NewMatchCommand(_repo); + NewMatchResult result = command.Execute(); + Match? m = result.Match; + Assert.NotNull(m); + + PrepareMatchCommand prepCommand = new PrepareMatchCommand() { InputMatch = m, PlayerIDs = new List() { 12, 15, 5, 14 } }; + var resultPrep = prepCommand.Execute(); + Assert.True(resultPrep.Success); + Assert.NotNull(resultPrep?.Match); + + for (int i = 0; i < 4; i++) + { + Player? p = resultPrep.Match.GetPlayer(i); + Assert.NotNull(p); + + Assert.Equal(2, p.NbVisibleTribalTokenCount); + Assert.Equal(0, p.NbHiddenTribalTokenCount); + Assert.Equal(4, p.NbUrnTribalTokenCount); + Assert.Equal(6, p.NbVisibleBase); + } + } + + [Fact] + void SimpleInit3Players() + { + NewMatchCommand command = new NewMatchCommand(_repo); + NewMatchResult result = command.Execute(); + Match? m = result.Match; + Assert.NotNull(m); + + PrepareMatchCommand prepCommand = new PrepareMatchCommand() { InputMatch = m, PlayerIDs = new List() { 12, 15, 5 } }; + var resultPrep = prepCommand.Execute(); + Assert.True(resultPrep.Success); + Assert.NotNull(resultPrep?.Match); + + for (int i = 0; i < 3; i++) + { + Player? p = resultPrep.Match.GetPlayer(i); + Assert.NotNull(p); + + Assert.Equal(2, p.NbVisibleTribalTokenCount); + Assert.Equal(0, p.NbHiddenTribalTokenCount); + Assert.Equal(4, p.NbUrnTribalTokenCount); + Assert.Equal(7, p.NbVisibleBase); + } } } \ No newline at end of file