diff --git a/Sources/Data/EF/Games/TurnEntity.cs b/Sources/Data/EF/Games/TurnEntity.cs index 21ff016..711e195 100644 --- a/Sources/Data/EF/Games/TurnEntity.cs +++ b/Sources/Data/EF/Games/TurnEntity.cs @@ -2,10 +2,11 @@ using Data.EF.Dice.Faces; using Data.EF.Joins; using Data.EF.Players; +using Model.Players; namespace Data.EF.Games { - public sealed class TurnEntity: IEquatable + public sealed class TurnEntity : IEquatable { public Guid ID { get; set; } public DateTime When { get; set; } @@ -27,12 +28,41 @@ namespace Data.EF.Games public bool Equals(TurnEntity other) { - return other is not null - && this.ID.Equals(other.ID) - && this.When.Equals(other.When) - && this.PlayerEntity.Equals(other.PlayerEntity) - && this.Dice.SequenceEqual(other.Dice) - && this.Faces.SequenceEqual(other.Faces); + 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() diff --git a/Sources/Data/EF/Games/TurnExtensions.cs b/Sources/Data/EF/Games/TurnExtensions.cs index 469fd64..5380a1f 100644 --- a/Sources/Data/EF/Games/TurnExtensions.cs +++ b/Sources/Data/EF/Games/TurnExtensions.cs @@ -10,7 +10,7 @@ namespace Data.EF.Games public static class TurnExtensions { - private static (List, List) ToModels(ICollection diceEntities, ICollection faceEntities) + private static (List, List) ToModelsByTypes(ICollection diceEntities, ICollection faceEntities) { List dice = new(); List faces = new(); @@ -38,7 +38,7 @@ namespace Data.EF.Games List keysList; List valuesList; - (keysList, valuesList) = ToModels(entity.Dice, entity.Faces); + (keysList, valuesList) = ToModelsByTypes(entity.Dice, entity.Faces); DiceNFaces = Utils.Enumerables.FeedListsToDict(DiceNFaces, keysList, valuesList); diff --git a/Sources/Model/Dice/Faces/Face.cs b/Sources/Model/Dice/Faces/Face.cs index 815d603..e05ab6f 100644 --- a/Sources/Model/Dice/Faces/Face.cs +++ b/Sources/Model/Dice/Faces/Face.cs @@ -3,12 +3,8 @@ public abstract class Face { public string StringValue { get; protected set; } - - public override string ToString() - { - return StringValue; - } } + public abstract class Face : Face { public T Value { get; protected set; } diff --git a/Sources/Model/Games/Turn.cs b/Sources/Model/Games/Turn.cs index c6a118f..7709e0c 100644 --- a/Sources/Model/Games/Turn.cs +++ b/Sources/Model/Games/Turn.cs @@ -89,10 +89,39 @@ namespace Model.Games public bool Equals(Turn other) { - return Player.Equals(other.Player) + if (other is null + || + !(Player.Equals(other.Player) && When.Equals(other.When) - && DiceNFaces.SequenceEqual(other.DiceNFaces); + && DiceNFaces.Count() == other.DiceNFaces.Count())) + { + return false; + } + + 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) @@ -106,7 +135,16 @@ namespace Model.Games public override int GetHashCode() { - return HashCode.Combine(Player, When, DiceNFaces); + int hash = Player.GetHashCode() + When.GetHashCode(); + foreach (KeyValuePair kvp in DiceNFaces) + { + hash = hash * 31 + kvp.Value.StringValue.GetHashCode(); + foreach (Face face in kvp.Key.Faces) + { + hash = hash * 19 + face.StringValue.GetHashCode(); + } + } + return hash; } } } diff --git a/Sources/Tests/Data_UTs/Games/TurnEntityTest.cs b/Sources/Tests/Data_UTs/Games/TurnEntityTest.cs index 0afb061..31df762 100644 --- a/Sources/Tests/Data_UTs/Games/TurnEntityTest.cs +++ b/Sources/Tests/Data_UTs/Games/TurnEntityTest.cs @@ -25,6 +25,12 @@ namespace Tests.Data_UTs.Games 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; @@ -147,14 +153,6 @@ namespace Tests.Data_UTs.Games }; } - - - 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); - [Fact] public void TestGetSetID() { diff --git a/Sources/Tests/Data_UTs/Games/TurnExtensionsTest.cs b/Sources/Tests/Data_UTs/Games/TurnExtensionsTest.cs index 7d13e86..6f3a57e 100644 --- a/Sources/Tests/Data_UTs/Games/TurnExtensionsTest.cs +++ b/Sources/Tests/Data_UTs/Games/TurnExtensionsTest.cs @@ -1,12 +1,132 @@ -using System; +using Data.EF.Dice.Faces; +using Data.EF.Dice; +using Data.EF.Players; +using Model.Dice; +using Model.Players; +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; namespace Tests.Data_UTs.Games { public class TurnExtensionsTest { + private readonly DateTime datetime = new(2013, 2, 1, 1, 19, 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() { numFace1Entity as NumberFaceEntity, numFace2Entity as NumberFaceEntity } }; + (numFace1Entity as NumberFaceEntity).NumberDieEntity = (NumberDieEntity)numDieEntity; + (numFace2Entity as NumberFaceEntity).NumberDieEntity = (NumberDieEntity)numDieEntity; + + clrDieEntity = new ColorDieEntity() { Faces = new List() { clrFace1Entity as ColorFaceEntity, clrFace2Entity as ColorFaceEntity } }; + (clrFace1Entity as ColorFaceEntity).ColorDieEntity = (ColorDieEntity)clrDieEntity; + (clrFace2Entity as ColorFaceEntity).ColorDieEntity = (ColorDieEntity)clrDieEntity; + + imgDieEntity = new ImageDieEntity() { Faces = new List() { 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 = datetime, + PlayerEntity = playerEntity, + Dice = new List + { + numDieEntity, + clrDieEntity, + imgDieEntity + }, + Faces = new List + { + numFace1Entity, + clrFace2Entity, + imgFace2Entity + } + }; + + Turn expected = Turn.CreateWithSpecifiedTime( + datetime, + playerEntity.ToModel(), + new() + { + {(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 TestToEntity() + { + // Arrange + + Turn model = Turn.CreateWithSpecifiedTime( + datetime, + playerEntity.ToModel(), + new() + { + {(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 = datetime, + PlayerEntity = playerEntity, + Dice = new List + { + numDieEntity, + clrDieEntity, + imgDieEntity + }, + Faces = new List + { + numFace2Entity, + clrFace2Entity, + imgFace1Entity + } + }; + + // Act + TurnEntity actual = model.ToEntity(); + + // Assert + Assert.True(expected.Equals(actual)); + } } } diff --git a/Sources/Tests/Model_UTs/Games/GameTest.cs b/Sources/Tests/Model_UTs/Games/GameTest.cs index 95cb95c..d602512 100644 --- a/Sources/Tests/Model_UTs/Games/GameTest.cs +++ b/Sources/Tests/Model_UTs/Games/GameTest.cs @@ -78,7 +78,7 @@ namespace Tests.Model_UTs.Games } [Fact] - public async void TestGetHistory() + public async Task TestGetHistory() { // Arrange Dictionary diceNFaces = @@ -87,8 +87,8 @@ namespace Tests.Model_UTs.Games .GetHistory() .First().DiceNFaces; - Turn turn1 = Turn.CreateWithDefaultTime(PLAYER_1, diceNFaces); - Turn turn2 = Turn.CreateWithDefaultTime(PLAYER_2, diceNFaces); // yeah they rolled the same + 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 IEnumerable expected = new List() { turn1, turn2 };