-
Notifications
You must be signed in to change notification settings - Fork 88
/
Models.cs
36 lines (30 loc) · 1.18 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
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; private init; } = new List<Move>();
public override string ToString() => $"""
{GameId}: {GameType}, {PlayerName}
{string.Join(':', Moves)}
""";
}
[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() => 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}";
}