1 Commits

Author SHA1 Message Date
c95b29c3d2 Mise en place des MatchCommand
Some checks failed
check main state / build (9.0.x) (push) Has been cancelled
Ajout des données dynamiques au match
2025-04-02 23:14:59 +02:00
20 changed files with 36 additions and 425 deletions

View File

@@ -5,8 +5,6 @@ root = true
indent_style = space
indent_size = 4
charset = utf-8
dotnet_diagnostic.CA1307.severity = none
dotnet_diagnostic.CA5394.severity = none //Pour l'utilisation de random
[Src/HexagonalLib/**]
generated_code = true

View File

@@ -11,9 +11,9 @@ jobs:
dotnet-version: [ '9.0.x' ]
steps:
- uses: actions/checkout@v6
- uses: actions/checkout@v4
- name: Setup dotnet
uses: actions/setup-dotnet@v5
uses: actions/setup-dotnet@v3
with:
dotnet-version: '9.0.x'
- name: Install dependencies

View File

@@ -4,10 +4,6 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<AnalysisMode>All</AnalysisMode>
<GenerateDocumentationFile>false</GenerateDocumentationFile>
</PropertyGroup>
</Project>

View File

@@ -8,9 +8,6 @@ public abstract class BaseCommand<T> where T : BaseCommandResult
{
protected abstract T LocalExecute();
public required Random Random { get; init; }
/// <summary> Executer la commande, si nécessaire il faut avoir rempli les parametres en amont </summary>
public T Execute()
{

View File

@@ -7,5 +7,5 @@ namespace Giants.Core.Commands;
public abstract class BaseCommandResult
{
/// <summary> indique si le resultat est un success (true) ou un echec(false) </summary>
public bool Success { get; init; }
public bool Success { get; init; } = false;
}

View File

@@ -1,110 +1,41 @@
using Giants.Core.Enums;
using Giants.Core.Exceptions;
using System.Security.Claims;
using System.Security.Cryptography.X509Certificates;
namespace Giants.Core.Commands;
public class PrepareMatchCommand : BaseMatchCommand<PrepareMatchResult>
{
public required IList<int> PlayerIDs { get; init; }
public required int NbPlayer { get; init; }
protected override PrepareMatchResult LocalMatchExecute(Match match)
{
if (PlayerIDs.Count < 3 || PlayerIDs.Count > 5 || null == match)
if (NbPlayer < 3 || NbPlayer > 5)
return new PrepareMatchResult();
//Gestion des assets des joueurs
for (int itePlayer = 0; itePlayer < PlayerIDs.Count; itePlayer++)
for (int i = 0; i < NbPlayer; i++)
{
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)
//marqueurs tribaux
int nbMarker = NbMarkerPerPlayer(NbPlayer);
for (int t = 0; t < nbMarker; t++)
{
if (nbSocle > 0)
{
player.PutPieceInHiddenArea(socle);
nbSocle--;
}
else
{
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.PutPieceInHiddenArea(marker);
}
else
{
match.AssignPiece(marker, PiecePosition.Urne);
}
i++;
}
//assigner les ouvriers (1 chef, 1 shaman et 1 worker chacun)
match.AssignPiece(player.Chef, player.HiddenPosition);
match.AssignPiece(player.Shaman, player.HiddenPosition);
int nbWorker = 1;
foreach (PieceIndex worker in player.Workers)
{
if (nbWorker > 0)
{
match.AssignPiece(worker, player.HiddenPosition);
nbWorker--;
}
else
{
match.AssignPiece(worker, PiecePosition.Urne);
}
}
match.AssignPiece(player.Workers.First(), player.HiddenPosition);
//score a zero
for (int iteScore = 0; iteScore < match.NbPlayer; iteScore++)
{
match.SetScore(iteScore, 0);
}
//choix d'un 1er joueur
Player? firstPlayer = match.GetPlayer(Random.Next(match.NbPlayer));
if (null == firstPlayer)
throw new InitialisationException("Cannot select first player, indexOutOfRange");
match.AssignPiece(PieceIndex.StartPlayer, firstPlayer.VisiblePosition);
}
return new PrepareMatchResult()
{
Success = true,
Match = match
};
return new PrepareMatchResult();
}
private static int NbSoclePerPlayer(int nbTotalPlayer)
private int NbMarkerPerPlayer(int nbTotalPlayer)
{
return nbTotalPlayer switch
{
3 => 7,
4 => 6,
5 => 5,
_ => throw new DataConversionException("Wrong player count for NbMarkerPerPlayer")
_ => throw new Exception("Wrong player count for NbMarkerPerPlayer")
};
}
}
public class PrepareMatchResult : BaseCommandResult
{
public Match? Match { get; init; }
}
public class PrepareMatchResult : BaseCommandResult { }

View File

@@ -1,5 +1,3 @@
using System.Collections;
using System.Reflection.Metadata.Ecma335;
using Giants.Core.Enums;
namespace Giants.Core;
@@ -36,61 +34,19 @@ public class Match
/// </summary>
int[] dices = [0, 0, 0, 0, 0,];
/// <summary>
/// Indique si pour une foret donnée le bois a ete ramassé, l'index et les quantités sont dans le boardLayout
/// </summary>
bool[] forestUsed = [false, false, false, false, false, false, false];
/// <summary>
/// un tableau indiquant ou sont placé les pieces, l'index est l'id de la piece et la valeur la position de la piece ///
/// </summary>
PiecePosition[] pieces = new PiecePosition[(int)Enum.GetValues(typeof(PieceIndex)).Cast<PieceIndex>().Max() + 1];
PiecePosition[] pieces = new PiecePosition[(int)Enum.GetValues(typeof(PieceIndex)).Cast<PieceIndex>().Max()];
#endregion
#region Getters
public static int MaxTribalMarker { get => 6; }
public static int MaxSocle { get => 7; }
public int NbPlayer { get => players.Count(p => p != -1); }
public Player? GetPlayer(int index)
{
if (index >= 0 && index < players.Length && players[index] != -1)
return new Player(this)
{
Index = index,
};
return null;
}
#endregion
#region Setters for commands
#region acessors for commands
public void AssignPiece(PieceIndex piece, PiecePosition position)
{
pieces[(int)piece] = position;
}
public void SetPlayer(int index, int playerId)
{
players[index] = playerId;
}
public void SetScore(int index, int score)
{
scores[index] = score;
}
public int GetScore(int index)
{
return scores[index];
}
public PiecePosition GetPiece(PieceIndex PieceIndex)
{
return pieces[(int)PieceIndex];
}
#endregion
/// <summary>
@@ -109,8 +65,6 @@ public class Match
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));
Buffer.BlockCopy(forestUsed, 0, result.forestUsed, 0, forestUsed.Length * sizeof(bool));
return result;
@@ -126,8 +80,7 @@ public class Match
return Enumerable.SequenceEqual(players, match.players) &&
Enumerable.SequenceEqual(scores, match.scores) &&
Enumerable.SequenceEqual(dices, match.dices) &&
Enumerable.SequenceEqual(pieces, match.pieces) &&
Enumerable.SequenceEqual(forestUsed, match.forestUsed);
Enumerable.SequenceEqual(pieces, match.pieces);
}
return false;
}

View File

@@ -1,74 +0,0 @@
using System.Collections.ObjectModel;
using System.Reflection.Metadata.Ecma335;
using Giants.Core.Enums;
namespace Giants.Core;
/// <summary>
/// Structure pour agréger les getters liés aux informations d'un joueur
/// </summary>
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;
}
#region Collections
public static readonly ICollection<PieceIndex>[] PlayerPieces ={
[.. Enum.GetValues<PieceIndex>().Where(p => p.ToString().Contains("player1"))],
[.. Enum.GetValues<PieceIndex>().Where(p => p.ToString().Contains("player2"))],
[.. Enum.GetValues<PieceIndex>().Where(p => p.ToString().Contains("player3"))],
[.. Enum.GetValues<PieceIndex>().Where(p => p.ToString().Contains("player4"))],
[.. Enum.GetValues<PieceIndex>().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 PiecePosition HiddenPosition => PlayerHiddenAreas[Index];
public PiecePosition VisiblePosition => PlayerVisibileAreas[Index];
public void PutPieceInVisibleArea(PieceIndex piece)
{
_match.AssignPiece(piece, VisiblePosition);
}
public void PutPieceInHiddenArea(PieceIndex piece)
{
_match.AssignPiece(piece, HiddenPosition);
}
public int Score => _match.GetScore(Index);
#endregion
#region workers
public PieceIndex Chef => PlayerPieces[Index].Where(p => p.ToString().Contains("Chief")).FirstOrDefault();
public PieceIndex Shaman => PlayerPieces[Index].Where(p => p.ToString().Contains("Shaman")).FirstOrDefault();
public ICollection<PieceIndex> Workers => [.. PlayerPieces[Index].Where(p => p.ToString().Contains("Worker"))];
#endregion
#region socles
public ICollection<PieceIndex> Bases => PlayerPieces[Index]?.Where(p => p.ToString().Contains("Base")).ToList() ?? [];
public int NbVisibleBase => Bases.Count(p => _match.GetPiece(p) == VisiblePosition);
public int NbHiddenBase => Bases.Count(p => _match.GetPiece(p) == HiddenPosition);
#endregion
#region Tribal markers
public ICollection<PieceIndex> Markers => PlayerPieces[Index]?.Where(p => p.ToString().Contains("TribalMarker")).ToList() ?? [];
public int NbVisibleTribalTokenCount => Markers.Count(p => _match.GetPiece(p) == VisiblePosition);
public int NbHiddenTribalTokenCount => Markers.Count(p => _match.GetPiece(p) == HiddenPosition);
public int NbUrnTribalTokenCount => Markers.Count(p => _match.GetPiece(p) == PiecePosition.Urne);
#endregion
}

View File

@@ -2,7 +2,6 @@ namespace Giants.Core.Enums;
public enum PieceIndex
{
Unknown,
StartPlayer,
player1Chief,
player1Shaman,

View File

@@ -1,14 +0,0 @@
namespace Giants.Core.Exceptions;
/// <summary>
/// Exception levée lors d'un probleme de conversion d'un objet du core
/// </summary>
public class DataConversionException : System.Exception
{
/// <inheritdoc/>>
public DataConversionException() { }
/// <inheritdoc/>>
public DataConversionException(string message) : base(message) { }
/// <inheritdoc/>>
public DataConversionException(string message, System.Exception inner) : base(message, inner) { }
}

View File

@@ -1,14 +0,0 @@
namespace Giants.Core.Exceptions;
/// <summary>
/// Exception levée lors d'un probleme pendant l'initialisation de la partie
/// </summary>
public class InitialisationException : System.Exception
{
/// <inheritdoc/>>
public InitialisationException() { }
/// <inheritdoc/>>
public InitialisationException(string message) : base(message) { }
/// <inheritdoc/>>
public InitialisationException(string message, System.Exception inner) : base(message, inner) { }
}

View File

@@ -1,6 +1,6 @@
namespace Giants.Core;
public record struct AxialCoords
public struct AxialCoords
{
public int Q { get; init; }
public int R { get; init; }

View File

@@ -16,18 +16,6 @@ public class BoardLayout
Dictionary<int, TileInfos> _tiles;
Dictionary<AxialCoords, int> _forest = new Dictionary<AxialCoords, int>(){
{new AxialCoords(4,2),0},
{new AxialCoords(4,1),1},
{new AxialCoords(5,0),2},
{new AxialCoords(5,1),3},
{new AxialCoords(5,2),4},
{new AxialCoords(6,0),5},
{new AxialCoords(6,1),6},
};
int[] _forestQte = [5, 5, 4, 4, 3, 3, 3];
int width = 16;
public BoardLayout(IHexagonalGrid gridSystem)
@@ -43,21 +31,6 @@ public class BoardLayout
return tmp.Where(t => _tiles.ContainsKey(Index(t.Q, t.R))).ToList();
}
public int ForestQte(AxialCoords coords)
{
int index = _forest[coords];
return ForestQte(index);
}
public int ForestIndex(AxialCoords coords)
{
if (_forest.TryGetValue(coords, out int value))
{
return value;
}
return -1;
}
public int ForestQte(int index) => _forestQte[index];
[MemberNotNull(nameof(_tiles))]
private void BuildDefaultBoard()
{

View File

@@ -15,9 +15,9 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="nunit" Version="4.4.0" />
<PackageReference Include="NUnit3TestAdapter" Version="6.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
<PackageReference Include="nunit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
</ItemGroup>
<ItemGroup>

View File

@@ -8,11 +8,11 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="10.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
<PackageReference Include="coverlet.collector" Version="6.0.2" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
</ItemGroup>
<ItemGroup>

View File

@@ -21,13 +21,10 @@ public class GetMatchCommandTest
[Fact]
public void GetMatchNotExisting()
{
Random random = new Random(1);
GetMatchCommand command = new GetMatchCommand(_repo)
{
MatchId = 2,
Random = random
MatchId = 2
};
GetMatchResult result = command.Execute();
Assert.False(result.Success);
Assert.Null(result.Match);
@@ -36,11 +33,9 @@ public class GetMatchCommandTest
[Fact]
public void GetMatchSimple()
{
Random random = new Random(1);
GetMatchCommand command = new GetMatchCommand(_repo)
{
MatchId = 1,
Random = random
MatchId = 1
};
GetMatchResult result = command.Execute();
Assert.True(result.Success);

View File

@@ -19,8 +19,7 @@ public class NewMatchCommandTest
[Fact]
public void CreateBasicMatch()
{
Random random = new Random(1);
NewMatchCommand command = new NewMatchCommand(_repo) { Random = random };
NewMatchCommand command = new NewMatchCommand(_repo);
NewMatchResult result = command.Execute();
Assert.True(result.Success);
}
@@ -28,13 +27,12 @@ public class NewMatchCommandTest
[Fact]
public void TwoNewMatchShouldHaveDifferentID()
{
Random random = new Random(1);
NewMatchCommand command = new NewMatchCommand(_repo) { Random = random };
NewMatchCommand command = new NewMatchCommand(_repo);
NewMatchResult result = command.Execute();
Assert.True(result.Success);
Assert.NotNull(result.Match);
NewMatchCommand command2 = new NewMatchCommand(_repo) { Random = random };
NewMatchCommand command2 = new NewMatchCommand(_repo);
NewMatchResult result2 = command2.Execute();
Assert.True(result2.Success);
Assert.NotNull(result2.Match);

View File

@@ -1,115 +0,0 @@
using Giants.Application;
using Giants.Core.Commands;
using Giants.Core.Enums;
using Giants.Core.Interfaces;
using Giants.Infrastructure;
namespace Giants.Core.Tests;
public class PrepareMatchCommandTest
{
private readonly IMatchRepository _repo;
public PrepareMatchCommandTest()
{
IHexagonalGrid _grid = new HexagonalGridImpl();
BoardLayout layout = new BoardLayout(_grid);
_repo = new MatchRepositoryMock(layout);
}
[Fact]
void SimpleInit5Players()
{
Random random = new Random(1);
NewMatchCommand command = new NewMatchCommand(_repo) { Random = random };
NewMatchResult result = command.Execute();
Match? m = result.Match;
Assert.NotNull(m);
PrepareMatchCommand prepCommand = new PrepareMatchCommand() { InputMatch = m, PlayerIDs = new List<int>() { 12, 15, 5, 14, 9 }, Random = random };
var resultPrep = prepCommand.Execute();
Assert.True(resultPrep.Success);
Assert.NotNull(resultPrep?.Match);
for (int i = 0; i < 5; i++)
{
Player? p = resultPrep.Match.GetPlayer(i);
Assert.NotNull(p);
Assert.Equal(0, p.NbVisibleTribalTokenCount);
Assert.Equal(2, p.NbHiddenTribalTokenCount);
Assert.Equal(4, p.NbUrnTribalTokenCount);
Assert.Equal(5, p.NbHiddenBase);
Assert.Equal(p.HiddenPosition, resultPrep.Match.GetPiece(p.Chef));
Assert.Equal(p.HiddenPosition, resultPrep.Match.GetPiece(p.Shaman));
Assert.Equal(1, p.Workers.Count(w => resultPrep.Match.GetPiece(w) == p.HiddenPosition));
Assert.Equal(5, p.Workers.Count(w => resultPrep.Match.GetPiece(w) == Enums.PiecePosition.Urne));
Assert.Equal(0, p.Score);
Assert.NotEqual(PiecePosition.boite, resultPrep.Match.GetPiece(PieceIndex.StartPlayer));
}
}
[Fact]
void SimpleInit4Players()
{
Random random = new Random(1);
NewMatchCommand command = new NewMatchCommand(_repo) { Random = random };
NewMatchResult result = command.Execute();
Match? m = result.Match;
Assert.NotNull(m);
PrepareMatchCommand prepCommand = new PrepareMatchCommand() { InputMatch = m, PlayerIDs = new List<int>() { 12, 15, 5, 14 }, Random = random };
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(0, p.NbVisibleTribalTokenCount);
Assert.Equal(2, p.NbHiddenTribalTokenCount);
Assert.Equal(4, p.NbUrnTribalTokenCount);
Assert.Equal(6, p.NbHiddenBase);
Assert.Equal(p.HiddenPosition, resultPrep.Match.GetPiece(p.Chef));
Assert.Equal(p.HiddenPosition, resultPrep.Match.GetPiece(p.Shaman));
Assert.Equal(1, p.Workers.Count(w => resultPrep.Match.GetPiece(w) == p.HiddenPosition));
Assert.Equal(5, p.Workers.Count(w => resultPrep.Match.GetPiece(w) == Enums.PiecePosition.Urne));
Assert.Equal(0, p.Score);
Assert.NotEqual(PiecePosition.boite, resultPrep.Match.GetPiece(PieceIndex.StartPlayer));
}
}
[Fact]
void SimpleInit3Players()
{
Random random = new Random(1);
NewMatchCommand command = new NewMatchCommand(_repo) { Random = random };
NewMatchResult result = command.Execute();
Match? m = result.Match;
Assert.NotNull(m);
PrepareMatchCommand prepCommand = new PrepareMatchCommand() { InputMatch = m, PlayerIDs = new List<int>() { 12, 15, 5 }, Random = random };
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(0, p.NbVisibleTribalTokenCount);
Assert.Equal(2, p.NbHiddenTribalTokenCount);
Assert.Equal(4, p.NbUrnTribalTokenCount);
Assert.Equal(7, p.NbHiddenBase);
Assert.Equal(p.HiddenPosition, resultPrep.Match.GetPiece(p.Chef));
Assert.Equal(p.HiddenPosition, resultPrep.Match.GetPiece(p.Shaman));
Assert.Equal(1, p.Workers.Count(w => resultPrep.Match.GetPiece(w) == p.HiddenPosition));
Assert.Equal(5, p.Workers.Count(w => resultPrep.Match.GetPiece(w) == Enums.PiecePosition.Urne));
Assert.Equal(0, p.Score);
Assert.NotEqual(PiecePosition.boite, resultPrep.Match.GetPiece(PieceIndex.StartPlayer));
}
}
}

View File

@@ -52,16 +52,4 @@ public class BoardLayoutTests
BoardLayout layout = new BoardLayout(grid);
Assert.Equal(PiecePosition.tile144, layout.FromTileIndex(144));
}
[Fact]
public void CheckForestQuantities()
{
IHexagonalGrid grid = new HexagonalGridImpl();
BoardLayout layout = new BoardLayout(grid);
int forest1Index = layout.ForestIndex(new AxialCoords(4, 2));
Assert.Equal(0, forest1Index);
int qteForest1 = layout.ForestQte(forest1Index);
Assert.Equal(5, qteForest1);
}
}

View File

@@ -8,10 +8,10 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
<PackageReference Include="coverlet.collector" Version="6.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
</ItemGroup>
<ItemGroup>