diff --git a/Src/Giants.Core/Src/Commands/CommandBase.cs b/Src/Giants.Core/Src/Commands/BaseCommand.cs similarity index 100% rename from Src/Giants.Core/Src/Commands/CommandBase.cs rename to Src/Giants.Core/Src/Commands/BaseCommand.cs diff --git a/Src/Giants.Core/Src/Commands/BaseMatchCommand.cs b/Src/Giants.Core/Src/Commands/BaseMatchCommand.cs new file mode 100644 index 0000000..dcc7906 --- /dev/null +++ b/Src/Giants.Core/Src/Commands/BaseMatchCommand.cs @@ -0,0 +1,13 @@ +namespace Giants.Core.Commands; + +public abstract class BaseMatchCommand : BaseCommand where T : BaseCommandResult +{ + public required Match InputMatch { get; init; } + + protected abstract T LocalMatchExecute(Match match); + + protected override T LocalExecute() + { + return LocalMatchExecute(InputMatch.Clone()); + } +} \ No newline at end of file diff --git a/Src/Giants.Core/Src/Commands/PrepareMatchCommand.cs b/Src/Giants.Core/Src/Commands/PrepareMatchCommand.cs new file mode 100644 index 0000000..995ddda --- /dev/null +++ b/Src/Giants.Core/Src/Commands/PrepareMatchCommand.cs @@ -0,0 +1,41 @@ +using System.Security.Claims; +using System.Security.Cryptography.X509Certificates; + +namespace Giants.Core.Commands; + +public class PrepareMatchCommand : BaseMatchCommand +{ + public required int NbPlayer { get; init; } + + protected override PrepareMatchResult LocalMatchExecute(Match match) + { + if (NbPlayer < 3 || NbPlayer > 5) + return new PrepareMatchResult(); + + + //Gestion des assets des joueurs + for (int i = 0; i < NbPlayer; i++) + { + //marqueurs tribaux + int nbMarker = NbMarkerPerPlayer(NbPlayer); + for (int t = 0; t < nbMarker; t++) + { + + } + } + return new PrepareMatchResult(); + } + + private int NbMarkerPerPlayer(int nbTotalPlayer) + { + return nbTotalPlayer switch + { + 3 => 7, + 4 => 6, + 5 => 5, + _ => throw new Exception("Wrong player count for NbMarkerPerPlayer") + }; + } +} + +public class PrepareMatchResult : BaseCommandResult { } diff --git a/Src/Giants.Core/Src/Entities/Match.cs b/Src/Giants.Core/Src/Entities/Match.cs index bbdb5ff..f888758 100644 --- a/Src/Giants.Core/Src/Entities/Match.cs +++ b/Src/Giants.Core/Src/Entities/Match.cs @@ -1,5 +1,4 @@ -using System.Security.Cryptography.X509Certificates; -using Giants.Core.Interfaces; +using Giants.Core.Enums; namespace Giants.Core; @@ -18,15 +17,72 @@ public class Match #endregion #region données dynamiques + /// + /// IDs des joueurs qui participent a cette partie, + // -2 Pour une place non ouverte + // -1 pour une place non occupée mais ouverte + /// + int[] players = [-1, -1, -1, -1, -1]; + + /// Les scores des joueurs + int[] scores = [-1, -1, -1, -1, -1]; + + /// + /// etat des dés, -1 indique non utilisé et positif donne le numero de la face, + /// les 3 premiers dés sont les blanc et les 2 suivant les marrons + /// 4e dé utile pour la partie a 4 joueur, le 5e pour les parties a 5 joueurs + /// + int[] dices = [0, 0, 0, 0, 0,]; + + /// + /// 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()]; + #endregion + #region acessors for commands + public void AssignPiece(PieceIndex piece, PiecePosition position) + { + pieces[(int)piece] = position; + + } + #endregion + + /// + /// Cloner ce match pour pouvoir le modifier + /// + /// + public Match Clone() + { + Match result = new Match() + { + Board = Board, + Id = Id, + }; + + Buffer.BlockCopy(players, 0, result.players, 0, players.Length * sizeof(int)); + Buffer.BlockCopy(scores, 0, result.scores, 0, scores.Length * sizeof(int)); + Buffer.BlockCopy(dices, 0, result.dices, 0, dices.Length * sizeof(int)); + Buffer.BlockCopy(pieces, 0, result.pieces, 0, pieces.Length * sizeof(PieceIndex)); + + return result; + + } /// Permet de comparer des Match, cette methode est critique pour l'ia pour reduire son parcour d'arbre /// /// public override bool Equals(object? obj) { - return base.Equals(obj); + if (obj is Match match) + { + return Enumerable.SequenceEqual(players, match.players) && + Enumerable.SequenceEqual(scores, match.scores) && + Enumerable.SequenceEqual(dices, match.dices) && + Enumerable.SequenceEqual(pieces, match.pieces); + } + return false; } /// diff --git a/Src/Giants.Core/Src/Enums/PieceIndex.cs b/Src/Giants.Core/Src/Enums/PieceIndex.cs new file mode 100644 index 0000000..d28c373 --- /dev/null +++ b/Src/Giants.Core/Src/Enums/PieceIndex.cs @@ -0,0 +1,111 @@ +namespace Giants.Core.Enums; + +public enum PieceIndex +{ + StartPlayer, + player1Chief, + player1Shaman, + player1Worker1, + player1Worker2, + player1Worker3, + player1Worker4, + player1Worker5, + player1Worker6, + player1Base1, + player1Base2, + player1Base3, + player1Base4, + player1Base5, + player1Base6, + player1Base7, + player1TribalMarker1, + player1TribalMarker2, + player1TribalMarker3, + player1TribalMarker4, + player1TribalMarker5, + player1TribalMarker6, + player2Chief, + player2Shaman, + player2Worker1, + player2Worker2, + player2Worker3, + player2Worker4, + player2Worker5, + player2Worker6, + player2Base1, + player2Base2, + player2Base3, + player2Base4, + player2Base5, + player2Base6, + player2Base7, + player2TribalMarker1, + player2TribalMarker2, + player2TribalMarker3, + player2TribalMarker4, + player2TribalMarker5, + player2TribalMarker6, + player3Chief, + player3Shaman, + player3Worker1, + player3Worker2, + player3Worker3, + player3Worker4, + player3Worker5, + player3Worker6, + player3Base1, + player3Base2, + player3Base3, + player3Base4, + player3Base5, + player3Base6, + player3Base7, + player3TribalMarker1, + player3TribalMarker2, + player3TribalMarker3, + player3TribalMarker4, + player3TribalMarker5, + player3TribalMarker6, + player4Chief, + player4Shaman, + player4Worker1, + player4Worker2, + player4Worker3, + player4Worker4, + player4Worker5, + player4Worker6, + player4Base1, + player4Base2, + player4Base3, + player4Base4, + player4Base5, + player4Base6, + player4Base7, + player4TribalMarker1, + player4TribalMarker2, + player4TribalMarker3, + player4TribalMarker4, + player4TribalMarker5, + player4TribalMarker6, + player5Chief, + player5Shaman, + player5Worker1, + player5Worker2, + player5Worker3, + player5Worker4, + player5Worker5, + player5Worker6, + player5Base1, + player5Base2, + player5Base3, + player5Base4, + player5Base5, + player5Base6, + player5Base7, + player5TribalMarker1, + player5TribalMarker2, + player5TribalMarker3, + player5TribalMarker4, + player5TribalMarker5, + player5TribalMarker6 +} diff --git a/Src/Giants.Core/Src/Enums/PiecePosition.cs b/Src/Giants.Core/Src/Enums/PiecePosition.cs new file mode 100644 index 0000000..e920fff --- /dev/null +++ b/Src/Giants.Core/Src/Enums/PiecePosition.cs @@ -0,0 +1,98 @@ +using System.Xml.XPath; + +namespace Giants.Core.Enums; + +/// +/// Position que peut prendre une piece dans le jeu +/// +public enum PiecePosition +{ + boite, + player1Visible, + player2Visible, + player3Visible, + player4Visible, + player5Visible, + player1Hidden, + player2Hidden, + player3Hidden, + player4Hidden, + player5Hidden, + Carry, + Urne, + tile22, + tile39, + tile38, + tile37, + tile36, + tile54, + tile53, + tile52, + tile51, + tile50, + tile49, + tile70, + tile69, + tile68, + tile67, + tile66, + tile65, + tile64, + tile84, + tile83, + tile82, + tile81, + tile80, + tile79, + tile100, + tile99, + tile98, + tile97, + tile96, + tile95, + tile115, + tile114, + tile113, + tile112, + tile111, + tile130, + tile129, + tile128, + tile127, + tile145, + tile144, + tile143, + tile161, + tile160, + tile159, + tile177, + tile176, + tile175, + tile192, + tile191, + tile208, + tile207, + tile206, + tile223, + tile222 +} + +public static class PiecePositionExtension +{ + + public static PiecePosition FromTileIndex(this BoardLayout piece, int index) + { + string tmp = $"tile{index}"; + + object? result; + if (Enum.TryParse(typeof(PiecePosition), tmp, out result)) + { + if (null != result) + { + return (PiecePosition)result; + } + } + + throw new InvalidCastException($"Cannot cast tile index {index}"); + } +} \ No newline at end of file diff --git a/Tests/Giants.Core.Tests/Entities/BoardLayoutTests.cs b/Tests/Giants.Core.Tests/Entities/BoardLayoutTests.cs index 5731b03..17a05c1 100644 --- a/Tests/Giants.Core.Tests/Entities/BoardLayoutTests.cs +++ b/Tests/Giants.Core.Tests/Entities/BoardLayoutTests.cs @@ -1,3 +1,4 @@ +using Giants.Core.Enums; using Giants.Core.Interfaces; using Giants.Infrastructure; @@ -43,4 +44,12 @@ public class BoardLayoutTests } Assert.Equal(3, test.Count); } + + [Fact] + public void CheckIndexToEnum() + { + IHexagonalGrid grid = new HexagonalGridImpl(); + BoardLayout layout = new BoardLayout(grid); + Assert.Equal(PiecePosition.tile144, layout.FromTileIndex(144)); + } } \ No newline at end of file diff --git a/Tests/Giants.Core.Tests/Entities/MatchTests.cs b/Tests/Giants.Core.Tests/Entities/MatchTests.cs new file mode 100644 index 0000000..9ba159d --- /dev/null +++ b/Tests/Giants.Core.Tests/Entities/MatchTests.cs @@ -0,0 +1,34 @@ +using System.Reflection; +using Giants.Application; +using Giants.Core.Interfaces; +using Giants.Infrastructure; + +namespace Giants.Core.Tests; + +public class MatchTests +{ + + private readonly IMatchRepository _repo; + + public MatchTests() + { + IHexagonalGrid _grid = new HexagonalGridImpl(); + BoardLayout layout = new BoardLayout(_grid); + _repo = new MatchRepositoryMock(layout); + + var match1 = _repo.CreateMatch(); + } + + [Fact] + public void MatchClone() + { + Match? match = _repo.CreateMatch(); + Assert.NotNull(match); + + match.AssignPiece(Enums.PieceIndex.StartPlayer, Enums.PiecePosition.player1Visible); + + Match clone = match.Clone(); + + Assert.Equal(clone, match); + } +}