|
|
|
@ -6,7 +6,7 @@ import android.widget.*
|
|
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
|
|
|
|
import com.example.mathseduc.controllers.ControllerChapter
|
|
|
|
|
import com.example.mathseduc.controllers.ControllerLobby
|
|
|
|
|
import com.example.mathseduc.models.Lobby
|
|
|
|
|
import okhttp3.MultipartBody
|
|
|
|
|
import org.json.JSONObject
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -34,30 +34,37 @@ class CreateLobbyActivity : AppCompatActivity() {
|
|
|
|
|
buttonCreateLobby.setOnClickListener {
|
|
|
|
|
val lobbyName = findViewById<EditText>(R.id.editTextLobbyName).text.toString()
|
|
|
|
|
val password = findViewById<EditText>(R.id.editTextPassword).text.toString()
|
|
|
|
|
val nbPlayers = findViewById<EditText>(R.id.editTextNbPlayers).text.toString().toInt()
|
|
|
|
|
val nbPlayers = findViewById<EditText>(R.id.editTextNbPlayers).text.toString()
|
|
|
|
|
val difficulty = findViewById<Spinner>(R.id.spinnerDifficulty).selectedItemPosition + 1
|
|
|
|
|
val selectedChapterPosition = findViewById<Spinner>(R.id.spinnerChapter).selectedItemPosition
|
|
|
|
|
val chapterId = chaptersFromApi?.getOrNull(selectedChapterPosition)?.id?.toInt() ?: -1
|
|
|
|
|
val idChapter = chaptersFromApi?.getOrNull(selectedChapterPosition)?.id?: -1
|
|
|
|
|
|
|
|
|
|
// Créer un objet Lobby avec les valeurs du formulaire
|
|
|
|
|
val lobbyData = JSONObject().apply {
|
|
|
|
|
put("name", lobbyName)
|
|
|
|
|
put("password", password)
|
|
|
|
|
put("nbplayers", nbPlayers)
|
|
|
|
|
put("idplayercreator", 53)
|
|
|
|
|
put("idchapter", chapterId)
|
|
|
|
|
put("difficulty", difficulty)
|
|
|
|
|
if (lobbyName.isNullOrBlank() || password.isNullOrBlank() || nbPlayers.isNullOrBlank() || nbPlayers.toInt() <= 0) {
|
|
|
|
|
Toast.makeText(this, "Échec de la création du lobby. Veuillez réessayer.", Toast.LENGTH_SHORT).show()
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Create form data
|
|
|
|
|
val formDataBuilder = MultipartBody.Builder().setType(MultipartBody.FORM)
|
|
|
|
|
|
|
|
|
|
// Appeler la fonction pour créer le lobby
|
|
|
|
|
val lobbyCreatedSuccessfully = ControllerLobby.createLobby(lobbyData)
|
|
|
|
|
// Add fields to form data
|
|
|
|
|
formDataBuilder.addFormDataPart("name", lobbyName)
|
|
|
|
|
formDataBuilder.addFormDataPart("password", password)
|
|
|
|
|
formDataBuilder.addFormDataPart("nbplayers", nbPlayers.toString())
|
|
|
|
|
formDataBuilder.addFormDataPart("idplayercreator", "53")
|
|
|
|
|
formDataBuilder.addFormDataPart("idchapter", idChapter.toString())
|
|
|
|
|
formDataBuilder.addFormDataPart("difficulty", difficulty.toString())
|
|
|
|
|
|
|
|
|
|
// Vérifier si la création du lobby a réussi
|
|
|
|
|
if (lobbyCreatedSuccessfully) {
|
|
|
|
|
val intent = Intent(this, ServerDetailsActivity::class.java)
|
|
|
|
|
startActivity(intent)
|
|
|
|
|
} else {
|
|
|
|
|
Toast.makeText(this, "Échec de la création du lobby. Veuillez réessayer.", Toast.LENGTH_SHORT).show()
|
|
|
|
|
// Appeler la fonction pour créer le lobby
|
|
|
|
|
val lobbyCreatedSuccessfully = ControllerLobby.createLobby(formDataBuilder)
|
|
|
|
|
|
|
|
|
|
// Vérifier si la création du lobby a réussi
|
|
|
|
|
if (lobbyCreatedSuccessfully) {
|
|
|
|
|
val intent = Intent(this, ServerDetailsActivity::class.java)
|
|
|
|
|
startActivity(intent)
|
|
|
|
|
} else {
|
|
|
|
|
Toast.makeText(this, "Échec de la création du lobby. Veuillez réessayer.", Toast.LENGTH_SHORT).show()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|