2 Commits

Author SHA1 Message Date
01bf0193da ajout projet backend vide
All checks were successful
dotnet package / build (8.0.x) (push) Successful in 1m23s
2024-08-25 00:24:54 +02:00
bf41b9f2e4 temp
All checks were successful
dotnet package / build (8.0.x) (push) Successful in 1m29s
temp

fin de l'ajout de données au match
2024-06-18 23:02:49 +02:00
17 changed files with 438 additions and 10 deletions

View File

@@ -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]

View File

@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<InvariantGlobalization>true</InvariantGlobalization>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,6 @@
@LittleTown.Api_HostAddress = http://localhost:5166
GET {{LittleTown.Api_HostAddress}}/helloWorld/
Accept: application/json
###

View File

@@ -0,0 +1,26 @@
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.MapGet("/helloWorld", () =>
{
return "Hello World";
})
.WithName("HelloWorld")
.WithOpenApi();
app.Run();

View File

@@ -0,0 +1,41 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:12798",
"sslPort": 44325
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5166",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7114;http://localhost:5166",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}

View File

@@ -10,6 +10,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoMapper" Version="13.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">

View File

@@ -1,3 +1,5 @@
using LittleTown.Core.Enums;
using LittleTown.Core.Exceptions;
using LittleTown.StaticDataAcces;
namespace LittleTown.Core.Tests;
@@ -8,20 +10,51 @@ public class MatchTesting
public void EnforcePlayerCountInMatchCreation()
{
StaticDataGetter getter = new();
Assert.Throws<ArgumentOutOfRangeException>(() => { new Match(1, getter); });
var match = new Match(getter);
match.AddPlayer("Player1");
Assert.Throws<ArgumentOutOfRangeException>(() => { match.Init(); });
Match match2Player = new Match(2, getter);
Match match2 = new Match(getter);
match2.AddPlayer("Player1");
match2.AddPlayer("Player2");
match2.Init();
Match match3Player = new Match(3, getter);
Match match3 = new Match(getter);
match3.AddPlayer("Player1");
match3.AddPlayer("Player2");
match3.AddPlayer("Player3");
match3.Init();
Match match4Player = new Match(4, getter);
Match match4 = new Match(getter);
match4.AddPlayer("Player1");
match4.AddPlayer("Player2");
match4.AddPlayer("Player3");
match4.AddPlayer("Player4");
match4.Init();
Assert.Throws<ArgumentOutOfRangeException>(() => { new Match(5, getter); });
Match match5 = new Match(getter);
match5.AddPlayer("Player1");
match5.AddPlayer("Player2");
match5.AddPlayer("Player3");
match5.AddPlayer("Player4");
Assert.Throws<MatchConfigException>(() => { match5.AddPlayer("Player5"); });
}
[Fact]
public void CheckBoardBoundaries()
public void TwoPlayerInitMatchTest()
{
StaticDataGetter getter = new();
Match match = new Match(getter);
match.AddPlayer("Player1");
match.AddPlayer("Player2");
match.Init();
PlayerZone player1 = match.GetPlayerZone("Player1");
PlayerZone player1_3 = match.GetPlayerZone("Player2");
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);
}
}

View File

@@ -1,3 +1,4 @@
using LittleTown.Core.Exceptions;
using LittleTown.Core.Ports;
namespace LittleTown.Core;
@@ -9,22 +10,125 @@ 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<Building> _buildings;
private ICollection<Objective> _objectives;
private Random _random = new Random();
private Dictionary<string, PlayerZone> _players = new();
/// <summary> LE numero du tour en cours (Partant de 1) </summary>
public int CurrentTurn { get; private set; } = 1;
/// <summary>la liste indiquant l'ordre des joueurs, _playerTurnsOrder[0] donne l'index du 1er joueur, _playerTurnsOrder[1] du second..... </summary>
private List<int> _playerTurnsOrder = new List<int>();
/// <summary>
/// Constructeur d'une nouvelle partie avec un nombre de joueurs données en parametres
/// </summary>
/// <param name="nbPlayer"></param>
/// <param name="staticData">un objet permettant de récupérer les données statiques du jeu</param>
public Match(int nbPlayer, IStaticDataGetter staticData)
public Match(IStaticDataGetter staticData)
{
ArgumentNullException.ThrowIfNull(staticData);
_board = staticData.GetBoard(1);
_buildings = staticData.GetBuildings();
_objectives = staticData.GetObjectives();
}
/// <summary> Ajouter un nouveau joueur a la partie </summary>
/// <param name="playerName">le nom du joueur</param>
public void AddPlayer(string playerName)
{
if (_players.Count < _maxPlayerCount)
{
if (_players.ContainsKey(playerName))
{
throw new MatchConfigException("Un joueur existe déjà avec ce nom");
}
_players.Add(playerName, new PlayerZone());
}
else
{
throw new MatchConfigException("Impossible d'ajouter de nouveau joueur, la partie est pleine");
}
}
/// <summary> Initialiser la partie, il faut avoir ajouté les joueurs au préalable </summary>
/// <exception cref="MatchConfigException"></exception>
public void Init()
{
int nbPlayer = _players.Count;
ArgumentOutOfRangeException.ThrowIfLessThan(nbPlayer, _minPlayerCount);
ArgumentOutOfRangeException.ThrowIfGreaterThan(nbPlayer, _maxPlayerCount);
_board = staticData.GetBoard(1);
List<int> freeObjectiveIndexs = Enumerable.Range(0, _objectives.Count).ToList();
foreach (PlayerZone zone in _players.Values)
{
zone.AddObjectives(GetRandomObjectives(nbPlayer switch
{
2 => 4,
3 => 3,
4 => 2,
}, freeObjectiveIndexs));
zone.AddRessources(Enums.ResourceType.Piece, 3);
}
_buildings = staticData.GetBuildings();
_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;
}
}
/// <summary> Permet de récuperer une player zone(une copie) </summary>
/// <param name="playerName">le nom ou ID du joueur</param>
/// <returns></returns>
public PlayerZone GetPlayerZone(string playerName)
{
if (!_players.TryGetValue(playerName, out PlayerZone? value))
throw new ArgumentException("playerID is out of bound");
return value.Clone() as PlayerZone ?? throw new ArgumentException("Cast exception in GetPlayerZone"); ;
}
private List<Objective> GetRandomObjectives(int number, List<int> freeIndex)
{
List<Objective> result = new List<Objective>();
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;
}
}

