private void CreateRoom_Click(object sender, RoutedEventArgs e) { try { if (roomname.Text == "" || int.Parse(num_questions.Text) == 0 || int.Parse(time.Text) == 0 || int.Parse(num_players.Text) < 2 || roomname.Text.Contains(":")) { error.Text = "Wrong input! Please try again."; } } catch { error.Text = "Wrong input! Please try again."; return; } CreateRoomRequest request = new CreateRoomRequest { answerTimeout = int.Parse(time.Text), roomName = roomname.Text, questionCount = int.Parse(num_questions.Text), maxUsers = int.Parse(num_players.Text) }; WaitingRoom.roomName = roomname.Text; WaitingRoom.isAdmin = true; string ans = Communicator.Send(JsonConvert.SerializeObject(request), (byte)ReqCode.CREATEROOM); CreateRoomResponse response; string subStr = ans.Substring(4, 23); response = JsonConvert.DeserializeObject <CreateRoomResponse>(subStr); Communicator.roomId = response.roomId; NavigationService.Navigate(new WaitingRoom()); }
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(); } }); }