From 81b7faae13bb9a064f1787cb0d4c8579b68f6505 Mon Sep 17 00:00:00 2001 From: mcmuzzle Date: Wed, 5 Jun 2024 23:16:22 +0200 Subject: [PATCH] =?UTF-8?q?fin=20de=20l'ajout=20de=20donn=C3=A9es=20au=20m?= =?UTF-8?q?atch?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .editorconfig | 1 + .../LittleTown.Core.Tests.csproj | 1 + src/LittleTown.Core.Tests/MatchTesting.cs | 12 ++- src/LittleTown.Core/MatchAggregate/Match.cs | 26 +++++- src/LittleTown.Core/PlayerZone/Objective.cs | 2 + src/LittleTown.Core/PlayerZone/PlayerZone.cs | 18 +++- .../Data/Objectives.json | 92 +++++++++++++++++++ .../StaticDataGetter.cs | 10 ++ 8 files changed, 158 insertions(+), 4 deletions(-) create mode 100644 src/LittleTown.StaticDataAccess/Data/Objectives.json diff --git a/.editorconfig b/.editorconfig index 90ac7fb..43ea4c3 100644 --- a/.editorconfig +++ b/.editorconfig @@ -5,6 +5,7 @@ root = true [*.cs] dotnet_diagnostic.IDE1006.severity = warning dotnet_diagnostic.IDE0005.severity = error +dotnet_diagnostic.CA5394.severity = none # Exclude generated code [src/**/Migrations/*.cs] diff --git a/src/LittleTown.Core.Tests/LittleTown.Core.Tests.csproj b/src/LittleTown.Core.Tests/LittleTown.Core.Tests.csproj index eea66cf..0ca78a7 100644 --- a/src/LittleTown.Core.Tests/LittleTown.Core.Tests.csproj +++ b/src/LittleTown.Core.Tests/LittleTown.Core.Tests.csproj @@ -10,6 +10,7 @@ + diff --git a/src/LittleTown.Core.Tests/MatchTesting.cs b/src/LittleTown.Core.Tests/MatchTesting.cs index bb56907..f7e7825 100644 --- a/src/LittleTown.Core.Tests/MatchTesting.cs +++ b/src/LittleTown.Core.Tests/MatchTesting.cs @@ -1,3 +1,4 @@ +using LittleTown.Core.Enums; using LittleTown.StaticDataAcces; namespace LittleTown.Core.Tests; @@ -20,8 +21,17 @@ public class MatchTesting } [Fact] - public void CheckBoardBoundaries() + public void TwoPlayerInitMatchTest() { + StaticDataGetter getter = new(); + Match match = new Match(2, getter); + PlayerZone player1 = match.GetPlayerZone(0); + PlayerZone player1_3 = match.GetPlayerZone(0); + Assert.Equal(3, player1.Ressources[Enums.ResourceType.Piece]); + player1.AddRessources(ResourceType.Piece, 1); + Assert.Equal(3, player1_3.Ressources[Enums.ResourceType.Piece]); + + Assert.Equal(4, player1.Objectives.Count); } } \ No newline at end of file diff --git a/src/LittleTown.Core/MatchAggregate/Match.cs b/src/LittleTown.Core/MatchAggregate/Match.cs index b6b4b07..60c99b6 100644 --- a/src/LittleTown.Core/MatchAggregate/Match.cs +++ b/src/LittleTown.Core/MatchAggregate/Match.cs @@ -25,6 +25,9 @@ public class Match /// LE numero du tour en cours (Partant de 1) public int CurrentTurn { get; private set; } = 1; + /// la liste indiquant l'ordre des joueurs, _playerTurnsOrder[0] donne l'index du 1er joueur, _playerTurnsOrder[1] du second..... + private List _playerTurnsOrder = new List(); + /// /// Constructeur d'une nouvelle partie avec un nombre de joueurs données en parametres /// @@ -72,6 +75,26 @@ public class Match 4 => 6, _ => throw new MatchConfigException("Mauvais nombre de joueurs lors building") }; + + // preparer l'ordre des joueurs + int index = _random.Next(nbPlayer); + for (int i = 0; i < nbPlayer; ++i) + { + _playerTurnsOrder.Add(index++); + if (index >= nbPlayer) + index = 0; + } + } + + /// Permet de récuperer une player zone(une copie) + /// + /// + public PlayerZone GetPlayerZone(int playerId) + { + ArgumentOutOfRangeException.ThrowIfLessThan(playerId, 0); + ArgumentOutOfRangeException.ThrowIfGreaterThan(playerId, _playersZones.Count - 1); + + return _playersZones[playerId].Clone() as PlayerZone ?? throw new ArgumentException("playerID is out of bound"); } private List GetRandomObjectives(int number, List freeIndex) @@ -80,8 +103,9 @@ public class Match for (int i = 0; i < number; i++) { #pragma warning disable CA5394 - int randomIndex = freeIndex[_random.Next(freeIndex.Count)]; + int randomIndex = _random.Next(freeIndex.Count); #pragma warning restore CA5394 + int cardIndex = freeIndex[randomIndex]; freeIndex.RemoveAt(randomIndex); result.Add(_objectives.ElementAt(randomIndex)); } diff --git a/src/LittleTown.Core/PlayerZone/Objective.cs b/src/LittleTown.Core/PlayerZone/Objective.cs index 67d0ba9..2288576 100644 --- a/src/LittleTown.Core/PlayerZone/Objective.cs +++ b/src/LittleTown.Core/PlayerZone/Objective.cs @@ -5,6 +5,8 @@ namespace LittleTown.Core; /// public class Objective { + /// la description, une clé pour la traduction + public string Description { get; init; } /// la condition de l'objectif sous forme de formule public string Formula { get; init; } = string.Empty; diff --git a/src/LittleTown.Core/PlayerZone/PlayerZone.cs b/src/LittleTown.Core/PlayerZone/PlayerZone.cs index 4fd14a1..b8aac26 100644 --- a/src/LittleTown.Core/PlayerZone/PlayerZone.cs +++ b/src/LittleTown.Core/PlayerZone/PlayerZone.cs @@ -2,7 +2,7 @@ using LittleTown.Core.Enums; namespace LittleTown.Core; /// Représente les données propre à un joueur -public class PlayerZone +public class PlayerZone : ICloneable { /// Les ressources que possede le joueur public IDictionary Ressources { get; init; } = new Dictionary(); @@ -18,7 +18,7 @@ public class PlayerZone /// la quantité non nulle non négative public void AddRessources(ResourceType res, int qte) { - ArgumentOutOfRangeException.ThrowIfLessThan(1, qte); + ArgumentOutOfRangeException.ThrowIfLessThan(qte, 1); if (Ressources.ContainsKey(res)) { Ressources[res] += qte; @@ -28,4 +28,18 @@ public class PlayerZone Ressources.Add(res, qte); } } + + /// Cloner ce playerZone + /// Une copie de l'objet + public object Clone() + { + PlayerZone result = new PlayerZone() + { + Ressources = new Dictionary(Ressources), + Objectives = new List(Objectives), + ScoreMarker = ScoreMarker + }; + + return result; + } } \ No newline at end of file diff --git a/src/LittleTown.StaticDataAccess/Data/Objectives.json b/src/LittleTown.StaticDataAccess/Data/Objectives.json new file mode 100644 index 0000000..ed666ae --- /dev/null +++ b/src/LittleTown.StaticDataAccess/Data/Objectives.json @@ -0,0 +1,92 @@ +[ + { + "Description": "desc", + "Formula": "formula", + "Points": 2 + }, + { + "Description": "desc", + "Formula": "formula", + "Points": 2 + }, + { + "Description": "desc", + "Formula": "formula", + "Points": 2 + }, + { + "Description": "desc", + "Formula": "formula", + "Points": 2 + }, + { + "Description": "desc", + "Formula": "formula", + "Points": 2 + }, + { + "Description": "desc", + "Formula": "formula", + "Points": 2 + }, + { + "Description": "desc", + "Formula": "formula", + "Points": 2 + }, + { + "Description": "desc", + "Formula": "formula", + "Points": 2 + }, + { + "Description": "desc", + "Formula": "formula", + "Points": 2 + }, + { + "Description": "desc", + "Formula": "formula", + "Points": 2 + }, + { + "Description": "desc", + "Formula": "formula", + "Points": 2 + }, + { + "Description": "desc", + "Formula": "formula", + "Points": 3 + }, + { + "Description": "desc", + "Formula": "formula", + "Points": 3 + }, + { + "Description": "desc", + "Formula": "formula", + "Points": 3 + }, + { + "Description": "desc", + "Formula": "formula", + "Points": 3 + }, + { + "Description": "desc", + "Formula": "formula", + "Points": 3 + }, + { + "Description": "desc", + "Formula": "formula", + "Points": 3 + }, + { + "Description": "desc", + "Formula": "formula", + "Points": 3 + } +] \ No newline at end of file diff --git a/src/LittleTown.StaticDataAccess/StaticDataGetter.cs b/src/LittleTown.StaticDataAccess/StaticDataGetter.cs index 5a3b7c8..9cc7df8 100644 --- a/src/LittleTown.StaticDataAccess/StaticDataGetter.cs +++ b/src/LittleTown.StaticDataAccess/StaticDataGetter.cs @@ -29,4 +29,14 @@ public class StaticDataGetter : IStaticDataGetter return buildings; } + + /// + public ICollection GetObjectives() + { + string path = Path.Combine(Environment.CurrentDirectory, "../../../../LittleTown.StaticDataAccess/Data/Objectives.json"); + string data = System.IO.File.ReadAllText(path); + List objectives = JsonSerializer.Deserialize>(data) ?? throw new JsonException("Cannot deserialize Objectives"); + + return objectives; + } } \ No newline at end of file