diff --git a/Server/src/SOR.Ai/Handlers/GameAddAiHandler.cs b/Server/src/SOR.Ai/Handlers/GameAddAiHandler.cs index d726da8..b0ac93f 100644 --- a/Server/src/SOR.Ai/Handlers/GameAddAiHandler.cs +++ b/Server/src/SOR.Ai/Handlers/GameAddAiHandler.cs @@ -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; } } } diff --git a/Server/src/SOR.Game/Handlers/GamePlaceArchSpiritHandler.cs b/Server/src/SOR.Game/Handlers/GamePlaceArchSpiritHandler.cs index 697dee7..cbde843 100644 --- a/Server/src/SOR.Game/Handlers/GamePlaceArchSpiritHandler.cs +++ b/Server/src/SOR.Game/Handlers/GamePlaceArchSpiritHandler.cs @@ -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; /// @@ -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)); }