Ejemplo n.º 1
0
        private void fetchResults()
        {
            byte[] message = new ASCIIEncoding().GetBytes("133");
            ClientSocket.client.Send(message);
            byte[] response  = new byte[1024];
            int    bytesRead = ClientSocket.client.Receive(response);

            Array.Resize <byte>(ref response, bytesRead);
            if (GlobalHelpers.GetCode(response) == "233")
            {
                var gameResults = MessagePack.MessagePackSerializer.Deserialize <JSON_Classes.GetGameResultsResponse>(GlobalHelpers.GetMsgpack(response));
                switch (gameResults.Status)
                {
                case 0:
                    if (!stopThread)
                    {
                        this.Invoke((MethodInvoker) delegate
                        {
                            for (int i = 1; i < ResultsPanel.RowCount && i < gameResults.Results.Length; i++)
                            {
                                ResultsPanel.GetControlFromPosition(0, i).Text = gameResults.Results[i - 1].Username;
                                ResultsPanel.GetControlFromPosition(1, i).Text = (gameResults.Results[i - 1].CorrectAnswers + gameResults.Results[i - 1].WrongAnswers).ToString();
                                ResultsPanel.GetControlFromPosition(2, i).Text = gameResults.Results[i - 1].CorrectAnswers.ToString();
                                ResultsPanel.GetControlFromPosition(3, i).Text = gameResults.Results[i - 1].WrongAnswers.ToString();
                                ResultsPanel.GetControlFromPosition(4, i).Text = gameResults.Results[i - 1].AverageTime.ToString();
                            }
                            for (int i = ResultsPanel.RowCount; i <= gameResults.Results.Length; i++)
                            {
                                AddItem(gameResults.Results[i - 1].Username, gameResults.Results[i - 1].CorrectAnswers + gameResults.Results[i - 1].WrongAnswers, gameResults.Results[i - 1].CorrectAnswers, gameResults.Results[i - 1].WrongAnswers, gameResults.Results[i - 1].AverageTime);
                            }
                        });
                    }
                    break;

                default:
                    if (!stopThread)
                    {
                        this.Invoke((MethodInvoker) delegate
                        {
                            MessageBox.Show("There Was An Error Getting The Game Results!");
                            this.Close();
                            DialogResult = DialogResult.Abort;
                        });
                    }
                    break;
                }
            }
        }
Ejemplo n.º 2
0
        private void JoinButton_Click(object sender, EventArgs e)
        {
            if (RoomListBox.SelectedIndex != -1)
            {
                var request = new JSON_Classes.JoinRoomRequest {
                    Id = rooms[RoomListBox.SelectedIndex].Metadata.id
                };
                byte[] message = GlobalHelpers.CreateMessage("115", request);
                ClientSocket.client.Send(message);
                byte[] response  = new byte[1024];
                int    bytesRead = ClientSocket.client.Receive(response);
                Array.Resize <byte>(ref response, bytesRead);
                if (GlobalHelpers.GetCode(response) == "215")
                {
                    var status = MessagePackSerializer.Deserialize <JSON_Classes.JoinRoomResponse>(GlobalHelpers.GetMsgpack(response));
                    switch (status.Status)
                    {
                    case 0:
                        string[] players = rooms[RoomListBox.SelectedIndex].Players;
                        Array.Resize(ref players, players.Length + 1);
                        players[players.Length - 1] = username;     // Insert new players into list
                        rooms[RoomListBox.SelectedIndex].Players = players;
                        var gameLobby = new LobbyScreenRegular(rooms[RoomListBox.SelectedIndex]);
                        this.Hide();
                        gameLobby.ShowDialog();
                        this.Close();
                        DialogResult = DialogResult.No;
                        break;

                    default:
                        MessageBox.Show("Could Not Join Lobby!");
                        break;
                    }
                }
                else
                {
                    MessageBox.Show("Could Not Join Lobby!");
                }
            }
        }
