Пример #1
0
        public async Task UpdatePlayerCounts()
        {
            List <string> addresses = config.GetAddresses();

            if (addresses.Count == 0)
            {
                if (config._isDebug)
                {
                    Console.WriteLine("No Addresses to request data.");
                }
            }

            if (config._isDebug)
            {
                Console.WriteLine("Printing addresses:");
                addresses.ToList().ForEach(i => Console.WriteLine(i));
            }

            foreach (string address in addresses)
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create($"https://api.steampowered.com/IGameServersService/GetServerList/v1/?key={config._steamAPIKey}&filter=\\addr\\{address}&limit=10");

                if (config._isDebug)
                {
                    Console.WriteLine("Response Received");
                }

                string responseDataStr = string.Empty;

                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    using (Stream stream = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(stream))
                        {
                            responseDataStr = reader.ReadToEnd();
                        }
                    }
                }

                if (responseDataStr != string.Empty)
                {
                    SteamServerListResponse responseObject = JsonConvert.DeserializeObject <SteamServerListResponse>(responseDataStr);

                    if (responseObject != null)
                    {
                        if (responseData.ContainsKey(address))
                        {
                            responseData.Remove(address);
                        }

                        if (config._isDebug)
                        {
                            Console.WriteLine("Adding Address Response Data for Address: " + address);
                        }

                        responseData.Add(address, responseObject);
                    }
                }
            }

            if (config._isDebug)
            {
                Console.WriteLine("Response Data Size: " + responseData.Count);
                responseData.ToList().ForEach(i => Console.WriteLine("Key: " + i.Key + ", Value: " + i.Value));
            }

            foreach (KeyValuePair <string, DiscordSocketClient> entry in serverBots)
            {
                string serverAddress = entry.Key.Split(":")[0];
                string serverPort    = entry.Key.Split(":")[1];


                if (config._isDebug)
                {
                    Console.WriteLine("Updating bot that is watching address: " + serverAddress + ":" + serverPort);
                }

                if (responseData.ContainsKey(serverAddress))
                {
                    SteamServerListResponse responseList = responseData[serverAddress];

                    if (responseList != null)
                    {
                        SteamApiResponseData data = responseList.GetServerDataByPort(serverPort);

                        if (data != null)
                        {
                            string playersInQueue = data.GetQueueCount();

                            DiscordSocketClient client = entry.Value;

                            if (client != null)
                            {
                                string gameStatus = $"{data.players}/{data.max_players}";
                                string queueCount = data.GetQueueCount();
                                if (queueCount != string.Empty && queueCount != "0")
                                {
                                    gameStatus += $" - {queueCount} In Queue";
                                }


                                if (config._isDebug)
                                {
                                    Console.WriteLine("Changed Status of : " + serverAddress + ", Status: " + gameStatus);
                                }

                                await client.SetGameAsync(gameStatus);
                            }
                        }
                    }
                }
            }
        }
        public async Task UpdatePlayerCounts()
        {
            List <string> addresses = config.GetAddresses();

            if (addresses.Count == 0)
            {
                if (config._isDebug)
                {
                    Console.WriteLine("No Addresses to request data.");
                }
            }

            if (config._isDebug)
            {
                Console.WriteLine("Printing addresses:");
                addresses.ToList().ForEach(i => Console.WriteLine(i));
            }

            foreach (string address in addresses)
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create($"https://api.steampowered.com/IGameServersService/GetServerList/v1/?key={config._steamAPIKey}&filter=\\addr\\{address}");

                if (config._isDebug)
                {
                    Console.WriteLine("Response Received");
                }

                string responseDataStr = string.Empty;

                try
                {
                    using (HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync())
                    {
                        using (Stream stream = response.GetResponseStream())
                        {
                            using (StreamReader reader = new StreamReader(stream))
                            {
                                responseDataStr = await reader.ReadToEndAsync();
                            }
                        }
                    }
                }catch (WebException ex)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine($"[WebException]:: {ex.Message}");
                    Console.WriteLine($"[Discord-PlayerCount-Bot]:: Update failed, steam could not be reached. Waiting 30 seconds before continuing.");
                    Console.ForegroundColor = ConsoleColor.White;


                    await Task.Delay(TimeSpan.FromSeconds(30));

                    return;
                }

                if (responseDataStr != string.Empty)
                {
                    SteamServerListResponse responseObject = JsonConvert.DeserializeObject <SteamServerListResponse>(responseDataStr);

                    if (responseObject != null)
                    {
                        if (responseData.ContainsKey(address))
                        {
                            responseData.Remove(address);
                        }

                        if (config._isDebug)
                        {
                            Console.WriteLine("Adding Address Response Data for Address: " + address);
                        }

                        responseData.Add(address, responseObject);
                    }
                }
            }

            if (config._isDebug)
            {
                Console.WriteLine("Response Data Size: " + responseData.Count);
            }

            foreach (KeyValuePair <string, DiscordSocketClient> entry in serverBots)
            {
                string serverAddress = entry.Key.Split(":")[0];
                string serverPort    = entry.Key.Split(":")[1];


                if (config._isDebug)
                {
                    Console.WriteLine("Updating bot that is watching address: " + serverAddress + ":" + serverPort);
                }

                if (responseData.ContainsKey(serverAddress))
                {
                    SteamServerListResponse responseList = responseData[serverAddress];

                    if (responseList != null)
                    {
                        SteamApiResponseData data = responseList.GetServerDataByPort(serverPort);

                        if (data != null)
                        {
                            string playersInQueue = data.GetQueueCount();

                            DiscordSocketClient client = entry.Value;

                            if (client != null)
                            {
                                string gameStatus = "";

                                if (config._userConfigNameAsLabel)
                                {
                                    if (configs.ContainsKey(entry.Key))
                                    {
                                        DayZServerBot botConfig = configs[entry.Key];

                                        if (botConfig != null)
                                        {
                                            gameStatus += $"{botConfig.botName} ";
                                        }
                                    }
                                }

                                gameStatus += $"{data.players}/{data.max_players} ";
                                string queueCount = data.GetQueueCount();
                                if (queueCount != string.Empty && queueCount != "0")
                                {
                                    gameStatus += $"Q: {queueCount}";
                                }


                                if (config._isDebug)
                                {
                                    Console.WriteLine("Changed Status of : " + serverAddress + ", Status: " + gameStatus);
                                }

                                await client.SetGameAsync(gameStatus);
                            }
                        }
                    }
                }
            }
        }