/// <summary>
        /// Uses network discovery to find servers
        /// </summary>
        private void DiscoverServers()
        {
            // Discover on default port
            Console.WriteLine("Discovering Servers...");
            client.DiscoverLocalPeers(Program.DefaultPort);

            // Check for answers after x seconds
            Thread.Sleep(2000);
            System.Console.WriteLine($"Found {gameServers.Count} server(s)");

            // Ask user if to continue to discover on custom port
            do
            {
                // Get input
                string s = ConsoleManager.WaitGetPriorityInput("Input server port to continue discovering or leave empty to stop");
                if (s.Length > 0)
                {
                    // Parse input
                    if (int.TryParse(s, out int p))
                    {
                        Console.WriteLine("Discovering Servers...");
                        client.DiscoverLocalPeers(p);
                        Thread.Sleep(1200);
                        System.Console.WriteLine($"Found {gameServers.Count} server(s)");
                    }
                    else
                    {
                        System.Console.WriteLine("input is not a valid port");
                    }
                }
                else
                {
                    break; // If input is empty stop discovery
                }
            } while (true);
        }
Esempio n. 2
0
        public static Grid GetGameSettingsInput() // TODO: Move to Core/ConsoleManager
        {
            int maxPlayers;
            int minPlayers;
            int gridSizeX;
            int gridSizeY;

            // Get grid size X
            while (true)
            {
                string input = ConsoleManager.WaitGetPriorityInput("Input grid size for the X axis: ");
                if (!int.TryParse(input, out int result))
                {
                    Console.WriteLine("Input is not an integer, try again.", System.Drawing.Color.Red);
                    continue;
                }

                if (result < minLineLength || result > 2048) // TODO: make max size based on screen resolution
                {
                    Console.WriteLine($"Input must be in range {minLineLength} to 2048", System.Drawing.Color.Red);
                    continue;
                }

                gridSizeX = result;
                break;
            }

            // Get grid size Y
            while (true)
            {
                string input = ConsoleManager.WaitGetPriorityInput("Input grid size for the Y axis: ");
                if (!int.TryParse(input, out int result))
                {
                    Console.WriteLine("Input is not an integer, try again.", System.Drawing.Color.Red);
                    continue;
                }

                if (result < minLineLength || result > 2048) // TODO: make max size based on screen resolution
                {
                    Console.WriteLine($"Input must be in range {minLineLength} to 2048", System.Drawing.Color.Red);
                    continue;
                }

                gridSizeY = result;
                break;
            }

            // Get max players Q: move max and min players out of Grid?
            while (true)
            {
                string input = ConsoleManager.WaitGetPriorityInput("Input maximum amount of players: ");
                if (!int.TryParse(input, out int result))
                {
                    Console.WriteLine("Input is not an integer, try again.", System.Drawing.Color.Red);
                    continue;
                }

                if (result < 1 || result > MaxPlayers)
                {
                    Console.WriteLine($"Input must be in range 1 to {MaxPlayers}", System.Drawing.Color.Red);
                    continue;
                }

                maxPlayers = result;
                break;
            }

            // Get min players
            while (true)
            {
                string input = ConsoleManager.WaitGetPriorityInput("Input minimum amount of players: ");
                if (!int.TryParse(input, out int result))
                {
                    Console.WriteLine("Input is not an integer, try again.", System.Drawing.Color.Red);
                    continue;
                }

                if (result < 1 || result > MaxPlayers)
                {
                    Console.WriteLine($"Input must be in range 1 to {MaxPlayers}", System.Drawing.Color.Red);
                    continue;
                }

                minPlayers = result;
                break;
            }

            return(new Grid(gridSizeX, gridSizeY, maxPlayers, minPlayers));
        }
Esempio n. 3
0
        /// <summary>
        ///     Gets input for a player
        /// </summary>
        /// <param name="player"></param>
        public static Player GetPlayerSettingsInput()
        {
            // Get name
            string    name          = null;
            const int maxNameLength = 12;

            while (true)
            {
                Console.Write("Input name:  ", System.Drawing.Color.White);
                name = ConsoleManager.GetPriorityInput();
                if (name.Length > maxNameLength)
                {
                    Console.WriteLine($"That name is too long, max length is {maxNameLength}", System.Drawing.Color.Red);
                    continue;
                }

                if (name.Length < 1)
                {
                    Console.WriteLine("Name can not be empty", System.Drawing.Color.Red);
                    continue;
                }

                if (name == "empty")
                {
                    System.Console.WriteLine("i guess it can be empty...");
                }
                break;
            }

            // Get shape
            PlayerShape shape;

            while (true)
            {
                // Write available shapes
                Console.WriteLine("Available shapes: ", System.Drawing.Color.White);
                foreach (string s in Enum.GetNames(typeof(PlayerShape)))
                {
                    Console.WriteLine(s, System.Drawing.Color.White);
                }
                Console.Write("Input shape: ", System.Drawing.Color.White);
                string str = ConsoleManager.GetPriorityInput();
                if (int.TryParse(str, out int shapeInt))
                {
                    if (shapeInt < 1 || shapeInt > Enum.GetNames(typeof(PlayerShape)).Length) // TODO: Separate
                    {
                        Console.WriteLine("incorrect input, try again.", System.Drawing.Color.Red);
                        continue;
                    }

                    shape = (PlayerShape)shapeInt - 1;
                }
                else
                {
                    if (!Enum.TryParse(str, true, out shape))
                    {
                        Console.WriteLine("incorrect input, try again.", System.Drawing.Color.Red);
                        continue;
                    }
                }

                break;
            }

            // Get drawingColor
            PlayerColor color;

            while (true)
            {
                Console.Write("Colors: ", System.Drawing.Color.White);
                string[] cs = Enum.GetNames(typeof(PlayerColor));
                for (int i = 0; i < cs.Length; i++)
                {
                    Console.Write($"{cs[i]} ", PlayerColors.Colors[i].ToSystemColor());
                }
                if (!Enum.TryParse(ConsoleManager.WaitGetPriorityInput("\nInput color: ", false), true, out color))
                {
                    Console.WriteLine("incorrect input, try again.", System.Drawing.Color.Red);
                    continue;
                }

                break;
            }

            Console.WriteLine($"Created player {name} using {color} {shape}.", PlayerColors.Colors[(int)color].ToSystemColor());
            return(new Player(name, shape, color));
        }