View File

@@ -0,0 +1,18 @@
namespace LittleTown.Core;
/// <summary>
/// Représente une carte objectif dans le jeu, les objectif sont des conditions qui une fois remplie accordent des points au joueur
/// </summary>
public class Objective
{
/// <summary> la description, une clé pour la traduction </summary>
public required string Description { get; init; }
/// <summary> la condition de l'objectif sous forme de formule </summary>
public string Formula { get; init; } = string.Empty;
/// <summary> le nombre de points que procure cet objectif </summary>
public int Points { get; init; }
/// <summary> indique si l'objectif a deja été atteint ou non, un objectif ne peut etre atteint qu'une fois </summary>
public bool Filled { get; init; }
}

View File

@@ -0,0 +1,53 @@
using LittleTown.Core.Enums;
namespace LittleTown.Core;
/// <summary> Représente les données propre à un joueur </summary>
public class PlayerZone : ICloneable
{
/// <summary> Les ressources que possede le joueur </summary>
public IDictionary<ResourceType, int> Ressources { get; init; } = new Dictionary<ResourceType, int>();
/// <summary> La liste des objectifs que le joueur possede/// </summary>
public IReadOnlyCollection<Objective> Objectives { get => _objectives.AsReadOnly(); init => _objectives = new List<Objective>(value); }
private List<Objective> _objectives = new List<Objective>();
/// <summary> Le marqueur de score pendant le match </summary>
public int ScoreMarker { get; init; }
/// <summary> Permet d'ajouter d'une type de ressources au stock du joueur </summary>
/// <param name="res">le type de ressources</param>
/// <param name="qte">la quantité non nulle non négative</param>
public void AddRessources(ResourceType res, int qte)
{
ArgumentOutOfRangeException.ThrowIfLessThan(qte, 1);
if (Ressources.ContainsKey(res))
{
Ressources[res] += qte;
}
else
{
Ressources.Add(res, qte);
}
}
public void AddObjectives(ICollection<Objective> objectives)
{
_objectives.AddRange(objectives);
}
/// <summary> Cloner ce playerZone </summary>
/// <returns>Une copie de l'objet</returns>
public object Clone()
{
PlayerZone result = new PlayerZone()
{
Ressources = new Dictionary<ResourceType, int>(Ressources),
Objectives = new List<Objective>(Objectives),
ScoreMarker = ScoreMarker
};
return result;
}
}

View File

@@ -13,4 +13,9 @@ public interface IStaticDataGetter
/// <summary> Recupérer la liste des batiments et leurs données statiques </summary>
/// <returns></returns>
public ICollection<Building> GetBuildings();
}
/// <summary> Récupérer la liste des objectifs du jeu </summary>
/// <returns></returns>
public ICollection<Objective> GetObjectives();
}

View File

@@ -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
}
]

View File

@@ -29,4 +29,14 @@ public class StaticDataGetter : IStaticDataGetter
return buildings;
}
/// <inheritdoc/>
public ICollection<Objective> GetObjectives()
{
string path = Path.Combine(Environment.CurrentDirectory, "../../../../LittleTown.StaticDataAccess/Data/Objectives.json");
string data = System.IO.File.ReadAllText(path);
List<Objective> objectives = JsonSerializer.Deserialize<List<Objective>>(data) ?? throw new JsonException("Cannot deserialize Objectives");
return objectives;
}
}

View File

@@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LittleTown.Core.Tests", "Li
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LittleTown.StaticDataAccess", "LittleTown.StaticDataAccess\LittleTown.StaticDataAccess.csproj", "{FA0DE9D0-F788-4734-BDA3-F89F71D757BA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LittleTown.Api", "LittleTown.Api\LittleTown.Api.csproj", "{87327E52-A393-44C1-8247-EE51753F975F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -30,5 +32,9 @@ Global
{FA0DE9D0-F788-4734-BDA3-F89F71D757BA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FA0DE9D0-F788-4734-BDA3-F89F71D757BA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FA0DE9D0-F788-4734-BDA3-F89F71D757BA}.Release|Any CPU.Build.0 = Release|Any CPU
{87327E52-A393-44C1-8247-EE51753F975F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{87327E52-A393-44C1-8247-EE51753F975F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{87327E52-A393-44C1-8247-EE51753F975F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{87327E52-A393-44C1-8247-EE51753F975F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal