Compare commits

..

No commits in common. 'main' and 'EF_Dice_Faces' have entirely different histories.

@ -9,9 +9,6 @@ trigger:
steps:
- name: build
image: mcr.microsoft.com/dotnet/sdk:6.0
volumes:
- name: docs
path: /docs
commands:
- cd Sources/
- dotnet restore DiceApp.sln
@ -44,25 +41,4 @@ steps:
# accessible en ligne de commande par $${PLUGIN_SONAR_TOKEN}
sonar_token:
from_secret: SECRET_SONAR_LOGIN
depends_on: [tests]
# La documentation Doxygen doit être dans le répertoire
# Documentation/doxygen
# avec le ficher
# Documentation/doxygen/Doxyfile contenant
# OUTPUT_DIRECTORY = /docs/doxygen
- name: generate-and-deploy-docs
image: hub.codefirst.iut.uca.fr/thomas.bellembois/codefirst-docdeployer
volumes:
- name: docs
path: /docs
commands:
- /entrypoint.sh
when:
branch:
- master
depends_on: [ build ]
volumes:
- name: docs
temp: {}
depends_on: [tests]

Binary file not shown.

Before

Width:  |  Height:  |  Size: 76 KiB

File diff suppressed because it is too large Load Diff

@ -1,8 +0,0 @@
<html><body>
<p>
<hr size="1"/><address style="text-align: right;"><small>Generated on $datetime with &nbsp;
<img src="CodeFirst.png" alt="Code#0" align="middle" border="0" height="40px"/>
by Doxygen version $doxygenversion</small></address>
</p>
</body>
</html>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.6 KiB

@ -6,19 +6,11 @@
<StartWorkingDirectory>$(MSBuildProjectDirectory)</StartWorkingDirectory>
</PropertyGroup>
<ItemGroup>
<Content Include="NLog.config">
<BuildAction>Content</BuildAction>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.9">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NLog" Version="5.0.4" />
</ItemGroup>
<ItemGroup>

@ -1,17 +0,0 @@
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
throwConfigExceptions="true">
<targets async="true">
<target name="logfile" xsi:type="File" fileName="log.txt" />
<target name="logdebug" xsi:type="Debug" />
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="logdebug"/>
<logger name="*" minlevel="Warn" writeTo="logfile"/>
</rules>
</nlog>

@ -7,19 +7,15 @@ using Model.Games;
using Model.Players;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace App
{
internal static class Program
{
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
static async Task Main(string[] args)
{
// MODEL stuff
@ -31,8 +27,9 @@ namespace App
}
catch (Exception ex)
{
logger.Warn(ex);
masterOfCeremonies = new(new PlayerManager(), new DiceGroupManager(), new GameManager());
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
masterOfCeremonies = new(new PlayerManager(), new DiceGroupManager(), null);
}
try
@ -43,8 +40,9 @@ namespace App
// Later, we'll use the DiceAppDbContext to get a GameDbRunner
// get all the players from the DB
PlayerDbManager playerDbManager = new(db);
IEnumerable<PlayerEntity> entities = await playerDbManager.GetAll();
IEnumerable<PlayerEntity> entities = db.Players;
Debug.WriteLine("Loading players");
foreach (PlayerEntity entity in entities)
{
@ -52,12 +50,13 @@ namespace App
{
// persist them as models !
await masterOfCeremonies.GlobalPlayerManager.Add(entity.ToModel());
Debug.WriteLine($"{entity.ID} -- {entity.Name}");
}
catch (Exception ex) { Debug.WriteLine($"{ex.Message}\n... Never mind"); }
}
}
}
catch (Exception ex) { Console.WriteLine($"{ex.Message}\n... Couldn't use the database"); }
catch (Exception ex) { Debug.WriteLine($"{ex.Message}\n... Couldn't use the database"); }
string menuChoice = "nothing";
@ -153,7 +152,7 @@ namespace App
newGroupDice.Add(die);
}
}
await masterOfCeremonies.DiceGroupManager.Add(new DiceGroup(newGroupName, newGroupDice));
await masterOfCeremonies.DiceGroupManager.Add(new KeyValuePair<string, IEnumerable<Die>>(newGroupName, newGroupDice));
break;
case "p":
@ -185,21 +184,22 @@ namespace App
// create a PlayerDbManager (and inject it with the DB)
PlayerDbManager playerDbManager = new(db);
Debug.WriteLine("Saving players");
foreach (Player model in models)
{
try // to persist them
{ // as entities !
PlayerEntity entity = model.ToEntity();
await playerDbManager.Add(entity);
Debug.WriteLine($"{entity.ID} -- {entity.Name}");
}
// what if there's already a player with that name? Exception (see PlayerEntity's annotations)
catch (ArgumentException ex) { Debug.WriteLine($"{ex.Message}\n... Never mind"); }
}
}
// flushing and closing NLog before quitting completely
NLog.LogManager.Shutdown();
}
catch (Exception ex) { Console.WriteLine($"{ex.Message}\n... Couldn't use the database"); }
catch (Exception ex) { Debug.WriteLine($"{ex.Message}\n... Couldn't use the database"); }
}
private static async Task Play(MasterOfCeremonies masterOfCeremonies, string name)
@ -208,7 +208,7 @@ namespace App
while (menuChoicePlay != "q")
{
Game game = await masterOfCeremonies.GameManager.GetOneByName(name);
Console.WriteLine($"{PlayerToString(await game.GetWhoPlaysNow())}'s turn\n" +
Console.WriteLine($"{game.GetWhoPlaysNow()}'s turn\n" +
"q... quit\n" +
"h... show history\n" +
"s... save\n" +
@ -219,14 +219,17 @@ namespace App
case "q":
break;
case "h":
foreach (Turn turn in game.GetHistory()) { Console.WriteLine(TurnToString(turn)); }
foreach (Turn turn in game.GetHistory())
{
Console.WriteLine(turn);
}
break;
case "s":
await masterOfCeremonies.GameManager.Add(game);
break;
default:
await MasterOfCeremonies.PlayGame(game);
Console.WriteLine(TurnToString(game.GetHistory().Last()));
Console.WriteLine(game.GetHistory().Last());
break;
}
}
@ -238,7 +241,7 @@ namespace App
Console.WriteLine("which of these games?\n(choose by name)\n>");
foreach (Game game in await masterOfCeremonies.GameManager.GetAll())
{
Console.WriteLine(GameToString(game));
Console.WriteLine(game);
}
name = Console.ReadLine();
return name;
@ -249,15 +252,15 @@ namespace App
Console.WriteLine("Look at all them players!");
foreach (Player player in await masterOfCeremonies.GlobalPlayerManager.GetAll())
{
Console.WriteLine(PlayerToString(player));
Console.WriteLine(player);
}
}
private static async Task ShowDice(MasterOfCeremonies masterOfCeremonies)
{
foreach ((string name, ReadOnlyCollection<Die> dice) in await masterOfCeremonies.DiceGroupManager.GetAll())
foreach ((string name, IEnumerable<Die> dice) in await masterOfCeremonies.DiceGroupManager.GetAll())
{
Console.WriteLine($"{name} -- {dice}"); // maybe code a quick and dirty DieToString()
Console.WriteLine($"{name} -- {dice}");
}
}
@ -330,13 +333,11 @@ namespace App
catch (ArgumentNullException ex)
{
Console.WriteLine(ex.Message);
logger.Warn(ex);
}
catch (UriFormatException ex)
{
Console.WriteLine("that URI was not valid");
Console.WriteLine(ex.Message);
logger.Warn(ex);
}
}
}
@ -353,7 +354,7 @@ namespace App
if (menuChoice.Equals("ok") && count == 0)
{
Console.WriteLine("create at least one valid face");
menuChoice = ""; // persists outside the scope of this function
menuChoice = ""; // persiste en dehors du scope de cette fonction
}
}
@ -369,7 +370,7 @@ namespace App
menuChoiceDice = Console.ReadLine();
if (!menuChoiceDice.Equals("ok"))
{
IEnumerable<Die> chosenDice = (await masterOfCeremonies.DiceGroupManager.GetOneByName(menuChoiceDice)).Dice;
IEnumerable<Die> chosenDice = (await masterOfCeremonies.DiceGroupManager.GetOneByName(menuChoiceDice)).Value;
foreach (Die die in chosenDice)
{
result.Add(die);
@ -393,7 +394,7 @@ namespace App
Player player = new(menuChoicePlayers);
if (!(await masterOfCeremonies.GlobalPlayerManager.GetAll()).Contains(player))
{
// if the player didn't exist, now it does...
// if the player didn't exist, now it does... this is temporary
await masterOfCeremonies.GlobalPlayerManager.Add(player);
}
// almost no checks, this is temporary
@ -401,57 +402,12 @@ namespace App
{
await result.Add(player);
}
catch (ArgumentException ex) { Console.WriteLine($"{ex.Message}\n... Never mind"); }
catch (ArgumentException ex) { Debug.WriteLine($"{ex.Message}\n... Never mind"); }
}
}
return result;
}
private static string TurnToString(Turn turn)
{
string[] datetime = turn.When.ToString("s", System.Globalization.CultureInfo.InvariantCulture).Split("T");
string date = datetime[0];
string time = datetime[1];
StringBuilder sb = new();
sb.AppendFormat("{0} {1} -- {2} rolled:",
date,
time,
PlayerToString(turn.Player));
foreach (KeyValuePair<Die, Face> kvp in turn.DiceNFaces)
{
sb.Append(" " + kvp.Value.StringValue);
}
return sb.ToString();
}
private async static Task<string> GameToString(Game game)
{
StringBuilder sb = new();
sb.Append($"Game: {game.Name}");
sb.Append("\nPlayers:");
foreach (Player player in game.PlayerManager.GetAll()?.Result)
{
sb.Append($" {PlayerToString(player)}");
}
sb.Append($"\nNext: {PlayerToString(await game.GetWhoPlaysNow())}");
sb.Append("\nLog:\n");
foreach (Turn turn in game.GetHistory())
{
sb.Append($"\t{TurnToString(turn)}\n");
}
return sb.ToString();
}
private static string PlayerToString(Player player)
{
return player.Name;
}
}
}

@ -13,12 +13,10 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NLog" Version="5.0.4" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Model\Model.csproj" />
<ProjectReference Include="..\Utils\Utils\Utils.csproj" />
</ItemGroup>
</Project>

@ -1,9 +1,16 @@
using Data.EF.Dice.Faces;
using Model.Dice.Faces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Data.EF.Dice
{
public class ColorDieEntity : DieEntity
public class ColorDieEntity
{
public new ICollection<ColorFaceEntity> Faces { get; set; } = new List<ColorFaceEntity>();
public Guid Id { get; set; }
public ICollection<ColorFaceEntity> Faces { get; set; }
}
}

@ -6,30 +6,39 @@ namespace Data.EF.Dice
{
public static class ColorDieExtensions
{
public static ColorDie ToModel(this ColorDieEntity dieEntity)
public static ColorDie ToModel(this ColorDieEntity clrDieEntity)
{
/*
* creating an array of faces model
*/
ColorFace[] faces = dieEntity.Faces.ToModels().ToArray();
ColorFace[] faces= new ColorFace[clrDieEntity.Faces.Count-1];
List<ColorFace> clrFacesList = (List<ColorFace>)ColorFaceExtensions.ToModels(clrDieEntity.Faces);
clrFacesList.CopyTo(faces, 1);
/*
* creating the die
*/
ColorDie die = new(faces[0], faces[1..]);
ColorDie die = new (ColorFaceExtensions.ToModel(clrDieEntity.Faces.ElementAt(0)), faces);
return die;
}
public static IEnumerable<ColorDie> ToModels(this IEnumerable<ColorDieEntity> entities) => entities.Select(entity => entity.ToModel());
public static IEnumerable<ColorDie> ToModels(this IEnumerable<ColorDieEntity> entities)
{
return entities.Select(entity => ToModel(entity));
}
public static ColorDieEntity ToEntity(this ColorDie model)
{
var entity = new ColorDieEntity();
foreach (var face in model.Faces) { entity.Faces.Add(((ColorFace)face).ToEntity()); }
foreach (var face in model.Faces) { entity.Faces.Add(ColorFaceExtensions.ToEntity((ColorFace)face)); }
return entity;
}
public static IEnumerable<ColorDieEntity> ToEntities(this IEnumerable<ColorDie> models) => models.Select(model => model.ToEntity());
public static IEnumerable<ColorFaceEntity> ToEntities(this IEnumerable<ColorFace> models)
{
return models.Select(model => model.ToEntity());
}
}
}

@ -1,32 +0,0 @@
using Data.EF.Dice.Faces;
using Data.EF.Games;
using Data.EF.Joins;
using System.Diagnostics.CodeAnalysis;
namespace Data.EF.Dice
{
/// <summary>
/// not designed to be instantiated, but not abstract in order to allow extensions
/// </summary>
///
public class DieEntity : IEqualityComparer<DieEntity>
{
public Guid ID { get; set; }
public ICollection<FaceEntity> Faces { get; set; } = new List<FaceEntity>(); // one to many
public ICollection<TurnEntity> Turns { get; set; } = new List<TurnEntity>(); // many to many
public List<DieTurn> DieTurns { get; set; } = new();
public bool Equals(DieEntity x, DieEntity y)
{
return x is not null
&& y is not null
&& x.ID.Equals(y.ID)
&& x.Faces.Equals(y.Faces);
}
public int GetHashCode([DisallowNull] DieEntity obj)
{
return HashCode.Combine(ID, Faces);
}
}
}

@ -1,23 +1,27 @@
using System.Drawing;
using Model.Dice;
using System;
using System.ComponentModel.DataAnnotations.Schema;
using System.Drawing;
namespace Data.EF.Dice.Faces
{
public class ColorFaceEntity : FaceEntity
public class ColorFaceEntity
{
public Guid ID { get; set; }
public byte A { get; set; }
public byte R { get; set; }
public byte G { get; set; }
public byte B { get; set; }
public Guid ColorDieEntityID { get; set; }
public ColorDieEntity ColorDieEntity { get; set; }
[ForeignKey("ColorDieFK")]
public ColorDie ColorDie { get; set; }
public void SetValue(Color c)
{
A = c.A;
R = c.R;
G = c.G;
B = c.B;
A= c.A;
R= c.R;
G= c.G;
B= c.B;
}
}
}

@ -10,12 +10,27 @@ namespace Data.EF.Dice.Faces
{
public static class ColorFaceExtensions
{
public static ColorFace ToModel(this ColorFaceEntity clrFaceEntity) => new(Color.FromArgb(clrFaceEntity.A, clrFaceEntity.R, clrFaceEntity.G, clrFaceEntity.B));
public static ColorFace ToModel(this ColorFaceEntity clrFaceEntity)
{
ColorFace colorFace = new (Color.FromArgb(clrFaceEntity.A, clrFaceEntity.R, clrFaceEntity.G, clrFaceEntity.B));
return colorFace;
}
public static IEnumerable<ColorFace> ToModels(this IEnumerable<ColorFaceEntity> entities) => entities.Select(entity => entity.ToModel());
public static IEnumerable<ColorFace> ToModels(this IEnumerable<ColorFaceEntity> entities)
{
return entities.Select(entity => entity.ToModel());
}
public static ColorFaceEntity ToEntity(this ColorFace model) => new() { A = model.Value.A, R = model.Value.R, G = model.Value.G, B = model.Value.B };
public static ColorFaceEntity ToEntity(this ColorFace model)
{
return new ColorFaceEntity() { A=model.Value.A,R=model.Value.R,G=model.Value.G,B=model.Value.B };
}
public static IEnumerable<ColorFaceEntity> ToEntities(this IEnumerable<ColorFace> models) => models.Select(model => model.ToEntity());
public static IEnumerable<ColorFaceEntity> ToEntities(this IEnumerable<ColorFace> models)
{
return models.Select(model => model.ToEntity());
}
}
}

@ -1,28 +0,0 @@
using Data.EF.Games;
using Data.EF.Joins;
using System.Diagnostics.CodeAnalysis;
namespace Data.EF.Dice.Faces
{
/// <summary>
/// not designed to be instantiated, but not abstract in order to allow extensions
/// </summary>
public class FaceEntity : IEqualityComparer<FaceEntity>
{
public Guid ID { get; set; }
public ICollection<TurnEntity> Turns { get; set; } // many to many
public List<FaceTurn> FaceTurns { get; set; }
public bool Equals(FaceEntity x, FaceEntity y)
{
return x is not null
&& y is not null
&& x.ID.Equals(y.ID);
}
public int GetHashCode([DisallowNull] FaceEntity obj)
{
return ID.GetHashCode();
}
}
}

@ -1,9 +1,18 @@
namespace Data.EF.Dice.Faces
using Model.Dice;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Data.EF.Dice.Faces
{
public class ImageFaceEntity : FaceEntity
public class ImageFaceEntity
{
public Guid ID { get; set; }
public string Value { get; set; }
public Guid ImageDieEntityID { get; set; }
public ImageDieEntity ImageDieEntity { get; set; }
[ForeignKey("ImgDieFK")]
public ImageDie ImageDie { get; set; }
}
}

@ -11,12 +11,24 @@ namespace Data.EF.Dice.Faces
{
public static class ImageFaceExtensions
{
public static ImageFace ToModel(this ImageFaceEntity entity) => new(new Uri(entity.Value));
public static ImageFace ToModel(this ImageFaceEntity entity)
{
return new ImageFace(new Uri(entity.Value));
}
public static IEnumerable<ImageFace> ToModels(this IEnumerable<ImageFaceEntity> entities) => entities.Select(entity => entity.ToModel());
public static IEnumerable<ImageFace> ToModels(this IEnumerable<ImageFaceEntity> entities)
{
return entities.Select(entity => entity.ToModel());
}
public static ImageFaceEntity ToEntity(this ImageFace model) => new() { Value = model.StringValue };
public static ImageFaceEntity ToEntity(this ImageFace model)
{
return new ImageFaceEntity() { Value = model.Value.ToString() };
}
public static IEnumerable<ImageFaceEntity> ToEntities(this IEnumerable<ImageFace> models) => models.Select(model => model.ToEntity());
public static IEnumerable<ImageFaceEntity> ToEntities(this IEnumerable<ImageFace> models)
{
return models.Select(model => model.ToEntity());
}
}
}

@ -1,9 +1,18 @@
namespace Data.EF.Dice.Faces
using Model.Dice;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Data.EF.Dice.Faces
{
public class NumberFaceEntity : FaceEntity
public class NumberFaceEntity
{
public Guid Id { get; set; }
public int Value { get; set; }
public Guid NumberDieEntityID { get; set; }
public NumberDieEntity NumberDieEntity { get; set; }
[ForeignKey("NumDieFK")]
public NumberDie NumberDie { get; set; }
}
}

@ -9,12 +9,24 @@ namespace Data.EF.Dice.Faces
{
public static class NumberFaceExtensions
{
public static NumberFace ToModel(this NumberFaceEntity entity) => new(entity.Value);
public static NumberFace ToModel(this NumberFaceEntity entity)
{
return new NumberFace(entity.Value);
}
public static IEnumerable<NumberFace> ToModels(this IEnumerable<NumberFaceEntity> entities) => entities.Select(entity => entity.ToModel());
public static IEnumerable<NumberFace> ToModels(this IEnumerable<NumberFaceEntity> entities)
{
return entities.Select(entity => entity.ToModel());
}
public static NumberFaceEntity ToEntity(this NumberFace model) => new() { Value = model.Value };
public static NumberFaceEntity ToEntity(this NumberFace model)
{
return new NumberFaceEntity() { Value = model.Value };
}
public static IEnumerable<NumberFaceEntity> ToEntities(this IEnumerable<NumberFace> models) => models.Select(model => model.ToEntity());
public static IEnumerable<NumberFaceEntity> ToEntities(this IEnumerable<NumberFace> models)
{
return models.Select(model => model.ToEntity());
}
}
}

@ -1,9 +1,16 @@
using Data.EF.Dice.Faces;
using Model.Dice.Faces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Data.EF.Dice
{
public class ImageDieEntity : DieEntity
public class ImageDieEntity
{
public new ICollection<ImageFaceEntity> Faces { get; set; } = new List<ImageFaceEntity>();
public Guid Id { get; set; }
public ICollection<ImageFaceEntity> Faces { get; set; }
}
}

@ -1,35 +1,44 @@
using Data.EF.Dice.Faces;
using Model.Dice;
using Model.Dice.Faces;
using Model.Dice;
namespace Data.EF.Dice
{
public static class ImageDieExtensions
{
public static ImageDie ToModel(this ImageDieEntity dieEntity)
public static ImageDie ToModel(this ImageDieEntity clrDieEntity)
{
/*
* creating an array of faces model
*/
ImageFace[] faces = dieEntity.Faces.ToModels().ToArray();
ImageFace[] faces = new ImageFace[clrDieEntity.Faces.Count - 1];
List<ImageFace> clrFacesList = (List<ImageFace>)ImageFaceExtensions.ToModels(clrDieEntity.Faces);
clrFacesList.CopyTo(faces, 1);
/*
* creating the die
*/
ImageDie die = new(faces[0], faces[1..]);
ImageDie die = new( ImageFaceExtensions.ToModel(clrDieEntity.Faces.ElementAt(0)), faces);
return die;
}
public static IEnumerable<ImageDie> ToModels(this IEnumerable<ImageDieEntity> entities) => entities.Select(entity => entity.ToModel());
public static IEnumerable<ImageDie> ToModels(this IEnumerable<ImageDieEntity> entities)
{
return entities.Select(entity => entity.ToModel());
}
public static ImageDieEntity ToEntity(this ImageDie model)
{
var entity = new ImageDieEntity();
foreach (var face in model.Faces) { entity.Faces.Add(((ImageFace)face).ToEntity()); }
foreach (var face in model.Faces) { entity.Faces.Add(ImageFaceExtensions.ToEntity((ImageFace)face)); }
return entity;
}
public static IEnumerable<ImageDieEntity> ToEntities(this IEnumerable<ImageDie> models) => models.Select(model => model.ToEntity());
public static IEnumerable<ImageDieEntity> ToEntities(this IEnumerable<ImageDie> models)
{
return models.Select(model => model.ToEntity());
}
}
}

@ -1,9 +1,16 @@
using Data.EF.Dice.Faces;
using Model.Dice.Faces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Data.EF.Dice
{
public class NumberDieEntity : DieEntity
public class NumberDieEntity
{
public new ICollection<NumberFaceEntity> Faces { get; set; } = new List<NumberFaceEntity>();
public Guid Id { get; set; }
public ICollection<NumberFaceEntity> Faces { get; set; }
}
}

@ -1,35 +1,48 @@
using Data.EF.Dice.Faces;
using Model.Dice;
using Model.Dice.Faces;
using Model.Dice;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Data.EF.Dice
{
public static class NumberDieExtensions
{
public static NumberDie ToModel(this NumberDieEntity dieEntity)
public static NumberDie ToModel(this NumberDieEntity clrDieEntity)
{
/*
* creating an array of faces model
*/
NumberFace[] faces = dieEntity.Faces.ToModels().ToArray();
NumberFace[] faces = new NumberFace[clrDieEntity.Faces.Count - 1];
List<NumberFace> clrFacesList = (List<NumberFace>)NumberFaceExtensions.ToModels(clrDieEntity.Faces);
clrFacesList.CopyTo(faces, 1);
/*
* creating the die
*/
NumberDie die = new(faces[0], faces[1..]);
NumberDie die = new(NumberFaceExtensions.ToModel(clrDieEntity.Faces.ElementAt(0)), faces);
return die;
}
public static IEnumerable<NumberDie> ToModels(this IEnumerable<NumberDieEntity> entities) => entities.Select(entity => ToModel(entity));
public static IEnumerable<NumberDie> ToModels(this IEnumerable<NumberDieEntity> entities)
{
return entities.Select(entity => ToModel(entity));
}
public static NumberDieEntity ToEntity(this NumberDie model)
public static ColorDieEntity ToEntity(this ColorDie model)
{
var entity = new NumberDieEntity();
foreach (var face in model.Faces) { entity.Faces.Add(((NumberFace)face).ToEntity()); }
var entity = new ColorDieEntity();
foreach (var face in model.Faces) { entity.Faces.Add(ColorFaceExtensions.ToEntity((ColorFace)face)); }
return entity;
}
public static IEnumerable<NumberDieEntity> ToEntities(this IEnumerable<NumberDie> models) => models.Select(model => model.ToEntity());
public static IEnumerable<ColorFaceEntity> ToEntities(this IEnumerable<ColorFace> models)
{
return models.Select(model => model.ToEntity());
}
}
}

@ -1,8 +1,4 @@
using Data.EF.Dice;
using Data.EF.Dice.Faces;
using Data.EF.Games;
using Data.EF.Joins;
using Data.EF.Players;
using Data.EF.Players;
using Microsoft.EntityFrameworkCore;
using Model.Games;
@ -13,14 +9,7 @@ namespace Data.EF
// will be async!
public virtual Task<MasterOfCeremonies> LoadApp() { throw new NotImplementedException(); }
public DbSet<PlayerEntity> PlayerEntity { get; set; }
public DbSet<TurnEntity> TurnEntity { get; set; }
public DbSet<NumberDieEntity> NumberDieEntity { get; set; }
public DbSet<NumberFaceEntity> NumberFaceEntity { get; set; }
public DbSet<ImageDieEntity> ImageDieEntity { get; set; }
public DbSet<ImageFaceEntity> ImageFaceEntity { get; set; }
public DbSet<ColorDieEntity> ColorDieEntity { get; set; }
public DbSet<ColorFaceEntity> ColorFaceEntity { get; set; }
public DbSet<PlayerEntity> Players { get; set; }
public DiceAppDbContext() { }
@ -31,52 +20,5 @@ namespace Data.EF
{
if (!optionsBuilder.IsConfigured) optionsBuilder.UseSqlite("Data Source=EFDice.DiceApp.db").EnableSensitiveDataLogging();
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// thanks to https://learn.microsoft.com/en-us/ef/core/modeling/relationships?tabs=fluent-api%2Cfluent-api-simple-key%2Csimple-key#join-entity-type-configuration
// many to many TurnEntity <-> FaceEntity
modelBuilder.Entity<FaceEntity>()
.HasMany(face => face.Turns)
.WithMany(turn => turn.Faces)
.UsingEntity<FaceTurn>(
join => join
// FaceTurn --> TurnEntity
.HasOne(faceturn => faceturn.TurnEntity)
.WithMany(turn => turn.FaceTurns)
.HasForeignKey(faceturn => faceturn.TurnEntityID),
join => join
// FaceTurn --> FaceEntity
.HasOne(faceturn => faceturn.FaceEntity)
.WithMany(face => face.FaceTurns)
.HasForeignKey(faceturn => faceturn.FaceEntityID),
// FaceTurn.PK = (ID1, ID2)
join => join.HasKey(faceturn => new { faceturn.FaceEntityID, faceturn.TurnEntityID })
);
// many to many TurnEntity <-> DieEntity
modelBuilder.Entity<DieEntity>()
.HasMany(die => die.Turns)
.WithMany(turn => turn.Dice)
.UsingEntity<DieTurn>(
join => join
// DieTurn --> TurnEntity
.HasOne(dieturn => dieturn.TurnEntity)
.WithMany(turn => turn.DieTurns)
.HasForeignKey(dieturn => dieturn.TurnEntityID),
join => join
// DieTurn --> DieEntity
.HasOne(dieturn => dieturn.DieEntity)
.WithMany(die => die.DieTurns)
.HasForeignKey(dieturn => dieturn.DieEntityID),
// DieTurn.PK = (ID1, ID2)
join => join.HasKey(dieturn => new { dieturn.DieEntityID, dieturn.TurnEntityID })
);
}
}
}

@ -1,11 +1,6 @@
using Data.EF.Dice;
using Data.EF.Dice.Faces;
using Data.EF.Games;
using Data.EF.Joins;
using Data.EF.Players;
using Data.EF.Players;
using Microsoft.EntityFrameworkCore;
using Model.Games;
using System.Drawing;
using System.Linq.Expressions;
using System.Security.Cryptography.X509Certificates;
@ -25,88 +20,12 @@ namespace Data.EF
{
base.OnModelCreating(modelBuilder);
Guid playerID_1 = Guid.NewGuid();
Guid playerID_2 = new("6e856818-92f1-4d7d-b35c-f9c6687ef8e1");
Guid playerID_3 = Guid.NewGuid();
Guid playerID_4 = Guid.NewGuid();
PlayerEntity player_1 = new() { ID = playerID_1, Name = "Alice" };
PlayerEntity player_2 = new() { ID = playerID_2, Name = "Bob" };
PlayerEntity player_3 = new() { ID = playerID_3, Name = "Clyde" };
PlayerEntity player_4 = new() { ID = playerID_4, Name = "Dahlia" };
Guid turnID_1 = Guid.NewGuid();
Guid turnID_2 = Guid.NewGuid();
DateTime datetime_1 = new(2017, 1, 6, 17, 30, 0, DateTimeKind.Utc);
TurnEntity turn_1 = new() { ID = turnID_1, When = datetime_1, PlayerEntityID = playerID_1 };
TurnEntity turn_2 = new() { ID = turnID_2, When = DateTime.UtcNow, PlayerEntityID = playerID_2 };
Guid dieID_1 = Guid.NewGuid();
Guid dieID_2 = Guid.NewGuid();
Guid dieID_3 = Guid.NewGuid();
NumberDieEntity die_1 = new() { ID = dieID_1 };
ImageDieEntity die_2 = new() { ID = dieID_2 };
ColorDieEntity die_3 = new() { ID = dieID_3 };
Guid faceID_1 = Guid.NewGuid();
Guid faceID_2 = Guid.NewGuid();
Guid faceID_3 = Guid.NewGuid();
Guid faceID_4 = Guid.NewGuid();
Guid faceID_5 = Guid.NewGuid();
Guid faceID_6 = Guid.NewGuid();
NumberFaceEntity face_1 = new() { ID = faceID_1, Value = 1, NumberDieEntityID = dieID_1 };
NumberFaceEntity face_2 = new() { ID = faceID_2, Value = 2, NumberDieEntityID = dieID_1 };
ImageFaceEntity face_3 = new() { ID = faceID_3, Value = "https://1", ImageDieEntityID = dieID_2 };
ImageFaceEntity face_4 = new() { ID = faceID_4, Value = "https://2", ImageDieEntityID = dieID_2 };
ColorFaceEntity face_5 = new() { ID = faceID_5, ColorDieEntityID = dieID_3 };
face_5.SetValue(Color.FromArgb(255, 255, 0, 0));
ColorFaceEntity face_6 = new() { ID = faceID_6, ColorDieEntityID = dieID_3 };
face_6.SetValue(Color.FromName("green"));
modelBuilder.Entity<PlayerEntity>().HasData(player_1, player_2, player_3, player_4);
modelBuilder.Entity<TurnEntity>().HasData(turn_1, turn_2);
modelBuilder.Entity<NumberDieEntity>().HasData(die_1);
modelBuilder.Entity<NumberFaceEntity>().HasData(face_1, face_2);
modelBuilder.Entity<ImageDieEntity>().HasData(die_2);
modelBuilder.Entity<ImageFaceEntity>().HasData(face_3, face_4);
modelBuilder.Entity<ColorDieEntity>().HasData(die_3);
modelBuilder.Entity<ColorFaceEntity>().HasData(face_5, face_6);
// die 1 die 2 die 3
// turn 1 : num->2, img->https://2, clr->red
// turn 2 : num->1, clr->green
modelBuilder
.Entity<TurnEntity>()
.HasMany(turn => turn.Faces)
.WithMany(face => face.Turns)
.UsingEntity<FaceTurn>(join => join.HasData(
new { TurnEntityID = turnID_1, FaceEntityID = faceID_2 },
new { TurnEntityID = turnID_1, FaceEntityID = faceID_4 },
new { TurnEntityID = turnID_1, FaceEntityID = faceID_5 },
new { TurnEntityID = turnID_2, FaceEntityID = faceID_1 },
new { TurnEntityID = turnID_2, FaceEntityID = faceID_6 }));
modelBuilder
.Entity<TurnEntity>()
.HasMany(turn => turn.Dice)
.WithMany(die => die.Turns)
.UsingEntity<DieTurn>(join => join.HasData(
new { TurnEntityID = turnID_1, DieEntityID = dieID_1 },
new { TurnEntityID = turnID_1, DieEntityID = dieID_2 },
new { TurnEntityID = turnID_1, DieEntityID = dieID_3 },
new { TurnEntityID = turnID_2, DieEntityID = dieID_1 },
new { TurnEntityID = turnID_2, DieEntityID = dieID_3 }));
modelBuilder.Entity<PlayerEntity>().HasData(
new PlayerEntity { ID = Guid.NewGuid(), Name = "Alice" }, // some tests depend on this name
new PlayerEntity { ID = new("6e856818-92f1-4d7d-b35c-f9c6687ef8e1"), Name = "Bob" }, // some tests depend on this name and this ID
new PlayerEntity { ID = Guid.NewGuid(), Name = "Clyde" }, // some tests depend on this name
new PlayerEntity { ID = Guid.NewGuid(), Name = "Dahlia" } // some tests depend on this name
);
}
}
}

@ -1,88 +1,12 @@
using Data.EF.Dice;
using Data.EF.Dice.Faces;
using Data.EF.Joins;
using Data.EF.Players;
using Model.Players;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Data.EF.Games
{
public sealed class TurnEntity : IEquatable<TurnEntity>
public class TurnEntity
{
public Guid ID { get; set; }
public DateTime When { get; set; }
public PlayerEntity PlayerEntity { get; set; }
public Guid PlayerEntityID { get; set; }
public ICollection<DieEntity> Dice { get; set; } = new List<DieEntity>(); // many to many
public List<DieTurn> DieTurns { get; set; } = new();
public ICollection<FaceEntity> Faces { get; set; } = new List<FaceEntity>(); // many to many
public List<FaceTurn> FaceTurns { get; set; } = new();
public override bool Equals(object obj)
{
if (obj is not TurnEntity)
{
return false;
}
return Equals(obj as TurnEntity);
}
public bool Equals(TurnEntity other)
{
if (other is null
||
!(PlayerEntity.Equals(other.PlayerEntity)
&& When.Equals(other.When)
&& ID.Equals(other.ID)
&& Dice.Count == other.Dice.Count
&& Faces.Count == other.Faces.Count))
{
return false;
}
for (int i = 0; i < Faces.Count; i++)
{
if (Dice.ElementAt(i).Faces.Count
!= other.Dice.ElementAt(i).Faces.Count)
{
return false;
}
if (!other.Faces.ElementAt(i).ID
.Equals(Faces.ElementAt(i).ID))
{
return false;
}
for (int j = 0; j < Dice.ElementAt(i).Faces.Count; j++)
{
if (!other.Dice.ElementAt(i).Faces.ElementAt(j).ID
.Equals(Dice.ElementAt(i).Faces.ElementAt(j).ID))
{
return false;
}
}
}
return true;
}
public override int GetHashCode()
{
int result = HashCode.Combine(
ID,
When,
PlayerEntity);
foreach (DieEntity die in Dice)
{
result += die.GetHashCode();
}
foreach (FaceEntity face in Faces)
{
result += face.GetHashCode();
}
return result;
}
}
}
}

@ -1,68 +1,12 @@
using Data.EF.Dice;
using Data.EF.Dice.Faces;
using Data.EF.Players;
using Model.Dice;
using Model.Dice.Faces;
using Model.Games;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Data.EF.Games
{
public static class TurnExtensions
{
private static (List<Die>, List<Face>) ToModelsByTypes(ICollection<DieEntity> diceEntities, ICollection<FaceEntity> faceEntities)
{
List<Die> dice = new();
List<Face> faces = new();
foreach (DieEntity dieEntity in diceEntities)
{
if (dieEntity.GetType() == typeof(NumberDieEntity)) { dice.Add((dieEntity as NumberDieEntity).ToModel()); }
if (dieEntity.GetType() == typeof(ColorDieEntity)) { dice.Add((dieEntity as ColorDieEntity).ToModel()); }
if (dieEntity.GetType() == typeof(ImageDieEntity)) { dice.Add((dieEntity as ImageDieEntity).ToModel()); }
}
foreach (FaceEntity faceEntity in faceEntities)
{
if (faceEntity.GetType() == typeof(NumberFaceEntity)) { faces.Add((faceEntity as NumberFaceEntity).ToModel()); }
if (faceEntity.GetType() == typeof(ColorFaceEntity)) { faces.Add((faceEntity as ColorFaceEntity).ToModel()); }
if (faceEntity.GetType() == typeof(ImageFaceEntity)) { faces.Add((faceEntity as ImageFaceEntity).ToModel()); }
}
return (dice, faces);
}
public static Turn ToModel(this TurnEntity entity)
{
Dictionary<Die, Face> DiceNFaces = new();
List<Die> keysList;
List<Face> valuesList;
(keysList, valuesList) = ToModelsByTypes(entity.Dice, entity.Faces);
DiceNFaces = Utils.Enumerables.FeedListsToDict(DiceNFaces, keysList, valuesList);
return Turn.CreateWithSpecifiedTime(when: entity.When, player: entity.PlayerEntity.ToModel(), diceNFaces: DiceNFaces);
}
public static IEnumerable<Turn> ToModels(this IEnumerable<TurnEntity> entities) => entities.Select(entity => entity.ToModel());
public static TurnEntity ToEntity(this Turn model)
{
List<DieEntity> DiceEntities = new();
List<FaceEntity> FaceEntities = new();
foreach (KeyValuePair<Die, Face> kvp in model.DiceNFaces)
{
if (kvp.Key.GetType() == typeof(NumberDie)) { DiceEntities.Add((kvp.Key as NumberDie).ToEntity()); FaceEntities.Add((kvp.Value as NumberFace).ToEntity()); }
if (kvp.Key.GetType() == typeof(ImageDie)) { DiceEntities.Add((kvp.Key as ImageDie).ToEntity()); FaceEntities.Add((kvp.Value as ImageFace).ToEntity()); }
if (kvp.Key.GetType() == typeof(ColorDie)) { DiceEntities.Add((kvp.Key as ColorDie).ToEntity()); FaceEntities.Add((kvp.Value as ColorFace).ToEntity()); }
}
return new TurnEntity() { When = model.When, PlayerEntity = model.Player.ToEntity(), Dice = DiceEntities, Faces = FaceEntities };
}
public static IEnumerable<TurnEntity> ToEntities(this IEnumerable<Turn> models) => models.Select(model => model.ToEntity());
}
}
}

@ -1,15 +0,0 @@
using Data.EF.Dice;
using Data.EF.Dice.Faces;
using Data.EF.Games;
namespace Data.EF.Joins
{
public class DieTurn
{
public Guid DieEntityID { get; set; }
public DieEntity DieEntity { get; set; }
public Guid TurnEntityID { get; set; }
public TurnEntity TurnEntity { get; set; }
}
}

@ -1,19 +0,0 @@
using Data.EF.Dice.Faces;
using Data.EF.Games;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Data.EF.Joins
{
public class FaceTurn
{
public Guid FaceEntityID { get; set; }
public FaceEntity FaceEntity { get; set; }
public Guid TurnEntityID { get; set; }
public TurnEntity TurnEntity { get; set; }
}
}

