Exemple #1
0
        private void join_btn_Click(object sender, EventArgs e)
        {
            NetworkStream clientStream = this._tcpClient.GetStream();
            int           bytesRead    = 0;

            byte[] bufferIn = new byte[8];
            int    index = this.rooms_names_listBox.SelectedIndex, int_room_id = 0, temp_length = 0;
            string room_name = "", message = "", str_room_id = "", number_of_questions = "", time_for_question = "";

            if (index != -1)
            {
                room_name   = this.rooms_names_listBox.SelectedItem.ToString();
                int_room_id = this._ids_of_rooms[index];
                temp_length = 4 - int_room_id.ToString().Length;
                for (int i = 0; i < temp_length; i++)
                {
                    str_room_id += "0";
                }
                str_room_id += int_room_id.ToString();
                try
                {
                    byte[] buffer = new ASCIIEncoding().GetBytes("209" + str_room_id);
                    clientStream.Write(buffer, 0, buffer.Length);
                    clientStream.Flush();
                    bytesRead = clientStream.Read(bufferIn, 0, 8);
                    if (bytesRead == 0)
                    {
                        //the client has disconnected from the server
                        this._tcpClient.Close();
                    }
                    message = new ASCIIEncoding().GetString(bufferIn);
                }
                catch { }
                if (message.Substring(0, 4).Equals("1100"))
                {
                    number_of_questions = int.Parse(message.Substring(4, 2)).ToString();
                    time_for_question   = int.Parse(message.Substring(6, 2)).ToString();
                    try
                    {
                        byte[] buffer = new ASCIIEncoding().GetBytes("207" + str_room_id);
                        clientStream.Write(buffer, 0, buffer.Length);
                        clientStream.Flush();
                    }
                    catch { }
                    this._write_into_players_listBox.Abort();
                    this.Hide();
                    BeforeBeginGame bbg = new BeforeBeginGame(this._tcpClient, false, "", room_name, str_room_id, "", number_of_questions, time_for_question);
                    bbg.ShowDialog();
                    this.Close();
                }

                else
                {
                    if (message.Substring(0, 4).Equals("1101"))
                    {
                        this.problem_with_joining_label.Text = "room is full";
                    }

                    else
                    {
                        this.problem_with_joining_label.Text = "game has started/room has closed etc...";
                    }
                    this.problem_with_joining_label.Show();
                }
            }

            else
            {
                if (this.rooms_names_listBox.Items.Count > 0)
                {
                    this.problem_with_joining_label.Text = "pick a room";
                    this.problem_with_joining_label.Show();
                }
            }
        }
Exemple #2
0
        private void submit_btn_Click(object sender, EventArgs e)
        {
            NetworkStream clientStream = this._tcpClient.GetStream();
            int           bytesRead    = 0;

            byte[] bufferIn = new byte[109];
            string message = "";
            string number_of_players = "", number_of_questions = "", time_for_question = "";
            int    room_length = this.room_name_textBox.Text.Length;

            if (room_length == 0 || room_length > 99)
            {
                this.problem_with_creation_label.Text = "wrong room name";
                this.problem_with_creation_label.Show();
            }

            else
            {
                number_of_players = this.number_of_players_textBox.Text;
                if (number_of_players.Equals("") || number_of_players.Equals("0") || number_of_players.Length > 1)
                {
                    this.problem_with_creation_label.Text = "wrong number of players";
                    this.problem_with_creation_label.Show();
                }

                else
                {
                    number_of_questions = this.number_of_questions_textBox.Text;
                    if (number_of_questions.Equals("") || number_of_questions.Length > 2)
                    {
                        this.problem_with_creation_label.Text = "wrong number of questions";
                        this.problem_with_creation_label.Show();
                    }

                    else
                    {
                        time_for_question = this.time_for_question_textBox.Text;
                        if (time_for_question.Equals("") || time_for_question.Equals("0") || time_for_question.Length > 2)
                        {
                            this.problem_with_creation_label.Text = "wrong time for question";
                            this.problem_with_creation_label.Show();
                        }

                        else
                        {
                            this.problem_with_creation_label.Text = "";
                            this.problem_with_creation_label.Hide();
                        }
                    }
                }
            }

            if (this.problem_with_creation_label.Text.Length == 0)
            {
                try
                {
                    message = "213" + LengthForSend(this.room_name_textBox.Text) + this.room_name_textBox.Text
                              + number_of_players;
                    if (number_of_questions.Length < 2)
                    {
                        number_of_questions = "0" + number_of_questions;
                    }
                    message += number_of_questions;
                    if (time_for_question.Length < 2)
                    {
                        time_for_question = "0" + time_for_question;
                    }
                    message += time_for_question;
                    byte[] buffer = new ASCIIEncoding().GetBytes(message);
                    clientStream.Write(buffer, 0, buffer.Length);
                    clientStream.Flush();
                    bytesRead = clientStream.Read(bufferIn, 0, 4);
                    if (bytesRead == 0)
                    {
                        //the client has disconnected from the server
                        this._tcpClient.Close();
                    }
                    message = new ASCIIEncoding().GetString(bufferIn);
                }
                catch { }
            }
            if (message.Substring(0, 4).Equals("1140"))
            {
                this.Hide();
                BeforeBeginGame bbg = new BeforeBeginGame(this._tcpClient, true, this._username, this.room_name_textBox.Text, "", this.number_of_players_textBox.Text, this.number_of_questions_textBox.Text, this.time_for_question_textBox.Text);
                bbg.ShowDialog();
                this.Close();
            }
        }