Exemple #1
0
        private void ReceivedDataFromSLServer(object sender, SimpleTcp.DataReceivedEventArgs ev)
        {
            string jsonStr = Encoding.UTF8.GetString(ev.Data);

            if (!jsonStr.TryDeserializeJson(out DataBase dataBase))
            {
                return;
            }

            //  If Bot & SL Server are on the same machine, make the identifier / key the localhost variant
            //  Why?
            // - The user shall only have to enter 127.0.0.1 in the config instead of the possibly complicated public IPv4
            // - One less headache to worry about when you have the bot and the SL Server on the same machine
            //   while also having a dynamic ip - You don't have to re-type the IP every changing interval
            string ipAddress = dataBase.SameMachine ? $"127.0.0.1:{dataBase.SLFullAddress.Split(':')[1]}" : dataBase.SLFullAddress;

            if (Bot.BotConfig.DebugMode)
            {
                Console.WriteLine($"Received the following (From same machine? {dataBase.SameMachine} | {dataBase.SLFullAddress}): >>{jsonStr}<<{Environment.NewLine}----------------------");
            }

            switch (dataBase.MessageType)
            {
            case MessageType.Event:
            {
                if (jsonStr.TryDeserializeJson(out PlayerJoinLeave joinLeave))
                {
                    if (joinLeave.Identifier == "join")
                    {
                        Console.WriteLine($"{joinLeave.Player.Nickname} joined {joinLeave.SLFullAddress}!");

                        var embedQueueElement = _embedQueues.Find(_ => _.PlayerJoinedQueue.ContainsKey(ipAddress));
                        if (embedQueueElement == null)
                        {
                            _logger.Warning($"ReceivedDataFromSLServer: Received join data from unconfigured SL Server: {dataBase.SLFullAddress}");
                            return;
                        }
                        Queue <PlayerJoinLeave> joinQueue = embedQueueElement.PlayerJoinedQueue[ipAddress];
                        if (joinQueue == null)
                        {
                            _logger.Warning($"ReceivedDataFromSLServer: A configured SL Server has a null-queue");
                            return;
                        }
                        joinQueue.Enqueue(joinLeave);
                    }
                    else if (joinLeave.Identifier == "leave")
                    {
                        Debug.WriteLine($"{joinLeave.Player.Nickname} left {joinLeave.SLFullAddress}!");
                        var embedQueueElement = _embedQueues.Find(_ => _.PlayerLeftQueue.ContainsKey(ipAddress));
                        if (embedQueueElement == null)
                        {
                            _logger.Warning($"ReceivedDataFromSLServer: Received leave data from unconfigured SL Server");
                            return;
                        }
                        Queue <PlayerJoinLeave> joinQueue = embedQueueElement.PlayerLeftQueue[ipAddress];
                        if (joinQueue == null)
                        {
                            _logger.Warning($"ReceivedDataFromSLServer: A configured SL Server has a null-queue");
                            return;
                        }
                        joinQueue.Enqueue(joinLeave);
                    }
                }
                else if (jsonStr.TryDeserializeJson(out RoundEnd roundEnd))
                {
                    Debug.WriteLine($"Round ended for {roundEnd.SLFullAddress}!");

                    var embedQueueElement = _embedQueues.Find(_ => _.RoundEndQueue.ContainsKey(ipAddress));
                    if (embedQueueElement == null)
                    {
                        _logger.Warning($"ReceivedDataFromSLServer: Received round end data from unconfigured SL Server: {dataBase.SLFullAddress}");
                        return;
                    }

                    Queue <RoundEnd> roundEndQueue = embedQueueElement.RoundEndQueue[ipAddress];
                    if (roundEndQueue == null)
                    {
                        _logger.Warning($"ReceivedDataFromSLServer: A configured SL Server has a null-queue");
                        return;
                    }

                    roundEndQueue.Enqueue(roundEnd);
                }
                else if (jsonStr.TryDeserializeJson(out PlayerDeath playerDeath))
                {
                    Debug.WriteLine($"Player Death for {playerDeath.SLFullAddress}!");

                    var embedQueueElement = _embedQueues.Find(_ => _.PlayerDeathQueue.ContainsKey(ipAddress));
                    if (embedQueueElement == null)
                    {
                        _logger.Warning($"ReceivedDataFromSLServer: Received join data from unconfigured SL Server: {dataBase.SLFullAddress}");
                        return;
                    }

                    Queue <PlayerDeath> deathQueue = embedQueueElement.PlayerDeathQueue[ipAddress];
                    if (deathQueue == null)
                    {
                        _logger.Warning($"ReceivedDataFromSLServer: A configured SL Server has a null-queue");
                        return;
                    }

                    deathQueue.Enqueue(playerDeath);
                }
                else if (jsonStr.TryDeserializeJson(out PlayerBan playerBan))
                {
                    Debug.WriteLine($"Player Death for {playerBan.SLFullAddress}!");

                    var embedQueueElement = _embedQueues.Find(_ => _.PlayerBanQueue.ContainsKey(ipAddress));
                    if (embedQueueElement == null)
                    {
                        _logger.Warning($"ReceivedDataFromSLServer: Received join data from unconfigured SL Server: {dataBase.SLFullAddress}");
                        return;
                    }

                    Queue <PlayerBan> banQueue = embedQueueElement.PlayerBanQueue[ipAddress];
                    if (banQueue == null)
                    {
                        _logger.Warning($"ReceivedDataFromSLServer: A configured SL Server has a null-queue");
                        return;
                    }

                    banQueue.Enqueue(playerBan);
                }

                break;
            }

            case MessageType.Query:
            {
                if (jsonStr.TryDeserializeJson(out Ping ping))
                {
                    ping.Received = DateTime.Now;
                    _tcpServer.SendAsJson(ev.IpPort, ping);
                }

                break;
            }

            case MessageType.Response:
            {
                if (jsonStr.TryDeserializeJson(out Response response))
                {
                    switch (response.QueryType)
                    {
                    case QueryType.PlayerCount:
                    {
                        break;
                    }

                    case QueryType.ServerFps:
                    {
                        break;
                    }
                    }
                }

                break;
            }
            }
        }