Enhance school simulation and management by integrating roster functionality, allowing for the installation and persistence of student and staff data. Update the school architecture to include a roster alongside existing components, ensuring proper validation against map layouts. Revise room definitions to support homeroom designations and update related tests to validate new functionalities and ensure robustness in roster handling.
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace HSchool.People;
|
||||
|
||||
/// <summary>On-disk shape of <c>saves/{id}.people.json</c>. Composition only — needs are not live values.</summary>
|
||||
public sealed class RosterDocument
|
||||
{
|
||||
public const int CurrentFormat = 1;
|
||||
|
||||
public int Format { get; init; } = CurrentFormat;
|
||||
|
||||
public int Seed { get; init; }
|
||||
|
||||
public required IReadOnlyList<Person> People { get; init; }
|
||||
|
||||
public required IReadOnlyList<Family> Families { get; init; }
|
||||
|
||||
public required IReadOnlyList<SchoolClass> Classes { get; init; }
|
||||
|
||||
public Roster ToRoster() => new(People, Families, Classes);
|
||||
|
||||
public static RosterDocument From(int seed, Roster roster) =>
|
||||
new()
|
||||
{
|
||||
Format = CurrentFormat,
|
||||
Seed = seed,
|
||||
People = roster.People,
|
||||
Families = roster.Families,
|
||||
Classes = roster.Classes,
|
||||
};
|
||||
}
|
||||
|
||||
public static class RosterJson
|
||||
{
|
||||
public static JsonSerializerOptions Options { get; } = new()
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
PropertyNameCaseInsensitive = true,
|
||||
WriteIndented = true,
|
||||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
|
||||
};
|
||||
|
||||
public static string Serialize(RosterDocument document) =>
|
||||
JsonSerializer.Serialize(document, Options);
|
||||
|
||||
public static RosterDocument Parse(string json)
|
||||
{
|
||||
var document = JsonSerializer.Deserialize<RosterDocument>(json, Options);
|
||||
if (document is null || document.People is null || document.Families is null || document.Classes is null)
|
||||
{
|
||||
throw new InvalidOperationException("People save deserialized to nothing.");
|
||||
}
|
||||
|
||||
return document;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user