Synchronise les parties entre les deux devices
This commit is contained in:
@@ -9,12 +9,14 @@ public sealed class SocialHub(
|
||||
ConnectedUserTracker tracker,
|
||||
MySqlSocialStore socialStore,
|
||||
MySqlUserProfileStore profileStore,
|
||||
PlayInviteCoordinator playInviteCoordinator) : Hub
|
||||
PlayInviteCoordinator playInviteCoordinator,
|
||||
CollaborativeMatchCoordinator collaborativeMatchCoordinator) : Hub
|
||||
{
|
||||
private readonly ConnectedUserTracker _tracker = tracker;
|
||||
private readonly MySqlSocialStore _socialStore = socialStore;
|
||||
private readonly MySqlUserProfileStore _profileStore = profileStore;
|
||||
private readonly PlayInviteCoordinator _playInviteCoordinator = playInviteCoordinator;
|
||||
private readonly CollaborativeMatchCoordinator _collaborativeMatchCoordinator = collaborativeMatchCoordinator;
|
||||
|
||||
public override async Task OnConnectedAsync()
|
||||
{
|
||||
@@ -99,6 +101,7 @@ public sealed class SocialHub(
|
||||
if (accept)
|
||||
{
|
||||
var accepted = _playInviteCoordinator.AcceptInvite(inviteId, recipient.Subject);
|
||||
_collaborativeMatchCoordinator.RegisterSession(accepted.Session);
|
||||
await Clients.Users([accepted.SenderSubject, accepted.RecipientSubject])
|
||||
.SendAsync("PlayInviteAccepted", accepted.Session, Context.ConnectionAborted);
|
||||
return;
|
||||
@@ -130,6 +133,50 @@ public sealed class SocialHub(
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<CollaborativeMatchStateMessage?> JoinPlaySession(string sessionId)
|
||||
{
|
||||
var siteUser = RequireCurrentUser();
|
||||
var normalizedSessionId = sessionId.Trim();
|
||||
|
||||
try
|
||||
{
|
||||
var latestState = _collaborativeMatchCoordinator.GetLatestState(normalizedSessionId, siteUser.Subject);
|
||||
await Groups.AddToGroupAsync(Context.ConnectionId, BuildSessionGroupName(normalizedSessionId), Context.ConnectionAborted);
|
||||
return latestState;
|
||||
}
|
||||
catch (SocialValidationException exception)
|
||||
{
|
||||
throw new HubException(exception.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task LeavePlaySession(string sessionId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(sessionId))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await Groups.RemoveFromGroupAsync(Context.ConnectionId, BuildSessionGroupName(sessionId.Trim()), Context.ConnectionAborted);
|
||||
}
|
||||
|
||||
public async Task PublishMatchState(string sessionId, string? matchJson, string route)
|
||||
{
|
||||
var siteUser = RequireCurrentUser();
|
||||
var normalizedSessionId = sessionId.Trim();
|
||||
|
||||
try
|
||||
{
|
||||
var message = _collaborativeMatchCoordinator.PublishState(normalizedSessionId, siteUser.Subject, matchJson, route);
|
||||
await Clients.GroupExcept(BuildSessionGroupName(normalizedSessionId), [Context.ConnectionId])
|
||||
.SendAsync("CollaborativeMatchStateUpdated", message, Context.ConnectionAborted);
|
||||
}
|
||||
catch (SocialValidationException exception)
|
||||
{
|
||||
throw new HubException(exception.Message);
|
||||
}
|
||||
}
|
||||
|
||||
private AuthenticatedSiteUser RequireCurrentUser()
|
||||
=> AuthenticatedSiteUserFactory.FromClaimsPrincipal(Context.User ?? new System.Security.Claims.ClaimsPrincipal())
|
||||
?? throw new HubException("La session utilisateur est incomplete.");
|
||||
@@ -165,4 +212,7 @@ public sealed class SocialHub(
|
||||
IsOnline = isOnline,
|
||||
});
|
||||
}
|
||||
|
||||
private static string BuildSessionGroupName(string sessionId)
|
||||
=> $"play-session:{sessionId}";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user