@ -1,23 +1,18 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Model;
using System.Collections.ObjectModel;
namespace Data.EF.Players
{
public sealed class PlayerDbManager : IManager<PlayerEntity>
{
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
private readonly DiceAppDbContext db;
public PlayerDbManager(DiceAppDbContext db)
{
if (db is null)
{
ArgumentNullException ex = new(nameof(db), "param should not be null");
logger.Error(ex, "attempted to construct PlayerDbManager with a null context");
throw ex;
throw new ArgumentNullException(nameof(db), "param should not be null");
}
this.db = db;
}
@ -32,15 +27,11 @@ namespace Data.EF.Players
{
if (entity is null)
{
ArgumentNullException ex = new(nameof(entity), "param should not be null");
logger.Warn(ex);
throw ex;
throw new ArgumentNullException(nameof(entity), "param should not be null");
}
if (string.IsNullOrWhiteSpace(entity.Name))
{
ArgumentException ex = new("Name property should not be null or whitespace", nameof(entity));
logger.Warn(ex);
throw ex;
throw new ArgumentException("Name property should not be null or whitespace", nameof(entity));
}
entity.Name = entity.Name.Trim();
}
@ -56,11 +47,9 @@ namespace Data.EF.Players
{
CleanPlayerEntity(toAdd);
if (db.PlayerEntity.Where(entity => entity.Name == toAdd.Name).Any())
if (db.Players.Where(entity => entity.Name == toAdd.Name).Any())
{
ArgumentException ex = new("this username is already taken", nameof(toAdd));
logger.Warn(ex);
throw ex;
throw new ArgumentException("this username is already taken", nameof(toAdd));
}
return InternalAdd(toAdd);
@ -68,17 +57,16 @@ namespace Data.EF.Players
private async Task<PlayerEntity> InternalAdd(PlayerEntity toAdd)
{
EntityEntry ee = await db.PlayerEntity.AddAsync(toAdd);
EntityEntry ee = await db.Players.AddAsync(toAdd);
await db.SaveChangesAsync();
logger.Info("Added {0}", ee.Entity.ToString());
return (PlayerEntity)ee.Entity;
}
public async Task<ReadOnlyCollection<PlayerEntity>> GetAll()
public async Task<IEnumerable<PlayerEntity>> GetAll()
{
List<PlayerEntity> players = new();
await Task.Run(() => players.AddRange(db.PlayerEntity));
return new ReadOnlyCollection<PlayerEntity>(players);
await Task.Run(() => players.AddRange(db.Players));
return players.AsEnumerable();
}
/// <summary>
@ -102,7 +90,7 @@ namespace Data.EF.Players
private async Task<PlayerEntity> InternalGetOneByName(string name)
{
return await db.PlayerEntity.Where(p => p.Name == name).FirstAsync();
return await db.Players.Where(p => p.Name == name).FirstAsync();
}
public async Task<bool> IsPresentByName(string name)
@ -112,7 +100,7 @@ namespace Data.EF.Players
return false;
}
name = name.Trim();
return await db.PlayerEntity.Where(p => p.Name == name).FirstOrDefaultAsync() is not null;
return await db.Players.Where(p => p.Name == name).FirstOrDefaultAsync() is not null;
}
/// <summary>
@ -127,8 +115,7 @@ namespace Data.EF.Players
bool isPresent = IsPresentByID(toRemove.ID).Result;
if (isPresent)
{
db.PlayerEntity.Remove(toRemove);
logger.Info("Removed {0}", toRemove.ToString());
db.Players.Remove(toRemove);
db.SaveChanges();
}
}
@ -152,12 +139,11 @@ namespace Data.EF.Players
if (before.ID != after.ID)
{
ArgumentException ex = new("ID cannot be updated", nameof(after));
logger.Warn(ex);
throw ex;
throw new ArgumentException("ID cannot be updated", nameof(after));
}
return InternalUpdate(before, after);
}
private async Task<PlayerEntity> InternalUpdate(PlayerEntity before, PlayerEntity after)
@ -175,12 +161,12 @@ namespace Data.EF.Players
/// <exception cref="InvalidOperationException"></exception>
public async Task<PlayerEntity> GetOneByID(Guid ID)
{
return await db.PlayerEntity.FirstAsync(p => p.ID == ID);
return await db.Players.FirstAsync(p => p.ID == ID);
}
public async Task<bool> IsPresentByID(Guid ID)
{
return await db.PlayerEntity.FirstOrDefaultAsync(p => p.ID == ID) is not null;
return await db.Players.FirstOrDefaultAsync(p => p.ID == ID) is not null;
}
}
}

@ -1,23 +1,18 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Model;
using System.Collections.ObjectModel;
namespace Data.EF.Players
{
public sealed class PlayerDbManager : IManager<PlayerEntity>
{
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
private readonly DiceAppDbContext db;
public PlayerDbManager(DiceAppDbContext db)
{
if (db is null)
{
ArgumentNullException ex = new(nameof(db), "param should not be null");
logger.Error(ex, "attempted to construct PlayerDbManager with a null context");
throw ex;
throw new ArgumentNullException(nameof(db), "param should not be null");
}
this.db = db;
}
@ -32,15 +27,11 @@ namespace Data.EF.Players
{
if (entity is null)
{
ArgumentNullException ex = new(nameof(entity), "param should not be null");
logger.Warn(ex);
throw ex;
throw new ArgumentNullException(nameof(entity), "param should not be null");
}
if (string.IsNullOrWhiteSpace(entity.Name))
{
ArgumentException ex = new("Name property should not be null or whitespace", nameof(entity));
logger.Warn(ex);
throw ex;
throw new ArgumentException("Name property should not be null or whitespace", nameof(entity));
}
entity.Name = entity.Name.Trim();
}
@ -56,11 +47,9 @@ namespace Data.EF.Players
{
CleanPlayerEntity(toAdd);
if (db.PlayerEntity.Where(entity => entity.Name == toAdd.Name).Any())
if (db.Players.Where(entity => entity.Name == toAdd.Name).Any())
{
ArgumentException ex = new("this username is already taken", nameof(toAdd));
logger.Warn(ex);
throw ex;
throw new ArgumentException("this username is already taken", nameof(toAdd));
}
return InternalAdd(toAdd);
@ -68,17 +57,16 @@ namespace Data.EF.Players
private async Task<PlayerEntity> InternalAdd(PlayerEntity toAdd)
{
EntityEntry ee = await db.PlayerEntity.AddAsync(toAdd);
EntityEntry ee = await db.Players.AddAsync(toAdd);
await db.SaveChangesAsync();
logger.Info("Added {0}", ee.Entity.ToString());
return (PlayerEntity)ee.Entity;
}
public async Task<ReadOnlyCollection<PlayerEntity>> GetAll()
public async Task<IEnumerable<PlayerEntity>> GetAll()
{
List<PlayerEntity> players = new();
await Task.Run(() => players.AddRange(db.PlayerEntity));
return new ReadOnlyCollection<PlayerEntity>(players);
await Task.Run(() => players.AddRange(db.Players));
return players.AsEnumerable();
}
/// <summary>
@ -102,7 +90,7 @@ namespace Data.EF.Players
private async Task<PlayerEntity> InternalGetOneByName(string name)
{
return await db.PlayerEntity.Where(p => p.Name == name).FirstAsync();
return await db.Players.Where(p => p.Name == name).FirstAsync();
}
public async Task<bool> IsPresentByName(string name)
@ -112,7 +100,7 @@ namespace Data.EF.Players
return false;
}
name = name.Trim();
return await db.PlayerEntity.Where(p => p.Name == name).FirstOrDefaultAsync() is not null;
return await db.Players.Where(p => p.Name == name).FirstOrDefaultAsync() is not null;
}
/// <summary>
@ -127,8 +115,7 @@ namespace Data.EF.Players
bool isPresent = IsPresentByID(toRemove.ID).Result;
if (isPresent)
{
db.PlayerEntity.Remove(toRemove);
logger.Info("Removed {0}", toRemove.ToString());
db.Players.Remove(toRemove);
db.SaveChanges();
}
}
@ -152,12 +139,11 @@ namespace Data.EF.Players
if (before.ID != after.ID)
{
ArgumentException ex = new("ID cannot be updated", nameof(after));
logger.Warn(ex);
throw ex;
throw new ArgumentException("ID cannot be updated", nameof(after));
}
return InternalUpdate(before, after);
}
private async Task<PlayerEntity> InternalUpdate(PlayerEntity before, PlayerEntity after)
@ -175,12 +161,12 @@ namespace Data.EF.Players
/// <exception cref="InvalidOperationException"></exception>
public async Task<PlayerEntity> GetOneByID(Guid ID)
{
return await db.PlayerEntity.FirstAsync(p => p.ID == ID);
return await db.Players.FirstAsync(p => p.ID == ID);
}
public async Task<bool> IsPresentByID(Guid ID)
{
return await db.PlayerEntity.FirstOrDefaultAsync(p => p.ID == ID) is not null;
return await db.Players.FirstOrDefaultAsync(p => p.ID == ID) is not null;
}
}
}

