Beispiel #1
0
        //ROOMINTERFACE
        public async void listenToReplies()
        {
            bool   exists = true;
            string responseCode;

            do /*while (exists)*/
            {
                responseCode = await Task.Factory.StartNew(() => _client.myReceive(3));

                switch (responseCode)
                {
                case "108":
                    lblStatus.Content = "recieved user list.";
                    readUserList();
                    break;

                case "116":
                    lblStatus.Content = "Room Closed By Admin";
                    Close();
                    exists = false;
                    break;

                case "117":
                    //game begun
                    string code = await Task.Factory.StartNew(() => _client.myReceive(1));

                    if (code == "0")    //success
                    {
                        Hide();
                        Game s = new Game(_client, _timePerQuestion, _numberOfQuestions);
                        s.listenToReplies();
                        s.ShowDialog();

                        //after game ends, returns to main menu instead of roomInterface
                        exists = false;
                        Close();
                    }
                    else if (code == "1")
                    {
                        lblStatus.Content = "game creation failed because you are not admin";
                    }
                    else if (code == "2")
                    {
                        lblStatus.Content = "game creation failed because server doesn't have enough questions";
                    }
                    break;

                case "112":
                    exists = false;
                    Close();
                    break;

                default:
                    lblStatus.Content = "Error - wrong code detected";
                    break;
                }
            } while (exists);
        }
Beispiel #2
0
        private void usersList()
        {        //handle all recieved messages in form to avoid bugs and to update the users list in one place
            try
            {
                while (Program.notClosed)                //not to access after form closed and thread still running
                {
                    string input = Program.RecvMsg(sock);

                    this.reply = Program.StrSplit(input, '#');

                    this.Invoke((MethodInvoker) delegate
                    {
                        if (reply[0] == "108")
                        {
                            this.users.Items.Clear();

                            for (int i = 1; i < reply.Count(); i++)
                            {
                                this.users.Items.Add(reply[i]);
                            }
                        }
                        else
                        {
                            if (this.startOrLeave.Text == "Start Game")
                            {
                                if (reply[0] != "118" && reply[0] != "116")
                                {
                                    MessageBox.Show("Could not start room!");
                                }
                                else if (reply[0] == "116")
                                {
                                    this.Hide();
                                    //this.Close(); GOING TO CLOSE FORM OUTSIDE OF THE THREAD
                                    Program.notClosed = false;
                                }
                                else
                                {
                                    Game runningGame = new Game(sock, reply, qTime, qNum);
                                    this.Hide();
                                    runningGame.ShowDialog();
                                    this.Show();
                                    //this.Close(); GOING TO CLOSE FORM OUTSIDE OF THE THREAD
                                    Program.notClosed = false;
                                }
                            }
                            else
                            {
                                if (reply[0] == "1120")
                                {
                                    this.Hide();
                                    //this.Close(); GOING TO CLOSE FORM OUTSIDE OF THE THREAD
                                    Program.notClosed = false;
                                }
                                else if (reply[0] == "116")
                                {
                                    this.Hide();
                                    //this.Close(); GOING TO CLOSE FORM OUTSIDE OF THE THREAD
                                    Program.notClosed = false;
                                }
                                else if (reply[0] == "118")
                                {
                                    Game runningGame = new Game(sock, reply, qTime, qNum);
                                    this.Hide();
                                    runningGame.ShowDialog();
                                    this.Show();
                                    //this.Close(); GOING TO CLOSE FORM OUTSIDE OF THE THREAD
                                    Program.notClosed = false;
                                }
                                else
                                {
                                    MessageBox.Show("ERROR!!!\n\nRoom already started or closed");
                                }
                            }
                        }
                    });
                }
            }
            catch (ThreadAbortException e)
            {
                MessageBox.Show(e.Message);
            }
        }