diff --git a/src/Titano.Core/Project/ProjectSettings.cs b/src/Titano.Core/Project/ProjectSettings.cs index 2c9d274f..6e5402e2 100644 --- a/src/Titano.Core/Project/ProjectSettings.cs +++ b/src/Titano.Core/Project/ProjectSettings.cs @@ -349,7 +349,8 @@ public sealed record ProjectSettings /// BRIEF ยง14.5) to the frames of , by photo: each exclusion, the hero and each camera keyframe /// moves to the frame of holding its photo. One whose photo lacks becomes /// a , and pending frames whose photo holds become positions again; a - /// pending hero only while no other hero is chosen, a pending keyframe only where no keyframe is. With + /// pending hero only while no other hero is chosen (a hero chosen since one was left pending is newer, so that one no + /// longer waits even when the chosen one must wait in turn), a pending keyframe only where no keyframe is. With /// null the positions already name 's frames and only pending frames are /// placed (photos that arrived). A position naming no frame of names nothing and goes. Edited /// in place like ; null when nothing changes or is not a @@ -373,7 +374,15 @@ public sealed record ProjectSettings else unreadable.Add(node?.DeepClone()); } } - var changed = from is not null && !from.SameAs(to) && TranslatePositions(project, from, to, pending); + var changed = false; + if (project["heroFrame"] is JsonValue && pending.Any(p => Flag(p.Entry, "hero"))) + { + // Checked before the hero itself may be parked below, which leaves no hero chosen. + foreach (var parked in pending) parked.Entry["hero"] = false; + pending.RemoveAll(p => !Waits(p.Entry)); + changed = true; + } + changed |= from is not null && !from.SameAs(to) && TranslatePositions(project, from, to, pending); changed |= PlacePending(project, to, pending); if (!changed) return null; SortPositions(project); @@ -492,7 +501,7 @@ public sealed record ProjectSettings foreach (var parked in pending.Where(p => !p.Placed && Flag(p.Entry, "hero"))) { parked.Entry["hero"] = false; - parked.Placed = !Flag(parked.Entry, "excluded") && parked.Entry["keyframes"] is not JsonArray { Count: > 0 }; + parked.Placed = !Waits(parked.Entry); changed = true; } return changed; @@ -541,6 +550,9 @@ public sealed record ProjectSettings private static bool Flag(JsonObject entry, string name) => entry[name] is JsonValue value && value.TryGetValue(out var set) && set; + /// A pending entry still has a position to give its photo. + private static bool Waits(JsonObject entry) => Flag(entry, "excluded") || Flag(entry, "hero") || entry["keyframes"] is JsonArray { Count: > 0 }; + private static bool IsPosition(JsonNode? node, out int position) { position = 0; diff --git a/tests/Titano.Core.Tests/Project/ProjectSettingsTests.cs b/tests/Titano.Core.Tests/Project/ProjectSettingsTests.cs index 5f9d12e0..08943aa5 100644 --- a/tests/Titano.Core.Tests/Project/ProjectSettingsTests.cs +++ b/tests/Titano.Core.Tests/Project/ProjectSettingsTests.cs @@ -245,6 +245,44 @@ public sealed class ProjectSettingsTests Assert.Null(placed.PendingFrames); } + [Test] + public void AHeroChosenSinceAnotherWaitedStaysTheHeroWhenItMustWaitToo() + { + // This PC chose photo 3 as the hero before the server had it, so it waits there; an editor on the server then + // chose photo 2, which this PC lacks. + var photos = Enumerable.Range(0, 4).Select(Photo).ToList(); + var server = Frames(photos[..3]); + var here = Frames([photos[0], photos[1], photos[3]]); + var edited = new ProjectSettings + { + ExcludedFrames = [0], + HeroFrame = 2, + PendingFrames = [new PendingFrame { Sha256 = photos[3].Sha256, Size = photos[3].Size, CapturedAt = photos[3].CapturedAt, Hero = true }], + }; + + var taken = ProjectSettings.FromJson(Assert.NotNull(ProjectSettings.TranslateFramePositions(edited.ToJson(), server, here))); + + Assert.Null(taken.HeroFrame); + Assert.SequenceEqual([0], taken.ExcludedFrames); + var waiting = Assert.NotNull(taken.PendingFrames); + Assert.Count(1, waiting); + Assert.True(waiting[0].Hero && waiting[0].Sha256 == photos[2].Sha256, "the newer hero waits for its photo"); + Assert.True(taken.HeroPending); + + // Its photo arrives: it is the hero again. + var arrived = Frames([photos[0], photos[1], photos[2], photos[3]]); + var moved = ProjectSettings.MoveFramePositions(taken.ToJson(), new Dictionary { [2] = 3 }) ?? taken.ToJson(); + var placed = ProjectSettings.FromJson(Assert.NotNull(ProjectSettings.TranslateFramePositions(moved, null, arrived))); + Assert.Equal(2, placed.HeroFrame); + Assert.Null(placed.PendingFrames); + + // A hero chosen here while one waits clears the waiting one, even before its photo arrives. + var chosen = (taken with { HeroFrame = 1 }).ToJson(); + var cleared = ProjectSettings.FromJson(Assert.NotNull(ProjectSettings.TranslateFramePositions(chosen, null, here))); + Assert.Equal(1, cleared.HeroFrame); + Assert.Null(cleared.PendingFrames); + } + [Test] public void AProjectNamingNoFrameCrossesAsItIs() {