using CommonDomain; using DotNetCore.CAP; using Microsoft.AspNetCore.SignalR; using Microsoft.Extensions.Logging; namespace ChessGameService.Subscribers; public sealed class AdminKickChessPlayerSubscriber : ICapSubscribe { private readonly IChessGameService _chessService; private readonly IHubContext _hubContext; public AdminKickChessPlayerSubscriber( IChessGameService chessService, IHubContext hubContext) { _chessService = chessService; _hubContext = hubContext; } [CapSubscribe(TopicKeys.后台踢出象棋玩家)] public async Task Handle(AdminKickChessPlayerMessage message) { if (string.IsNullOrWhiteSpace(message.RoomId) || string.IsNullOrWhiteSpace(message.UserId)) { return; } var room = _chessService.GetRoom(message.RoomId); var target = room?.users?.FirstOrDefault(u => u.userId == message.UserId); if (room == null || target == null) { return; } if (room.state == 2) { return; } var targetCid = target.Cid; var (success, resultMessage, updatedRoom) = _chessService.AdminKickPlayer(message.RoomId, message.UserId); if (!success || updatedRoom == null) { return; } await _chessService.UpdateUserGameType(message.UserId, 0); if (!string.IsNullOrWhiteSpace(targetCid)) { await _hubContext.Clients.Client(targetCid).SendAsync("KickedFromRoom", new { gameId = message.RoomId, userId = message.UserId, reason = string.IsNullOrWhiteSpace(message.Reason) ? "被管理员移出房间" : message.Reason, source = "admin" }); await _hubContext.Groups.RemoveFromGroupAsync(targetCid, message.RoomId); await _hubContext.Groups.AddToGroupAsync(targetCid, ChessGameHub.LobbyGroup); } await _hubContext.Clients.Group(message.RoomId).SendAsync("RoomStateNotice", updatedRoom); await _hubContext.Clients.Group(message.RoomId).SendAsync("BeginGame", updatedRoom); await _hubContext.Clients.Group(ChessGameHub.LobbyGroup).SendAsync("ExitRoomNotice", updatedRoom); } }