feat : ajout connexion auto quand ajout d'un lobby
continuous-integration/drone/push Build is passing Details

androidCompose
Jeremy DUCOURTHIAL 1 year ago
parent 70bd4fffd8
commit 01667c31b0

@ -25,7 +25,7 @@ class ConnexionPlayerActivity : AppCompatActivity() {
val isAuthenticated = ControllerPlayer.authenticateUser(nickname,password) val isAuthenticated = ControllerPlayer.authenticateUser(nickname,password)
if (isAuthenticated != -1) { if (isAuthenticated != -1) {
// Save authentication status for global accessibility // Save authentication status for global accessibility
val intent = Intent(this, ServerDetailsActivity::class.java) val intent = Intent(this, MainActivity::class.java)
startActivity(intent) startActivity(intent)
MainActivity.idPlayerConnected = isAuthenticated MainActivity.idPlayerConnected = isAuthenticated

@ -6,6 +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.controllers.ControllerUtiliser
import okhttp3.MultipartBody import okhttp3.MultipartBody
import org.json.JSONObject import org.json.JSONObject
@ -51,16 +52,25 @@ class CreateLobbyActivity : AppCompatActivity() {
formDataBuilder.addFormDataPart("name", lobbyName) formDataBuilder.addFormDataPart("name", lobbyName)
formDataBuilder.addFormDataPart("password", password) formDataBuilder.addFormDataPart("password", password)
formDataBuilder.addFormDataPart("nbplayers", nbPlayers.toString()) formDataBuilder.addFormDataPart("nbplayers", nbPlayers.toString())
formDataBuilder.addFormDataPart("idplayercreator", "53") formDataBuilder.addFormDataPart("idplayercreator", MainActivity.idPlayerConnected.toString())
formDataBuilder.addFormDataPart("idchapter", idChapter.toString()) formDataBuilder.addFormDataPart("idchapter", idChapter.toString())
formDataBuilder.addFormDataPart("difficulty", difficulty.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(formDataBuilder) val idLobbyCreatedSuccessfully = 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 (idLobbyCreatedSuccessfully != -1) {
val intent = Intent(this, ServerDetailsActivity::class.java) val intent = Intent(this, ServerDetailsActivity::class.java)
intent.putExtra("serverName", lobbyName)
intent.putExtra("lobbyId", idLobbyCreatedSuccessfully.toString())
val formDataBuilder = MultipartBody.Builder().setType(MultipartBody.FORM)
formDataBuilder.addFormDataPart("idplayer", MainActivity.idPlayerConnected.toString())
formDataBuilder.addFormDataPart("idlobby", idLobbyCreatedSuccessfully.toString())
formDataBuilder.addFormDataPart("playertime", "0")
ControllerUtiliser.createUtiliserByIdLobby(formDataBuilder)
startActivity(intent) startActivity(intent)
} else { } else {
Toast.makeText(this, "Échec de la création du lobby. Veuillez réessayer.", Toast.LENGTH_SHORT).show() Toast.makeText(this, "Échec de la création du lobby. Veuillez réessayer.", Toast.LENGTH_SHORT).show()

@ -13,7 +13,9 @@ import android.widget.ListView
import android.widget.TextView import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import com.example.mathseduc.controllers.ControllerLobby import com.example.mathseduc.controllers.ControllerLobby
import com.example.mathseduc.controllers.ControllerUtiliser
import com.example.mathseduc.models.Lobby import com.example.mathseduc.models.Lobby
import okhttp3.MultipartBody
class MultiActivity : AppCompatActivity() { class MultiActivity : AppCompatActivity() {
@ -36,6 +38,13 @@ class MultiActivity : AppCompatActivity() {
val intent = Intent(this, ServerDetailsActivity::class.java) val intent = Intent(this, ServerDetailsActivity::class.java)
intent.putExtra("serverName", serverList[position].name) intent.putExtra("serverName", serverList[position].name)
intent.putExtra("lobbyId", serverList[position].id) intent.putExtra("lobbyId", serverList[position].id)
val formDataBuilder = MultipartBody.Builder().setType(MultipartBody.FORM)
formDataBuilder.addFormDataPart("idplayer", MainActivity.idPlayerConnected.toString())
formDataBuilder.addFormDataPart("idlobby", serverList[position].id.toString())
formDataBuilder.addFormDataPart("playertime", "0")
ControllerUtiliser.createUtiliserByIdLobby(formDataBuilder)
startActivity(intent) startActivity(intent)
} }
} else { } else {

@ -5,11 +5,10 @@ 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.toMediaType
import okhttp3.MultipartBody import okhttp3.MultipartBody
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import okhttp3.Request import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody import org.json.JSONObject
import java.io.IOException import java.io.IOException
@ -25,7 +24,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/all/lobbies/qUOGkWdoPCgbmuqxIC8xiaX0rV1Pw1LoPafkaoHOgszEyD9P2vcOu493xCDZpAqO/1") .url("https://trusting-panini.87-106-126-109.plesk.page/api/all/lobbies/qUOGkWdoPCgbmuqxIC8xiaX0rV1Pw1LoPafkaoHOgszEyD9P2vcOu493xCDZpAqO")
.build() .build()
// API Response // API Response
@ -41,11 +40,8 @@ class ControllerLobby {
return null return null
} }
fun createLobby(lobbyData: MultipartBody.Builder): Boolean { fun createLobby(lobbyData: MultipartBody.Builder): Int {
try { try {
// Log avant l'appel API
Log.d("CreateLobby", "Request Data: ${lobbyData.toString()}")
val policy = StrictMode.ThreadPolicy.Builder().permitAll().build() val policy = StrictMode.ThreadPolicy.Builder().permitAll().build()
StrictMode.setThreadPolicy(policy) StrictMode.setThreadPolicy(policy)
@ -60,19 +56,20 @@ class ControllerLobby {
// API Response // API Response
client.newCall(request).execute().use { response -> client.newCall(request).execute().use { response ->
// Log après la réponse API if (response.isSuccessful) {
Log.d("CreateLobby", "Response Code: ${response.code}") // Si la réponse est réussie, extraire l'ID du lobby du corps de la réponse JSON
Log.d("CreateLobby", "Response Body: ${response.body?.string()}") val responseBody = response.body?.string()
val jsonObject = JSONObject(responseBody)
// Vérifier si la création du lobby a réussi // Retourner l'ID du lobby
return response.isSuccessful return jsonObject.optInt("lobby_id", -1)
}
} }
} catch (e: Exception) { } catch (e: Exception) {
// Log en cas d'erreur // Log en cas d'erreur
Log.e("CreateLobby", "Error creating lobby", e) Log.e("CreateLobby", "Error creating lobby", e)
return false
} }
return -1
} }
} }
} }

@ -1,10 +1,7 @@
package com.example.mathseduc.controllers package com.example.mathseduc.controllers
import android.os.StrictMode import android.os.StrictMode
import com.example.mathseduc.models.Chapter import com.example.mathseduc.models.*
import com.example.mathseduc.models.Lobby
import com.example.mathseduc.models.LobbyInfo
import com.example.mathseduc.models.Player
import com.google.gson.Gson import com.google.gson.Gson
import com.google.gson.reflect.TypeToken import com.google.gson.reflect.TypeToken
import okhttp3.FormBody import okhttp3.FormBody
@ -30,9 +27,9 @@ class ControllerPlayer {
if (!response.isSuccessful) throw IOException("Unexpected code $response") if (!response.isSuccessful) throw IOException("Unexpected code $response")
val gson = Gson() val gson = Gson()
val typeTokenProduct = object : TypeToken<List<LobbyInfo>>() {}.type val typeTokenProduct = object : TypeToken<List<Utiliser>>() {}.type
val playerInfoList: List<LobbyInfo> = gson.fromJson(response.body!!.string(), typeTokenProduct) val playerInfoList: List<Utiliser> = gson.fromJson(response.body!!.string(), typeTokenProduct)
// Extract player IDs from PlayerInfo list // Extract player IDs from PlayerInfo list
val playerIds: List<Int> = playerInfoList.map { it.idplayer } val playerIds: List<Int> = playerInfoList.map { it.idplayer }

@ -0,0 +1,43 @@
package com.example.mathseduc.controllers
import android.os.StrictMode
import android.util.Log
import com.example.mathseduc.models.Lobby
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import okhttp3.MultipartBody
import okhttp3.OkHttpClient
import okhttp3.Request
import java.io.IOException
class ControllerUtiliser {
companion object {
fun createUtiliserByIdLobby(utiliserData: MultipartBody.Builder): Boolean {
try {
val policy = StrictMode.ThreadPolicy.Builder().permitAll().build()
StrictMode.setThreadPolicy(policy)
// Client HTTP API
val client = OkHttpClient()
// API Access
val request = Request.Builder()
.url("https://trusting-panini.87-106-126-109.plesk.page/api/add/utiliser/qUOGkWdoPCgbmuqxIC8xiaX0rV1Pw1LoPafkaoHOgszEyD9P2vcOu493xCDZpAqO")
.post(utiliserData.build())
.build()
// API Response
client.newCall(request).execute().use { response ->
// Vérifier si la création du lobby a réussi
return response.isSuccessful
}
} catch (e: Exception) {
// Log en cas d'erreur
Log.e("CreateLobby", "Error creating lobby", e)
return false
}
}
}
}

@ -1,6 +1,6 @@
package com.example.mathseduc.models package com.example.mathseduc.models
data class LobbyInfo ( data class Utiliser (
val idlobby: Int, val idlobby: Int,
val idplayer: Int, val idplayer: Int,
val playertime: Int val playertime: Int
Loading…
Cancel
Save