fix(project): a hero chosen later outranks one still waiting for its photo

TranslateFramePositions read the stored pending frames, then parked the
project's own hero when the receiving copy lacked its photo (leaving no
hero chosen), and only then placed pending entries: a stale pending hero
whose photo the copy held became the hero again and the newer choice was
lost. When a hero is chosen, stored pending entries no longer carry the
hero flag (an entry left with nothing to place goes) before positions
are translated, so only the hero parked now may wait.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-12 17:09:19 +00:00
1 parent 20c08a9250
commit 98e1486f83
2 files changed
+53 -3

No files matched your search

+15 -3
View File
@@ -349,7 +349,8 @@ public sealed record ProjectSettings
/// BRIEF §14.5) to the frames of <paramref name="to"/>, by photo: each exclusion, the hero and each camera keyframe
/// moves to the frame of <paramref name="to"/> holding its photo. One whose photo <paramref name="to"/> lacks becomes
/// a <see cref="PendingFrame"/>, and pending frames whose photo <paramref name="to"/> 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
/// <paramref name="from"/> null the positions already name <paramref name="to"/>'s frames and only pending frames are
/// placed (photos that arrived). A position naming no frame of <paramref name="from"/> names nothing and goes. Edited
/// in place like <see cref="MoveFramePositions"/>; null when nothing changes or <paramref name="json"/> 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<bool>(out var set) && set;
/// <summary>A pending entry still has a position to give its photo.</summary>
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;
@@ -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<int, int> { [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()
{