Fix free game: display names in lobby/game screen + rules fallback
- CustomLobbyGuestJoinedPush: add GuestDisplayName (host now sees guest's real name) - JoinCustomLobbyResponse: add HostAccountId/Name + GuestAccountId/Name so guest lobby room shows correct state instead of "Waiting for guest…" - CustomLobbyGameStartedPush: add OpponentDisplayName so game screen shows name not GUID - GameJoinCustomLobbyHandler: use 3-tier rules fallback (lobby override → db custom-default → hard-coded default) matching StartGameAsync, fixing inconsistent rule display on join - GameLobbyReadyHandler: inject SorIdentityDbContext, fetch display names for both players before pushing game.custom.started - CustomLobby.gd: set Game.opponent_name on game.custom.started push; emit display names in guest_joined and game_starting signals; patch join response lobby dict - lobby_room_screen.gd: use display name in _on_guest_joined; update _on_game_starting signature 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>
This commit is contained in:
@@ -16,7 +16,7 @@ extends Node
|
||||
# ── Signals ───────────────────────────────────────────────────────────────────
|
||||
|
||||
## Pushed when a guest joins the host's open lobby.
|
||||
signal guest_joined(guest_account_id: String)
|
||||
signal guest_joined(guest_account_id: String, guest_display_name: String)
|
||||
|
||||
## Pushed when the host updates lobby rules (or a preset vote is approved).
|
||||
signal lobby_rules_updated(new_rules: Dictionary)
|
||||
@@ -29,7 +29,7 @@ signal ready_status_changed(host_ready: bool, guest_ready: bool)
|
||||
|
||||
## Pushed when both players are ready and the game session has been created.
|
||||
## Navigate to GameScreen.tscn after receiving this.
|
||||
signal game_starting(session_id: String, opponent_account_id: String)
|
||||
signal game_starting(session_id: String, opponent_account_id: String, opponent_display_name: String)
|
||||
|
||||
## Pushed when the host proposes loading a preset; guests should show a vote banner.
|
||||
signal preset_vote_requested(payload: Dictionary)
|
||||
@@ -120,6 +120,10 @@ func join(lid: String, deck_id: String, password: String,
|
||||
lobby_id = str(d.get("lobbyId", ""))
|
||||
lobby = d
|
||||
is_host = false
|
||||
# Ensure the lobby dict has the fields _refresh_display needs.
|
||||
lobby["guestAccountId"] = str(d.get("guestAccountId", ""))
|
||||
lobby["guestDisplayName"] = str(d.get("guestDisplayName", ""))
|
||||
lobby["hasGuest"] = true
|
||||
if on_success.is_valid(): on_success.call(d),
|
||||
func(code: String, msg: String) -> void:
|
||||
print("[CustomLobby] ERR join ", code, " ", msg)
|
||||
@@ -256,9 +260,10 @@ func _on_server_push(type: String, payload: Variant) -> void:
|
||||
print("[CustomLobby] PUSH ", type, " ", JSON.stringify(payload).substr(0, 200))
|
||||
match type:
|
||||
"game.custom.lobby.guest_joined":
|
||||
var d := payload as Dictionary
|
||||
var gid : String = str(d.get("guestAccountId", ""))
|
||||
guest_joined.emit(gid)
|
||||
var d := payload as Dictionary
|
||||
var gid : String = str(d.get("guestAccountId", ""))
|
||||
var gname: String = str(d.get("guestDisplayName", gid.substr(0, 8)))
|
||||
guest_joined.emit(gid, gname)
|
||||
|
||||
"game.custom.lobby_updated":
|
||||
var d := payload as Dictionary
|
||||
@@ -284,12 +289,14 @@ func _on_server_push(type: String, payload: Variant) -> void:
|
||||
ready_status_changed.emit(hr, gr)
|
||||
|
||||
"game.custom.started":
|
||||
var d := payload as Dictionary
|
||||
var sid : String = str(d.get("sessionId", ""))
|
||||
var opp : String = str(d.get("opponentAccountId", ""))
|
||||
Game.session_id = sid
|
||||
Game.state = {}
|
||||
game_starting.emit(sid, opp)
|
||||
var d := payload as Dictionary
|
||||
var sid : String = str(d.get("sessionId", ""))
|
||||
var opp : String = str(d.get("opponentAccountId", ""))
|
||||
var oname : String = str(d.get("opponentDisplayName", opp.substr(0, 8)))
|
||||
Game.session_id = sid
|
||||
Game.opponent_name = oname
|
||||
Game.state = {}
|
||||
game_starting.emit(sid, opp, oname)
|
||||
|
||||
"game.custom.preset.vote_requested":
|
||||
preset_vote_requested.emit(payload as Dictionary)
|
||||
|
||||
@@ -215,8 +215,9 @@ func _update_rules_label() -> void:
|
||||
|
||||
# ── Signal handlers ───────────────────────────────────────────────────────────
|
||||
|
||||
func _on_guest_joined(guest_account_id: String) -> void:
|
||||
guest_name_label.text = "Guest: %s" % guest_account_id.substr(0, 8)
|
||||
func _on_guest_joined(guest_account_id: String, guest_display_name: String) -> void:
|
||||
var name := guest_display_name if not guest_display_name.is_empty() else guest_account_id.substr(0, 8)
|
||||
guest_name_label.text = "Guest: %s" % name
|
||||
guest_deck_label.text = "Deck: (selected)"
|
||||
|
||||
|
||||
@@ -234,7 +235,7 @@ func _on_deck_changed(_account_id: String, _deck_id: String) -> void:
|
||||
guest_deck_label.text = "Deck: (updated)"
|
||||
|
||||
|
||||
func _on_game_starting(session_id: String, _opponent_account_id: String) -> void:
|
||||
func _on_game_starting(session_id: String, _opponent_account_id: String, _opponent_display_name: String) -> void:
|
||||
ready_btn.disabled = true
|
||||
ready_btn.text = "Starting…"
|
||||
await get_tree().create_timer(0.8).timeout
|
||||
|
||||
@@ -236,7 +236,11 @@ public sealed record JoinCustomLobbyResponse(
|
||||
Guid LobbyId,
|
||||
string Status, // "WaitingForReady"
|
||||
CustomRulesDto ActiveRules,
|
||||
Guid? GameSessionId); // always null; reserved for future use
|
||||
Guid? GameSessionId, // always null; reserved for future use
|
||||
Guid HostAccountId,
|
||||
string HostDisplayName,
|
||||
Guid GuestAccountId,
|
||||
string GuestDisplayName);
|
||||
|
||||
public sealed record LeaveCustomLobbyResponse(Guid LobbyId);
|
||||
|
||||
@@ -339,13 +343,15 @@ public sealed record GameBothIdlePush(Guid SessionId);
|
||||
/// Players should navigate to the game screen and send game.ready.
|
||||
/// </summary>
|
||||
public sealed record CustomLobbyGameStartedPush(
|
||||
Guid SessionId,
|
||||
Guid OpponentAccountId);
|
||||
Guid SessionId,
|
||||
Guid OpponentAccountId,
|
||||
string OpponentDisplayName);
|
||||
|
||||
/// <summary>Pushed to the host when a guest joins their open lobby.</summary>
|
||||
public sealed record CustomLobbyGuestJoinedPush(
|
||||
Guid LobbyId,
|
||||
Guid GuestAccountId,
|
||||
string GuestDisplayName,
|
||||
string LobbyName);
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
namespace SOR.Game.Handlers;
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SOR.Database.Contexts;
|
||||
using SOR.Database.Enums;
|
||||
using SOR.Networking.Abstractions.Messaging;
|
||||
@@ -16,17 +17,20 @@ using SOR.Shared.Networking;
|
||||
/// </summary>
|
||||
internal sealed class GameJoinCustomLobbyHandler : GameHandlerBase, IMessageHandler
|
||||
{
|
||||
private readonly IMessageSerializer _serializer;
|
||||
private readonly GameDbContext _gameDb;
|
||||
private readonly IClientPush _clientPush;
|
||||
private readonly IMessageSerializer _serializer;
|
||||
private readonly GameDbContext _gameDb;
|
||||
private readonly SorIdentityDbContext _identityDb;
|
||||
private readonly IClientPush _clientPush;
|
||||
|
||||
public GameJoinCustomLobbyHandler(
|
||||
IMessageSerializer serializer,
|
||||
GameDbContext gameDb,
|
||||
IClientPush clientPush)
|
||||
IMessageSerializer serializer,
|
||||
GameDbContext gameDb,
|
||||
SorIdentityDbContext identityDb,
|
||||
IClientPush clientPush)
|
||||
{
|
||||
_serializer = serializer;
|
||||
_gameDb = gameDb;
|
||||
_identityDb = identityDb;
|
||||
_clientPush = clientPush;
|
||||
}
|
||||
|
||||
@@ -66,42 +70,65 @@ internal sealed class GameJoinCustomLobbyHandler : GameHandlerBase, IMessageHand
|
||||
|
||||
await _gameDb.SaveChangesAsync(ct);
|
||||
|
||||
// Build effective rules so the guest can pre-populate their rule editor.
|
||||
// Load admin custom-default rules so the join response matches
|
||||
// the same 3-tier fallback used when the game actually starts.
|
||||
var dbDefaults = await _gameDb.GameRuleSets
|
||||
.AsNoTracking()
|
||||
.Where(r => r.IsCustomDefault)
|
||||
.FirstOrDefaultAsync(ct);
|
||||
|
||||
// Build effective rules: lobby override → admin custom-default → hard-coded default.
|
||||
var effective = new GameRules
|
||||
{
|
||||
OpeningHandSize = lobby.OpeningHandSizeOverride ?? GameRules.Default.OpeningHandSize,
|
||||
FirstPlayerSkipsDraw = lobby.FirstPlayerSkipsDrawOverride ?? GameRules.Default.FirstPlayerSkipsDraw,
|
||||
SpAccumulationPerTurn = lobby.SpAccumulationPerTurnOverride ?? GameRules.Default.SpAccumulationPerTurn,
|
||||
DrawPerTurn = lobby.DrawPerTurnOverride ?? GameRules.Default.DrawPerTurn,
|
||||
SpHasCap = lobby.SpHasCapOverride ?? GameRules.Default.SpHasCap,
|
||||
SpCap = lobby.SpCapOverride ?? GameRules.Default.SpCap,
|
||||
BoardWidth = lobby.BoardWidthOverride ?? GameRules.Default.BoardWidth,
|
||||
BoardHeight = lobby.BoardHeightOverride ?? GameRules.Default.BoardHeight,
|
||||
HandSizeCap = lobby.HandSizeCapOverride ?? GameRules.Default.HandSizeCap,
|
||||
DiscardOnMaxHand = lobby.DiscardOnMaxHandOverride ?? GameRules.Default.DiscardOnMaxHand,
|
||||
MaxDeckSize = lobby.MaxDeckSizeOverride ?? GameRules.Default.MaxDeckSize,
|
||||
MinDeckSize = lobby.MinDeckSizeOverride ?? GameRules.Default.MinDeckSize,
|
||||
MaxArchTier1 = lobby.MaxArchTier1Override ?? GameRules.Default.MaxArchTier1,
|
||||
TurnTimerSeconds = lobby.TurnTimerSecondsOverride ?? GameRules.Default.TurnTimerSeconds,
|
||||
TimeLimitMinutes = lobby.TimeLimitMinutesOverride ?? GameRules.Default.TimeLimitMinutes,
|
||||
TurnLimit = lobby.TurnLimitOverride ?? GameRules.Default.TurnLimit,
|
||||
MpBuff = lobby.MpBuffOverride ?? GameRules.Default.MpBuff,
|
||||
StartingRandomTilePct = lobby.StartingRandomTilePctOverride ?? GameRules.Default.StartingRandomTilePct,
|
||||
OpeningHandSize = lobby.OpeningHandSizeOverride ?? dbDefaults?.OpeningHandSize ?? GameRules.Default.OpeningHandSize,
|
||||
FirstPlayerSkipsDraw = lobby.FirstPlayerSkipsDrawOverride ?? dbDefaults?.FirstPlayerSkipsDraw ?? GameRules.Default.FirstPlayerSkipsDraw,
|
||||
SpAccumulationPerTurn = lobby.SpAccumulationPerTurnOverride ?? dbDefaults?.SpAccumulationPerTurn ?? GameRules.Default.SpAccumulationPerTurn,
|
||||
DrawPerTurn = lobby.DrawPerTurnOverride ?? dbDefaults?.DrawPerTurn ?? GameRules.Default.DrawPerTurn,
|
||||
SpHasCap = lobby.SpHasCapOverride ?? dbDefaults?.SpHasCap ?? GameRules.Default.SpHasCap,
|
||||
SpCap = lobby.SpCapOverride ?? dbDefaults?.SpCap ?? GameRules.Default.SpCap,
|
||||
BoardWidth = lobby.BoardWidthOverride ?? dbDefaults?.BoardWidth ?? GameRules.Default.BoardWidth,
|
||||
BoardHeight = lobby.BoardHeightOverride ?? dbDefaults?.BoardHeight ?? GameRules.Default.BoardHeight,
|
||||
HandSizeCap = lobby.HandSizeCapOverride ?? dbDefaults?.HandSizeCap ?? GameRules.Default.HandSizeCap,
|
||||
DiscardOnMaxHand = lobby.DiscardOnMaxHandOverride ?? dbDefaults?.DiscardOnMaxHand ?? GameRules.Default.DiscardOnMaxHand,
|
||||
MaxDeckSize = lobby.MaxDeckSizeOverride ?? dbDefaults?.MaxDeckSize ?? GameRules.Default.MaxDeckSize,
|
||||
MinDeckSize = lobby.MinDeckSizeOverride ?? dbDefaults?.MinDeckSize ?? GameRules.Default.MinDeckSize,
|
||||
MaxArchTier1 = lobby.MaxArchTier1Override ?? dbDefaults?.MaxArchTier1 ?? GameRules.Default.MaxArchTier1,
|
||||
TurnTimerSeconds = lobby.TurnTimerSecondsOverride ?? dbDefaults?.TurnTimerSeconds ?? GameRules.Default.TurnTimerSeconds,
|
||||
TimeLimitMinutes = lobby.TimeLimitMinutesOverride ?? dbDefaults?.TimeLimitMinutes ?? GameRules.Default.TimeLimitMinutes,
|
||||
TurnLimit = lobby.TurnLimitOverride ?? dbDefaults?.TurnLimit ?? GameRules.Default.TurnLimit,
|
||||
MpBuff = lobby.MpBuffOverride ?? dbDefaults?.MpBuff ?? GameRules.Default.MpBuff,
|
||||
StartingRandomTilePct = lobby.StartingRandomTilePctOverride ?? dbDefaults?.StartingRandomTilePct ?? GameRules.Default.StartingRandomTilePct,
|
||||
};
|
||||
|
||||
// Fetch display names for host and guest.
|
||||
var ids = new[] { lobby.HostAccountId, accountId };
|
||||
var names = await _identityDb.Users
|
||||
.AsNoTracking()
|
||||
.Where(a => ids.Contains(a.Id))
|
||||
.Select(a => new { a.Id, Name = a.DisplayName ?? a.UserName ?? a.Id.ToString().Substring(0, 8) })
|
||||
.ToDictionaryAsync(a => a.Id, a => a.Name, ct);
|
||||
|
||||
var hostName = names.GetValueOrDefault(lobby.HostAccountId, lobby.HostAccountId.ToString()[..8]);
|
||||
var guestName = names.GetValueOrDefault(accountId, accountId.ToString()[..8]);
|
||||
|
||||
// Notify the host that their guest has arrived.
|
||||
await _clientPush.PushAsync(lobby.HostAccountId.ToString(),
|
||||
ServerMessage.Push("game.custom.lobby.guest_joined",
|
||||
new CustomLobbyGuestJoinedPush(
|
||||
LobbyId : lobby.Id,
|
||||
GuestAccountId: accountId,
|
||||
LobbyName : lobby.Name)), ct);
|
||||
LobbyId : lobby.Id,
|
||||
GuestAccountId : accountId,
|
||||
GuestDisplayName: guestName,
|
||||
LobbyName : lobby.Name)), ct);
|
||||
|
||||
return ServerMessage.Reply("game.custom.join.ack", message.Seq,
|
||||
new JoinCustomLobbyResponse(
|
||||
LobbyId : lobby.Id,
|
||||
Status : "WaitingForReady",
|
||||
ActiveRules : GameCreateCustomLobbyHandler.ToDto(effective, lobby.Password != null),
|
||||
GameSessionId: null));
|
||||
LobbyId : lobby.Id,
|
||||
Status : "WaitingForReady",
|
||||
ActiveRules : GameCreateCustomLobbyHandler.ToDto(effective, lobby.Password != null),
|
||||
GameSessionId : null,
|
||||
HostAccountId : lobby.HostAccountId,
|
||||
HostDisplayName : hostName,
|
||||
GuestAccountId : accountId,
|
||||
GuestDisplayName: guestName));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,21 +22,24 @@ using SOR.Shared.Networking;
|
||||
/// </summary>
|
||||
internal sealed class GameLobbyReadyHandler : GameHandlerBase, IMessageHandler
|
||||
{
|
||||
private readonly IMessageSerializer _serializer;
|
||||
private readonly GameDbContext _gameDb;
|
||||
private readonly IClientPush _clientPush;
|
||||
private readonly IEventBus _eventBus;
|
||||
private readonly IMessageSerializer _serializer;
|
||||
private readonly GameDbContext _gameDb;
|
||||
private readonly SorIdentityDbContext _identityDb;
|
||||
private readonly IClientPush _clientPush;
|
||||
private readonly IEventBus _eventBus;
|
||||
private readonly ILogger<GameLobbyReadyHandler> _logger;
|
||||
|
||||
public GameLobbyReadyHandler(
|
||||
IMessageSerializer serializer,
|
||||
GameDbContext gameDb,
|
||||
IClientPush clientPush,
|
||||
IEventBus eventBus,
|
||||
IMessageSerializer serializer,
|
||||
GameDbContext gameDb,
|
||||
SorIdentityDbContext identityDb,
|
||||
IClientPush clientPush,
|
||||
IEventBus eventBus,
|
||||
ILogger<GameLobbyReadyHandler> logger)
|
||||
{
|
||||
_serializer = serializer;
|
||||
_gameDb = gameDb;
|
||||
_identityDb = identityDb;
|
||||
_clientPush = clientPush;
|
||||
_eventBus = eventBus;
|
||||
_logger = logger;
|
||||
@@ -184,13 +187,24 @@ internal sealed class GameLobbyReadyHandler : GameHandlerBase, IMessageHandler
|
||||
session.Id);
|
||||
}
|
||||
|
||||
// Fetch display names for both players for the game-started push.
|
||||
var ids = new[] { playerOneId, playerTwoId };
|
||||
var names = await _identityDb.Users
|
||||
.AsNoTracking()
|
||||
.Where(a => ids.Contains(a.Id))
|
||||
.Select(a => new { a.Id, Name = a.DisplayName ?? a.UserName ?? a.Id.ToString().Substring(0, 8) })
|
||||
.ToDictionaryAsync(a => a.Id, a => a.Name, ct);
|
||||
|
||||
var p1Name = names.GetValueOrDefault(playerOneId, playerOneId.ToString()[..8]);
|
||||
var p2Name = names.GetValueOrDefault(playerTwoId, playerTwoId.ToString()[..8]);
|
||||
|
||||
// Push game-started notification to both players.
|
||||
await Task.WhenAll(
|
||||
_clientPush.PushAsync(playerOneId.ToString(),
|
||||
ServerMessage.Push("game.custom.started",
|
||||
new CustomLobbyGameStartedPush(session.Id, playerTwoId)), ct),
|
||||
new CustomLobbyGameStartedPush(session.Id, playerTwoId, p2Name)), ct),
|
||||
_clientPush.PushAsync(playerTwoId.ToString(),
|
||||
ServerMessage.Push("game.custom.started",
|
||||
new CustomLobbyGameStartedPush(session.Id, playerOneId)), ct));
|
||||
new CustomLobbyGameStartedPush(session.Id, playerOneId, p1Name)), ct));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user