Exemplo n.º 1
0
            protected override async Task Handle(TcpRequest <ServerStatus> request, CancellationToken cancellationToken)
            {
                var msg         = request.Message;
                var serverId    = new PolychatServerIdString(msg.ServerId);
                var sanitisedId = serverId.ToSanitisedUppercase();
                var server      = _polychatService.GetOnlineServerOrDefault(sanitisedId);

                if (server is not null)
                {
                    await _polychatService.ForwardMessage(sanitisedId, msg);

                    if (
                        msg.Status is
                        ServerStatus.Types.ServerStatusEnum.Stopped or ServerStatus.Types.ServerStatusEnum.Crashed
                        )
                    {
                        _polychatService.RemoveOnlineServer(sanitisedId);
                        _logger.LogInformation("Removed server {id} from the list of online servers.", sanitisedId);
                        request.ConnectedClient.StopListening();
                    }

                    var getChatChannelResult =
                        await _channelApi.GetChannelAsync(new(_polychatSettings.ChatChannelId), cancellationToken);

                    if (!getChatChannelResult.IsSuccess)
                    {
                        throw new Exception(getChatChannelResult.Error.Message);
                    }

                    var message           = $"Server {msg.Status.ToString().ToLowerInvariant()}.";
                    var chatMessage       = new PolychatChatMessageString(sanitisedId, message);
                    var sendMessageResult =
                        await _channelApi.CreateMessageAsync(new(_polychatSettings.ChatChannelId),
                                                             chatMessage.ToDiscordFormattedString(), ct : cancellationToken);

                    if (!sendMessageResult.IsSuccess)
                    {
                        throw new Exception(getChatChannelResult.Error?.Message ??
                                            "Could not forward message to Discord.");
                    }

                    _logger.LogInformation("Server {id} changed status to {newStatus}", sanitisedId,
                                           msg.Status);
                }
                else
                {
                    if (msg.Status == ServerStatus.Types.ServerStatusEnum.Started)
                    {
                        _logger.LogWarning(
                            "Server {id} has unexpectedly sent ServerStatus message before sending ServerInfo message.",
                            sanitisedId);
                    }
                    else
                    {
                        throw new KeyNotFoundException($"Could not find server {sanitisedId} in the list of online servers");
                    }
                }
            }
Exemplo n.º 2
0
            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.");
                }
            }
Exemplo n.º 3
0
            protected override async Task Handle(TcpRequest <ChatMessage> request, CancellationToken cancellationToken)
            {
                var protoMessage = request.Message;
                var id           = new PolychatServerIdString(protoMessage.ServerId);
                var sanitisedId  = id.ToSanitisedUppercase();
                await _polychatService.ForwardMessage(sanitisedId, protoMessage);

                var getChatChannelResult =
                    await _channelApi.GetChannelAsync(new(_polychatSettings.ChatChannelId), cancellationToken);

                if (!getChatChannelResult.IsSuccess)
                {
                    throw new Exception(getChatChannelResult.Error.Message);
                }

                var messageTextContent    = protoMessage.Message;
                var sanitisedString       = new PolychatChatMessageString(messageTextContent).ToSanitisedString();
                var discordMessageInfo    = sanitisedString[..protoMessage.MessageOffset];