Ajout de la base de PrepareMatchCommand avec un test #24
@@ -1,41 +1,58 @@
|
|||||||
using System.Security.Claims;
|
using Giants.Core.Enums;
|
||||||
using System.Security.Cryptography.X509Certificates;
|
using Giants.Core.Exceptions;
|
||||||
|
|
||||||
namespace Giants.Core.Commands;
|
namespace Giants.Core.Commands;
|
||||||
|
|
||||||
public class PrepareMatchCommand : BaseMatchCommand<PrepareMatchResult>
|
public class PrepareMatchCommand : BaseMatchCommand<PrepareMatchResult>
|
||||||
{
|
{
|
||||||
public required int NbPlayer { get; init; }
|
public required IList<int> PlayerIDs { get; init; }
|
||||||
|
|
||||||
protected override PrepareMatchResult LocalMatchExecute(Match match)
|
protected override PrepareMatchResult LocalMatchExecute(Match match)
|
||||||
{
|
{
|
||||||
if (NbPlayer < 3 || NbPlayer > 5)
|
if (PlayerIDs.Count < 3 || PlayerIDs.Count > 5 || null == match)
|
||||||
return new PrepareMatchResult();
|
return new PrepareMatchResult();
|
||||||
|
|
||||||
|
|
||||||
//Gestion des assets des joueurs
|
//Gestion des assets des joueurs
|
||||||
for (int i = 0; i < NbPlayer; i++)
|
for (int i = 0; i < PlayerIDs.Count; i++)
|
||||||
{
|
{
|
||||||
|
match.SetPlayer(i, PlayerIDs[i]);
|
||||||
//marqueurs tribaux
|
//marqueurs tribaux
|
||||||
int nbMarker = NbMarkerPerPlayer(NbPlayer);
|
int nbMarker = NbMarkerPerPlayer(PlayerIDs.Count);
|
||||||
for (int t = 0; t < nbMarker; t++)
|
for (int t = 0; t < Match.MaxTribalMarker; t++)
|
||||||
{
|
{
|
||||||
|
if (t < nbMarker)
|
||||||
|
{
|
||||||
|
match.AddTribalMarkerToPlayer(i);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var piece = match.GetPlayerTribalTokenFromBox(i);
|
||||||
|
match.AssignPiece(piece, PiecePosition.Urne);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return new PrepareMatchResult();
|
return new PrepareMatchResult()
|
||||||
|
{
|
||||||
|
Success = true,
|
||||||
|
Match = match
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private int NbMarkerPerPlayer(int nbTotalPlayer)
|
private static int NbMarkerPerPlayer(int nbTotalPlayer)
|
||||||
{
|
{
|
||||||
return nbTotalPlayer switch
|
return nbTotalPlayer switch
|
||||||
{
|
{
|
||||||
3 => 7,
|
3 => 7,
|
||||||
4 => 6,
|
4 => 6,
|
||||||
5 => 5,
|
5 => 5,
|
||||||
_ => throw new Exception("Wrong player count for NbMarkerPerPlayer")
|
_ => throw new DataConversionException("Wrong player count for NbMarkerPerPlayer")
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class PrepareMatchResult : BaseCommandResult { }
|
public class PrepareMatchResult : BaseCommandResult
|
||||||
|
{
|
||||||
|
public Match? Match { get; init; }
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
using System.Collections;
|
||||||
|
using System.Reflection.Metadata.Ecma335;
|
||||||
using Giants.Core.Enums;
|
using Giants.Core.Enums;
|
||||||
|
|
||||||
namespace Giants.Core;
|
namespace Giants.Core;
|
||||||
@@ -41,11 +43,83 @@ public class Match
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region acessors for commands
|
#region Getters
|
||||||
|
public static int MaxTribalMarker { get => 6; }
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
public void AssignPiece(PieceIndex piece, PiecePosition position)
|
public void AssignPiece(PieceIndex piece, PiecePosition position)
|
||||||
{
|
{
|
||||||
pieces[(int)piece] = position;
|
pieces[(int)piece] = position;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetPlayer(int index, int playerId)
|
||||||
|
{
|
||||||
|
players[index] = playerId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PiecePosition GetPiece(PieceIndex PieceIndex)
|
||||||
|
{
|
||||||
|
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
|
#endregion
|
||||||
|
|
||||||
|
|||||||
77
Src/Giants.Core/Src/Entities/Player.cs
Normal file
77
Src/Giants.Core/Src/Entities/Player.cs
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
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; }
|
||||||
|
|
||||||
|
public Player(Match match)
|
||||||
|
{
|
||||||
|
_match = match;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int NbVisibleTribalTokenCount
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int NbHiddenTribalTokenCount
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private PieceIndex FirstTribalMarker { get => (PieceIndex)Enum.Parse(typeof(PieceIndex), $"player1TribalMarker{Index + 1}"); }
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ namespace Giants.Core.Enums;
|
|||||||
|
|
||||||
public enum PieceIndex
|
public enum PieceIndex
|
||||||
{
|
{
|
||||||
|
Unknown,
|
||||||
StartPlayer,
|
StartPlayer,
|
||||||
player1Chief,
|
player1Chief,
|
||||||
player1Shaman,
|
player1Shaman,
|
||||||
|
|||||||
14
Src/Giants.Core/Src/Exceptions/DataConversionException.cs
Normal file
14
Src/Giants.Core/Src/Exceptions/DataConversionException.cs
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
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) { }
|
||||||
|
}
|
||||||
40
Tests/Giants.Core.Tests/Commands/PrepareMatchCommandTest.cs
Normal file
40
Tests/Giants.Core.Tests/Commands/PrepareMatchCommandTest.cs
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
using Giants.Application;
|
||||||
|
using Giants.Core.Commands;
|
||||||
|
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 SimpleInit()
|
||||||
|
{
|
||||||
|
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<int>() { 12, 15, 5, 14, 9 } };
|
||||||
|
var resultPrep = prepCommand.Execute();
|
||||||
|
Assert.True(resultPrep.Success);
|
||||||
|
Assert.NotNull(resultPrep?.Match);
|
||||||
|
|
||||||
|
Player? p1 = resultPrep.Match.GetPlayer(0);
|
||||||
|
Assert.NotNull(p1);
|
||||||
|
|
||||||
|
Assert.Equal(5, p1.NbVisibleTribalTokenCount);
|
||||||
|
Assert.Equal(0, p1.NbHiddenTribalTokenCount);
|
||||||
|
Assert.Equal(1, p1.NbUrnTribalTokenCount);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user