Ejemplo n.º 1
0
        private void BtnCreateClick(object sender, RoutedEventArgs e)
        {
            if (IsNumericValue(TBPlayers.Text) && IsNumericValue(TBQuestions.Text) && IsNumericValue(TBTimePerQuestion.Text))
            {
                this.room = new Room(TBRoomName.Text, uint.Parse(TBPlayers.Text), uint.Parse(TBQuestions.Text), uint.Parse(TBTimePerQuestion.Text));

                CreateRoomResponse response = (CreateRoomResponse)Communicator.Communicate(
                    new CreateRoomRequest(this.room.roomName, this.room.numberOfPlayers, this.room.numberOfQuestions, this.room.TimeForQuestion));

                if (response.status == 1)
                {
                    WaitingRoomWindow wind = new WaitingRoomWindow(true, this.username, this.room);
                    wind.Show();
                    base.Hide();
                    base.Close();
                }
            }
            else
            {
                TBInvalid.Text = "One or more arguments are invalid";
            }
        }
Ejemplo n.º 2
0
        private async void createButton_Click(object sender, RoutedEventArgs e)
        {
            CreateRoomRequest  createRoomRequest  = new CreateRoomRequest();
            CreateRoomResponse createRoomResponse = new CreateRoomResponse()
            {
                status = -1
            };

            byte[] buf;

            Dialogs.CreateRoomDialogViewModel viewModel = new Dialogs.CreateRoomDialogViewModel()
            {
                MaxPlayers      = -1,
                TimePerQuestion = -1,
                QuestionCount   = -1
            };

            // Create the create room dialog using the room data as a data context
            Dialogs.CreateRoomDialog createRoomDialog = new Dialogs.CreateRoomDialog
            {
                DataContext = viewModel
            };

            // Show the create room dialog
            await DialogHost.Show(createRoomDialog, async delegate(object s1, DialogClosingEventArgs eventArgs)
            {
                // If the dialog is canceled ignore
                if (eventArgs.Parameter != null && (bool)eventArgs.Parameter == false)
                {
                    return;
                }

                // If the create room failed, return
                if (createRoomResponse.status == 1)
                {
                    return;
                }

                // If the current content is an error message, re-set the create dialog
                if (eventArgs.Session.Content.GetType() == typeof(Dialogs.MessageDialog))
                {
                    // Cancel the close of the dialog
                    eventArgs.Cancel();

                    // Set the create dialog
                    eventArgs.Session.UpdateContent(createRoomDialog);
                    return;
                }

                // Check if the data entered is invalid
                if (viewModel.Name == null || viewModel.Name.Length == 0 || viewModel.MaxPlayers == -1 || viewModel.TimePerQuestion == -1 || viewModel.QuestionCount == -1)
                {
                    // Prevent from the dialog to close
                    eventArgs.Cancel();

                    // Set the current dialog as an error dialog
                    eventArgs.Session.UpdateContent(new Dialogs.MessageDialog {
                        Message = "One or more of the values is empty or invalid!"
                    });
                    return;
                }

                // Create the create room request using the user's values
                createRoomRequest.roomName      = viewModel.Name;
                createRoomRequest.maxPlayers    = viewModel.MaxPlayers;
                createRoomRequest.questionCount = viewModel.QuestionCount;
                createRoomRequest.answerTimeout = viewModel.TimePerQuestion;

                try
                {
                    // Send the create room request to the server
                    await Client.Send(CreateRoomRequest.CODE, Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(createRoomRequest)));

                    // Get the response from the server and deserialize it
                    buf = await Client.Recv();
                    createRoomResponse = JsonConvert.DeserializeObject <CreateRoomResponse>(Encoding.UTF8.GetString(buf));

                    // If the room creation failed, print message
                    if (createRoomResponse.status == 1)
                    {
                        // Prevent from the dialog to close
                        eventArgs.Cancel();

                        // Set the current dialog as an error dialog
                        eventArgs.Session.UpdateContent(new Dialogs.MessageDialog {
                            Message = "An error occurred during creating the room!\nPlease try again later."
                        });
                        return;
                    }
                    else
                    {
                        RoomData room = new RoomData
                        {
                            id              = createRoomResponse.roomId,
                            name            = viewModel.Name,
                            maxPlayers      = viewModel.MaxPlayers,
                            questionCount   = viewModel.QuestionCount,
                            timePerQuestion = viewModel.TimePerQuestion
                        };

                        // Show the room admin window
                        new RoomAdminWindow {
                            room = room
                        }.Show();

                        // Close this window
                        Close();
                    }
                } catch
                {
                    // Close this window and re-show the auth window to connect to the server
                    new AuthWindow().Show();
                    Close();
                }
            });
        }