@ -1,5 +1,4 @@
using Data.EF.Games;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
namespace Data.EF.Players
{
@ -10,8 +9,6 @@ namespace Data.EF.Players
public string Name { get; set; }
public ICollection<TurnEntity> Turns { get; set; } = new List<TurnEntity>();
public override bool Equals(object obj)
{
if (obj is not PlayerEntity)
@ -23,7 +20,7 @@ namespace Data.EF.Players
public bool Equals(PlayerEntity other)
{
return other is not null && this.ID.Equals(other.ID) && this.Name.Equals(other.Name);
return other is not null && this.ID == other.ID && this.Name == other.Name;
}
public override int GetHashCode()

@ -4,12 +4,24 @@ namespace Data.EF.Players
{
public static class PlayerExtensions
{
public static Player ToModel(this PlayerEntity entity) => new(name: entity.Name);
public static Player ToModel(this PlayerEntity entity)
{
return new Player(name: entity.Name);
}
public static IEnumerable<Player> ToModels(this IEnumerable<PlayerEntity> entities) => entities.Select(entity => entity.ToModel());
public static IEnumerable<Player> ToModels(this IEnumerable<PlayerEntity> entities)
{
return entities.Select(entity => entity.ToModel());
}
public static PlayerEntity ToEntity(this Player model) => new() { Name = model.Name };
public static PlayerEntity ToEntity(this Player model)
{
return new PlayerEntity() { Name = model.Name };
}
public static IEnumerable<PlayerEntity> ToEntities(this IEnumerable<Player> models) => models.Select(model => model.ToEntity());
public static IEnumerable<PlayerEntity> ToEntities(this IEnumerable<Player> models)
{
return models.Select(model => model.ToEntity());
}
}
}

@ -10,13 +10,13 @@ namespace Data
{
public async Task<MasterOfCeremonies> LoadApp()
{
MasterOfCeremonies mc = new(new PlayerManager(), new DiceGroupManager(), new GameManager());
MasterOfCeremonies gr = new(new PlayerManager(), new DiceGroupManager(), new GameManager());
Player player1 = new("Alice(Old Stub)"), player2 = new("Bob(Old Stub)"), player3 = new("Clyde(Old Stub)");
await mc.GlobalPlayerManager.Add(player1);
await mc.GlobalPlayerManager.Add(player2);
await mc.GlobalPlayerManager.Add(player3);
await gr.GlobalPlayerManager.Add(player1);
await gr.GlobalPlayerManager.Add(player2);
await gr.GlobalPlayerManager.Add(player3);
List<Die> monopolyDice = new();
@ -63,26 +63,26 @@ namespace Data
dndDice.Add(new NumberDie(d20Faces[0], d20Faces[1..]));
await mc.DiceGroupManager.Add(new DiceGroup(dndName, dndDice));
await mc.DiceGroupManager.Add(new DiceGroup(monopolyName, monopolyDice));
await gr.DiceGroupManager.Add(new KeyValuePair<string, IEnumerable<Die>>(dndName, dndDice.AsEnumerable()));
await gr.DiceGroupManager.Add(new KeyValuePair<string, IEnumerable<Die>>(monopolyName, monopolyDice.AsEnumerable()));
string game1 = "Forgotten Realms", game2 = "4e", game3 = "The Coopers";
await mc.GameManager.Add(new(game1, new PlayerManager(), dndDice.AsEnumerable()));
await mc.GameManager.Add(new(game2, new PlayerManager(), dndDice.AsEnumerable()));
await mc.GameManager.Add(new(game3, new PlayerManager(), monopolyDice.AsEnumerable()));
await gr.GameManager.Add(new(game1, new PlayerManager(), dndDice.AsEnumerable()));
await gr.GameManager.Add(new(game2, new PlayerManager(), dndDice.AsEnumerable()));
await gr.GameManager.Add(new(game3, new PlayerManager(), monopolyDice.AsEnumerable()));
await (await mc.GameManager.GetOneByName(game1)).PlayerManager.Add(player1);
await (await mc.GameManager.GetOneByName(game1)).PlayerManager.Add(player2);
await (await gr.GameManager.GetOneByName(game1)).PlayerManager.Add(player1);
await (await gr.GameManager.GetOneByName(game1)).PlayerManager.Add(player2);
await (await mc.GameManager.GetOneByName(game2)).PlayerManager.Add(player1);
await (await mc.GameManager.GetOneByName(game2)).PlayerManager.Add(player2);
await (await mc.GameManager.GetOneByName(game2)).PlayerManager.Add(player3);
await (await gr.GameManager.GetOneByName(game2)).PlayerManager.Add(player1);
await (await gr.GameManager.GetOneByName(game2)).PlayerManager.Add(player2);
await (await gr.GameManager.GetOneByName(game2)).PlayerManager.Add(player3);
await (await mc.GameManager.GetOneByName(game3)).PlayerManager.Add(player1);
await (await mc.GameManager.GetOneByName(game3)).PlayerManager.Add(player3);
await (await gr.GameManager.GetOneByName(game3)).PlayerManager.Add(player1);
await (await gr.GameManager.GetOneByName(game3)).PlayerManager.Add(player3);
foreach (Game game in mc.GameManager.GetAll()?.Result)
foreach (Game game in gr.GameManager.GetAll()?.Result)
{
for (int i = 0; i < 10; i++)
{
@ -92,7 +92,7 @@ namespace Data
}
}
return mc;
return gr;
}
}
}

@ -9,9 +9,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tests", "Tests\Tests.csproj
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{953D2D67-BCE7-412C-B7BB-7C63B5592359}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Data", "Data\Data.csproj", "{E9683741-E603-4ED3-8088-4099D67FCA6D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Utils", "Utils\Utils\Utils.csproj", "{9300910D-9D32-4C79-8868-67D0ED56E2F3}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Data", "Data\Data.csproj", "{E9683741-E603-4ED3-8088-4099D67FCA6D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -31,10 +29,6 @@ Global
{E9683741-E603-4ED3-8088-4099D67FCA6D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E9683741-E603-4ED3-8088-4099D67FCA6D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E9683741-E603-4ED3-8088-4099D67FCA6D}.Release|Any CPU.Build.0 = Release|Any CPU
{9300910D-9D32-4C79-8868-67D0ED56E2F3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9300910D-9D32-4C79-8868-67D0ED56E2F3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9300910D-9D32-4C79-8868-67D0ED56E2F3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9300910D-9D32-4C79-8868-67D0ED56E2F3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

@ -14,9 +14,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tests", "Tests\Tests.csproj
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{953D2D67-BCE7-412C-B7BB-7C63B5592359}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Data", "Data\Data.csproj", "{E9683741-E603-4ED3-8088-4099D67FCA6D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Utils", "Utils\Utils\Utils.csproj", "{9300910D-9D32-4C79-8868-67D0ED56E2F3}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Data", "Data\Data.csproj", "{E9683741-E603-4ED3-8088-4099D67FCA6D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -40,10 +38,6 @@ Global
{E9683741-E603-4ED3-8088-4099D67FCA6D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E9683741-E603-4ED3-8088-4099D67FCA6D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E9683741-E603-4ED3-8088-4099D67FCA6D}.Release|Any CPU.Build.0 = Release|Any CPU
{9300910D-9D32-4C79-8868-67D0ED56E2F3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9300910D-9D32-4C79-8868-67D0ED56E2F3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9300910D-9D32-4C79-8868-67D0ED56E2F3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9300910D-9D32-4C79-8868-67D0ED56E2F3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

@ -1,45 +0,0 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
namespace Model.Dice
{
public sealed class DiceGroup : IEquatable<DiceGroup>
{
public string Name { get; private set; }
public ReadOnlyCollection<Die> Dice => new(dice);
private readonly List<Die> dice = new();
public DiceGroup(string name, IEnumerable<Die> dice)
{
Name = name;
this.dice.AddRange(dice);
}
public bool Equals(DiceGroup other)
{
return Name == other.Name && Dice.SequenceEqual(other.Dice);
}
public override bool Equals(object obj)
{
if (obj is null) return false; // is null
if (ReferenceEquals(obj, this)) return true; // is me
if (!obj.GetType().Equals(GetType())) return false; // is different type
return Equals(obj as DiceGroup); // is not me, is not null, is same type : send up
}
public override int GetHashCode()
{
return HashCode.Combine(Name, dice);
}
public void Deconstruct(out string name, out ReadOnlyCollection<Die> dice)
{
dice = Dice;
name = Name;
}
}
}

@ -1,41 +1,40 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
namespace Model.Dice
{
public class DiceGroupManager : IManager<DiceGroup>
public class DiceGroupManager : IManager<KeyValuePair<string, IEnumerable<Die>>>
{
private readonly List<DiceGroup> diceGroups = new();
private readonly Dictionary<string, IEnumerable<Die>> diceGroups = new();
public Task<DiceGroup> Add(DiceGroup toAdd)
public Task<KeyValuePair<string, IEnumerable<Die>>> Add(KeyValuePair<string, IEnumerable<Die>> toAdd)
{
if (string.IsNullOrWhiteSpace(toAdd.Name))
if (string.IsNullOrWhiteSpace(toAdd.Key))
{
throw new ArgumentNullException(nameof(toAdd), "param should not be null or empty");
}
if (diceGroups.Contains(toAdd))
{
throw new ArgumentException("this dice group already exists", nameof(toAdd));
throw new ArgumentException("this username is already taken", nameof(toAdd));
}
diceGroups.Add(new(toAdd.Name.Trim(), toAdd.Dice));
diceGroups.Add(toAdd.Key.Trim(), toAdd.Value);
return Task.FromResult(toAdd);
}
public Task<ReadOnlyCollection<DiceGroup>> GetAll()
public Task<IEnumerable<KeyValuePair<string, IEnumerable<Die>>>> GetAll()
{
return Task.FromResult(new ReadOnlyCollection<DiceGroup>(diceGroups));
return Task.FromResult(diceGroups.AsEnumerable());
}
public Task<DiceGroup> GetOneByID(Guid ID)
public Task<KeyValuePair<string, IEnumerable<Die>>> GetOneByID(Guid ID)
{
throw new NotImplementedException();
}
public Task<DiceGroup> GetOneByName(string name)
public Task<KeyValuePair<string, IEnumerable<Die>>> GetOneByName(string name)
{
// les groupes de dés nommés :
// ils sont case-sensistive, mais "mon jeu" == "mon jeu " == " mon jeu"
@ -43,41 +42,41 @@ namespace Model.Dice
{
throw new ArgumentNullException(nameof(name), "param should not be null or empty");
}
return Task.FromResult(diceGroups.First(diceGroup => diceGroup.Name.Equals(name.Trim())));
return Task.FromResult(new KeyValuePair<string, IEnumerable<Die>>(name, diceGroups[name]));
}
public void Remove(DiceGroup toRemove)
public void Remove(KeyValuePair<string, IEnumerable<Die>> toRemove)
{
if (toRemove.Name is null)
if (toRemove.Key is null)
{
throw new ArgumentNullException(nameof(toRemove), "param should not be null");
}
else
{
diceGroups.Remove(toRemove);
diceGroups.Remove(toRemove.Key);
}
}
/// <summary>
/// updates a (string, ReadOnlyCollection-of-Die) couple. only the name can be updated
/// updates a (string, IEnumerable-of-Die) couple. only the name can be updated
/// </summary>
/// <param name="before">original couple</param>
/// <param name="after">new couple</param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ArgumentNullException"></exception>
public Task<DiceGroup> Update(DiceGroup before, DiceGroup after)
public Task<KeyValuePair<string, IEnumerable<Die>>> Update(KeyValuePair<string, IEnumerable<Die>> before, KeyValuePair<string, IEnumerable<Die>> after)
{
// pas autorisé de changer les dés, juste le nom
if (!before.Dice.SequenceEqual(after.Dice))
if (!before.Value.Equals(after.Value))
{
throw new ArgumentException("the group of dice cannot be updated, only the name", nameof(before));
}
if (string.IsNullOrWhiteSpace(before.Name) || string.IsNullOrWhiteSpace(after.Name))
if (string.IsNullOrWhiteSpace(before.Key) || string.IsNullOrWhiteSpace(after.Key))
{
throw new ArgumentNullException(nameof(before), "dice group name should not be null or empty");
}
Remove(before);
diceGroups.Remove(before.Key);
Add(after);
return Task.FromResult(after);

@ -1,14 +1,13 @@
using Model.Dice.Faces;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
namespace Model.Dice
{
public abstract class Die
{
public ReadOnlyCollection<Face> Faces => new(faces);
public IEnumerable<Face> Faces => faces;
protected static readonly Random rnd = new();
@ -21,7 +20,7 @@ namespace Model.Dice
public virtual Face GetRandomFace()
{
int faceIndex = rnd.Next(0, Faces.Count);
int faceIndex = rnd.Next(0, Faces.Count());
return Faces.ElementAt(faceIndex);
}
}

@ -3,8 +3,12 @@
public abstract class Face
{
public string StringValue { get; protected set; }
}
public override string ToString()
{
return StringValue;
}
}
public abstract class Face<T> : Face
{
public T Value { get; protected set; }

@ -3,8 +3,8 @@ using Model.Dice.Faces;
using Model.Players;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Model.Games
@ -35,18 +35,18 @@ namespace Model.Games
/// <summary>
/// references the position in list of the current player, for a given game.
/// </summary>
private int nextIndex = 0;
private int nextIndex;
/// <summary>
/// the turns that have been done so far
/// </summary>
private readonly List<Turn> turns = new();
private readonly List<Turn> turns;
/// </summary>
/// get a READ ONLY enumerable of all turns belonging to this game
/// </summary>
/// <returns>a readonly enumerable of all this game's turns</returns>
public ReadOnlyCollection<Turn> GetHistory() => new(turns);
public IEnumerable<Turn> GetHistory() => turns.AsEnumerable();
/// <summary>
/// the game's player manager, doing CRUD on players and switching whose turn it is
@ -56,8 +56,8 @@ namespace Model.Games
/// <summary>
/// the group of dice used for this game
/// </summary>
public ReadOnlyCollection<Die> Dice => new(dice);
private readonly List<Die> dice = new();
public IEnumerable<Die> Dice => dice;
private readonly IEnumerable<Die> dice;
/// <summary>
/// constructs a Game with its own history of Turns.
@ -71,19 +71,19 @@ namespace Model.Games
{
Name = name;
PlayerManager = playerManager;
this.dice.AddRange(dice);
this.turns.AddRange(turns);
this.turns = turns is null ? new List<Turn>() : turns.ToList();
this.dice = dice;
this.nextIndex = 0;
}
/// <summary>
/// constructs a Game with no history of turns.
///
/// </summary>
/// <param name="name">the name of the game 😎</param>
/// <param name="playerManager">the game's player manager, doing CRUD on players and switching whose turn it is</param>
/// <param name="favGroup">the group of dice used for this game</param>
public Game(string name, IManager<Player> playerManager, IEnumerable<Die> dice)
: this(name, playerManager, dice, new List<Turn>())
: this(name, playerManager, dice, null)
{ }
/// <summary>
@ -144,8 +144,16 @@ namespace Model.Games
{
throw new ArgumentException("param could not be found in this collection\n did you forget to add it?", nameof(current));
}
nextIndex = (nextIndex + 1) % players.Count();
if (players.Last() == current)
{
// if we've reached the last player, we need the index to loop back around
nextIndex = 0;
}
else
{
// else we can just move up by one from the current index
nextIndex++;
}
}
/// <summary>
@ -161,5 +169,31 @@ namespace Model.Games
}
return faces;
}
/// <summary>
/// represents a Game in string format
/// </summary>
/// <returns>a Game in string format</returns>
public override string ToString()
{
StringBuilder sb = new();
sb.Append($"Game: {Name}");
sb.Append("\nPlayers:");
foreach (Player player in PlayerManager.GetAll()?.Result)
{
sb.Append($" {player.ToString()}");
}
sb.Append($"\nNext: {GetWhoPlaysNow()}");
sb.Append("\nLog:\n");
foreach (Turn turn in this.turns)
{
sb.Append($"\t{turn.ToString()}\n");
}
return sb.ToString();
}
}
}

@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
@ -11,14 +10,18 @@ namespace Model.Games
/// <summary>
/// the games managed by this instance
/// </summary>
private readonly List<Game> games = new();
private readonly List<Game> games;
public GameManager()
{
games = new();
}
/// <summary>
/// gets an unmodifiable collection of the games
/// </summary>
/// <returns>unmodifiable collection of the games</returns>
public Task<ReadOnlyCollection<Game>> GetAll() => Task.FromResult(new ReadOnlyCollection<Game>(games));
public Task<IEnumerable<Game>> GetAll() => Task.FromResult(games.AsEnumerable());
/// <summary>
/// finds the game with that name and returns it

@ -8,10 +8,10 @@ namespace Model.Games
public class MasterOfCeremonies
{
public IManager<Player> GlobalPlayerManager { get; private set; }
public IManager<DiceGroup> DiceGroupManager { get; private set; }
public IManager<KeyValuePair<string, IEnumerable<Die>>> DiceGroupManager { get; private set; }
public IManager<Game> GameManager { get; private set; }
public MasterOfCeremonies(IManager<Player> globalPlayerManager, IManager<DiceGroup> globalDiceGroupManager, IManager<Game> gameManager)
public MasterOfCeremonies(IManager<Player> globalPlayerManager, IManager<KeyValuePair<string, IEnumerable<Die>>> globalDiceGroupManager, IManager<Game> gameManager)
{
GlobalPlayerManager = globalPlayerManager;
DiceGroupManager = globalDiceGroupManager;

@ -3,8 +3,8 @@ using Model.Dice.Faces;
using Model.Players;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
namespace Model.Games
{
@ -28,7 +28,7 @@ namespace Model.Games
/// <summary>
/// the collection of Face that were rolled
/// </summary>
public ReadOnlyDictionary<Die, Face> DiceNFaces => new(diceNFaces);
public IEnumerable<KeyValuePair<Die, Face>> DiceNFaces => diceNFaces.AsEnumerable();
private readonly Dictionary<Die, Face> diceNFaces;
/// <summary>
@ -37,7 +37,24 @@ namespace Model.Games
/// <param name="when">date and time of the turn</param>
/// <param name="player">player who played the turn</param>
/// <param name="faces">faces that were rolled</param>
private Turn(DateTime when, Player player, IEnumerable<KeyValuePair<Die, Face>> diceNFaces)
private Turn(DateTime when, Player player, Dictionary<Die, Face> diceNFaces)
{
When = when;
Player = player;
this.diceNFaces = diceNFaces;
}
/// <summary>
/// creates a Turn with a specified time, passed as a parameter.
/// <br/>
/// whatever the DateTimeKind of <paramref name="when"/> might be,
/// it will become UTC during construction
/// </summary>
/// <param name="when">date and time of the turn</param>
/// <param name="player">player who played the turn</param>
/// <param name="faces">faces that were rolled</param>
/// <returns>a new Turn object</returns>
public static Turn CreateWithSpecifiedTime(DateTime when, Player player, Dictionary<Die, Face> diceNFaces)
{
if (player is null)
{
@ -47,7 +64,7 @@ namespace Model.Games
{
throw new ArgumentNullException(nameof(diceNFaces), "param should not be null");
}
if (!diceNFaces.Any())
if (diceNFaces.Count == 0)
{
throw new ArgumentException("param should not be null", nameof(diceNFaces));
}
@ -56,73 +73,50 @@ namespace Model.Games
when = when.ToUniversalTime();
}
When = when;
Player = player;
this.diceNFaces = diceNFaces.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
return new Turn(when, player, diceNFaces);
}
/// <summary>
/// creates a Turn with a specified time, passed as a parameter.
/// <br/>
/// whatever the DateTimeKind of <paramref name="when"/> might be,
/// it will become UTC during construction
/// creates a Turn with a default time, which is "now" in UTC.
/// </summary>
/// <param name="when">date and time of the turn</param>
/// <param name="player">player who played the turn</param>
/// <param name="faces">faces that were rolled</param>
/// <returns>a new Turn object</returns>
public static Turn CreateWithSpecifiedTime(DateTime when, Player player, IEnumerable<KeyValuePair<Die, Face>> diceNFaces)
public static Turn CreateWithDefaultTime(Player player, Dictionary<Die, Face> diceNFaces)
{
return new Turn(when, player, diceNFaces);
return CreateWithSpecifiedTime(DateTime.UtcNow, player, diceNFaces);
}
/// <summary>
/// creates a Turn with a default time, which is "now" in UTC.
/// represents a turn in string format
/// </summary>
/// <param name="player">player who played the turn</param>
/// <param name="faces">faces that were rolled</param>
/// <returns>a new Turn object</returns>
public static Turn CreateWithDefaultTime(Player player, IEnumerable<KeyValuePair<Die, Face>> diceNFaces)
/// <returns>a turn in string format</returns>
public override string ToString()
{
return CreateWithSpecifiedTime(DateTime.UtcNow, player, diceNFaces);
string[] datetime = When.ToString("s", System.Globalization.CultureInfo.InvariantCulture).Split("T");
string date = datetime[0];
string time = datetime[1];
StringBuilder sb = new();
sb.AppendFormat("{0} {1} -- {2} rolled:",
date,
time,
Player.ToString());
foreach (Face face in this.diceNFaces.Values)
{
sb.Append(" " + face.ToString());
}
return sb.ToString();
}
public bool Equals(Turn other)
{
if (other is null
||
!(Player.Equals(other.Player)
return Player.Equals(other.Player)
&& When.Equals(other.When)
&& DiceNFaces.Count == other.DiceNFaces.Count))
{
return false;
}
&& DiceNFaces.SequenceEqual(other.DiceNFaces);
// 🤮
for (int i = 0; i < DiceNFaces.Count; i++)
{
if (DiceNFaces.ElementAt(i).Key.Faces.Count
!= other.DiceNFaces.ElementAt(i).Key.Faces.Count)
{
return false;
}
if (!other.DiceNFaces.ElementAt(i).Value.StringValue
.Equals(DiceNFaces.ElementAt(i).Value.StringValue))
{
return false;
}
for (int j = 0; j < DiceNFaces.ElementAt(i).Key.Faces.Count; j++)
{
if (!other.DiceNFaces.ElementAt(i).Key.Faces.ElementAt(j).StringValue
.Equals(DiceNFaces.ElementAt(i).Key.Faces.ElementAt(j).StringValue))
{
return false;
}
}
}
return true;
}
public override bool Equals(object obj)
@ -136,7 +130,7 @@ namespace Model.Games
public override int GetHashCode()
{
return When.GetHashCode();
return HashCode.Combine(Player, When, DiceNFaces);
}
}
}

@ -1,5 +1,5 @@
using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Model
@ -12,7 +12,7 @@ namespace Model
public Task<T> GetOneByID(Guid ID);
public Task<ReadOnlyCollection<T>> GetAll();
public Task<IEnumerable<T>> GetAll();
public Task<T> Update(T before, T after);

@ -4,8 +4,4 @@
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NLog" Version="5.0.4" />
</ItemGroup>
</Project>

@ -30,6 +30,11 @@ namespace Model.Players
: this(player?.Name) // passes the player's name if player exists, else null
{ }
public override string ToString()
{
return Name;
}
public bool Equals(Player other)
{
return other is not null && Name.ToUpper() == other.Name.ToUpper(); // equality is case insensitive

@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
@ -11,8 +10,12 @@ namespace Model.Players
/// <summary>
/// a collection of the players that this manager is in charge of
/// </summary>
private readonly List<Player> players = new();
private readonly List<Player> players;
public PlayerManager()
{
players = new();
}
/// <summary>
/// add a new player
/// </summary>
@ -57,7 +60,7 @@ namespace Model.Players
/// so that the only way to modify the collection of players is to use this class's methods
/// </summary>
/// <returns>a readonly enumerable of all this manager's players</returns>
public Task<ReadOnlyCollection<Player>> GetAll() => Task.FromResult(new ReadOnlyCollection<Player>(players));
public Task<IEnumerable<Player>> GetAll() => Task.FromResult(players.AsEnumerable());
/// <summary>
/// update a player from <paramref name="before"/> to <paramref name="after"/>

@ -41,7 +41,7 @@ namespace Tests.Data_UTs
// Assert
Assert.True(db.PlayerEntity.Where(p => p.Name.Equals(name)).Any());
Assert.True(db.Players.Where(p => p.Name.Equals(name)).Any());
}
}

@ -1,407 +1,12 @@
using Data.EF.Dice;
using Data.EF.Dice.Faces;
using Data.EF.Games;
using Data.EF.Joins;
using Data.EF.Players;
using System;
using System;
using System.Collections.Generic;
using System.Drawing;
using Xunit;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tests.Data_UTs.Games
{
public class TurnEntityTest
{
private readonly DieEntity numDie;
private readonly FaceEntity numFace1 = new NumberFaceEntity() { ID = Guid.NewGuid(), Value = 7 };
private readonly FaceEntity numFace2 = new NumberFaceEntity() { ID = Guid.NewGuid(), Value = 8 };
private readonly DieEntity clrDie;
private readonly FaceEntity clrFace1 = new ColorFaceEntity() { ID = Guid.NewGuid(), A = 255, R = 255, G = 255, B = 255 };
private readonly FaceEntity clrFace2 = new ColorFaceEntity() { ID = Guid.NewGuid() };
private readonly DieEntity imgDie;
private readonly FaceEntity imgFace1 = new ImageFaceEntity() { ID = Guid.NewGuid(), Value = "https://a" };
private readonly FaceEntity imgFace2 = new ImageFaceEntity() { ID = Guid.NewGuid(), Value = "https://b" };
private readonly PlayerEntity player1 = new() { ID = Guid.NewGuid(), Name = "Marvin" };
private readonly PlayerEntity player2 = new() { ID = Guid.NewGuid(), Name = "Barbara" };
private readonly DateTime datetime1 = new(2020, 6, 15, 12, 15, 3, DateTimeKind.Utc);
private readonly DateTime datetime2 = new(2016, 12, 13, 14, 15, 16, DateTimeKind.Utc);
private readonly DieTurn dieTurn1;
private readonly DieTurn dieTurn2;
private readonly DieTurn dieTurn3;
private readonly DieTurn dieTurn4;
private readonly DieTurn dieTurn5;
private readonly FaceTurn faceTurn1;
private readonly FaceTurn faceTurn2;
private readonly FaceTurn faceTurn3;
private readonly FaceTurn faceTurn4;
private readonly FaceTurn faceTurn5;
private readonly TurnEntity turn1;
private readonly TurnEntity turn2;
private readonly TurnEntity turn3;
public TurnEntityTest()
{
numDie = new NumberDieEntity() { ID = Guid.NewGuid(), Faces = new List<NumberFaceEntity>() { numFace1 as NumberFaceEntity, numFace2 as NumberFaceEntity } };
(numFace1 as NumberFaceEntity).NumberDieEntity = (NumberDieEntity)numDie;
(numFace2 as NumberFaceEntity).NumberDieEntity = (NumberDieEntity)numDie;
(clrFace2 as ColorFaceEntity).SetValue(Color.FromName("blue"));
clrDie = new ColorDieEntity() { ID = Guid.NewGuid(), Faces = new List<ColorFaceEntity>() { clrFace1 as ColorFaceEntity, clrFace2 as ColorFaceEntity } };
(clrFace1 as ColorFaceEntity).ColorDieEntity = (ColorDieEntity)clrDie;
(clrFace2 as ColorFaceEntity).ColorDieEntity = (ColorDieEntity)clrDie;
imgDie = new ImageDieEntity() { ID = Guid.NewGuid(), Faces = new List<ImageFaceEntity>() { imgFace1 as ImageFaceEntity, imgFace2 as ImageFaceEntity } };
(imgFace1 as ImageFaceEntity).ImageDieEntity = (ImageDieEntity)imgDie;
(imgFace2 as ImageFaceEntity).ImageDieEntity = (ImageDieEntity)imgDie;
turn1 = new()
{
ID = Guid.NewGuid(),
When = datetime1,
PlayerEntity = player1,
PlayerEntityID = player1.ID,
Dice = new List<DieEntity>
{
numDie,
clrDie,
imgDie
},
Faces = new List<FaceEntity>
{
numFace1,
clrFace2,
imgFace2
},
};
dieTurn1 = new() { DieEntityID = numDie.ID, DieEntity = numDie, TurnEntityID = turn1.ID, TurnEntity = turn1 };
dieTurn2 = new() { DieEntityID = clrDie.ID, DieEntity = clrDie, TurnEntityID = turn1.ID, TurnEntity = turn1 };
dieTurn3 = new() { DieEntityID = imgDie.ID, DieEntity = imgDie, TurnEntityID = turn1.ID, TurnEntity = turn1 };
turn1.DieTurns = new() { dieTurn1, dieTurn2, dieTurn3 };
numDie.DieTurns = new() { dieTurn1 };
clrDie.DieTurns = new() { dieTurn2 };
imgDie.DieTurns = new() { dieTurn3 };
faceTurn1 = new() { FaceEntityID = numFace1.ID, FaceEntity = numFace1, TurnEntityID = turn1.ID, TurnEntity = turn1 };
faceTurn2 = new() { FaceEntityID = clrFace2.ID, FaceEntity = clrFace2, TurnEntityID = turn1.ID, TurnEntity = turn1 };
faceTurn3 = new() { FaceEntityID = imgFace2.ID, FaceEntity = imgFace2, TurnEntityID = turn1.ID, TurnEntity = turn1 };
turn1.FaceTurns = new() { faceTurn1, faceTurn2, faceTurn3 };
numFace1.FaceTurns = new() { faceTurn1 };
clrFace2.FaceTurns = new() { faceTurn2 };
imgFace2.FaceTurns = new() { faceTurn3 };
Guid turn2ID = Guid.NewGuid();
turn2 = new()
{
ID = turn2ID,
When = datetime2,
PlayerEntity = player2,
PlayerEntityID = player2.ID,
Dice = new List<DieEntity>
{
numDie,
clrDie
},
Faces = new List<FaceEntity>
{
numFace2,
clrFace2
},
};
dieTurn4 = new() { DieEntityID = numDie.ID, DieEntity = numDie, TurnEntityID = turn2.ID, TurnEntity = turn2 };
dieTurn5 = new() { DieEntityID = clrDie.ID, DieEntity = clrDie, TurnEntityID = turn2.ID, TurnEntity = turn2 };
turn2.DieTurns = new() { dieTurn4, dieTurn5 };
numDie.DieTurns = new() { dieTurn4 };
clrDie.DieTurns = new() { dieTurn5 };
faceTurn4 = new() { FaceEntityID = numFace2.ID, FaceEntity = numFace2, TurnEntityID = turn2.ID, TurnEntity = turn2 };
faceTurn5 = new() { FaceEntityID = clrFace2.ID, FaceEntity = clrFace2, TurnEntityID = turn2.ID, TurnEntity = turn2 };
turn2.FaceTurns = new() { faceTurn4, faceTurn5 };
numFace2.FaceTurns = new() { faceTurn4 };
clrFace2.FaceTurns = new() { faceTurn5 };
turn3 = new()
{
ID = turn2ID,
When = datetime2,
PlayerEntity = player2,
PlayerEntityID = player2.ID,
Dice = new List<DieEntity>
{
numDie,
clrDie
},
Faces = new List<FaceEntity>
{
numFace2,
clrFace2
},
};
}
[Fact]
public void TestGetSetID()
{
// Arrange
TurnEntity turn = new();
Guid expected = Guid.NewGuid();
// Act
turn.ID = expected;
Guid actual = turn.ID;
// Assert
Assert.Equal(expected, actual);
}
[Fact]
public void TestGetSetWhen()
{
// Arrange
TurnEntity turn = new();
DateTime expected = datetime1;
// Act
turn.When = expected;
DateTime actual = turn.When;
// Assert
Assert.Equal(expected, actual);
}
[Fact]
public void TestGetSetPlayer()
{
// Arrange
TurnEntity turn = new();
PlayerEntity expected = player1;
// Act
turn.PlayerEntity = expected;
PlayerEntity actual = turn.PlayerEntity;
// Assert
Assert.Equal(expected, actual);
}
[Fact]
public void TestGetSetPlayerID()
{
// Arrange
TurnEntity turn = new();
Guid expected = player1.ID;
// Act
turn.PlayerEntityID = expected;
Guid actual = turn.PlayerEntityID;
// Assert
Assert.Equal(expected, actual);
}
[Fact]
public void TestGetSetDice()
{
// Arrange
TurnEntity turn = new();
ICollection<DieEntity> expected = new List<DieEntity>
{
numDie,
clrDie,
imgDie
};
// Act
turn.Dice = expected;
ICollection<DieEntity> actual = turn.Dice;
// Assert
Assert.Equal(expected, actual);
}
[Fact]
public void TestGetSetDieTurns()
{
// Arrange
TurnEntity turn = new();
List<DieTurn> expected = new() { dieTurn1, dieTurn2, dieTurn3 };
// Act
turn.DieTurns = expected;
List<DieTurn> actual = turn.DieTurns;
// Assert
Assert.Equal(expected, actual);
}
[Fact]
public void TestGetSetFaces()
{
// Arrange
TurnEntity turn = new();
ICollection<FaceEntity> expected = new List<FaceEntity>
{
numFace1,
clrFace1,
imgFace1
};
// Act
turn.Faces = expected;
ICollection<FaceEntity> actual = turn.Faces;
// Assert
Assert.Equal(expected, actual);
}
[Fact]
public void TestGetSetFaceTurns()
{
// Arrange
TurnEntity turn = new();
List<FaceTurn> expected = new() { faceTurn1, faceTurn2 };
// Act
turn.FaceTurns = expected;
List<FaceTurn> actual = turn.FaceTurns;
// Assert
Assert.Equal(expected, actual);
}
[Fact]
public void TestEqualsWhenNotTurnEntityThenFalse()
{
// Arrange
Point point;
TurnEntity entity;
// Act
point = new(1, 2);
entity = turn1;
// Assert
Assert.False(point.Equals(entity));
Assert.False(entity.Equals(point));
}
[Fact]
public void TestEqualsWhenNullThenFalse()
{
// Arrange
TurnEntity entity;
// Act
entity = turn2;
// Assert
Assert.False(entity.Equals(null));
}
[Fact]
public void TestGoesThruToSecondMethodIfObjIsTypeTurnEntity()
{
// Arrange
object t1;
TurnEntity t2;
// Act
t1 = turn1;
t2 = turn2;
// Assert
Assert.False(t1.Equals(t2));
Assert.False(t2.Equals(t1));
}
[Fact]
public void TestEqualsFalseIfNotSame()
{
// Arrange
TurnEntity t1;
TurnEntity t2;
// Act
t1 = turn1;
t2 = turn2;
// Assert
Assert.False(t1.Equals(t2));
Assert.False(t2.Equals(t1));
}
[Fact]
public void TestEqualsTrueIfSame()
{
// Arrange
TurnEntity t1;
TurnEntity t2;
// Act
t1 = turn2;
t2 = turn3; // turns 2 and 3 should be same as far as Equals is concerned
// Assert
Assert.True(t1.Equals(t2));
Assert.True(t2.Equals(t1));
}
[Fact]
public void TestSameHashFalseIfNotSame()
{
// Arrange
TurnEntity t1;
TurnEntity t2;
// Act
t1 = turn1;
t2 = turn2;
// Assert
Assert.False(t1.GetHashCode().Equals(t2.GetHashCode()));
Assert.False(t2.GetHashCode().Equals(t1.GetHashCode()));
}
[Fact]
public void TestSameHashTrueIfSame()
{
// Arrange
TurnEntity t1;
TurnEntity t2;
// Act
t1 = turn2;
t2 = turn3;
// Assert
Assert.True(t1.GetHashCode().Equals(t2.GetHashCode()));
Assert.True(t2.GetHashCode().Equals(t1.GetHashCode()));
}
}
}

@ -1,274 +1,12 @@
using Data.EF.Dice.Faces;
using Data.EF.Dice;
using Data.EF.Players;
using Model.Dice;
using Model.Players;
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
using Data.EF.Games;
using System.Drawing;
using Model.Games;
using Newtonsoft.Json.Linq;
using Model.Dice.Faces;
using System.Diagnostics;
namespace Tests.Data_UTs.Games
{
public class TurnExtensionsTest
{
private readonly DateTime datetime1 = new(2013, 2, 1, 1, 19, 4, DateTimeKind.Utc);
private readonly DateTime datetime2 = new(2014, 8, 1, 23, 12, 4, DateTimeKind.Utc);
private readonly PlayerEntity playerEntity = new() { Name = "Paula" };
private readonly DieEntity numDieEntity;
private readonly FaceEntity numFace1Entity = new NumberFaceEntity() { Value = 7 };
private readonly FaceEntity numFace2Entity = new NumberFaceEntity() { Value = 8 };
private readonly DieEntity clrDieEntity;
private readonly FaceEntity clrFace1Entity = new ColorFaceEntity() { A = 255, R = 255, G = 255, B = 255 };
private readonly FaceEntity clrFace2Entity = new ColorFaceEntity() { A = 255, R = 0, G = 0, B = 128 };
private readonly DieEntity imgDieEntity;
private readonly FaceEntity imgFace1Entity = new ImageFaceEntity() { Value = "https://a" };
private readonly FaceEntity imgFace2Entity = new ImageFaceEntity() { Value = "https://b" };
public TurnExtensionsTest()
{
numDieEntity = new NumberDieEntity() { Faces = new List<NumberFaceEntity>() { numFace1Entity as NumberFaceEntity, numFace2Entity as NumberFaceEntity } };
(numFace1Entity as NumberFaceEntity).NumberDieEntity = (NumberDieEntity)numDieEntity;
(numFace2Entity as NumberFaceEntity).NumberDieEntity = (NumberDieEntity)numDieEntity;
clrDieEntity = new ColorDieEntity() { Faces = new List<ColorFaceEntity>() { clrFace1Entity as ColorFaceEntity, clrFace2Entity as ColorFaceEntity } };
(clrFace1Entity as ColorFaceEntity).ColorDieEntity = (ColorDieEntity)clrDieEntity;
(clrFace2Entity as ColorFaceEntity).ColorDieEntity = (ColorDieEntity)clrDieEntity;
imgDieEntity = new ImageDieEntity() { Faces = new List<ImageFaceEntity>() { imgFace1Entity as ImageFaceEntity, imgFace2Entity as ImageFaceEntity } };
(imgFace1Entity as ImageFaceEntity).ImageDieEntity = (ImageDieEntity)imgDieEntity;
(imgFace2Entity as ImageFaceEntity).ImageDieEntity = (ImageDieEntity)imgDieEntity;
}
[Fact]
public void TestToModel()
{
// Arrange
TurnEntity entity = new()
{
When = datetime1,
PlayerEntity = playerEntity,
Dice = new List<DieEntity>
{
numDieEntity,
clrDieEntity,
imgDieEntity
},
Faces = new List<FaceEntity>
{
numFace1Entity,
clrFace2Entity,
imgFace2Entity
}
};
Turn expected = Turn.CreateWithSpecifiedTime(
datetime1,
playerEntity.ToModel(),
new Dictionary<Die, Face>()
{
{(numDieEntity as NumberDieEntity).ToModel(), (numFace1Entity as NumberFaceEntity).ToModel() },
{(clrDieEntity as ColorDieEntity).ToModel(), (clrFace2Entity as ColorFaceEntity).ToModel() },
{(imgDieEntity as ImageDieEntity).ToModel(), (imgFace2Entity as ImageFaceEntity).ToModel() }
});
// Act
Turn actual = entity.ToModel();
// Assert
Assert.True(expected.Equals(actual));
}
[Fact]
public void TestToModels()
{
// Arrange
TurnEntity[] entities = new TurnEntity[]
{
new TurnEntity()
{
When = datetime1,
PlayerEntity = new PlayerEntity() {Name = "Aardvark"},
Dice = new List<DieEntity>
{
numDieEntity,
clrDieEntity
},
Faces = new List<FaceEntity>
{
numFace2Entity,
clrFace2Entity
}
},
new TurnEntity()
{
When = datetime2,
PlayerEntity = new PlayerEntity() {Name = "Chloe"},
Dice = new List<DieEntity>
{
clrDieEntity,
imgDieEntity
},
Faces = new List<FaceEntity>
{
clrFace1Entity,
imgFace1Entity
}
}
};
IEnumerable<Turn> expected = new Turn[]
{
Turn.CreateWithSpecifiedTime(
datetime1,
new("Aardvark"),
new Dictionary<Die, Face>()
{
{(numDieEntity as NumberDieEntity).ToModel(), (numFace2Entity as NumberFaceEntity).ToModel() },
{(clrDieEntity as ColorDieEntity).ToModel(), (clrFace2Entity as ColorFaceEntity).ToModel() },
}),
Turn.CreateWithSpecifiedTime(
datetime2,
new("Chloe"),
new Dictionary<Die, Face>()
{
{(clrDieEntity as ColorDieEntity).ToModel(), (clrFace1Entity as ColorFaceEntity).ToModel() },
{(imgDieEntity as ImageDieEntity).ToModel(), (imgFace1Entity as ImageFaceEntity).ToModel() }
})
}.AsEnumerable();
// Act
IEnumerable<Turn> actual = entities.ToModels();
// Assert
Assert.Equal(expected, actual);
}
[Fact]
public void TestToEntity()
{
// Arrange
Turn model = Turn.CreateWithSpecifiedTime(
datetime1,
playerEntity.ToModel(),
new Dictionary<Die, Face>()
{
{(numDieEntity as NumberDieEntity).ToModel(), (numFace2Entity as NumberFaceEntity).ToModel() },
{(clrDieEntity as ColorDieEntity).ToModel(), (clrFace2Entity as ColorFaceEntity).ToModel() },
{(imgDieEntity as ImageDieEntity).ToModel(), (imgFace1Entity as ImageFaceEntity).ToModel() }
});
TurnEntity expected = new()
{
When = datetime1,
PlayerEntity = playerEntity,
Dice = new List<DieEntity>
{
numDieEntity,
clrDieEntity,
imgDieEntity
},
Faces = new List<FaceEntity>
{
numFace2Entity,
clrFace2Entity,
imgFace1Entity
}
};
// Act
TurnEntity actual = model.ToEntity();
// Assert
Assert.True(expected.Equals(actual));
}
[Fact]
public void TestToEntities()
{
// Arrange
Turn[] models = new Turn[]
{
Turn.CreateWithSpecifiedTime(
datetime2,
new("Mimi"),
new Dictionary<Die, Face>()
{
{(numDieEntity as NumberDieEntity).ToModel(), (numFace2Entity as NumberFaceEntity).ToModel() },
{(clrDieEntity as ColorDieEntity).ToModel(), (clrFace2Entity as ColorFaceEntity).ToModel() },
}),
Turn.CreateWithSpecifiedTime(
datetime1,
new("blaaargh"),
new Dictionary<Die, Face>()
{
{(clrDieEntity as ColorDieEntity).ToModel(), (clrFace1Entity as ColorFaceEntity).ToModel() },
{(imgDieEntity as ImageDieEntity).ToModel(), (imgFace1Entity as ImageFaceEntity).ToModel() }
})
};
IEnumerable<TurnEntity> expected = new TurnEntity[]
{
new TurnEntity()
{
When = datetime2,
PlayerEntity = new PlayerEntity() {Name = "Mimi"},
Dice = new List<DieEntity>
{
numDieEntity,
clrDieEntity
},
Faces = new List<FaceEntity>
{
numFace2Entity,
clrFace2Entity
}
},
new TurnEntity()
{
When = datetime1,
PlayerEntity = new PlayerEntity() {Name = "blaaargh"},
Dice = new List<DieEntity>
{
clrDieEntity,
imgDieEntity
},
Faces = new List<FaceEntity>
{
clrFace1Entity,
imgFace1Entity
}
}
}.AsEnumerable();
// Act
IEnumerable<TurnEntity> actual = models.ToEntities();
// Assert
Assert.Equal(expected, actual);
}
}
}

@ -90,7 +90,7 @@ namespace Tests.Data_UTs.Players
db.Database.EnsureCreated();
mgr = new(db);
Assert.Equal(expectedName, (await mgr.GetOneByName(expectedName)).Name);
Assert.Equal(expectedCount, (await mgr.GetAll()).Count);
Assert.Equal(expectedCount, (await mgr.GetAll()).Count());
}
}
@ -368,7 +368,7 @@ namespace Tests.Data_UTs.Players
{
db.Database.EnsureCreated();
Assert.DoesNotContain(toRemove, db.PlayerEntity);
Assert.DoesNotContain(toRemove, db.Players);
}
}
@ -656,9 +656,11 @@ namespace Tests.Data_UTs.Players
PlayerDbManager mgr;
Guid id = Guid.NewGuid();
Guid otherId;
Guid otherId = Guid.NewGuid();
PlayerEntity presentEntity;
PlayerEntity absentEntity;
// Act
@ -668,10 +670,9 @@ namespace Tests.Data_UTs.Players
mgr = new(db);
presentEntity = new() { ID = id, Name = "Victor" };
await mgr.Add(presentEntity);
otherId = Guid.NewGuid();
// not added
await mgr.Add(presentEntity);
absentEntity = new() { ID = otherId, Name = "Victor" };
}
// Assert
@ -681,7 +682,7 @@ namespace Tests.Data_UTs.Players
db.Database.EnsureCreated();
mgr = new(db);
Assert.False(await mgr.IsPresentByID(otherId));
Assert.DoesNotContain(absentEntity, db.Players);
}
}
}

