fix: AI opponent now auto-readies and places arch spirit
Two bugs prevented AI from participating after being added to a lobby: 1. GameAddAiHandler never set GuestReady=true (or IsReady in additional players JSON), so the all-ready check in GameLobbyReadyHandler never fired — the game never started when the host clicked ready. Fix: set GuestReady=true for 1v1, IsReady=true for multi-player. 2. GamePlaceArchSpiritHandler never published AiTurnStartedEvent. If the human places first, the AI placement turn was never triggered. Likewise, if the AI goes first in the Active phase after both players place, no event fired. Fix: check the new active player after each placement and publish AiTurnStartedEvent if it is an AI seat. 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:
@@ -97,6 +97,7 @@ internal sealed class GameAddAiHandler : IMessageHandler
|
||||
// Standard 1v1: AI becomes the guest.
|
||||
lobby.GuestAccountId = aiId;
|
||||
lobby.GuestDeckId = deckId;
|
||||
lobby.GuestReady = true; // AI is always ready immediately
|
||||
lobby.Status = CustomLobbyStatus.Full;
|
||||
seatIndex = 1;
|
||||
}
|
||||
@@ -111,6 +112,7 @@ internal sealed class GameAddAiHandler : IMessageHandler
|
||||
DeckId = deckId,
|
||||
SeatIndex = seatIndex,
|
||||
TeamIndex = seatIndex % Math.Max(1, lobby.TeamCount),
|
||||
IsReady = true, // AI is always ready immediately
|
||||
});
|
||||
lobby.AdditionalPlayersJson = JsonSerializer.Serialize(addlPlayers);
|
||||
|
||||
@@ -189,5 +191,6 @@ internal sealed class GameAddAiHandler : IMessageHandler
|
||||
public Guid DeckId { get; set; }
|
||||
public int SeatIndex { get; set; }
|
||||
public int TeamIndex { get; set; }
|
||||
public bool IsReady { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,10 +2,13 @@ namespace SOR.Game.Handlers;
|
||||
|
||||
using System.Text.Json;
|
||||
using SOR.Database.Contexts;
|
||||
using SOR.Networking.Abstractions.EventBus;
|
||||
using SOR.Networking.Abstractions.Messaging;
|
||||
using SOR.Networking.Abstractions.Push;
|
||||
using SOR.Networking.Abstractions.Transport;
|
||||
using SOR.Game.Grid;
|
||||
using SOR.Shared;
|
||||
using SOR.Shared.Nats;
|
||||
using SOR.Shared.Networking;
|
||||
|
||||
/// <summary>
|
||||
@@ -23,17 +26,24 @@ internal sealed class GamePlaceArchSpiritHandler : GameHandlerBase, IMessageHand
|
||||
private readonly GameDbContext _gameDb;
|
||||
private readonly IClientPush _clientPush;
|
||||
private readonly GameTurnTimerService _timerService;
|
||||
private readonly IEventBus _eventBus;
|
||||
// Optional: only present when SOR.Ai is loaded.
|
||||
private readonly IAiAccountRegistry? _aiRegistry;
|
||||
|
||||
public GamePlaceArchSpiritHandler(
|
||||
IMessageSerializer serializer,
|
||||
GameDbContext gameDb,
|
||||
IClientPush clientPush,
|
||||
GameTurnTimerService timerService)
|
||||
GameTurnTimerService timerService,
|
||||
IEventBus eventBus,
|
||||
IAiAccountRegistry? aiRegistry = null)
|
||||
{
|
||||
_serializer = serializer;
|
||||
_gameDb = gameDb;
|
||||
_clientPush = clientPush;
|
||||
_timerService = timerService;
|
||||
_eventBus = eventBus;
|
||||
_aiRegistry = aiRegistry;
|
||||
}
|
||||
|
||||
public string MessageType => "game.place_arch_spirit";
|
||||
@@ -67,6 +77,18 @@ internal sealed class GamePlaceArchSpiritHandler : GameHandlerBase, IMessageHand
|
||||
await BroadcastActivityAsync(_clientPush, "started", session.Id, ct);
|
||||
}
|
||||
|
||||
// If the next player to act (either still placing, or first turn in Active phase) is an AI,
|
||||
// signal AiPlayerSessionManager to take over.
|
||||
if (_aiRegistry?.IsAi(newState.ActivePlayerAccountId) == true)
|
||||
{
|
||||
await _eventBus.PublishAsync(NatsSubjects.AiTurnStarted,
|
||||
new AiTurnStartedEvent(
|
||||
SessionId : session.Id,
|
||||
AiAccountId : newState.ActivePlayerAccountId,
|
||||
TurnNumber : newState.TurnNumber),
|
||||
ct);
|
||||
}
|
||||
|
||||
return ServerMessage.Reply("game.place_arch_spirit.ack", message.Seq,
|
||||
new GamePlaceArchSpiritResponse(dto));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user