protected override async Task Handle(TcpRequest <ServerPlayerStatusChangedEvent> request, CancellationToken cancellationToken) { var serverId = new PolychatServerIdString(request.Message.NewPlayersOnline.ServerId); var sanitisedId = serverId.ToSanitisedUppercase(); var server = _polychatService.GetOnlineServerOrDefault(sanitisedId); if (server is null) { throw new KeyNotFoundException($"Could not find server {sanitisedId} in the list of online servers"); } var playersOnline = request.Message.NewPlayersOnline.PlayersOnline; var newPlayersList = request.Message.NewPlayersOnline.PlayerNames.ToList(); if (request.Message.NewPlayerStatus is ServerPlayerStatusChangedEvent.Types.PlayerStatus.Left && newPlayersList.Contains(request.Message.PlayerUsername) ) { newPlayersList.Remove(request.Message.PlayerUsername); playersOnline = playersOnline == 0 ? playersOnline : playersOnline - 1; } else if (request.Message.NewPlayerStatus is ServerPlayerStatusChangedEvent.Types.PlayerStatus.Joined && !newPlayersList.Contains(request.Message.PlayerUsername) ) { newPlayersList.Add(request.Message.PlayerUsername); playersOnline += 1; } server.PlayersOnline = playersOnline; server.OnlinePlayerNames = newPlayersList; _polychatService.AddOrUpdateOnlineServer(sanitisedId, server); await _polychatService.ForwardMessage(sanitisedId, request.Message); var messageStr = new PolychatChatMessageString( sanitisedId, $"{request.Message.PlayerUsername} has {request.Message.NewPlayerStatus.ToString().ToLower()} the game."); var getChatChannelResult = await _channelApi.GetChannelAsync(new(_polychatSettings.ChatChannelId), cancellationToken); if (!getChatChannelResult.IsSuccess) { throw new Exception(getChatChannelResult.Error.Message); } var sendMessageResult = await _channelApi.CreateMessageAsync( new(_polychatSettings.ChatChannelId), messageStr.ToDiscordFormattedString(), ct : cancellationToken); if (!sendMessageResult.IsSuccess) { throw new Exception(getChatChannelResult.Error?.Message ?? "Could not forward message to Discord."); } }
protected override void Handle(TcpRequest <ServerInfo> request) { var onlineServer = new OnlineServer(request.Message, request.ConnectedClient); var id = new PolychatServerIdString(request.Message.ServerId); var sanitisedId = id.ToSanitisedUppercase(); _logger.LogInformation("Adding server {id} to the list of online servers...", sanitisedId); _polychatService.AddOrUpdateOnlineServer(sanitisedId, onlineServer); _logger.LogInformation("Added server {id} to the list of online servers.", sanitisedId); }
protected override void Handle(TcpRequest <ServerPlayersOnline> request) { var serverId = new PolychatServerIdString(request.Message.ServerId); var sanitisedId = serverId.ToSanitisedUppercase(); var server = _polychatService.GetOnlineServerOrDefault(sanitisedId); if (server is null) { throw new KeyNotFoundException($"Could not find server {sanitisedId} in the list of online servers"); } server.PlayersOnline = request.Message.PlayersOnline; server.OnlinePlayerNames = request.Message.PlayerNames.ToList(); _polychatService.AddOrUpdateOnlineServer(sanitisedId, server); }