refactor(CS): schedule + periods are config-owned, AdminPanel-editable
Some checks failed
.NET Core / build (push) Has been cancelled
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:
@@ -10,19 +10,29 @@ namespace MUnique.OpenMU.GameLogic.CastleSiege;
|
||||
/// </summary>
|
||||
public class CastleSiegeConfiguration
|
||||
{
|
||||
// --- Schedule & period lengths (editable in the AdminPanel) ---
|
||||
// A cycle runs: Ownership -> Registration -> Preparation -> Siege(war) -> Settlement -> Ownership.
|
||||
// It auto-starts when the current day/time matches RegistrationOpenDays + RegistrationOpenTimes.
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the times of day at which a new cycle opens registration.
|
||||
/// Empty by default; admins start cycles manually via chat command in P1.
|
||||
/// Gets or sets the days of week on which registration auto-opens (UTC). Empty = every day
|
||||
/// (still requires a time in <see cref="RegistrationOpenTimes"/>); with no times set, auto-start is off.
|
||||
/// </summary>
|
||||
public IList<DayOfWeek> RegistrationOpenDays { get; set; } = new List<DayOfWeek>();
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the times of day (UTC) at which a new cycle opens registration.
|
||||
/// Empty = no auto-start (admins start cycles manually via chat command).
|
||||
/// </summary>
|
||||
public IList<TimeOnly> RegistrationOpenTimes { get; set; } = new List<TimeOnly>();
|
||||
|
||||
/// <summary>Gets or sets how long the registration phase lasts.</summary>
|
||||
/// <summary>Gets or sets how long the registration period lasts (guilds may register).</summary>
|
||||
public TimeSpan RegistrationDuration { get; set; } = TimeSpan.FromMinutes(5);
|
||||
|
||||
/// <summary>Gets or sets how long the preparation phase lasts.</summary>
|
||||
/// <summary>Gets or sets how long the preparation period lasts (between registration close and the war).</summary>
|
||||
public TimeSpan PreparationDuration { get; set; } = TimeSpan.FromMinutes(2);
|
||||
|
||||
/// <summary>Gets or sets how long the siege phase lasts.</summary>
|
||||
/// <summary>Gets or sets how long the war (siege) period lasts.</summary>
|
||||
public TimeSpan SiegeDuration { get; set; } = TimeSpan.FromMinutes(10);
|
||||
|
||||
/// <summary>
|
||||
@@ -48,15 +58,10 @@ 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.
|
||||
/// Returns true if <paramref name="now"/> (UTC) matches a scheduled registration-open day and falls
|
||||
/// within a 5-second window of a configured open time. When <see cref="RegistrationOpenDays"/> is empty,
|
||||
/// the day is not restricted (every day). When no times are configured, auto-start is disabled.
|
||||
/// </summary>
|
||||
/// <param name="now">The current UTC time.</param>
|
||||
public bool IsRegistrationOpenTime(DateTime now)
|
||||
@@ -66,6 +71,11 @@ public class CastleSiegeConfiguration
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this.RegistrationOpenDays.Count > 0 && !this.RegistrationOpenDays.Contains(now.DayOfWeek))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var nowTime = TimeOnly.FromDateTime(now);
|
||||
var earlier = nowTime.Add(TimeSpan.FromSeconds(-5));
|
||||
return this.RegistrationOpenTimes.Any(p => p.IsBetween(earlier, nowTime));
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -88,12 +88,15 @@ public class CastleSiegeScheduleChatCommandPlugIn : IChatCommandPlugIn
|
||||
|
||||
private static string Describe(CastleSiegeContext context)
|
||||
{
|
||||
if (context.ScheduleTime is not { } time || context.ScheduleDays.Count == 0)
|
||||
var config = context.Configuration;
|
||||
if (config.RegistrationOpenTimes.Count == 0)
|
||||
{
|
||||
return "Castle Siege auto-schedule: (none). Set with /csschedule <days> <HH:mm UTC>.";
|
||||
}
|
||||
|
||||
return $"Castle Siege auto-schedule: {string.Join(",", context.ScheduleDays)} at {time:HH:mm} UTC.";
|
||||
var days = config.RegistrationOpenDays.Count > 0 ? string.Join(",", config.RegistrationOpenDays) : "every day";
|
||||
var times = string.Join(",", config.RegistrationOpenTimes.Select(t => t.ToString("HH\\:mm")));
|
||||
return $"Castle Siege auto-schedule: {days} at {times} UTC (reg {config.RegistrationDuration:g} -> prep {config.PreparationDuration:g} -> war {config.SiegeDuration:g}).";
|
||||
}
|
||||
|
||||
private static ValueTask ShowAsync(Player player, string text)
|
||||
|
||||
@@ -92,7 +92,7 @@ public sealed class CastleSiegeEventPlugIn : IPeriodicTaskPlugIn, ISupportCustom
|
||||
|
||||
// Restore state persisted before the last restart (owner/phase/registrations) BEFORE subscribing,
|
||||
// so restoring doesn't announce phases or re-spawn defenses.
|
||||
created.RestoreState(config.PersistedOwnerGuildName, config.PersistedPhase, config.PersistedPhaseStartedUtc, config.PersistedRegisteredGuilds, config.PersistedScheduleDays, config.PersistedScheduleTime);
|
||||
created.RestoreState(config.PersistedOwnerGuildName, config.PersistedPhase, config.PersistedPhaseStartedUtc, config.PersistedRegisteredGuilds);
|
||||
|
||||
// Announce phase changes to the whole server and, when the siege begins, warp registered members.
|
||||
created.PhaseChanged += phase => _ = OnPhaseChangedAsync(gc, created, phase);
|
||||
@@ -100,6 +100,12 @@ public sealed class CastleSiegeEventPlugIn : IPeriodicTaskPlugIn, ISupportCustom
|
||||
return created;
|
||||
});
|
||||
|
||||
// Point the context at the current (possibly AdminPanel-edited) config so schedule/durations are live.
|
||||
if (this.Configuration is { } liveConfig)
|
||||
{
|
||||
context.UpdateConfiguration(liveConfig);
|
||||
}
|
||||
|
||||
await context.TickAsync(DateTime.UtcNow).ConfigureAwait(false);
|
||||
|
||||
// During the siege, evaluate the Crown Switches (held by standing on them) and the throne capture every tick.
|
||||
@@ -177,8 +183,6 @@ public sealed class CastleSiegeEventPlugIn : IPeriodicTaskPlugIn, ISupportCustom
|
||||
config.PersistedPhase = context.Phase;
|
||||
config.PersistedPhaseStartedUtc = context.PhaseStartedUtc;
|
||||
config.PersistedRegisteredGuilds = context.RegisteredGuilds.ToList();
|
||||
config.PersistedScheduleDays = context.ScheduleDays.ToList();
|
||||
config.PersistedScheduleTime = context.ScheduleTime;
|
||||
|
||||
// Find our plugin-configuration row via the in-memory config graph to get its id.
|
||||
var pluginTypeId = typeof(CastleSiegeEventPlugIn).GUID;
|
||||
|
||||
Reference in New Issue
Block a user