refactor(CS): schedule + periods are config-owned, AdminPanel-editable
Some checks failed
.NET Core / build (push) Has been cancelled

Move the auto-schedule from context-owned state to the plugin config (single source
of truth): RegistrationOpenDays + RegistrationOpenTimes + Registration/Preparation/
SiegeDuration are all editable in the AdminPanel plugin config and take effect live
(context refreshes its config reference each tick via UpdateConfiguration). /csschedule
now writes the config. Removed the duplicate Persisted schedule fields. 12 tests pass.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Acentech Dev
2026-07-15 14:52:07 +03:00
parent f586321123
commit 4651ef232c
5 changed files with 61 additions and 69 deletions

View File

@@ -23,8 +23,6 @@ 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>
@@ -37,8 +35,13 @@ public class CastleSiegeContext
/// <summary>Raised after the phase changes. Argument is the new phase.</summary>
public event Action<CastleSiegePhase>? PhaseChanged;
/// <summary>Gets the configuration.</summary>
public CastleSiegeConfiguration Configuration { get; }
/// <summary>Gets the configuration (durations + schedule). Refreshed each tick from the live plugin config
/// so AdminPanel edits take effect without a restart.</summary>
public CastleSiegeConfiguration Configuration { get; private set; }
/// <summary>Points the context at the current (possibly AdminPanel-edited) plugin configuration.</summary>
/// <param name="configuration">The live configuration.</param>
public void UpdateConfiguration(CastleSiegeConfiguration configuration) => this.Configuration = configuration;
/// <summary>Gets the current phase.</summary>
public CastleSiegePhase Phase { get; private set; }
@@ -58,12 +61,6 @@ 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)
@@ -71,7 +68,7 @@ public class CastleSiegeContext
switch (this.Phase)
{
case CastleSiegePhase.Ownership:
if (this.ShouldOpenRegistration(now))
if (this.Configuration.IsRegistrationOpenTime(now))
{
return this.ForceStartRegistrationAsync(now);
}
@@ -159,40 +156,22 @@ public class CastleSiegeContext
}
/// <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).
/// Sets the weekly auto-schedule (days of week + UTC time) into the configuration and marks the state
/// dirty for persistence. Empty days disables auto-start (manual only). The configuration is the single
/// source of truth, so this is equivalent to editing the plugin config in the AdminPanel.
/// </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;
var orderedDays = days.Distinct().OrderBy(d => d).ToList();
this.Configuration.RegistrationOpenDays = orderedDays;
this.Configuration.RegistrationOpenTimes = orderedDays.Count > 0 && time is { } t
? new List<TimeOnly> { t }
: new List<TimeOnly>();
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 how much time is left in the running siege battle (the on-map countdown value), or
/// <see cref="TimeSpan.Zero"/> when the siege is not currently running.
@@ -228,9 +207,7 @@ 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>
/// <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)
public void RestoreState(string? owner, CastleSiegePhase phase, DateTime? phaseStartedUtc, IEnumerable<string>? registeredGuilds)
{
this.OwnerGuildName = owner;
this.Phase = phase;
@@ -241,8 +218,6 @@ 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;
}