Fix 3+ player custom lobby state/ready sync
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
namespace SOR.Game.Handlers;
|
||||
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using SOR.Database.Contexts;
|
||||
@@ -70,7 +69,7 @@ internal sealed class GameLobbyReadyHandler : GameHandlerBase, IMessageHandler
|
||||
bool isAdditional = false;
|
||||
if (!isHost && !isGuest && lobby.AdditionalPlayersJson != null)
|
||||
{
|
||||
var addlCheck = JsonSerializer.Deserialize<List<AdditionalLobbyPlayerReady>>(
|
||||
var addlCheck = JsonSerializer.Deserialize<List<AdditionalLobbyPlayer>>(
|
||||
lobby.AdditionalPlayersJson,
|
||||
new JsonSerializerOptions { PropertyNameCaseInsensitive = true })
|
||||
?? new();
|
||||
@@ -103,23 +102,43 @@ internal sealed class GameLobbyReadyHandler : GameHandlerBase, IMessageHandler
|
||||
// Update readiness and deck in AdditionalPlayersJson for players 3+.
|
||||
if (isAdditional && lobby.AdditionalPlayersJson != null)
|
||||
{
|
||||
var addl = JsonSerializer.Deserialize<List<AdditionalLobbyPlayerReady>>(
|
||||
var addl = JsonSerializer.Deserialize<List<AdditionalLobbyPlayer>>(
|
||||
lobby.AdditionalPlayersJson,
|
||||
new JsonSerializerOptions { PropertyNameCaseInsensitive = true })
|
||||
?? new();
|
||||
var entry = addl.FirstOrDefault(p => p.AccountId == accountId);
|
||||
if (entry != null)
|
||||
{
|
||||
entry.IsReady = true;
|
||||
entry.DeckId = req.DeckId;
|
||||
}
|
||||
var idx = addl.FindIndex(p => p.AccountId == accountId);
|
||||
if (idx >= 0)
|
||||
addl[idx] = addl[idx] with { IsReady = true, DeckId = req.DeckId };
|
||||
lobby.AdditionalPlayersJson = JsonSerializer.Serialize(addl);
|
||||
await _gameDb.SaveChangesAsync(ct);
|
||||
}
|
||||
|
||||
// Build additional-player readiness snapshot for pushes.
|
||||
var addlPush = lobby.AdditionalPlayersJson is null
|
||||
? new List<AdditionalLobbyPlayer>()
|
||||
: JsonSerializer.Deserialize<List<AdditionalLobbyPlayer>>(
|
||||
lobby.AdditionalPlayersJson,
|
||||
new JsonSerializerOptions { PropertyNameCaseInsensitive = true }) ?? new();
|
||||
|
||||
var addlIds = addlPush.Select(p => p.AccountId).Distinct().ToArray();
|
||||
var addlNames = addlIds.Length == 0
|
||||
? new Dictionary<Guid, string>()
|
||||
: await _identityDb.Users
|
||||
.AsNoTracking()
|
||||
.Where(u => addlIds.Contains(u.Id))
|
||||
.Select(u => new { u.Id, Name = u.DisplayName ?? u.UserName ?? u.Id.ToString().Substring(0, 8) })
|
||||
.ToDictionaryAsync(u => u.Id, u => u.Name, ct);
|
||||
|
||||
var additionalViews = addlPush
|
||||
.Select(p => new AdditionalLobbyPlayerViewDto(
|
||||
p.AccountId,
|
||||
addlNames.GetValueOrDefault(p.AccountId, p.AccountId.ToString()[..8]),
|
||||
p.IsReady))
|
||||
.ToList();
|
||||
|
||||
// Push updated ready status to all participants (host, guest, and additional players).
|
||||
var readyPush = ServerMessage.Push("game.custom.lobby.ready_status",
|
||||
new LobbyReadyStatusPush(lobby.Id, lobby.HostReady, lobby.GuestReady));
|
||||
new LobbyReadyStatusPush(lobby.Id, lobby.HostReady, lobby.GuestReady, additionalViews));
|
||||
|
||||
var pushTasks = new List<Task>
|
||||
{
|
||||
@@ -127,22 +146,15 @@ internal sealed class GameLobbyReadyHandler : GameHandlerBase, IMessageHandler
|
||||
};
|
||||
if (lobby.GuestAccountId.HasValue)
|
||||
pushTasks.Add(_clientPush.PushAsync(lobby.GuestAccountId.Value.ToString(), readyPush, ct));
|
||||
if (lobby.AdditionalPlayersJson != null)
|
||||
{
|
||||
var addlPush = JsonSerializer.Deserialize<List<AdditionalLobbyPlayer>>(
|
||||
lobby.AdditionalPlayersJson,
|
||||
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
||||
if (addlPush != null)
|
||||
foreach (var ap in addlPush)
|
||||
pushTasks.Add(_clientPush.PushAsync(ap.AccountId.ToString(), readyPush, ct));
|
||||
}
|
||||
foreach (var ap in addlPush)
|
||||
pushTasks.Add(_clientPush.PushAsync(ap.AccountId.ToString(), readyPush, ct));
|
||||
await Task.WhenAll(pushTasks);
|
||||
|
||||
// All-ready check: host + guest must be ready; for 3+ players all must be ready.
|
||||
bool allReady = lobby.GuestAccountId.HasValue && lobby.HostReady && lobby.GuestReady;
|
||||
if (allReady && lobby.AdditionalPlayersJson != null)
|
||||
{
|
||||
var addl = JsonSerializer.Deserialize<List<AdditionalLobbyPlayerReady>>(
|
||||
var addl = JsonSerializer.Deserialize<List<AdditionalLobbyPlayer>>(
|
||||
lobby.AdditionalPlayersJson,
|
||||
new JsonSerializerOptions { PropertyNameCaseInsensitive = true })
|
||||
?? new();
|
||||
@@ -323,13 +335,4 @@ internal sealed class GameLobbyReadyHandler : GameHandlerBase, IMessageHandler
|
||||
await Task.WhenAll(startTasks);
|
||||
}
|
||||
|
||||
// ── Helper types ──────────────────────────────────────────────────────────
|
||||
|
||||
/// <summary>Tracks a player's readiness state within AdditionalPlayersJson.</summary>
|
||||
private sealed class AdditionalLobbyPlayerReady
|
||||
{
|
||||
[JsonPropertyName("accountId")] public Guid AccountId { get; set; }
|
||||
[JsonPropertyName("deckId")] public Guid DeckId { get; set; }
|
||||
[JsonPropertyName("isReady")] public bool IsReady { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user