- Bumped the wire protocol version to 6, reflecting changes in the communication structure.
- Expanded the timetable API with new endpoints for fetching and managing lesson schedules, including `GET /api/schools/{id}/timetable` and `POST /api/schools/{id}/timetable/pin`.
- Updated the protocol documentation to include detailed descriptions of the new timetable features and message structures.
- Enhanced the client-side implementation to support the new timetable functionalities, including lesson pinning and unpinning.
- Revised server-side logic to handle timetable operations and ensure proper integration with existing school management features.
- Added tests to validate the new timetable functionalities and ensure robustness in handling lesson data.
100 lines
3.9 KiB
C#
100 lines
3.9 KiB
C#
using System.Net.WebSockets;
|
|
using HSchool.Server.Api;
|
|
using HSchool.Server.Game;
|
|
using HSchool.Server.Net;
|
|
using HSchool.Simulation;
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
builder.AddServiceDefaults();
|
|
builder.Services.AddProblemDetails();
|
|
builder.Services.AddOpenApi();
|
|
|
|
builder.Services
|
|
.AddOptions<SimulationOptions>()
|
|
.Bind(builder.Configuration.GetSection(SimulationOptions.SectionName))
|
|
.Validate(options => options.TickRate is > 0 and <= 120, "Simulation:TickRate must be between 1 and 120.")
|
|
.Validate(options => options.MaxSchools is > 0 and <= 255, "Simulation:MaxSchools must be between 1 and 255.")
|
|
.Validate(options => options.GameMinutesPerRealSecond > 0, "Simulation:GameMinutesPerRealSecond must be positive.")
|
|
.Validate(options => GameClock.IsValidStartDate(options.DefaultStartDate), "Simulation:DefaultStartDate is out of range.")
|
|
.Validate(options => !string.IsNullOrWhiteSpace(options.SavesDirectory), "Simulation:SavesDirectory must be set.")
|
|
.Validate(options => !string.IsNullOrWhiteSpace(options.ModsDirectory), "Simulation:ModsDirectory must be set.")
|
|
.Validate(options => options.SaveIntervalSeconds is > 0 and <= 3600, "Simulation:SaveIntervalSeconds must be between 1 and 3600.")
|
|
.Validate(options => options.MonthlyPayrollCap > 0, "Simulation:MonthlyPayrollCap must be positive.")
|
|
.Validate(options => options.SchoolWeekDays is >= 5 and <= 7, "Simulation:SchoolWeekDays must be between 5 and 7.")
|
|
.ValidateOnStart();
|
|
|
|
builder.Services.AddSingleton<GameCommandQueue>();
|
|
builder.Services.AddSingleton<ClientRegistry>();
|
|
builder.Services.AddSingleton<GameMetrics>();
|
|
builder.Services.AddSingleton<SchoolStore>();
|
|
builder.Services.AddSingleton<ModContent>();
|
|
builder.Services.AddSingleton<GameSocketHandler>();
|
|
builder.Services.AddSingleton<GameLoopService>();
|
|
builder.Services.AddHostedService(sp => sp.GetRequiredService<GameLoopService>());
|
|
|
|
builder.Services.AddOpenTelemetry().WithMetrics(metrics => metrics.AddMeter(GameMetrics.MeterName));
|
|
|
|
var app = builder.Build();
|
|
|
|
app.UseExceptionHandler();
|
|
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.MapOpenApi();
|
|
}
|
|
|
|
app.UseWebSockets(new WebSocketOptions
|
|
{
|
|
KeepAliveInterval = TimeSpan.FromSeconds(30),
|
|
});
|
|
|
|
app.MapSchoolEndpoints();
|
|
app.MapTimetableEndpoints();
|
|
app.MapModEndpoints();
|
|
|
|
app.MapGet("/api/status", (GameLoopService loop, ClientRegistry clients) =>
|
|
{
|
|
var state = loop.SchoolsState;
|
|
return new GameStatusResponse(loop.CurrentTick, loop.Options.TickRate, state.Schools.Count, state.MaxSchools, clients.Count);
|
|
})
|
|
.WithName("GetGameStatus");
|
|
|
|
if (app.Configuration.GetValue("HSchool:AllowSaveReload", false))
|
|
{
|
|
app.MapPost("/api/dev/reload-schools", async (GameLoopService loop, CancellationToken cancellationToken) =>
|
|
{
|
|
await loop.ReloadFromDiskAsync(cancellationToken);
|
|
return Results.NoContent();
|
|
})
|
|
.WithName("ReloadSchoolsFromDisk");
|
|
}
|
|
|
|
// The realtime channel: one binary frame per protocol message, see docs/protocol.md.
|
|
app.Map("/ws/game", async (HttpContext context, GameSocketHandler handler) =>
|
|
{
|
|
if (!context.WebSockets.IsWebSocketRequest)
|
|
{
|
|
context.Response.StatusCode = StatusCodes.Status400BadRequest;
|
|
await context.Response.WriteAsync("This endpoint expects a WebSocket upgrade.");
|
|
return;
|
|
}
|
|
|
|
using WebSocket socket = await context.WebSockets.AcceptWebSocketAsync();
|
|
await handler.HandleAsync(socket, context.RequestAborted);
|
|
});
|
|
|
|
app.MapDefaultEndpoints();
|
|
|
|
// In a published container the built client lands in wwwroot next to the server.
|
|
app.UseFileServer();
|
|
|
|
app.Run();
|
|
|
|
/// <summary>Loop health for dashboards and integration tests.</summary>
|
|
internal sealed record GameStatusResponse(uint Tick, int TickRate, int Schools, int MaxSchools, int Connections);
|
|
|
|
/// <summary>Exposed so <c>WebApplicationFactory</c>-style tests can reference the entry point.</summary>
|
|
public partial class Program;
|