Ejemplo n.º 3
0
        private void CreateLobbyButton_Click(object sender, EventArgs e)
        {
            var form = new LobbyDetailsForm();

            this.Hide();
            form.ShowDialog();
            var request = new JSON_Classes.CreateRoomRequest {
                Name = form.Name, MaxUsers = form.MaxUsers, QuestionCount = form.QuestionCount, TimePerQuestion = form.TimePerQuestion
            };

            byte[] message = GlobalHelpers.CreateMessage("116", request);
            ClientSocket.client.Send(message);
            byte[] response  = new byte[1024];
            int    bytesRead = ClientSocket.client.Receive(response);

            Array.Resize <byte>(ref response, bytesRead);
            if (GlobalHelpers.GetCode(response) == "216")
            {
                var result   = MessagePack.MessagePackSerializer.Deserialize <JSON_Classes.CreateRoomResponse>(GlobalHelpers.GetMsgpack(response));
                var room     = new JSON_Classes.Room();
                var metadata = new JSON_Classes.RoomData();
                metadata.IsActive      = 0;
                metadata.id            = result.Id;
                metadata.name          = request.Name;
                metadata.MaxPlayers    = request.MaxUsers;
                metadata.QuestionCount = request.QuestionCount;
                metadata.AnswerTimeout = request.TimePerQuestion;
                room.Players           = new[] { username };
                room.Metadata          = metadata;
                var gameLobby = new LobbyScreenAdmin(room);
                this.Hide();
                gameLobby.ShowDialog();
                this.Show();
            }
            else
            {
                MessageBox.Show("Error Creating Lobby!");
                this.Close();
            }
        }
Ejemplo n.º 4
0
        private void updateLoop()
        {
            while (!stopThread)
            {
                Thread.Sleep(5000);
                byte[] message = new ASCIIEncoding().GetBytes("123");
                ClientSocket.client.Send(message);
                byte[] response  = new byte[1024];
                int    bytesRead = ClientSocket.client.Receive(response);
                Array.Resize <byte>(ref response, bytesRead);
                if (GlobalHelpers.GetCode(response) == "223")
                {
                    var roomState = MessagePack.MessagePackSerializer.Deserialize <JSON_Classes.GetRoomStateResponse>(GlobalHelpers.GetMsgpack(response));
                    switch (roomState.Status)
                    {
                    case 0:
                        if (!stopThread)
                        {
                            this.Invoke((MethodInvoker) delegate
                            {
                                this.PlayerListBox.DataSource = roomState.Room.Players;
                            });
                        }
                        break;

                    default:
                        if (!stopThread)
                        {
                            this.Invoke((MethodInvoker) delegate
                            {
                                MessageBox.Show("There Was An Error Getting The Room Info!");
                                this.Close();
                                DialogResult = DialogResult.Abort;
                            });
                        }
                        break;
                    }
                    switch (roomState.Room.Metadata.IsActive) // Game Status Check
                    {
                    case 0:                                   // Still In Lobby, Do Nothing
                        break;

                    case -1:     // Admin Closed Lobby, Leave
                        if (!stopThread)
                        {
                            this.Invoke((MethodInvoker) delegate
                            {
                                MessageBox.Show("Room Has Been Closed!");
                                this.Close();
                                DialogResult = DialogResult.Abort;
                            });
                            stopThread = true;
                        }
                        break;

                    default:     // Game Started
                        if (!stopThread)
                        {
                            this.Invoke((MethodInvoker) delegate
                            {
                                this.Hide();
                                var game = new GameQuestion(this.room.Metadata);
                                game.ShowDialog();
                                this.Close();
                            });
                            stopThread = true;
                        }
                        break;
                    }
                }
            }
        }
Ejemplo n.º 5
0
        private void MyStatsButton_Click(object sender, EventArgs e)
        {
            byte[] message = new ASCIIEncoding().GetBytes("117");
            ClientSocket.client.Send(message);
            byte[] response  = new byte[1024];
            int    bytesRead = ClientSocket.client.Receive(response);

            Array.Resize <byte>(ref response, bytesRead);
            if (GlobalHelpers.GetCode(response) == "217")
            {
                var result = MessagePack.MessagePackSerializer.Deserialize <JSON_Classes.GetStatsResponse>(GlobalHelpers.GetMsgpack(response));
                switch (result.Status)
                {
                case 0:
                    this.Hide();
                    var statsPage = new MyStats(result.Stats);
                    statsPage.ShowDialog();
                    this.Show();
                    break;

                default:
                    MessageBox.Show("Could Not Get Stats!");
                    break;
                }
            }
            else
            {
                MessageBox.Show("Could Not Get Stats!");
            }
        }
Ejemplo n.º 6
0
        private void BrowseLobbiesButton_Click(object sender, EventArgs e)
        {
            byte[] message = new ASCIIEncoding().GetBytes("112");
            ClientSocket.client.Send(message);
            byte[] response  = new byte[1024];
            int    bytesRead = ClientSocket.client.Receive(response);

            Array.Resize <byte>(ref response, bytesRead);
            if (GlobalHelpers.GetCode(response) == "212")
            {
                var lobbies = MessagePack.MessagePackSerializer.Deserialize <JSON_Classes.GetRoomsResponse>(GlobalHelpers.GetMsgpack(response));
                switch (lobbies.Status)
                {
                case 0:
                    var browser = new LobbyBrowser(lobbies.Rooms, username);
                    this.Hide();
                    var result = browser.ShowDialog();
                    if (result != DialogResult.No)
                    {
                        this.Close();
                    }
                    else
                    {
                        this.Show();
                    }
                    break;

                default:
                    MessageBox.Show("Error Getting Lobbies!");
                    break;
                }
            }
            else
            {
                this.Close();
                DialogResult = DialogResult.No;
            }
        }
Ejemplo n.º 7
0
        private void ViewHighscoresButton_Click(object sender, EventArgs e)
        {
            byte[] message = new ASCIIEncoding().GetBytes("114");
            ClientSocket.client.Send(message);
            byte[] response  = new byte[1024];
            int    bytesRead = ClientSocket.client.Receive(response);

            Array.Resize <byte>(ref response, bytesRead);
            if (GlobalHelpers.GetCode(response) == "214")
            {
                var result = MessagePack.MessagePackSerializer.Deserialize <JSON_Classes.GetHighscoreResponse>(GlobalHelpers.GetMsgpack(response));
                switch (result.Status)
                {
                case 0:
                    Dictionary <string, uint> highscores = new Dictionary <string, uint>();
                    for (int i = 0; i < result.Highscores.Length; i++)
                    {
                        highscores[result.Highscores[i].Key] = result.Highscores[i].Value;
                    }
                    var browser = new HighscoreViewer(highscores);
                    this.Hide();
                    browser.ShowDialog();
                    this.Show();
                    break;

                default:
                    MessageBox.Show("Could Not Get Highscores!");
                    break;
                }
            }
            else
            {
                MessageBox.Show("Could Not Get Highscores!");
            }
        }
Ejemplo n.º 8
0
        private void SubmitButton_Click(object sender, EventArgs e)
        {
            var signupRequest = new JSON_Classes.SignupRequest {
                Username = UsernameTextBox.Text, Password = PasswordTextBox.Text, Email = EmailTextBox.Text
            };

            byte[] message = GlobalHelpers.CreateMessage("102", signupRequest);
            if (!ClientSocket.client.Connected)
            {
                ClientSocket.client.Connect(ClientSocket.serverEndPoint);
            }
            ClientSocket.client.Send(message);
            byte[] response  = new byte[1024];
            int    bytesRead = ClientSocket.client.Receive(response);

            Array.Resize <byte>(ref response, bytesRead);
            if (GlobalHelpers.GetCode(response) == "202")
            {
                var signupResponse = MessagePack.MessagePackSerializer.Deserialize <JSON_Classes.SignupResponse>(GlobalHelpers.GetMsgpack(response));
                switch (signupResponse.Status)
                {
                case 0:
                    this.username = UsernameTextBox.Text;
                    this.Close();
                    DialogResult = DialogResult.OK;
                    break;

                case 1:
                    MessageBox.Show("An Issue Occured With The Database, Can't Sign up!");
                    this.Close();
                    DialogResult = DialogResult.No;
                    break;

                default:
                    MessageBox.Show("Could Not Signup.. Please Check Your Credentials And Try Again!");
                    break;
                }
            }
            else
            {
                this.Close();
                DialogResult = DialogResult.No;
            }
        }
