From 1d4f373c8fce26ecfd99661d03b9301be5f5e3cb Mon Sep 17 00:00:00 2001 From: mcmuzzle Date: Mon, 3 Jun 2024 09:16:17 +0200 Subject: [PATCH] Ajout de commentaire automatique dans les PR --- .editorconfig | 1 + .gitea/workflows/Quality.yaml | 82 +++++++++++++++++ .gitea/workflows/tests.yaml | 24 ----- .../LittleTown.Core.Tests.csproj | 2 + src/LittleTown.Core.Tests/MatchTesting.cs | 12 ++- src/LittleTown.Core/MatchAggregate/Match.cs | 84 ++++++++++++++++- src/LittleTown.Core/PlayerZone/Objective.cs | 18 ++++ src/LittleTown.Core/PlayerZone/PlayerZone.cs | 45 +++++++++ .../PortInterfaces/IStaticDataGetter.cs | 7 +- .../{ => ValueTypes}/Building.cs | 0 .../Data/Objectives.json | 92 +++++++++++++++++++ .../StaticDataGetter.cs | 10 ++ 12 files changed, 350 insertions(+), 27 deletions(-) create mode 100644 .gitea/workflows/Quality.yaml delete mode 100644 .gitea/workflows/tests.yaml create mode 100644 src/LittleTown.Core/PlayerZone/Objective.cs create mode 100644 src/LittleTown.Core/PlayerZone/PlayerZone.cs rename src/LittleTown.Core/{ => ValueTypes}/Building.cs (100%) 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/.gitea/workflows/Quality.yaml b/.gitea/workflows/Quality.yaml new file mode 100644 index 0000000..6990f85 --- /dev/null +++ b/.gitea/workflows/Quality.yaml @@ -0,0 +1,82 @@ +name: "Main Build Process" + +# Runs on main branch commits, +# every commit in a pull request, any published release. +on: + push: + branches: ["main"] + pull_request: + branches: ["main"] + release: + types: [published] + +env: + REGISTRY: gitea.borealian.ovh + IMAGE_NAME: ${{ github.repository }} +jobs: + build: + name: "Build & Test" + + # Permissions this GitHub Action needs for other things in GitHub + permissions: write-all + + runs-on: ubuntu-latest + + steps: + - name: Check out the code + uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: Setup .NET + uses: actions/setup-dotnet@v4 + with: + dotnet-version: | + 8.x + - name: "Restore/Build/Test" + run: | + dotnet test ./src/LittleTown.sln --configuration Release --verbosity normal --logger trx --collect:"XPlat Code Coverage" --logger:"trx;LogFileName=test_results.xml" + - name: Combine Coverage Reports # This is because one report is produced per project, and we want one result for all of them. + uses: danielpalme/ReportGenerator-GitHub-Action@5.3.8 + with: + reports: "**/*.cobertura.xml" # REQUIRED # The coverage reports that should be parsed (separated by semicolon). Globbing is supported. + targetdir: "${{ github.workspace }}" # REQUIRED # The directory where the generated report should be saved. + reporttypes: "Cobertura" # The output formats and scope (separated by semicolon) Values: Badges, Clover, Cobertura, CsvSummary, Html, Html_Dark, Html_Light, Html_BlueRed, HtmlChart, HtmlInline, HtmlInline_AzurePipelines, HtmlInline_AzurePipelines_Dark, HtmlInline_AzurePipelines_Light, HtmlSummary, JsonSummary, Latex, LatexSummary, lcov, MarkdownSummary, MarkdownSummaryGithub, MarkdownDeltaSummary, MHtml, PngChart, SonarQube, TeamCitySummary, TextSummary, TextDeltaSummary, Xml, XmlSummary + verbosity: "Info" # The verbosity level of the log messages. Values: Verbose, Info, Warning, Error, Off + title: "Code Coverage" # Optional title. + tag: "${{ github.run_number }}_${{ github.run_id }}" # Optional tag or build version. + customSettings: "" # Optional custom settings (separated by semicolon). See: https://github.com/danielpalme/ReportGenerator/wiki/Settings. + toolpath: "reportgeneratortool" # Default directory for installing the dotnet tool. + - name: Upload Combined Coverage XML + uses: actions/upload-artifact@v3 + with: + name: coverage + path: ${{ github.workspace }}/Cobertura.xml + retention-days: 5 + - name: Publish Code Coverage Report + uses: irongut/CodeCoverageSummary@v1.3.0 + with: + filename: "Cobertura.xml" + badge: true + fail_below_min: false # just informative for now + format: markdown + hide_branch_rate: false + hide_complexity: false + indicators: true + output: both + thresholds: "10 30" + - name: Comment on Gitea PR + env: + GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }} + GITEA_URL: https://gitea.borealian.ovh + REPO_OWNER: mcmuzzle + REPO_NAME: LittleTown + PR_NUMBER: ${{ github.event.pull_request.number }} + + run: | + COMMENT_CONTENT=$(cat code-coverage-results.md) + curl -X POST \ + -H "Authorization: token ${GITEA_TOKEN}" \ + -H "Content-Type: application/json" \ + -d "{\"body\": \"${COMMENT_CONTENT}\"}" \ + ${GITEA_URL}/api/v1/repos/${REPO_OWNER}/${REPO_NAME}/issues/${PR_NUMBER}/comments + diff --git a/.gitea/workflows/tests.yaml b/.gitea/workflows/tests.yaml deleted file mode 100644 index df42205..0000000 --- a/.gitea/workflows/tests.yaml +++ /dev/null @@ -1,24 +0,0 @@ -name: dotnet package - -on: [push] - -jobs: - build: - - runs-on: ubuntu-latest - strategy: - matrix: - dotnet-version: [ '8.0.x' ] - - steps: - - uses: actions/checkout@v4 - - name: Setup dotnet - uses: actions/setup-dotnet@v3 - with: - dotnet-version: '8.0.x' - - name: Install dependencies - run: dotnet restore ./src/LittleTown.sln - - name: Build - run: dotnet build ./src/LittleTown.sln - - name: Test with the dotnet CLI - run: dotnet test ./src/LittleTown.sln \ No newline at end of file diff --git a/src/LittleTown.Core.Tests/LittleTown.Core.Tests.csproj b/src/LittleTown.Core.Tests/LittleTown.Core.Tests.csproj index eea66cf..4ffeaf3 100644 --- a/src/LittleTown.Core.Tests/LittleTown.Core.Tests.csproj +++ b/src/LittleTown.Core.Tests/LittleTown.Core.Tests.csproj @@ -10,7 +10,9 @@ + + runtime; build; native; contentfiles; analyzers; buildtransitive 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 0cc6639..b411451 100644 --- a/src/LittleTown.Core/MatchAggregate/Match.cs +++ b/src/LittleTown.Core/MatchAggregate/Match.cs @@ -1,3 +1,4 @@ +using LittleTown.Core.Exceptions; using LittleTown.Core.Ports; namespace LittleTown.Core; @@ -9,8 +10,23 @@ public class Match { private const int _minPlayerCount = 2; private const int _maxPlayerCount = 4; + + private int _maxWorkerPerPlayer; + private int _maxBuidingPerPlayer; + private readonly Board _board; private ICollection _buildings; + private ICollection _objectives; + + private Random _random = new Random(); + + private Dictionary _playersZones = new Dictionary(); + + /// 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 @@ -24,7 +40,73 @@ public class Match ArgumentOutOfRangeException.ThrowIfGreaterThan(nbPlayer, _maxPlayerCount); _board = staticData.GetBoard(1); - _buildings = staticData.GetBuildings(); + _objectives = staticData.GetObjectives(); + + List freeObjectiveIndexs = Enumerable.Range(0, _objectives.Count).ToList(); + for (int i = 0; i < nbPlayer; i++) + { + PlayerZone zone = new PlayerZone() + { + Objectives = GetRandomObjectives(nbPlayer switch + { + 2 => 4, + 3 => 3, + 4 => 2, + _ => throw new MatchConfigException("Mauvais nombre de joueurs lors des objectifs") + }, freeObjectiveIndexs) + }; + zone.AddRessources(Enums.ResourceType.Piece, 3); + _playersZones.Add(i, zone); + } + + _maxWorkerPerPlayer = nbPlayer switch + { + 2 => 5, + 3 => 4, + 4 => 3, + _ => throw new MatchConfigException("Mauvais nombre de joueurs lors Workers") + }; + + _maxBuidingPerPlayer = nbPlayer switch + { + 2 => 7, + 3 => 6, + 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) + { + List result = new List(); + for (int i = 0; i < number; i++) + { + int randomIndex = _random.Next(freeIndex.Count); + int cardIndex = freeIndex[randomIndex]; + freeIndex.RemoveAt(randomIndex); + result.Add(_objectives.ElementAt(randomIndex)); + } + return result; } } \ No newline at end of file diff --git a/src/LittleTown.Core/PlayerZone/Objective.cs b/src/LittleTown.Core/PlayerZone/Objective.cs new file mode 100644 index 0000000..2288576 --- /dev/null +++ b/src/LittleTown.Core/PlayerZone/Objective.cs @@ -0,0 +1,18 @@ +namespace LittleTown.Core; + +/// +/// Représente une carte objectif dans le jeu, les objectif sont des conditions qui une fois remplie accordent des points au joueur +/// +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; + + /// le nombre de points que procure cet objectif + public int Points { get; init; } + + /// indique si l'objectif a deja été atteint ou non, un objectif ne peut etre atteint qu'une fois + public bool Filled { get; init; } +} \ No newline at end of file diff --git a/src/LittleTown.Core/PlayerZone/PlayerZone.cs b/src/LittleTown.Core/PlayerZone/PlayerZone.cs new file mode 100644 index 0000000..b8aac26 --- /dev/null +++ b/src/LittleTown.Core/PlayerZone/PlayerZone.cs @@ -0,0 +1,45 @@ +using LittleTown.Core.Enums; + +namespace LittleTown.Core; +/// Représente les données propre à un joueur +public class PlayerZone : ICloneable +{ + /// Les ressources que possede le joueur + public IDictionary Ressources { get; init; } = new Dictionary(); + + /// La liste des objectifs que le joueur possede/// + public IReadOnlyCollection Objectives { get; init; } = new List(); + + /// Le marqueur de score pendant le match + public int ScoreMarker { get; init; } + + /// Permet d'ajouter d'une type de ressources au stock du joueur + /// le type de ressources + /// la quantité non nulle non négative + public void AddRessources(ResourceType res, int qte) + { + ArgumentOutOfRangeException.ThrowIfLessThan(qte, 1); + if (Ressources.ContainsKey(res)) + { + Ressources[res] += qte; + } + else + { + 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.Core/PortInterfaces/IStaticDataGetter.cs b/src/LittleTown.Core/PortInterfaces/IStaticDataGetter.cs index d580573..1e5f068 100644 --- a/src/LittleTown.Core/PortInterfaces/IStaticDataGetter.cs +++ b/src/LittleTown.Core/PortInterfaces/IStaticDataGetter.cs @@ -13,4 +13,9 @@ public interface IStaticDataGetter /// Recupérer la liste des batiments et leurs données statiques /// public ICollection GetBuildings(); -} \ No newline at end of file + + /// Récupérer la liste des objectifs du jeu + /// + public ICollection GetObjectives(); +} + diff --git a/src/LittleTown.Core/Building.cs b/src/LittleTown.Core/ValueTypes/Building.cs similarity index 100% rename from src/LittleTown.Core/Building.cs rename to src/LittleTown.Core/ValueTypes/Building.cs 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