- GameEngine.cs: 2-player Initialize overload was hardcoding "Rectangle" regardless of rules.BoardShape — now uses rules?.BoardShape ?? "Rectangle" - game_screen.gd: _on_player_eliminated was setting _is_spectating but not calling _setup_spectator_ui(), leaving eliminated players with no Leave button - GameGetStateHandler.cs: on reconnect (game.get_state), populate missing display names from identity DB so old/reconnected sessions show real names instead of truncated account IDs Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
74 lines
2.6 KiB
C#
74 lines
2.6 KiB
C#
namespace SOR.Game.Handlers;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
using SOR.Database.Contexts;
|
|
using SOR.Networking.Abstractions.Messaging;
|
|
using SOR.Networking.Abstractions.Transport;
|
|
using SOR.Shared.Networking;
|
|
|
|
/// <summary>
|
|
/// game.get_state — returns the current game state filtered for the caller's perspective.
|
|
/// Useful for reconnects or initial load.
|
|
/// </summary>
|
|
internal sealed class GameGetStateHandler : GameHandlerBase, IMessageHandler
|
|
{
|
|
private readonly IMessageSerializer _serializer;
|
|
private readonly GameDbContext _gameDb;
|
|
private readonly SorIdentityDbContext _identityDb;
|
|
|
|
public GameGetStateHandler(
|
|
IMessageSerializer serializer,
|
|
GameDbContext gameDb,
|
|
SorIdentityDbContext identityDb)
|
|
{
|
|
_serializer = serializer;
|
|
_gameDb = gameDb;
|
|
_identityDb = identityDb;
|
|
}
|
|
|
|
public string MessageType => "game.get_state";
|
|
|
|
public async Task<IServerMessage?> HandleAsync(
|
|
IClientMessage message,
|
|
AuthenticatedConnection connection,
|
|
CancellationToken ct)
|
|
{
|
|
var req = _serializer.DeserializePayload<GameGetStateRequest>(message.Payload);
|
|
var actorId = connection.UserId;
|
|
|
|
var session = await LoadActiveSessionAsync(_gameDb, req.SessionId, actorId, ct);
|
|
|
|
if (session.StateJson == null)
|
|
throw new GameException("STATE_NOT_INITIALIZED");
|
|
|
|
var state = GameEngine.Deserialize(session.StateJson);
|
|
|
|
// Populate any missing display names so reconnecting clients always see real names.
|
|
var missing = state.Players
|
|
.Where(kv => string.IsNullOrEmpty(kv.Value.DisplayName))
|
|
.Select(kv => kv.Key)
|
|
.ToList();
|
|
|
|
if (missing.Count > 0)
|
|
{
|
|
var guids = missing
|
|
.Select(k => Guid.TryParse(k, out var g) ? g : Guid.Empty)
|
|
.Where(g => g != Guid.Empty)
|
|
.ToArray();
|
|
|
|
var nameMap = await _identityDb.Users
|
|
.AsNoTracking()
|
|
.Where(u => guids.Contains(u.Id))
|
|
.Select(u => new { u.Id, Name = u.DisplayName ?? u.UserName ?? u.Id.ToString().Substring(0, 8) })
|
|
.ToDictionaryAsync(u => u.Id.ToString(), u => u.Name, ct);
|
|
|
|
foreach (var pid in missing)
|
|
if (nameMap.TryGetValue(pid, out var dn))
|
|
state.Players[pid].DisplayName = dn;
|
|
}
|
|
|
|
return ServerMessage.Reply("game.get_state.ack", message.Seq,
|
|
new GameGetStateResponse(GameEngine.ToDto(state, actorId)));
|
|
}
|
|
}
|