Ejemplo n.º 9
0
        private void FetchQuestion()
        {
            byte[] message = new ASCIIEncoding().GetBytes("131");
            ClientSocket.client.Send(message);
            byte[] response  = new byte[1024];
            int    bytesRead = ClientSocket.client.Receive(response);

            Array.Resize <byte>(ref response, bytesRead);
            if (GlobalHelpers.GetCode(response) == "231")
            {
                var QuestionResponse = MessagePack.MessagePackSerializer.Deserialize <JSON_Classes.GetQuestionResponse>(GlobalHelpers.GetMsgpack(response));
                switch (QuestionResponse.Status)
                {
                case 0:
                    QuestionLabel.Text = QuestionResponse.Question.QuestionString;
                    for (int i = 0; i < buttons.Length; i++)
                    {
                        buttons[i].Text = QuestionResponse.Question.PossibleAnswers[i];
                    }
                    break;

                default:
                    MessageBox.Show("Couldn't Get Question!");
                    this.Close();
                    DialogResult = DialogResult.Abort;
                    break;
                }
            }
            else
            {
                MessageBox.Show("Couldn't Get Question!");
                this.Close();
                DialogResult = DialogResult.Abort;
            }
        }
Ejemplo n.º 10
0
        private string SubmitAnswer(string answer, uint timeElapsed)
        {
            JSON_Classes.SubmitAnswerRequest request = new JSON_Classes.SubmitAnswerRequest {
                Answer = answer, TimeUntilResponse = timeElapsed
            };
            byte[] message = GlobalHelpers.CreateMessage("132", request);
            ClientSocket.client.Send(message);
            byte[] response  = new byte[1024];
            int    bytesRead = ClientSocket.client.Receive(response);

            Array.Resize <byte>(ref response, bytesRead);
            if (GlobalHelpers.GetCode(response) == "232")
            {
                var submissionResult = MessagePack.MessagePackSerializer.Deserialize <JSON_Classes.SubmitAnswerResponse>(GlobalHelpers.GetMsgpack(response));
                switch (submissionResult.Status)
                {
                case 0:
                    return(submissionResult.CorrectAnswer);

                default:
                    MessageBox.Show("Could Not Submit Answer!");
                    this.Close();
                    DialogResult = DialogResult.Abort;
                    break;
                }
            }
            else
            {
                MessageBox.Show("Could Not Submit Answer!");
                this.Close();
                DialogResult = DialogResult.Abort;
            }
            return("");
        }
Ejemplo n.º 11
0
        private void updateLoop()
        {
            while (!stopThread)
            {
                Thread.Sleep(5000);
                byte[] message = new ASCIIEncoding().GetBytes("123");
                ClientSocket.client.Send(message);
                byte[] response  = new byte[1024];
                int    bytesRead = ClientSocket.client.Receive(response);
                Array.Resize <byte>(ref response, bytesRead);
                if (GlobalHelpers.GetCode(response) == "223")
                {
                    var roomState = MessagePack.MessagePackSerializer.Deserialize <JSON_Classes.GetRoomStateResponse>(GlobalHelpers.GetMsgpack(response));
                    switch (roomState.Status)
                    {
                    case 0:
                        if (!stopThread)
                        {
                            this.Invoke((MethodInvoker) delegate
                            {
                                this.PlayerListBox.DataSource = roomState.Room.Players;
                            });
                        }
                        break;

                    default:
                        if (!stopThread)
                        {
                            this.Invoke((MethodInvoker) delegate
                            {
                                MessageBox.Show("There Was An Error Getting The Room Info!");
                                this.Close();
                                DialogResult = DialogResult.Abort;
                            });
                        }
                        break;
                    }
                    if (roomState.Room.Metadata.IsActive != 0) // Game Has Begun.
                    {
                        if (!stopThread)
                        {
                            this.Invoke((MethodInvoker) delegate
                            {
                                // Actually Bring Up A New Window For The Game
                            });
                        }
                        break;
                    }
                }
            }
        }