Synchronise les parties entre les deux devices

This commit is contained in:
2026-04-15 23:40:18 +02:00
parent 3b88b9abe6
commit e0c3a41ccd
11 changed files with 527 additions and 18 deletions

View File

@@ -4,6 +4,7 @@
@inject BrowserBridge Browser
@inject MatchStore Store
@inject NavigationManager Navigation
@inject SocialRealtimeService Realtime
<PageTitle>ChessCubing Arena | Phase Cube</PageTitle>
<PageBody Page="cube" BodyClass="phase-body" />
@@ -207,6 +208,7 @@ else if (Match is not null && summary is not null && blackZone is not null && wh
private bool ShowHelpModal;
private bool ShowResultModal;
private string? _resultModalKey;
private long _knownCollaborativeRevision;
private MatchState? Match => Store.Current;
@@ -217,7 +219,16 @@ else if (Match is not null && summary is not null && blackZone is not null && wh
return;
}
Realtime.Changed += HandleRealtimeChanged;
await Realtime.EnsureStartedAsync();
await Store.EnsureLoadedAsync();
if (!string.IsNullOrWhiteSpace(Store.Current?.CollaborationSessionId))
{
await Realtime.EnsureJoinedPlaySessionAsync(Store.Current.CollaborationSessionId);
await ApplyCollaborativeSyncAsync();
}
_ready = true;
if (Match is null)
@@ -247,9 +258,20 @@ else if (Match is not null && summary is not null && blackZone is not null && wh
_tickerCancellation.Dispose();
}
Realtime.Changed -= HandleRealtimeChanged;
await Store.FlushIfDueAsync(0);
}
private void HandleRealtimeChanged()
=> _ = InvokeAsync(HandleRealtimeChangedAsync);
private async Task HandleRealtimeChangedAsync()
{
await ApplyCollaborativeSyncAsync();
UpdateResultModalState();
StateHasChanged();
}
private async Task RunTickerAsync(CancellationToken cancellationToken)
{
try
@@ -418,6 +440,7 @@ else if (Match is not null && summary is not null && blackZone is not null && wh
MatchEngine.ApplyCubeOutcome(match);
Store.MarkDirty();
await Store.SaveAsync();
await Realtime.PublishMatchStateAsync(match, "/chrono.html");
Navigation.NavigateTo("/chrono.html");
}
@@ -438,7 +461,14 @@ else if (Match is not null && summary is not null && blackZone is not null && wh
private async Task ResetMatchAsync()
{
var sessionId = Match?.CollaborationSessionId;
await Store.ClearAsync();
if (!string.IsNullOrWhiteSpace(sessionId))
{
await Realtime.PublishMatchStateAsync(null, "/application.html");
}
Navigation.NavigateTo("/application.html", replace: true);
}
@@ -446,6 +476,7 @@ else if (Match is not null && summary is not null && blackZone is not null && wh
{
Store.MarkDirty();
await Store.SaveAsync();
await Realtime.PublishMatchStateAsync(Match, "/cube.html");
UpdateResultModalState();
StateHasChanged();
}
@@ -773,6 +804,31 @@ else if (Match is not null && summary is not null && blackZone is not null && wh
private static string BoolString(bool value)
=> value ? "true" : "false";
private async Task ApplyCollaborativeSyncAsync()
{
var snapshot = Realtime.CollaborativeSnapshot;
if (snapshot is null || snapshot.Revision <= _knownCollaborativeRevision)
{
return;
}
_knownCollaborativeRevision = snapshot.Revision;
Store.ReplaceCurrent(snapshot.Match);
await Store.SaveAsync();
var route = NormalizeRoute(snapshot.Route);
if (!string.Equals(route, "/cube.html", StringComparison.OrdinalIgnoreCase))
{
Navigation.NavigateTo(route, replace: true);
}
}
private static string NormalizeRoute(string? route)
{
var normalized = string.IsNullOrWhiteSpace(route) ? "/application.html" : route.Trim();
return normalized.StartsWith('/') ? normalized : $"/{normalized}";
}
private sealed class CubeHoldState
{
public bool Armed { get; set; }