< Summary - Code Coverage

Information
Class: LittleTown.Core.Match
Assembly: LittleTown.Core
File(s): /workspace/mcmuzzle/LittleTown/src/LittleTown.Core/Aggregates/MatchAggregate/Match.cs
Tag: 220_1454
Line coverage
94%
Covered lines: 73
Uncovered lines: 4
Coverable lines: 77
Total lines: 183
Line coverage: 94.8%
Branch coverage
88%
Covered branches: 30
Total branches: 34
Branch coverage: 88.2%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_CurrentTurn()100%11100%
get_CurrentPlayer()100%210%
get_IsDone()100%11100%
.ctor(...)100%11100%
AddPlayer(...)100%44100%
ExecuteAction(...)100%22100%
Init()83.33%181891.17%
GetPlayerZone(...)75%44100%
GetRandomObjectives(...)100%22100%
NextPlayer()100%44100%

File(s)

/workspace/mcmuzzle/LittleTown/src/LittleTown.Core/Aggregates/MatchAggregate/Match.cs

#LineLine coverage
 1using LittleTown.Core.Actions;
 2using LittleTown.Core.Exceptions;
 3using LittleTown.Core.Ports;
 4
 5namespace LittleTown.Core;
 6
 7/// <summary>
 8/// Represente un match de LittleTown
 9/// </summary>
 10public class Match
 11{
 12    /// <summary> LE numero du tour en cours (Partant de 1) </summary>
 4013    public int CurrentTurn { get; private set; } = 1;
 14
 15    /// <summary> l'id du joueur a qui c'est le tour de jouer</summary>
 016    public string CurrentPlayer { get => _players[_playerTurnsOrder[_currentPlayerIndex]].PlayerName; }
 17
 18    /// <summary> Indique si le match est terminé </summary>
 3019    public bool IsDone { get; private set; }
 20
 21    /// <summary>la liste indiquant l'ordre des joueurs, _playerTurnsOrder[0] donne l'index du 1er joueur, _playerTurnsO
 1622    private List<int> _playerTurnsOrder = new List<int>();
 23
 24    private const int _minPlayerCount = 2;
 25    private const int _maxPlayerCount = 4;
 26
 27    private int _maxWorkerPerPlayer;
 28    private int _maxBuidingPerPlayer;
 29
 30    private readonly Board _board;
 31    private ICollection<Building> _buildings;
 32    private ICollection<Objective> _objectives;
 33
 1634    private Random _random = new Random();
 35
 1636    private List<PlayerZone> _players = new();
 37    private int _currentPlayerIndex;
 38
 39    /// <summary>
 40    /// Constructeur d'une nouvelle partie avec un nombre de joueurs données en parametres
 41    /// </summary>
 42    /// <param name="staticData">un objet permettant de récupérer les données statiques du jeu</param>
 1643    public Match(IStaticDataGetter staticData)
 44    {
 1645        ArgumentNullException.ThrowIfNull(staticData);
 46
 1647        _board = staticData.GetBoard(1);
 1648        _buildings = staticData.GetBuildings();
 1649        _objectives = staticData.GetObjectives();
 1650    }
 51
 52    /// <summary> Ajouter un nouveau joueur a la partie </summary>
 53    /// <param name="playerName">le nom du joueur</param>
 54    public void AddPlayer(string playerName)
 55    {
 4456        if (_players.Count < _maxPlayerCount)
 57        {
 8458            if (_players.Any(p => p.PlayerName == playerName))
 59            {
 260                throw new MatchConfigException("Un joueur existe déjà avec ce nom");
 61            }
 4062            _players.Add(new PlayerZone()
 4063            {
 4064                PlayerName = playerName
 4065            });
 66        }
 67        else
 68        {
 269            throw new MatchConfigException("Impossible d'ajouter de nouveau joueur, la partie est pleine");
 70        }
 71    }
 72
 73
 74    /// <summary>
 75    /// Demander au match d'executer une action si elle est autorisée
 76    /// </summary>
 77    /// <param name="action">l'action a réaliser</param>
 78    public void ExecuteAction(IAction action)
 79    {
 1480        ArgumentNullException.ThrowIfNull(action);
 81
 82        //quelques vérification génériques pour savoir si l'action peut être jouée
 1483        if (IsDone)
 84        {
 285            throw new MatchFinishedException("Impossible d'effectuer une action sur un match terminé");
 86        }
 87
 88        //autoriser l'action a s'executer en lui donner les getters dont elle a besoin
 1289        action.Execute(this);
 1290    }
 91
 92    /// <summary> Initialiser la partie, il faut avoir ajouté les joueurs au préalable </summary>
 93    /// <exception cref="MatchConfigException"></exception>
 94    public void Init()
 95    {
 1496        int nbPlayer = _players.Count;
 97
 1498        ArgumentOutOfRangeException.ThrowIfLessThan(nbPlayer, _minPlayerCount);
 1299        ArgumentOutOfRangeException.ThrowIfGreaterThan(nbPlayer, _maxPlayerCount);
 100
 12101        List<int> freeObjectiveIndexs = Enumerable.Range(0, _objectives.Count).ToList();
 84102        foreach (PlayerZone zone in _players)
 103        {
 30104            zone.AddObjectives(GetRandomObjectives(nbPlayer switch
 30105            {
 16106                2 => 4,
 6107                3 => 3,
 8108                4 => 2,
 0109                _ => throw new MatchConfigException("Mauvais nombre de joueurs lors Workers")
 30110            }, freeObjectiveIndexs));
 30111            zone.AddRessources(Enums.ResourceType.Piece, 3);
 112        }
 113
 12114        _maxWorkerPerPlayer = nbPlayer switch
 12115        {
 8116            2 => 5,
 2117            3 => 4,
 2118            4 => 3,
 0119            _ => throw new MatchConfigException("Mauvais nombre de joueurs lors Workers")
 12120        };
 121
 12122        _maxBuidingPerPlayer = nbPlayer switch
 12123        {
 8124            2 => 7,
 2125            3 => 6,
 2126            4 => 6,
 0127            _ => throw new MatchConfigException("Mauvais nombre de joueurs lors building")
 12128        };
 129
 130        // preparer l'ordre des joueurs
 12131        int index = _random.Next(nbPlayer);
 84132        for (int i = 0; i < nbPlayer; ++i)
 133        {
 30134            _playerTurnsOrder.Add(index++);
 30135            if (index >= nbPlayer)
 12136                index = 0;
 137        }
 138
 12139        _currentPlayerIndex = 0;
 140
 12141    }
 142
 143    /// <summary> Permet de récuperer une player zone(une copie) </summary>
 144    /// <param name="playerName">le nom ou ID du joueur</param>
 145    /// <returns></returns>
 146    public PlayerZone GetPlayerZone(string playerName)
 147    {
 16148        var value = _players.Where(p => p.PlayerName == playerName).FirstOrDefault();
 149
 150
 6151        if (null == value)
 2152            throw new ArgumentException("playerID is out of bound");
 153
 4154        return value.Clone() as PlayerZone ?? throw new ArgumentException("Cast exception in GetPlayerZone"); ;
 155    }
 156
 157    private List<Objective> GetRandomObjectives(int number, List<int> freeIndex)
 158    {
 30159        List<Objective> result = new List<Objective>();
 256160        for (int i = 0; i < number; i++)
 161        {
 98162            int randomIndex = _random.Next(freeIndex.Count);
 98163            int cardIndex = freeIndex[randomIndex];
 98164            freeIndex.RemoveAt(randomIndex);
 98165            result.Add(_objectives.ElementAt(randomIndex));
 166        }
 30167        return result;
 168    }
 169    /// <summary> Changer le joueur en cours pour passer au suivant </summary>
 170    public void NextPlayer()
 171    {
 12172        _currentPlayerIndex++;
 12173        if (_currentPlayerIndex >= _players.Count)
 174        {
 6175            _currentPlayerIndex = 0;
 6176            CurrentTurn++;
 177        }
 12178        if (CurrentTurn >= 4)
 179        {
 2180            IsDone = true;
 181        }
 12182    }
 183}