@ -1,5 +1,6 @@
using Data.EF.Players;
using System;
using Tests.Model_UTs;
using Xunit;
namespace Tests.Data_UTs.Players

@ -188,9 +188,9 @@ namespace Tests.Model_UTs.Games
Game newGame = new(newName, oldGame.PlayerManager, oldGame.Dice);
// Act
int expectedSize = (await gm.GetAll()).Count;
int expectedSize = (await gm.GetAll()).Count();
await gm.Update(oldGame, newGame);
int actualSize = (await gm.GetAll()).Count;
int actualSize = (await gm.GetAll()).Count();
// Assert
Assert.NotEqual(oldName, newName);
@ -208,12 +208,12 @@ namespace Tests.Model_UTs.Games
// Arrange
IManager<Game> gm = stubGameRunner.GameManager;
int expectedSize = (await gm.GetAll()).Count;
int expectedSize = (await gm.GetAll()).Count();
Game oldGame = (await gm.GetAll()).First();
// Act
void action() => gm.Update(oldGame, new(badName, oldGame.PlayerManager, oldGame.Dice));
int actualSize = (await gm.GetAll()).Count;
int actualSize = (await gm.GetAll()).Count();
// Assert
Assert.Throws<ArgumentException>(action); // thrown by constructor
@ -226,12 +226,12 @@ namespace Tests.Model_UTs.Games
{
// Arrange
IManager<Game> gm = stubGameRunner.GameManager;
int expectedSize = (await gm.GetAll()).Count;
int expectedSize = (await gm.GetAll()).Count();
Game oldGame = (await gm.GetAll()).First();
// Act
async Task actionAsync() => await gm.Update(oldGame, null);
int actualSize = (await gm.GetAll()).Count;
int actualSize = (await gm.GetAll()).Count();
// Assert
await Assert.ThrowsAsync<ArgumentNullException>(actionAsync); // thrown by constructor
@ -244,12 +244,12 @@ namespace Tests.Model_UTs.Games
{
// Arrange
IManager<Game> gm = stubGameRunner.GameManager;
int expectedSize = (await gm.GetAll()).Count;
int expectedSize = (await gm.GetAll()).Count();
Game oldGame = (await gm.GetAll()).First();
// Act
async Task actionAsync() => await gm.Update(null, new("newgamename", oldGame.PlayerManager, oldGame.Dice));
int actualSize = (await gm.GetAll()).Count;
int actualSize = (await gm.GetAll()).Count();
// Assert
await Assert.ThrowsAsync<ArgumentNullException>(actionAsync); // thrown by constructor
@ -265,12 +265,12 @@ namespace Tests.Model_UTs.Games
{
// Arrange
IManager<Game> gm = stubGameRunner.GameManager;
int expectedSize = (await gm.GetAll()).Count;
int expectedSize = (await gm.GetAll()).Count();
Game oldGame = (await gm.GetAll()).First();
// Act
void action() => gm.Update(new(badName, oldGame.PlayerManager, oldGame.Dice), new("valid", oldGame.PlayerManager, oldGame.Dice));
int actualSize = (await gm.GetAll()).Count;
int actualSize = (await gm.GetAll()).Count();
// Assert
Assert.Throws<ArgumentException>(action); // thrown by constructor

@ -5,6 +5,7 @@ using Model.Games;
using Model.Players;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
@ -20,9 +21,9 @@ namespace Tests.Model_UTs.Games
private readonly IEnumerable<Die> DICE_1, DICE_2;
public GameTest()
{
IEnumerable<DiceGroup> diceGroups = stubMasterOfCeremonies.DiceGroupManager.GetAll()?.Result;
DICE_1 = diceGroups.First().Dice;
DICE_2 = diceGroups.Last().Dice;
IEnumerable<KeyValuePair<string, IEnumerable<Die>>> diceGroups = stubMasterOfCeremonies.DiceGroupManager.GetAll()?.Result;
DICE_1 = diceGroups.First().Value;
DICE_2 = diceGroups.Last().Value;
}
[Fact]
@ -77,17 +78,17 @@ namespace Tests.Model_UTs.Games
}
[Fact]
public async Task TestGetHistory()
public async void TestGetHistory()
{
// Arrange
IEnumerable<KeyValuePair<Die, Face>> diceNFaces =
(await stubMasterOfCeremonies.GameManager.GetAll())
Dictionary<Die, Face> diceNFaces =
(Dictionary<Die, Face>)(await stubMasterOfCeremonies.GameManager.GetAll())
.First()
.GetHistory()
.First().DiceNFaces;
Turn turn1 = Turn.CreateWithSpecifiedTime(new(1, 2, 3), PLAYER_1, diceNFaces);
Turn turn2 = Turn.CreateWithSpecifiedTime(new(1, 2, 3), PLAYER_2, diceNFaces); // yeah they rolled the same
Turn turn1 = Turn.CreateWithDefaultTime(PLAYER_1, diceNFaces);
Turn turn2 = Turn.CreateWithDefaultTime(PLAYER_2, diceNFaces); // yeah they rolled the same
IEnumerable<Turn> expected = new List<Turn>() { turn1, turn2 };
@ -141,8 +142,10 @@ namespace Tests.Model_UTs.Games
await game.PrepareNextPlayer(currentPlayer);
}
Debug.WriteLine(game);
// Act
int actual = game.GetHistory().Count;
int actual = game.GetHistory().Count();
int expected = n;
// Assert

@ -22,9 +22,9 @@ namespace Tests.Model_UTs.Games
Game game = (await masterOfCeremonies.GameManager.GetAll()).First();
// Act
int turnsBefore = game.GetHistory().Count;
int turnsBefore = game.GetHistory().Count();
await MasterOfCeremonies.PlayGame(game);
int turnsAfter = game.GetHistory().Count;
int turnsAfter = game.GetHistory().Count();
// Assert
Assert.Equal(turnsBefore + 1, turnsAfter);

@ -5,7 +5,6 @@ using Model.Games;
using Model.Players;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Xunit;
@ -15,14 +14,14 @@ namespace Tests.Model_UTs.Games
{
private readonly MasterOfCeremonies stubMasterOfCeremonies = new Stub().LoadApp()?.Result;
private readonly ReadOnlyDictionary<Die, Face> DICE_N_FACES_1;
private readonly ReadOnlyDictionary<Die, Face> DICE_N_FACES_2;
Dictionary<Die, Face> DICE_N_FACES_1, DICE_N_FACES_2;
public TurnTest()
{
DICE_N_FACES_1 = stubMasterOfCeremonies.GameManager.GetAll()?.Result.First().GetHistory().First().DiceNFaces;
DICE_N_FACES_2 = stubMasterOfCeremonies.GameManager.GetAll()?.Result.Last().GetHistory().Last().DiceNFaces;
DICE_N_FACES_1 = (Dictionary<Die, Face>)stubMasterOfCeremonies.GameManager.GetAll()?.Result.First().GetHistory().First().DiceNFaces;
DICE_N_FACES_2 = (Dictionary<Die, Face>)stubMasterOfCeremonies.GameManager.GetAll()?.Result.Last().GetHistory().Last().DiceNFaces;
}
[Fact]
@ -94,9 +93,10 @@ namespace Tests.Model_UTs.Games
// Arrange
DateTime dateTime = new(year: 2018, month: 06, day: 15, hour: 16, minute: 30, second: 0, kind: DateTimeKind.Utc);
Player player = new("Chucky");
DICE_N_FACES_1.Clear();
// Act
void action() => Turn.CreateWithSpecifiedTime(dateTime, player, new Dictionary<Die, Face>());
void action() => Turn.CreateWithSpecifiedTime(dateTime, player, DICE_N_FACES_1);
// Assert
Assert.Throws<ArgumentException>(action);

@ -204,7 +204,7 @@ namespace Tests.Model_UTs.Players
// Assert
Assert.DoesNotContain(oldPlayer, await playerManager.GetAll());
Assert.Contains(newPlayer, await playerManager.GetAll());
Assert.True((await playerManager.GetAll()).Count == 1);
Assert.True((await playerManager.GetAll()).Count() == 1);
}
[Theory]
@ -239,12 +239,12 @@ namespace Tests.Model_UTs.Players
PlayerManager playerManager = new();
Player oldPlayer = new("Ni!");
await playerManager.Add(oldPlayer);
int size1 = (await playerManager.GetAll()).Count;
int size1 = (await playerManager.GetAll()).Count();
// Act
Assert.Contains(oldPlayer, await playerManager.GetAll());
async Task actionAsync() => await playerManager.Update(oldPlayer, new Player(badName));// this is really testing the Player class...
int size2 = (await playerManager.GetAll()).Count;
int size2 = (await playerManager.GetAll()).Count();
// Assert
await Assert.ThrowsAsync<ArgumentException>(actionAsync); // thrown by Player constructor
@ -259,12 +259,12 @@ namespace Tests.Model_UTs.Players
PlayerManager playerManager = new();
Player oldPlayer = new("Ni!");
await playerManager.Add(oldPlayer);
int size1 = (await playerManager.GetAll()).Count;
int size1 = (await playerManager.GetAll()).Count();
// Act
Assert.Contains(oldPlayer, await playerManager.GetAll());
async Task actionAsync() => await playerManager.Update(oldPlayer, null);
int size2 = (await playerManager.GetAll()).Count;
int size2 = (await playerManager.GetAll()).Count();
// Assert
await Assert.ThrowsAsync<ArgumentNullException>(actionAsync); // thrown by Update()
@ -280,11 +280,11 @@ namespace Tests.Model_UTs.Players
Player newPlayer = new("Kevin");
Player oldPlayer = new("Ursula");
await playerManager.Add(oldPlayer);
int size1 = (await playerManager.GetAll()).Count;
int size1 = (await playerManager.GetAll()).Count();
// Act
async Task actionAsync() => await playerManager.Update(null, newPlayer);
int size2 = (await playerManager.GetAll()).Count;
int size2 = (await playerManager.GetAll()).Count();
// Assert
await Assert.ThrowsAsync<ArgumentNullException>(actionAsync); // thrown by Update()
@ -302,11 +302,11 @@ namespace Tests.Model_UTs.Players
PlayerManager playerManager = new();
Player oldPlayer = new("Ursula");
await playerManager.Add(oldPlayer);
int size1 = (await playerManager.GetAll()).Count;
int size1 = (await playerManager.GetAll()).Count();
// Act
async Task actionAsync() => await playerManager.Update(new Player(name), new Player("Vicky"));
int size2 = (await playerManager.GetAll()).Count;
int size2 = (await playerManager.GetAll()).Count();
// Assert
await Assert.ThrowsAsync<ArgumentException>(actionAsync); // thrown by Player constructor

@ -65,6 +65,20 @@ namespace Tests.Model_UTs.Players
Assert.Throws<ArgumentException>(action);
}
[Fact]
public void TestToStringCorrectName()
{
// Arrange
string expected = "Bob";
Player player = new(expected);
// Act
string actual = player.ToString();
// Assert
Assert.Equal(expected, actual);
}
[Fact]
public void TestEqualsFalseIfNotPlayer()
{

@ -1,13 +1,13 @@
namespace Tests
{
public class Point
{
public int X { get; private set; }
public int Y { get; private set; }
public Point(int x, int y)
{
X = x; Y = y;
}
}
}
namespace Tests.Model_UTs
{
public class Point
{
public int X { get; private set; }
public int Y { get; private set; }
public Point(int x, int y)
{
X = x; Y = y;
}
}
}

@ -1,121 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utils;
using Xunit;
namespace Tests.Utils_UTs
{
public class EnumerablesTest
{
[Fact]
public void TestFeedListsToDict()
{
// Arrange
string str1 = "blah";
string str2 = "blahblah";
string str3 = "azfyoaz";
int int1 = 5;
int int2 = 12;
int int3 = 3;
Dictionary<string, int> expected = new()
{
{ str1, int1 },
{ str2, int2 },
{ str3, int3 }
};
List<string> strings = new() { str2, str3 };
List<int> ints = new() { int2, int3 };
Dictionary<string, int> actual = new()
{
{str1, int1 }
}; // we will add on top of this
// Act
actual = Enumerables.FeedListsToDict(actual, strings, ints);
// Assert
Assert.Equal(expected, actual);
}
[Fact]
public void TestGetDictFromLists()
{
// Arrange
string str1 = "blah";
string str2 = "blahblah";
int int1 = 5;
int int2 = 12;
Dictionary<string, int> expected = new()
{
{ str1, int1 },
{ str2, int2 }
};
List<string> strings = new() { str1, str2 };
List<int> ints = new() { int1, int2 };
// Act
Dictionary<string, int> actual = Enumerables.GetDictFromLists(strings, ints);
// Assert
Assert.Equal(expected, actual);
}
public static IEnumerable<object[]> EmptyList()
{
yield return new object[] { new List<string>() };
}
[Theory]
[InlineData(null)]
[MemberData(nameof(EmptyList))]
public void TestGetDictFromListsWhenKeysNullOrEmptyThenNew(List<string> strings)
{
// Arrange
int int1 = 5;
int int2 = 12;
Dictionary<string, int> expected = new();
List<int> ints = new() { int1, int2 };
// Act
Dictionary<string, int> actual = Enumerables.GetDictFromLists(strings, ints);
// Assert
Assert.Equal(expected, actual);
}
[Theory]
[InlineData(null)]
[MemberData(nameof(EmptyList))]
public void TestGetDictFromListsWhenValuesNullOrEmptyThenNew(List<string> stringsB)
{
// Arrange
string str1 = "blah";
string str2 = "blahblah";
Dictionary<string, string> expected = new();
List<string> strings = new() { str1, str2 };
// Act
Dictionary<string, string> actual = Enumerables.GetDictFromLists(strings, stringsB);
// Assert
Assert.Equal(expected, actual);
}
}
}

@ -1,29 +0,0 @@
namespace Utils
{
public static class Enumerables
{
public static Dictionary<K, V> GetDictFromLists<K, V>(List<K> keys, List<V> values)
{
if (keys == null || values == null || keys.Count == 0 || values.Count == 0)
{
return new Dictionary<K, V>();
}
return keys.Zip(
values,
(key, value) => new { key, value })
.ToDictionary(kvp => kvp.key, kvp => kvp.value
);
}
public static Dictionary<K, V> FeedListsToDict<K, V>(Dictionary<K, V> kvps, List<K> keys, List<V> values)
{
foreach (KeyValuePair<K, V> kvp in GetDictFromLists(keys, values))
{
kvps.Add(kvp.Key, kvp.Value);
}
return kvps;
}
}
}

@ -1,8 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
</Project>
Loading…
Cancel
Save