feat(CS): weekly auto-schedule (day-of-week + UTC time), persisted, /csschedule GM cmd
Some checks failed
.NET Core / build (push) Has been cancelled

The siege can now auto-open registration on scheduled days/time instead of only
manual /csphase. Schedule is context-owned state (like owner), persisted across
restarts via the same config-JSON path. New GM command /csschedule sets/views/clears
it (e.g. /csschedule Sunday 20:00, UTC). +2 unit tests (11 total).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Acentech Dev
2026-07-15 12:47:29 +03:00
parent 05a10d495d
commit ac6700a356
5 changed files with 181 additions and 4 deletions

View File

@@ -48,6 +48,12 @@ public class CastleSiegeConfiguration
/// <summary>Gets or sets the persisted registered guild names for the current cycle.</summary>
public IList<string> PersistedRegisteredGuilds { get; set; } = new List<string>();
/// <summary>Gets or sets the persisted auto-schedule days of week (empty = manual start only).</summary>
public IList<DayOfWeek> PersistedScheduleDays { get; set; } = new List<DayOfWeek>();
/// <summary>Gets or sets the persisted auto-schedule UTC time of day, or null if unscheduled.</summary>
public TimeOnly? PersistedScheduleTime { get; set; }
/// <summary>
/// Returns true if <paramref name="now"/> falls within a 5-second window of any configured
/// registration-open time.

View File

@@ -23,6 +23,8 @@ public class CastleSiegeContext
private string? _occupier;
private int _defensesRemaining;
private bool _dirty;
private List<DayOfWeek> _scheduleDays = new();
private TimeOnly? _scheduleTime;
/// <summary>Initializes a new instance of the <see cref="CastleSiegeContext"/> class.</summary>
/// <param name="configuration">The cycle timing configuration.</param>
@@ -56,6 +58,12 @@ public class CastleSiegeContext
/// <summary>Gets the guild names registered for the current cycle.</summary>
public IReadOnlyList<string> RegisteredGuilds => this._registeredGuilds;
/// <summary>Gets the days of week the siege auto-opens registration (empty = manual start only).</summary>
public IReadOnlyList<DayOfWeek> ScheduleDays => this._scheduleDays;
/// <summary>Gets the UTC time of day registration auto-opens on scheduled days, or null if unscheduled.</summary>
public TimeOnly? ScheduleTime => this._scheduleTime;
/// <summary>Advances the state machine based on the current time.</summary>
/// <param name="now">The current UTC time.</param>
public ValueTask TickAsync(DateTime now)
@@ -63,7 +71,7 @@ public class CastleSiegeContext
switch (this.Phase)
{
case CastleSiegePhase.Ownership:
if (this.Configuration.IsRegistrationOpenTime(now))
if (this.ShouldOpenRegistration(now))
{
return this.ForceStartRegistrationAsync(now);
}
@@ -150,6 +158,41 @@ public class CastleSiegeContext
this._dirty = true;
}
/// <summary>
/// Sets the weekly auto-schedule (days of week + UTC time) and marks the state dirty for persistence.
/// Empty days disables auto-start (manual only).
/// </summary>
/// <param name="days">The days of week registration should auto-open.</param>
/// <param name="time">The UTC time of day registration should auto-open, or null to disable.</param>
public void SetSchedule(IEnumerable<DayOfWeek> days, TimeOnly? time)
{
this._scheduleDays = days.Distinct().OrderBy(d => d).ToList();
this._scheduleTime = this._scheduleDays.Count > 0 ? time : null;
this._dirty = true;
}
/// <summary>
/// Returns whether registration should auto-open now: a scheduled day matches and the current UTC time
/// falls within a 5-second window at/after the scheduled time. Only meaningful in the ownership phase.
/// </summary>
/// <param name="nowUtc">The current UTC time.</param>
public bool ShouldOpenRegistration(DateTime nowUtc)
{
if (this._scheduleTime is not { } scheduleTime || this._scheduleDays.Count == 0)
{
return false;
}
if (!this._scheduleDays.Contains(nowUtc.DayOfWeek))
{
return false;
}
var nowTime = TimeOnly.FromDateTime(nowUtc);
var windowEnd = scheduleTime.Add(TimeSpan.FromSeconds(5));
return scheduleTime <= nowTime && nowTime <= windowEnd;
}
/// <summary>
/// Returns whether the persistable state changed since the last call, resetting the flag.
/// Called each tick by the plugin to decide whether to write state to the database.
@@ -169,7 +212,9 @@ public class CastleSiegeContext
/// <param name="phase">The persisted phase.</param>
/// <param name="phaseStartedUtc">When the persisted phase started (UTC), or null to keep the default.</param>
/// <param name="registeredGuilds">The persisted registered guild names, or null.</param>
public void RestoreState(string? owner, CastleSiegePhase phase, DateTime? phaseStartedUtc, IEnumerable<string>? registeredGuilds)
/// <param name="scheduleDays">The persisted auto-schedule days of week, or null.</param>
/// <param name="scheduleTime">The persisted auto-schedule UTC time, or null.</param>
public void RestoreState(string? owner, CastleSiegePhase phase, DateTime? phaseStartedUtc, IEnumerable<string>? registeredGuilds, IEnumerable<DayOfWeek>? scheduleDays, TimeOnly? scheduleTime)
{
this.OwnerGuildName = owner;
this.Phase = phase;
@@ -180,6 +225,8 @@ public class CastleSiegeContext
this._registeredGuilds.AddRange(registeredGuilds);
}
this._scheduleDays = scheduleDays?.Distinct().OrderBy(d => d).ToList() ?? new List<DayOfWeek>();
this._scheduleTime = this._scheduleDays.Count > 0 ? scheduleTime : null;
this._dirty = false;
}