Example #1
0
 private void StartServerButton_Click(object sender, EventArgs e)
 {
     if (UserNameTextBox.Text != "")
     {
         server.StartServer(UserNameTextBox.Text);
         IPAddressOutputTextBox.Text = server.GetIPAddress().ToString() + ": " + port;
         Debug.WriteLine(IPAddressOutputTextBox.Text);
     }
     else
     {
         MessageBox.Show("Enter a username", "Error", MessageBoxButtons.OK);
     }
 }
Example #2
0
 private bool StartServer(string username)
 {
     server = new ServerNew(gameState);
     server.PlayersChanged += GameState_PlayersChanged;
     if (server.Initialize(username))
     {
         //Server started successfully, begin accepting connections
         server.StartAccept();
         IPAddress ip = server.GetIPAddress();
         clientState = new ClientState(username);
         client      = new ClientNew(ip, port, username, clientState);
         client.UserCardsReceived        += Client_UserCardsReceived;
         client.CurrentCardReceived      += Client_CurrentCardReceived;
         client.OtherPlayerCardsReceived += Client_OtherPlayerCardsReceived;
         client.OtherPlayerNamesReceived += Client_OtherPlayerNamesReceived;
         client.Start();
         return(true);
     }
     else
     {
         return(false);
     }
 }
Example #3
0
        private void StartGameJoinButton_Click(object sender, EventArgs e)
        {
            //Same for both hosting and joining game
            if (JoinGameButton.Visible && !HostGameButton.Visible)
            {
                //User wants to Host game
                Debug.WriteLine("Trying to Host game");

                string userName = UserNameTextBox.Text;

                if (CheckUserName(userName))
                {
                    Debug.WriteLine("Username is valid");
                    //disable connect button to prevent further tries
                    StartGameJoinButton.Visible = false;
                    InfoLabel.Text = "Starting server, waiting for players...";
                    //Starts server and also connects to the server
                    if (StartServer(userName))
                    {
                        InfoLabel.Text        = "Successfully connected to server!";
                        IPAddressTextBox.Text = server.GetIPAddress().ToString().Split(':')[0];
                    }
                }
                else
                {
                    Debug.WriteLine("Invalid username");
                    MessageBox.Show("Special Characters not allowed, try again", "Error", MessageBoxButtons.OK);
                }
            }
            else
            {
                //User wants to join game
                Debug.WriteLine("Trying to Join game");
                string userName = UserNameTextBox.Text;
                if (CheckUserName(userName))
                {
                    Debug.WriteLine("Username is valid");
                    //disable connect button to prevent further tries
                    StartGameJoinButton.Visible = false;
                    InfoLabel.Text = "Trying to connect...";
                    //Connects to server
                    if (!IPAddress.TryParse(IPAddressTextBox.Text, out IPAddress ipAddress))
                    {
                        MessageBox.Show("IP Address entered incorrectly", "Error");
                        StartGameJoinButton.Visible = true;
                    }
                    else
                    {
                        if (JoinServer(userName, ipAddress))
                        {
                            InfoLabel.Text = "Client successfully connected, waiting for information";
                        }
                        else
                        {
                            InfoLabel.Text = "Server does not exist, please try again!";
                        }
                    }
                }
                else
                {
                    Debug.WriteLine("Invalid username");
                    MessageBox.Show("Special Characters not allowed, try again", "Error", MessageBoxButtons.OK);
                }
            }
        }