Exemple #1
0
 private void BTN_Join_Click(object sender, EventArgs e)
 {
     if (CMB_Rooms.Text == "")
     {
         MessageBox.Show("You haven't selected a room.");
     }
     else
     {
         var room = CMB_Rooms.Text;
         roomId = room.Substring(0, 4);
         var roomName = CMB_Rooms.Text.Substring(5);
         client.SendMessage(Protocol.ROOM_JOIN_REQUEST + roomId);
         var msg = client.GetMessage();
         if (msg == "1101")
         {
             LBL_FlavorText.Text = "Room is full";
             return;
         }
         else if (msg == "1102")
         {
             LBL_FlavorText.Text = "Room doesn't exist or other reason";
             return;
         }
         else
         {
             msg = msg.Substring(4);
             var questionNumber = Convert.ToInt32(msg.Substring(0, 2));
             msg = msg.Substring(2);
             var questionTime = Convert.ToInt32(msg.Substring(0, 2));
             this.Hide();
             var waitForGame = new WaitForGameForm(client, false, roomName, 0, questionNumber, questionTime, uname);
             waitForGame.ShowDialog();
             if (waitForGame.gameStart)
             {
                 var gameForm = new GameForm(client, waitForGame.msg, roomName, waitForGame.qstNum, waitForGame.qstTime);
                 gameForm.ShowDialog();
             }
             this.Close();
         }
     }
 }
Exemple #2
0
        private void BTN_CreateRoom_Click(object sender, EventArgs e)
        {
            var roomName = TXT_RoomName.Text;

            if (roomName == "Enter room name here")
            {
                MessageBox.Show("Please enter room name.");
                return;
            }
            int numOfPlayers;

            if (TXT_NumOfPlayers.Text == "" || !Int32.TryParse(TXT_NumOfPlayers.Text, out numOfPlayers))
            {
                MessageBox.Show("Please enter number in number of players.");
                return;
            }
            if (numOfPlayers < 1 || numOfPlayers > 9)
            {
                MessageBox.Show("Please enter number between 1 to 9 in number of players.");
                return;
            }
            int numOfQuestions;

            if (TXT_NumOfQuestions.Text == "" || !Int32.TryParse(TXT_NumOfQuestions.Text, out numOfQuestions))
            {
                MessageBox.Show("Please enter number in number of questions.");
                return;
            }
            if (numOfQuestions < 1 || numOfQuestions > 99)
            {
                MessageBox.Show("Please enter number between 1 to 99 in number of questions.");
                return;
            }
            int timeToQuestion;

            if (TXT_TimeToQuestion.Text == "" || !Int32.TryParse(TXT_TimeToQuestion.Text, out timeToQuestion))
            {
                MessageBox.Show("Please enter number in time to question.");
                return;
            }
            if (timeToQuestion < 1 || timeToQuestion > 99)
            {
                MessageBox.Show("Please enter number between 1 to 99 in time to question.");
                return;
            }
            if (numOfPlayers >= 1 && numOfPlayers <= 9)
            {
                //message = "213##roomName playersNumber questionsNumber questionTimeInSec".
                var message = Protocol.ROOM_CREATE_REQUEST + Protocol.GetPaddedNumber(roomName.Length, 2) + roomName + Protocol.GetPaddedNumber(numOfPlayers, 1) + Protocol.GetPaddedNumber(numOfQuestions, 2) + Protocol.GetPaddedNumber(timeToQuestion, 2);
                client.SendMessage(message);
                var msg = client.GetMessage();
                switch (msg)
                {
                case Protocol.ROOM_CREATE_RESPONSE_SUCCESS:
                    this.Hide();
                    var waitForGame = new WaitForGameForm(client, true, roomName, numOfPlayers, numOfQuestions, timeToQuestion, uname);
                    waitForGame.ShowDialog();
                    if (waitForGame.gameStart)
                    {
                        var gameForm = new GameForm(client, waitForGame.msg, roomName, waitForGame.qstNum, waitForGame.qstTime);
                        gameForm.ShowDialog();
                    }
                    this.Close();
                    break;

                case Protocol.ROOM_CREATE_RESPONSE_TOO_MANY_QUESTIONS:
                    MessageBox.Show("Not enough questions in the database.");
                    break;

                case Protocol.ROOM_CREATE_RESPONSE_FAIL:
                    MessageBox.Show("Create room failed.");
                    break;

                default:
                    MessageBox.Show($@"Unknown message recieved.
Message: {msg}");
                    break;
                }
            }
            else
            {
                MessageBox.Show("Num of players must be between 1 and 9.");
            }
        }