|
|
|
@ -1,6 +1,8 @@
|
|
|
|
|
package com.example.mathseduc.ui
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import android.util.Log
|
|
|
|
|
import android.widget.Toast
|
|
|
|
|
import androidx.compose.foundation.background
|
|
|
|
|
import androidx.compose.foundation.clickable
|
|
|
|
|
import androidx.compose.foundation.layout.Box
|
|
|
|
@ -34,8 +36,11 @@ import androidx.compose.ui.text.input.KeyboardType
|
|
|
|
|
import androidx.compose.ui.text.input.PasswordVisualTransformation
|
|
|
|
|
import androidx.compose.ui.unit.IntSize
|
|
|
|
|
import androidx.compose.ui.unit.dp
|
|
|
|
|
import com.example.mathseduc.MainActivity
|
|
|
|
|
import com.example.mathseduc.controllers.ControllerChapter
|
|
|
|
|
import com.example.mathseduc.controllers.ControllerLobby
|
|
|
|
|
import com.example.mathseduc.ui.theme.Colors
|
|
|
|
|
import okhttp3.MultipartBody
|
|
|
|
|
|
|
|
|
|
@Composable
|
|
|
|
|
fun CreateLobbyPage() {
|
|
|
|
@ -46,8 +51,9 @@ fun CreateLobbyPage() {
|
|
|
|
|
var chapter by remember { mutableStateOf("") }
|
|
|
|
|
var expandedDifficulty by remember { mutableStateOf(false) }
|
|
|
|
|
var expandedChapter by remember { mutableStateOf(false) }
|
|
|
|
|
var sizeDifficulty by remember { mutableStateOf(IntSize.Zero) }
|
|
|
|
|
var sizeChapter by remember { mutableStateOf(IntSize.Zero) }
|
|
|
|
|
var size by remember { mutableStateOf(IntSize.Zero) }
|
|
|
|
|
|
|
|
|
|
val difficultyOptions = listOf("Facile", "Moyen", "Difficile")
|
|
|
|
|
|
|
|
|
|
val context = LocalContext.current
|
|
|
|
|
val keyboardController = LocalSoftwareKeyboardController.current
|
|
|
|
@ -96,7 +102,6 @@ fun CreateLobbyPage() {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Box {
|
|
|
|
|
val difficultyOptions = listOf("Facile", "Moyen", "Difficile")
|
|
|
|
|
TextField(
|
|
|
|
|
value = difficulty,
|
|
|
|
|
onValueChange = { difficulty = it },
|
|
|
|
@ -113,7 +118,7 @@ fun CreateLobbyPage() {
|
|
|
|
|
expandedDifficulty = !expandedDifficulty
|
|
|
|
|
})
|
|
|
|
|
.onGloballyPositioned {
|
|
|
|
|
sizeDifficulty = it.size
|
|
|
|
|
size = it.size
|
|
|
|
|
}
|
|
|
|
|
.fillMaxWidth()
|
|
|
|
|
.background(Colors.White)
|
|
|
|
@ -133,7 +138,7 @@ fun CreateLobbyPage() {
|
|
|
|
|
expanded = expandedDifficulty,
|
|
|
|
|
onDismissRequest = { expandedDifficulty = false },
|
|
|
|
|
modifier = Modifier
|
|
|
|
|
.width(with(LocalDensity.current) { sizeDifficulty.width.toDp() })
|
|
|
|
|
.width(with(LocalDensity.current) { size.width.toDp() })
|
|
|
|
|
.background(Colors.White)
|
|
|
|
|
.padding(8.dp)
|
|
|
|
|
) {
|
|
|
|
@ -169,7 +174,7 @@ fun CreateLobbyPage() {
|
|
|
|
|
expandedChapter = !expandedChapter
|
|
|
|
|
})
|
|
|
|
|
.onGloballyPositioned {
|
|
|
|
|
sizeChapter = it.size
|
|
|
|
|
size = it.size
|
|
|
|
|
}
|
|
|
|
|
.fillMaxWidth()
|
|
|
|
|
.background(Colors.White)
|
|
|
|
@ -209,7 +214,42 @@ fun CreateLobbyPage() {
|
|
|
|
|
Button(
|
|
|
|
|
onClick = {
|
|
|
|
|
// Handle button click (Create Lobby)
|
|
|
|
|
// Use the collected data: lobbyName, password, nbPlayers, difficulty, chapter
|
|
|
|
|
val selectedChapter = ControllerChapter.getChapters()?.find { it.name == chapter }
|
|
|
|
|
val difficultyNum = difficultyOptions.indexOf(difficulty) + 1
|
|
|
|
|
|
|
|
|
|
if (lobbyName.isBlank() || password.isBlank() || nbPlayers.isBlank() || nbPlayers.toInt() <= 0 || difficulty.isBlank() || selectedChapter == null) {
|
|
|
|
|
// Show error message if any field is invalid
|
|
|
|
|
Toast.makeText(context, "Invalid input. Please check all fields.", Toast.LENGTH_SHORT).show()
|
|
|
|
|
} else {
|
|
|
|
|
try {
|
|
|
|
|
val formDataBuilder = MultipartBody.Builder().setType(MultipartBody.FORM)
|
|
|
|
|
|
|
|
|
|
// Add fields to form data
|
|
|
|
|
formDataBuilder.addFormDataPart("name", lobbyName)
|
|
|
|
|
formDataBuilder.addFormDataPart("password", password)
|
|
|
|
|
formDataBuilder.addFormDataPart("nbplayers", nbPlayers)
|
|
|
|
|
formDataBuilder.addFormDataPart("idplayercreator", MainActivity.idPlayerConnected.toString())
|
|
|
|
|
formDataBuilder.addFormDataPart("idchapter", selectedChapter.id)
|
|
|
|
|
formDataBuilder.addFormDataPart("difficulty", difficultyNum.toString())
|
|
|
|
|
Log.e("formData : ",formDataBuilder.toString())
|
|
|
|
|
// Call createLobby function from ControllerLobby
|
|
|
|
|
val lobbyId = ControllerLobby.createLobby(formDataBuilder)
|
|
|
|
|
|
|
|
|
|
// Check if lobby creation is successful
|
|
|
|
|
if (lobbyId != -1) {
|
|
|
|
|
Toast.makeText(context, "Lobby created successfully!", Toast.LENGTH_SHORT).show()
|
|
|
|
|
// val intent = Intent(context, LobbyDetailsActivity::class.java)
|
|
|
|
|
// intent.putExtra("lobbyId", lobbyId)
|
|
|
|
|
// context.startActivity(intent)
|
|
|
|
|
} else {
|
|
|
|
|
Toast.makeText(context, "Failed to create lobby. Please try again.", Toast.LENGTH_SHORT).show()
|
|
|
|
|
}
|
|
|
|
|
} catch (e: Exception) {
|
|
|
|
|
// Log en cas d'erreur
|
|
|
|
|
Log.e("CreateLobby", "Error creating lobby", e)
|
|
|
|
|
Toast.makeText(context, "Failed to create lobby. Please try again.", Toast.LENGTH_SHORT).show()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
modifier = Modifier
|
|
|
|
|
.fillMaxWidth()
|
|
|
|
@ -218,7 +258,6 @@ fun CreateLobbyPage() {
|
|
|
|
|
Text(text = "Create Lobby", color = Colors.White)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|