ScoreboardRecorder owns the one scope the numbers promise. StartSession resets and marks running; Record ignores everything outside a session (a stale tally from the last stream is exactly the 'today' lie this design avoids); EndSession freezes the numbers but stops them moving, so an End screen can still show them. Changed fires only when a displayed value actually moves, which is what keeps the strip's raster cache honest.
52 lines
2.0 KiB
C#
52 lines
2.0 KiB
C#
namespace ytLive.Models;
|
|
|
|
/// <summary>Owns the session's <see cref="ScoreboardState"/> and the one lifecycle rule that
|
|
/// matters: a new session starts from zero.
|
|
///
|
|
/// <para>Kept separate from the view-model so the rule is testable without a window, and so the
|
|
/// scoreboard layer has exactly one place that decides when counting begins.</para></summary>
|
|
public sealed class ScoreboardRecorder
|
|
{
|
|
public ScoreboardState State { get; private set; } = new();
|
|
|
|
/// <summary>Called when a session starts (go-live, test, or a local recording). Every start
|
|
/// clears the tallies — "session" is the only scope the numbers promise.</summary>
|
|
public void StartSession()
|
|
{
|
|
State = new ScoreboardState();
|
|
IsRunning = true;
|
|
RaiseChanged();
|
|
}
|
|
|
|
/// <summary>Folds a chat event in. Outside a session nothing is counted: a stale tally left over
|
|
/// from the last stream would be the "today" lie this design exists to avoid.</summary>
|
|
public void Record(ChatMessage message)
|
|
{
|
|
if (!IsRunning) return;
|
|
var before = Fingerprint(State);
|
|
State.Apply(message);
|
|
if (Fingerprint(State) != before) RaiseChanged();
|
|
}
|
|
|
|
/// <summary>True while the tallies are live.</summary>
|
|
public bool IsRunning { get; private set; }
|
|
|
|
/// <summary>Marks the session finished. The tallies stay visible (the End screen may show
|
|
/// them) but stop moving.</summary>
|
|
public void EndSession()
|
|
{
|
|
IsRunning = false;
|
|
RaiseChanged();
|
|
}
|
|
|
|
public event System.Action? Changed;
|
|
|
|
private void RaiseChanged() => Changed?.Invoke();
|
|
|
|
/// <summary>Cheap change detector: only re-render the strip when a displayed value moved.</summary>
|
|
private static string Fingerprint(ScoreboardState state) =>
|
|
$"{state.NewMembers}|{state.MilestoneCount}|{state.LastMemberName}|{state.LastGifterName}|" +
|
|
$"{state.LastGiftCount}|{state.LastSuperChatAuthor}|{state.LastSuperChatDisplay}|" +
|
|
$"{state.SessionTotal.Currency}|{state.SessionTotal.Total:0.######}";
|
|
}
|