Clear step completion states when a round finishes

This commit is contained in:
sophelper 2026-11-06 00:14:24 +08:00
parent 6397a33644
commit ca7930b9af
2 changed files with 72 additions and 1 deletions

View File

@ -117,6 +117,7 @@ public class SopEngine
State = EngineState.Running;
Log("info", $"开始运行:{_config.Sop.Name}({_ordered.Count} 步)");
ClearStepStates();
NotifyCurrent();
}
@ -130,9 +131,11 @@ public class SopEngine
_index = 0;
_stepEnteredAt = DateTime.Now;
if (StepAt(0) is StepDef first) _evaluator.NoteStepEntered(first.Id, _stepEnteredAt);
State = EngineState.Running;
Log("info", "已复位");
ClearStepStates();
NotifyCurrent();
}
@ -201,8 +204,8 @@ public class SopEngine
{
State = EngineState.Completed;
Log("info", "★ 全部步骤完成");
ClearStepStates();
Completed?.Invoke();
NotifyCurrent();
if (_config.Sop.Settings.AutoResetOnComplete)
{
@ -224,6 +227,19 @@ public class SopEngine
return index >= 0 && index < _ordered.Count ? _ordered[index] : null;
}
private void ClearStepStates()
{
foreach (var step in _ordered)
StepSnapshotChanged?.Invoke(new StepSnapshot
{
StepId = step.Id,
Name = step.Name,
IsCurrent = false,
IsMet = false,
Message = State == EngineState.Completed ? "等待下一轮" : "等待判定…",
});
}
private void NotifyCurrent()
{
var step = CurrentStep;

View File

@ -45,6 +45,7 @@ internal static class Program
var portrait = RegionCanvas.FitImage(new Size(800, 600), new Size(1000, 2000));
if (portrait != new Rect(250, 0, 300, 600)) throw new Exception("纵向图像留白计算错误");
Console.WriteLine("PASS: 原图像素换算、横竖图留白、反向画框及越界截取");
await CheckCompletionResetAsync();
var saveDirectory = Path.Combine(Path.GetTempPath(), "SopHelper-save-" + Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(saveDirectory);
@ -226,6 +227,60 @@ internal static class Program
Console.WriteLine("PASS: 区域页布局、预览与所选区域绑定");
}
private static async Task CheckCompletionResetAsync()
{
foreach (var autoReset in new[] { false, true })
{
var config = new SopConfig();
config.Sop.Settings.AutoResetOnComplete = autoReset;
config.Sop.Settings.ResetDelaySec = 1;
foreach (var id in new[] { 3, 8 })
config.Sop.Steps.Add(new StepDef
{
Id = id, Name = $"步骤{id}",
Detection = new StepCondition { Method = ConditionMethod.TimeElapsed, DurationMs = 5 },
});
var engine = new SopEngine();
var snapshots = new Dictionary<int, StepSnapshot>();
engine.StepSnapshotChanged += snapshot => snapshots[snapshot.StepId] = snapshot;
var completions = 0;
engine.Completed += () =>
{
completions++;
if (snapshots.Count != 2 || snapshots.Values.Any(s => s.IsMet || s.IsCurrent))
throw new Exception("整轮完成时未清除所有完成状态或当前步骤高亮");
};
engine.Load(config);
engine.Start();
var frame = new SopHelper.Engine.Common.FrameResult();
await Task.Delay(20);
engine.ProcessFrame(frame);
if (!snapshots[3].IsMet || engine.CurrentStep?.Id != 8)
throw new Exception("最后一步完成前应保留已完成步骤状态");
await Task.Delay(20);
engine.ProcessFrame(frame);
if (completions != 1 || engine.State != EngineState.Completed)
throw new Exception("整轮完成事件或状态不正确");
if (autoReset)
{
var deadline = DateTime.UtcNow.AddSeconds(5);
while (engine.State == EngineState.Completed && DateTime.UtcNow < deadline)
await Task.Delay(20);
}
else engine.Reset();
if (engine.State != EngineState.Running || engine.CurrentStep?.Id != 3 ||
snapshots.Values.Any(s => s.IsMet) || !snapshots[3].IsCurrent || snapshots[8].IsCurrent)
throw new Exception("复位后状态未清空或未回到第一步");
await Task.Delay(20);
engine.ProcessFrame(frame);
if (!snapshots[3].IsMet || engine.CurrentStep?.Id != 8)
throw new Exception("复位后第一步计时条件未重新开始");
engine.Start();
if (snapshots.Values.Any(s => s.IsMet)) throw new Exception("重新开始后残留完成状态");
}
Console.WriteLine("PASS: 末步完成立即清空状态,手动/自动复位与重新开始无残留,首步计时可再次完成");
}
private static void SelectMethod(RecipeEditor view, string method)
{
var combo = Descendants(view).OfType<ComboBox>().Single(c =>