feat : ajout création d'un lobby utilisable (API)
continuous-integration/drone/push Build is passing Details

master
Jeremy DUCOURTHIAL 1 year ago
parent 5975fe39de
commit 9ad151d7be

@ -6,7 +6,7 @@ import android.widget.*
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import com.example.mathseduc.controllers.ControllerChapter import com.example.mathseduc.controllers.ControllerChapter
import com.example.mathseduc.controllers.ControllerLobby import com.example.mathseduc.controllers.ControllerLobby
import com.example.mathseduc.models.Lobby import okhttp3.MultipartBody
import org.json.JSONObject import org.json.JSONObject
@ -34,23 +34,29 @@ class CreateLobbyActivity : AppCompatActivity() {
buttonCreateLobby.setOnClickListener { buttonCreateLobby.setOnClickListener {
val lobbyName = findViewById<EditText>(R.id.editTextLobbyName).text.toString() val lobbyName = findViewById<EditText>(R.id.editTextLobbyName).text.toString()
val password = findViewById<EditText>(R.id.editTextPassword).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 difficulty = findViewById<Spinner>(R.id.spinnerDifficulty).selectedItemPosition + 1
val selectedChapterPosition = findViewById<Spinner>(R.id.spinnerChapter).selectedItemPosition 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 if (lobbyName.isNullOrBlank() || password.isNullOrBlank() || nbPlayers.isNullOrBlank() || nbPlayers.toInt() <= 0) {
val lobbyData = JSONObject().apply { Toast.makeText(this, "Échec de la création du lobby. Veuillez réessayer.", Toast.LENGTH_SHORT).show()
put("name", lobbyName)
put("password", password)
put("nbplayers", nbPlayers)
put("idplayercreator", 53)
put("idchapter", chapterId)
put("difficulty", difficulty)
} }
else
{
// Create form data
val formDataBuilder = MultipartBody.Builder().setType(MultipartBody.FORM)
// 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())
// Appeler la fonction pour créer le lobby // Appeler la fonction pour créer le lobby
val lobbyCreatedSuccessfully = ControllerLobby.createLobby(lobbyData) val lobbyCreatedSuccessfully = ControllerLobby.createLobby(formDataBuilder)
// Vérifier si la création du lobby a réussi // Vérifier si la création du lobby a réussi
if (lobbyCreatedSuccessfully) { if (lobbyCreatedSuccessfully) {
@ -62,3 +68,4 @@ class CreateLobbyActivity : AppCompatActivity() {
} }
} }
} }
}

@ -5,13 +5,14 @@ import android.util.Log
import com.example.mathseduc.models.Lobby import com.example.mathseduc.models.Lobby
import com.google.gson.Gson import com.google.gson.Gson
import com.google.gson.reflect.TypeToken import com.google.gson.reflect.TypeToken
import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MultipartBody
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import okhttp3.Request import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONObject
import java.io.IOException import java.io.IOException
class ControllerLobby { class ControllerLobby {
companion object { companion object {
fun getLobbies(): ArrayList<Lobby>? { fun getLobbies(): ArrayList<Lobby>? {
@ -40,7 +41,7 @@ class ControllerLobby {
return null return null
} }
fun createLobby(lobbyData: JSONObject): Boolean { fun createLobby(lobbyData: MultipartBody.Builder): Boolean {
try { try {
// Log avant l'appel API // Log avant l'appel API
Log.d("CreateLobby", "Request Data: ${lobbyData.toString()}") Log.d("CreateLobby", "Request Data: ${lobbyData.toString()}")
@ -54,7 +55,7 @@ class ControllerLobby {
// API Access // API Access
val request = Request.Builder() val request = Request.Builder()
.url("https://trusting-panini.87-106-126-109.plesk.page/api/add/lobbies/qUOGkWdoPCgbmuqxIC8xiaX0rV1Pw1LoPafkaoHOgszEyD9P2vcOu493xCDZpAqO") .url("https://trusting-panini.87-106-126-109.plesk.page/api/add/lobbies/qUOGkWdoPCgbmuqxIC8xiaX0rV1Pw1LoPafkaoHOgszEyD9P2vcOu493xCDZpAqO")
.post(lobbyData.toString().toRequestBody("application/json".toMediaTypeOrNull())) .post(lobbyData.build())
.build() .build()
// API Response // API Response

Loading…
Cancel
Save