-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathModels.cs
44 lines (37 loc) · 1.26 KB
/
Models.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System.Text.Json.Serialization;
namespace JsonInheritance;
public record class Game(Guid GameId, string GameType, string PlayerName)
{
// init modifier required for JSON deserialization
[JsonInclude]
public ICollection<Move> Moves { get; init; } = new List<Move>();
public override string ToString()
{
string movesText = string.Join(":", Moves);
return $"""
{GameId}: {GameType}, {PlayerName}
{movesText}
""";
}
}
[JsonPolymorphic(TypeDiscriminatorPropertyName = "$discriminator")]
[JsonDerivedType(typeof(Move<ColorField>), typeDiscriminator: "color")]
[JsonDerivedType(typeof(Move<ShapeColorField>), typeDiscriminator: "shape")]
public abstract record class Move(Guid GameId, Guid MoveId, int MoveNumber);
public record class Move<TField>(Guid GameId, Guid MoveId, int MoveNumber)
: Move(GameId, MoveId, MoveNumber)
{
public required ICollection<TField> Fields { get; init; }
public override string ToString()
{
return string.Join(":", Fields);
}
}
public record class ColorField(string Color)
{
public override string ToString() => $"{Color}";
}
public record class ShapeColorField(string Shape, string Color)
{
public override string ToString() => $"{Shape} {Color}";
}