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:
@@ -60,6 +60,12 @@ public sealed class RoomDef : Def
|
||||
public IReadOnlyList<string> Positions { get; init; } = [];
|
||||
|
||||
public IReadOnlyList<string> Works { get; init; } = [];
|
||||
|
||||
/// <summary>
|
||||
/// When true, a map room of this def with pupil slots becomes a roster class.
|
||||
/// Labs keep <see cref="ThingDef.PupilSlots"/> for lessons without forming a homeroom.
|
||||
/// </summary>
|
||||
public bool Homeroom { get; init; }
|
||||
}
|
||||
|
||||
public sealed class BuildingDef : Def;
|
||||
|
||||
@@ -151,7 +151,7 @@ public static class MapView
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Same rule as floors: <see cref="RoomNode.Label"/> is "1A", not a replacement for "Classroom".
|
||||
/// Same rule as floors: <see cref="RoomNode.Label"/> is "101", not a replacement for "Classroom".
|
||||
/// Do not treat "label equals defName" as "untranslated" — English classrooms are named Classroom.
|
||||
/// </summary>
|
||||
private static string RoomName(DefCatalog catalog, string locale, RoomNode room)
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
namespace HSchool.People;
|
||||
|
||||
/// <summary>
|
||||
/// Whether a generated or loaded roster still fills this map. A mismatch means the file is stale
|
||||
/// relative to the layout — the school must not start and the file must stay untouched.
|
||||
/// </summary>
|
||||
public static class RosterFit
|
||||
{
|
||||
public static bool Matches(Roster roster, SchoolDemand demand)
|
||||
{
|
||||
if (roster.Classes.Count != demand.Classes.Count || roster.People.Count(person => person.IsStudent) != demand.Seats.Count)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var classesByRoom = roster.Classes.ToDictionary(schoolClass => schoolClass.RoomId, StringComparer.Ordinal);
|
||||
foreach (var expected in demand.Classes)
|
||||
{
|
||||
if (!classesByRoom.TryGetValue(expected.RoomId, out var actual)
|
||||
|| actual.Capacity != expected.Capacity
|
||||
|| actual.PupilIds.Count != expected.Capacity)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
var staffed = roster.People
|
||||
.Where(person => person.IsStaff && person.Position is not null && person.WorkplaceRoomId is not null)
|
||||
.Select(person => new StaffOpening(person.WorkplaceRoomId!, person.Position!))
|
||||
.ToHashSet();
|
||||
|
||||
if (staffed.Count != demand.Staff.Count)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (var opening in demand.Staff)
|
||||
{
|
||||
if (!staffed.Contains(opening))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -5,8 +5,8 @@ public readonly record struct StaffOpening(string RoomId, string Position);
|
||||
public readonly record struct PupilSeat(string ClassId, string RoomId, int Year, string Letter);
|
||||
|
||||
/// <summary>
|
||||
/// How many pupils and staff a map asks for. Classrooms are rooms with pupil slots; each one
|
||||
/// becomes a roster class. Positions come from <see cref="RoomDef.Positions"/>, not the wire labels.
|
||||
/// How many pupils and staff a map asks for. Homeroom rooms with pupil slots become roster
|
||||
/// classes; every <see cref="RoomDef.Positions"/> entry is a staff opening.
|
||||
/// </summary>
|
||||
public sealed class SchoolDemand
|
||||
{
|
||||
@@ -34,17 +34,17 @@ public sealed class SchoolDemand
|
||||
foreach (var room in map.Rooms)
|
||||
{
|
||||
var slots = PupilSlotsOf(catalog, room);
|
||||
if (slots > 0)
|
||||
{
|
||||
classrooms.Add((room, slots));
|
||||
}
|
||||
|
||||
if (catalog.Rooms.TryGetValue(room.Def, out var def))
|
||||
{
|
||||
foreach (var position in def.Positions)
|
||||
{
|
||||
staff.Add(new StaffOpening(room.Id, position));
|
||||
}
|
||||
|
||||
if (slots > 0 && def.Homeroom)
|
||||
{
|
||||
classrooms.Add((room, slots));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System.Text.Json;
|
||||
using HSchool.Content;
|
||||
using HSchool.People;
|
||||
using HSchool.Simulation;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
@@ -101,7 +102,9 @@ internal sealed class SchoolStore
|
||||
|
||||
foreach (var path in Directory.EnumerateFiles(DirectoryPath, "*.json"))
|
||||
{
|
||||
if (string.Equals(Path.GetFileName(path), IndexFileName, StringComparison.OrdinalIgnoreCase))
|
||||
var fileName = Path.GetFileName(path);
|
||||
if (string.Equals(fileName, IndexFileName, StringComparison.OrdinalIgnoreCase)
|
||||
|| fileName.EndsWith(".people.json", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
@@ -179,15 +182,48 @@ internal sealed class SchoolStore
|
||||
{
|
||||
File.Delete(path);
|
||||
}
|
||||
|
||||
var people = PeoplePath(id);
|
||||
if (File.Exists(people))
|
||||
{
|
||||
File.Delete(people);
|
||||
}
|
||||
}
|
||||
|
||||
public RosterDocument? TryReadPeople(int id)
|
||||
{
|
||||
var path = PeoplePath(id);
|
||||
if (!File.Exists(path))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return RosterJson.Parse(File.ReadAllText(path));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new SchoolContentUnavailableException(
|
||||
$"School {id} people file could not be read.",
|
||||
ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void SavePeople(int id, RosterDocument document)
|
||||
{
|
||||
WriteAtomic(PeoplePath(id), document, RosterJson.Options);
|
||||
}
|
||||
|
||||
private string SchoolPath(int id) => Path.Combine(DirectoryPath, $"{id}.json");
|
||||
|
||||
private string PeoplePath(int id) => Path.Combine(DirectoryPath, $"{id}.people.json");
|
||||
|
||||
private string IndexPath() => Path.Combine(DirectoryPath, IndexFileName);
|
||||
|
||||
private static void WriteAtomic<T>(string path, T value)
|
||||
private static void WriteAtomic<T>(string path, T value, JsonSerializerOptions? options = null)
|
||||
{
|
||||
var json = JsonSerializer.Serialize(value, Json);
|
||||
var json = JsonSerializer.Serialize(value, options ?? Json);
|
||||
var temp = path + ".tmp";
|
||||
File.WriteAllText(temp, json);
|
||||
File.Move(temp, path, overwrite: true);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System.Diagnostics;
|
||||
using System.Threading.Channels;
|
||||
using HSchool.Content;
|
||||
using HSchool.People;
|
||||
using HSchool.Protocol;
|
||||
using HSchool.Server.Net;
|
||||
using HSchool.Simulation;
|
||||
@@ -39,6 +40,7 @@ internal sealed class SchoolWorker
|
||||
private readonly int _speedIndex;
|
||||
|
||||
private SchoolState _snapshot;
|
||||
private Roster? _rosterSnapshot;
|
||||
private School? _school;
|
||||
private Task? _run;
|
||||
private bool _persistOnStop = true;
|
||||
@@ -89,6 +91,9 @@ internal sealed class SchoolWorker
|
||||
/// <summary>Last clock the worker published. Menu requests read this; the live school stays here.</summary>
|
||||
public SchoolState Snapshot => Volatile.Read(ref _snapshot);
|
||||
|
||||
/// <summary>Last roster composition. Published like <see cref="Snapshot"/>; needs live on entities.</summary>
|
||||
public Roster? RosterSnapshot => Volatile.Read(ref _rosterSnapshot);
|
||||
|
||||
public void Start()
|
||||
{
|
||||
_run = Task.Factory.StartNew(
|
||||
@@ -194,6 +199,17 @@ internal sealed class SchoolWorker
|
||||
? School.Create(_id, _name, _time, catalog, map)
|
||||
: School.Load(_id, _name, _time, _running, _speedIndex, catalog, map);
|
||||
|
||||
var peopleDirty = false;
|
||||
try
|
||||
{
|
||||
peopleDirty = InstallPeople(school, catalog, map);
|
||||
}
|
||||
catch
|
||||
{
|
||||
school.Dispose();
|
||||
throw;
|
||||
}
|
||||
|
||||
_school = school;
|
||||
PublishSnapshot();
|
||||
|
||||
@@ -202,6 +218,11 @@ internal sealed class SchoolWorker
|
||||
Persist();
|
||||
}
|
||||
|
||||
if (peopleDirty)
|
||||
{
|
||||
PersistPeople();
|
||||
}
|
||||
|
||||
_started.TrySetResult();
|
||||
|
||||
using var timer = new PeriodicTimer(_options.TickInterval);
|
||||
@@ -391,6 +412,94 @@ internal sealed class SchoolWorker
|
||||
school.Clock.Time,
|
||||
school.Clock.IsRunning,
|
||||
(byte)school.Clock.SpeedIndex));
|
||||
Volatile.Write(ref _rosterSnapshot, school.Roster);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the composition file. Not called from the 30-second clock save — the roster changes
|
||||
/// on create, load-migration and (later) yearly intake, not every tick.
|
||||
/// </summary>
|
||||
private void PersistPeople()
|
||||
{
|
||||
var school = _school;
|
||||
if (school?.Roster is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
_store.SavePeople(school.Id, RosterDocument.From(school.PeopleSeed, school.Roster));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Could not save people for school {SchoolId}; composition stays in memory.", _id);
|
||||
}
|
||||
}
|
||||
|
||||
private bool InstallPeople(School school, DefCatalog catalog, MapLayout map)
|
||||
{
|
||||
var nameSetId = ResolveNameSetId(catalog, _nameSetId);
|
||||
if (nameSetId is null)
|
||||
{
|
||||
throw new SchoolContentUnavailableException($"School {_id} has no name set in its catalog.");
|
||||
}
|
||||
|
||||
var demand = SchoolDemand.From(catalog, map);
|
||||
Roster roster;
|
||||
int seed;
|
||||
var generated = false;
|
||||
|
||||
if (_isNew)
|
||||
{
|
||||
seed = school.Id;
|
||||
roster = RosterGenerator.Generate(catalog, map, seed, nameSetId, school.Clock.Time);
|
||||
generated = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
var loaded = _store.TryReadPeople(_id);
|
||||
if (loaded is null)
|
||||
{
|
||||
seed = school.Id;
|
||||
roster = RosterGenerator.Generate(catalog, map, seed, nameSetId, school.Clock.Time);
|
||||
generated = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
seed = loaded.Seed;
|
||||
roster = loaded.ToRoster();
|
||||
}
|
||||
}
|
||||
|
||||
if (!RosterFit.Matches(roster, demand))
|
||||
{
|
||||
throw new SchoolContentUnavailableException(
|
||||
$"School {_id} roster does not match its map; the people file was left untouched.");
|
||||
}
|
||||
|
||||
school.InstallPeople(roster, seed);
|
||||
return generated;
|
||||
}
|
||||
|
||||
private static string? ResolveNameSetId(DefCatalog catalog, string? requested)
|
||||
{
|
||||
var available = catalog.NameSets.Values
|
||||
.Where(def => !def.Abstract)
|
||||
.Select(def => def.DefName)
|
||||
.OrderBy(name => name, StringComparer.Ordinal)
|
||||
.ToArray();
|
||||
if (available.Length == 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(requested))
|
||||
{
|
||||
return available[0];
|
||||
}
|
||||
|
||||
return available.Contains(requested, StringComparer.Ordinal) ? requested : null;
|
||||
}
|
||||
|
||||
private void Persist()
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
<ProjectReference Include="..\HSchool.Protocol\HSchool.Protocol.csproj" />
|
||||
<ProjectReference Include="..\HSchool.ServiceDefaults\HSchool.ServiceDefaults.csproj" />
|
||||
<ProjectReference Include="..\HSchool.Simulation\HSchool.Simulation.csproj" />
|
||||
<ProjectReference Include="..\HSchool.People\HSchool.People.csproj" />
|
||||
<ProjectReference Include="..\HSchool.Content\HSchool.Content.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
],
|
||||
"positions": ["Teacher"],
|
||||
"works": ["TeachLesson"],
|
||||
"homeroom": true,
|
||||
},
|
||||
{
|
||||
"defName": "Library",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
// A small two-storey school: main building on the yard, gym across the yard.
|
||||
// Walkable graph is yard + rooms; floors are grouping only. Stairs link the two corridors.
|
||||
// Two-storey school plus a gym. Eleven homerooms, labelled as room numbers — class names
|
||||
// live on the roster and change every 1 September.
|
||||
"territory": { "id": "yard", "def": "SchoolYard" },
|
||||
"buildings": [
|
||||
{ "id": "main", "def": "MainBuilding" },
|
||||
@@ -47,11 +47,11 @@
|
||||
],
|
||||
},
|
||||
{
|
||||
"id": "classroom-1a",
|
||||
"id": "classroom-101",
|
||||
"def": "Classroom",
|
||||
"building": "main",
|
||||
"floor": "floor-1",
|
||||
"label": "1A",
|
||||
"label": "101",
|
||||
"slots": [
|
||||
{ "key": "board", "thing": "Blackboard" },
|
||||
{ "key": "teacherDesk", "thing": "Desk" },
|
||||
@@ -60,11 +60,37 @@
|
||||
],
|
||||
},
|
||||
{
|
||||
"id": "classroom-1b",
|
||||
"id": "classroom-102",
|
||||
"def": "Classroom",
|
||||
"building": "main",
|
||||
"floor": "floor-1",
|
||||
"label": "1B",
|
||||
"label": "102",
|
||||
"slots": [
|
||||
{ "key": "board", "thing": "Blackboard" },
|
||||
{ "key": "teacherDesk", "thing": "Desk" },
|
||||
{ "key": "teacherChair", "thing": "Chair" },
|
||||
{ "key": "studentDesks", "thing": "StudentDesk", "count": 16 },
|
||||
],
|
||||
},
|
||||
{
|
||||
"id": "classroom-103",
|
||||
"def": "Classroom",
|
||||
"building": "main",
|
||||
"floor": "floor-1",
|
||||
"label": "103",
|
||||
"slots": [
|
||||
{ "key": "board", "thing": "Blackboard" },
|
||||
{ "key": "teacherDesk", "thing": "Desk" },
|
||||
{ "key": "teacherChair", "thing": "Chair" },
|
||||
{ "key": "studentDesks", "thing": "StudentDesk", "count": 16 },
|
||||
],
|
||||
},
|
||||
{
|
||||
"id": "classroom-104",
|
||||
"def": "Classroom",
|
||||
"building": "main",
|
||||
"floor": "floor-1",
|
||||
"label": "104",
|
||||
"slots": [
|
||||
{ "key": "board", "thing": "Blackboard" },
|
||||
{ "key": "teacherDesk", "thing": "Desk" },
|
||||
@@ -96,11 +122,11 @@
|
||||
{ "id": "corridor-2", "def": "Corridor", "building": "main", "floor": "floor-2", "label": "2" },
|
||||
{ "id": "stairs-2", "def": "Stairwell", "building": "main", "floor": "floor-2", "label": "2" },
|
||||
{
|
||||
"id": "classroom-2a",
|
||||
"id": "classroom-201",
|
||||
"def": "Classroom",
|
||||
"building": "main",
|
||||
"floor": "floor-2",
|
||||
"label": "2A",
|
||||
"label": "201",
|
||||
"slots": [
|
||||
{ "key": "board", "thing": "Blackboard" },
|
||||
{ "key": "teacherDesk", "thing": "Desk" },
|
||||
@@ -109,11 +135,76 @@
|
||||
],
|
||||
},
|
||||
{
|
||||
"id": "classroom-2b",
|
||||
"id": "classroom-202",
|
||||
"def": "Classroom",
|
||||
"building": "main",
|
||||
"floor": "floor-2",
|
||||
"label": "2B",
|
||||
"label": "202",
|
||||
"slots": [
|
||||
{ "key": "board", "thing": "Blackboard" },
|
||||
{ "key": "teacherDesk", "thing": "Desk" },
|
||||
{ "key": "teacherChair", "thing": "Chair" },
|
||||
{ "key": "studentDesks", "thing": "StudentDesk", "count": 16 },
|
||||
],
|
||||
},
|
||||
{
|
||||
"id": "classroom-203",
|
||||
"def": "Classroom",
|
||||
"building": "main",
|
||||
"floor": "floor-2",
|
||||
"label": "203",
|
||||
"slots": [
|
||||
{ "key": "board", "thing": "Blackboard" },
|
||||
{ "key": "teacherDesk", "thing": "Desk" },
|
||||
{ "key": "teacherChair", "thing": "Chair" },
|
||||
{ "key": "studentDesks", "thing": "StudentDesk", "count": 16 },
|
||||
],
|
||||
},
|
||||
{
|
||||
"id": "classroom-204",
|
||||
"def": "Classroom",
|
||||
"building": "main",
|
||||
"floor": "floor-2",
|
||||
"label": "204",
|
||||
"slots": [
|
||||
{ "key": "board", "thing": "Blackboard" },
|
||||
{ "key": "teacherDesk", "thing": "Desk" },
|
||||
{ "key": "teacherChair", "thing": "Chair" },
|
||||
{ "key": "studentDesks", "thing": "StudentDesk", "count": 16 },
|
||||
],
|
||||
},
|
||||
{
|
||||
"id": "classroom-205",
|
||||
"def": "Classroom",
|
||||
"building": "main",
|
||||
"floor": "floor-2",
|
||||
"label": "205",
|
||||
"slots": [
|
||||
{ "key": "board", "thing": "Blackboard" },
|
||||
{ "key": "teacherDesk", "thing": "Desk" },
|
||||
{ "key": "teacherChair", "thing": "Chair" },
|
||||
{ "key": "studentDesks", "thing": "StudentDesk", "count": 16 },
|
||||
],
|
||||
},
|
||||
{
|
||||
"id": "classroom-206",
|
||||
"def": "Classroom",
|
||||
"building": "main",
|
||||
"floor": "floor-2",
|
||||
"label": "206",
|
||||
"slots": [
|
||||
{ "key": "board", "thing": "Blackboard" },
|
||||
{ "key": "teacherDesk", "thing": "Desk" },
|
||||
{ "key": "teacherChair", "thing": "Chair" },
|
||||
{ "key": "studentDesks", "thing": "StudentDesk", "count": 16 },
|
||||
],
|
||||
},
|
||||
{
|
||||
"id": "classroom-207",
|
||||
"def": "Classroom",
|
||||
"building": "main",
|
||||
"floor": "floor-2",
|
||||
"label": "207",
|
||||
"slots": [
|
||||
{ "key": "board", "thing": "Blackboard" },
|
||||
{ "key": "teacherDesk", "thing": "Desk" },
|
||||
@@ -169,15 +260,22 @@
|
||||
{ "a": "corridor-1", "b": "principals-office" },
|
||||
{ "a": "corridor-1", "b": "secretary-office" },
|
||||
{ "a": "corridor-1", "b": "teachers-room" },
|
||||
{ "a": "corridor-1", "b": "classroom-1a" },
|
||||
{ "a": "corridor-1", "b": "classroom-1b" },
|
||||
{ "a": "corridor-1", "b": "classroom-101" },
|
||||
{ "a": "corridor-1", "b": "classroom-102" },
|
||||
{ "a": "corridor-1", "b": "classroom-103" },
|
||||
{ "a": "corridor-1", "b": "classroom-104" },
|
||||
{ "a": "corridor-1", "b": "cafeteria" },
|
||||
{ "a": "corridor-1", "b": "restroom-1" },
|
||||
{ "a": "corridor-1", "b": "medical-office" },
|
||||
{ "a": "stairs-1", "b": "stairs-2" },
|
||||
{ "a": "stairs-2", "b": "corridor-2" },
|
||||
{ "a": "corridor-2", "b": "classroom-2a" },
|
||||
{ "a": "corridor-2", "b": "classroom-2b" },
|
||||
{ "a": "corridor-2", "b": "classroom-201" },
|
||||
{ "a": "corridor-2", "b": "classroom-202" },
|
||||
{ "a": "corridor-2", "b": "classroom-203" },
|
||||
{ "a": "corridor-2", "b": "classroom-204" },
|
||||
{ "a": "corridor-2", "b": "classroom-205" },
|
||||
{ "a": "corridor-2", "b": "classroom-206" },
|
||||
{ "a": "corridor-2", "b": "classroom-207" },
|
||||
{ "a": "corridor-2", "b": "library" },
|
||||
{ "a": "corridor-2", "b": "computer-lab" },
|
||||
{ "a": "corridor-2", "b": "restroom-2" },
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
using HSchool.People;
|
||||
|
||||
namespace HSchool.Simulation;
|
||||
|
||||
/// <summary>Identity, the one layer every person has.</summary>
|
||||
public readonly record struct PersonIdentity(
|
||||
string Id,
|
||||
string FamilyId,
|
||||
bool Female,
|
||||
DateTime BirthDate,
|
||||
PersonName Name);
|
||||
|
||||
/// <summary>Height, weight and categorical body attributes including derived <c>Build</c>.</summary>
|
||||
public readonly record struct PersonBody(
|
||||
IReadOnlyDictionary<string, int> Numbers,
|
||||
IReadOnlyDictionary<string, string> Choices);
|
||||
|
||||
public readonly record struct PersonSkills(IReadOnlyDictionary<string, int> Values);
|
||||
|
||||
public readonly record struct PersonTraits(IReadOnlyList<string> Ids);
|
||||
|
||||
/// <summary>Live need values. The dictionary is mutated in place as the clock advances.</summary>
|
||||
public readonly record struct PersonNeeds(Dictionary<string, float> Values);
|
||||
|
||||
public readonly record struct PersonRoles(
|
||||
bool IsStudent,
|
||||
bool IsStaff,
|
||||
bool IsParent,
|
||||
string? ClassId,
|
||||
string? Position,
|
||||
string? WorkplaceRoomId);
|
||||
|
||||
/// <summary>A roster class: year and letter live here, the room is the homeroom they occupy.</summary>
|
||||
public readonly record struct ClassIdentity(
|
||||
string Id,
|
||||
int Year,
|
||||
string Letter,
|
||||
string RoomId,
|
||||
int Capacity);
|
||||
@@ -54,14 +54,16 @@ public sealed class GameClock
|
||||
/// Advances the calendar by one fixed step of <paramref name="realSeconds"/>, scaled by the
|
||||
/// base rate and the current speed. Does nothing while paused.
|
||||
/// </summary>
|
||||
public void Advance(double realSeconds, double gameMinutesPerRealSecond)
|
||||
/// <returns>Game minutes actually added; zero while paused.</returns>
|
||||
public double Advance(double realSeconds, double gameMinutesPerRealSecond)
|
||||
{
|
||||
if (!IsRunning)
|
||||
{
|
||||
return;
|
||||
return 0;
|
||||
}
|
||||
|
||||
var gameMinutes = realSeconds * gameMinutesPerRealSecond * Multiplier;
|
||||
Time = Time.AddMinutes(gameMinutes);
|
||||
return gameMinutes;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,12 +5,13 @@
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Arch" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Arch" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\HSchool.Content\HSchool.Content.csproj" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\HSchool.Content\HSchool.Content.csproj" />
|
||||
<ProjectReference Include="..\HSchool.People\HSchool.People.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
using Arch.Core;
|
||||
using HSchool.Content;
|
||||
|
||||
namespace HSchool.Simulation;
|
||||
|
||||
/// <summary>
|
||||
/// Drains needs by <see cref="NeedDef.DecayPerHour"/> × simulated hours. Core ships decay at zero
|
||||
/// so this is a no-op until something exists that can refill them.
|
||||
/// </summary>
|
||||
public static class NeedDecay
|
||||
{
|
||||
private static readonly QueryDescription PeopleWithNeeds = new QueryDescription().WithAll<PersonNeeds>();
|
||||
|
||||
public static void Apply(World world, DefCatalog catalog, double gameMinutes)
|
||||
{
|
||||
if (gameMinutes <= 0 || catalog.Needs.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var hours = gameMinutes / 60d;
|
||||
world.Query(in PeopleWithNeeds, (ref PersonNeeds needs) =>
|
||||
{
|
||||
foreach (var def in catalog.Needs.Values)
|
||||
{
|
||||
if (def.Abstract || !needs.Values.TryGetValue(def.DefName, out var current))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var next = current - (float)(def.DecayPerHour * hours);
|
||||
needs.Values[def.DefName] = Math.Clamp(next, def.Min, def.Max);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
using Arch.Core;
|
||||
using HSchool.People;
|
||||
|
||||
namespace HSchool.Simulation;
|
||||
|
||||
/// <summary>Turns roster records into Arch entities. Parents have no map location — by design.</summary>
|
||||
public static class RosterSpawner
|
||||
{
|
||||
public static void Spawn(World world, Roster roster)
|
||||
{
|
||||
foreach (var schoolClass in roster.Classes)
|
||||
{
|
||||
world.Create(
|
||||
new ClassIdentity(
|
||||
schoolClass.Id,
|
||||
schoolClass.Year,
|
||||
schoolClass.Letter,
|
||||
schoolClass.RoomId,
|
||||
schoolClass.Capacity));
|
||||
}
|
||||
|
||||
foreach (var person in roster.People)
|
||||
{
|
||||
world.Create(
|
||||
new PersonIdentity(person.Id, person.FamilyId, person.Female, person.BirthDate, person.Name),
|
||||
new PersonBody(person.Numbers, person.Choices),
|
||||
new PersonSkills(person.Skills),
|
||||
new PersonTraits(person.Traits),
|
||||
new PersonNeeds(new Dictionary<string, float>(person.Needs, StringComparer.Ordinal)),
|
||||
new PersonRoles(
|
||||
person.IsStudent,
|
||||
person.IsStaff,
|
||||
person.IsParent,
|
||||
person.ClassId,
|
||||
person.Position,
|
||||
person.WorkplaceRoomId));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,12 @@
|
||||
using Arch.Core;
|
||||
using HSchool.Content;
|
||||
using HSchool.People;
|
||||
|
||||
namespace HSchool.Simulation;
|
||||
|
||||
/// <summary>
|
||||
/// One save: a name, a calendar, a frozen def catalog, a map instance, and the ECS world that will
|
||||
/// hold everything the school is made of. The world is empty for now — pupils, rooms and staff
|
||||
/// land in it as the game grows — but it is created and destroyed with the school so ownership is
|
||||
/// never in question.
|
||||
/// One save: a name, a calendar, a frozen def catalog, a map instance, the ECS world, and — once
|
||||
/// people exist — the roster those entities were built from.
|
||||
/// </summary>
|
||||
public sealed class School : IDisposable
|
||||
{
|
||||
@@ -66,12 +65,34 @@ public sealed class School : IDisposable
|
||||
/// <summary>The Arch world backing this school. Only this school's worker thread may touch it.</summary>
|
||||
public World World { get; }
|
||||
|
||||
/// <summary>Runs one fixed step of the school. Today that is only the calendar.</summary>
|
||||
/// <summary>Composition snapshot. Null in clock-only tests or before <see cref="InstallPeople"/>.</summary>
|
||||
public Roster? Roster { get; private set; }
|
||||
|
||||
public int PeopleSeed { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Installs a roster that already matches the map. Spawns entities; does not write to disk.
|
||||
/// </summary>
|
||||
public void InstallPeople(Roster roster, int seed)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
ArgumentNullException.ThrowIfNull(roster);
|
||||
|
||||
Roster = roster;
|
||||
PeopleSeed = seed;
|
||||
RosterSpawner.Spawn(World, roster);
|
||||
}
|
||||
|
||||
/// <summary>Runs one fixed step of the school: calendar, then need decay.</summary>
|
||||
public void Tick(double deltaTime, double gameMinutesPerRealSecond)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
|
||||
Clock.Advance(deltaTime, gameMinutesPerRealSecond);
|
||||
var gameMinutes = Clock.Advance(deltaTime, gameMinutesPerRealSecond);
|
||||
if (gameMinutes > 0 && Catalog is not null)
|
||||
{
|
||||
NeedDecay.Apply(World, Catalog, gameMinutes);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
|
||||
Reference in New Issue
Block a user