feat : ajout recup des questions pour QuizMultiActivity
continuous-integration/drone/push Build is passing Details

master
Jeremy DUCOURTHIAL 1 year ago
parent 5db24cad2f
commit 8e11a953d4

@ -6,6 +6,8 @@ import android.widget.Button
import android.widget.ProgressBar import android.widget.ProgressBar
import android.widget.Toast import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import com.example.mathseduc.controllers.ControllerLobby
import com.example.mathseduc.controllers.ControllerQuestion
class QuizMultiActivity : AppCompatActivity() { class QuizMultiActivity : AppCompatActivity() {
@ -20,13 +22,15 @@ class QuizMultiActivity : AppCompatActivity() {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_quiz_multi) setContentView(R.layout.activity_quiz_multi)
val lobbyId = intent.getIntExtra("lobbyId", -1)
var progressBar1 = findViewById<ProgressBar>(R.id.progressBar1) var progressBar1 = findViewById<ProgressBar>(R.id.progressBar1)
var chrono = findViewById<ProgressBar>(R.id.chrono) var chrono = findViewById<ProgressBar>(R.id.chrono)
progressBar1.max = 100 progressBar1.max = 100
chrono.max = 30 chrono.max = 30
val toto = ControllerQuestion.getQuestionsForLobby(ControllerLobby.getIdQuestionsLobby(lobbyId))
var incrementeButton = findViewById<Button>(R.id.buttonValider) var incrementeButton = findViewById<Button>(R.id.buttonValider)

@ -29,7 +29,7 @@ class ServerDetailsActivity : AppCompatActivity() {
private val onBackPressedCallback: OnBackPressedCallback = object : OnBackPressedCallback(true) { private val onBackPressedCallback: OnBackPressedCallback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() { override fun handleOnBackPressed() {
MyBackPressed() myBackPressed()
} }
} }
@ -64,7 +64,7 @@ class ServerDetailsActivity : AppCompatActivity() {
val btnLaunchQuiz = findViewById<Button>(R.id.btnLaunchQuiz) val btnLaunchQuiz = findViewById<Button>(R.id.btnLaunchQuiz)
if (ControllerLobby.PlayerCreatorIdPresentInLobby(MainActivity.idPlayerConnected, lobbyId)) { if (ControllerLobby.playerCreatorIdPresentInLobby(MainActivity.idPlayerConnected, lobbyId)) {
btnLaunchQuiz.visibility = View.VISIBLE btnLaunchQuiz.visibility = View.VISIBLE
btnLaunchQuiz.setOnClickListener { btnLaunchQuiz.setOnClickListener {
@ -103,7 +103,7 @@ class ServerDetailsActivity : AppCompatActivity() {
val btnLaunchQuiz = findViewById<Button>(R.id.btnLaunchQuiz) val btnLaunchQuiz = findViewById<Button>(R.id.btnLaunchQuiz)
if (ControllerLobby.PlayerCreatorIdPresentInLobby(MainActivity.idPlayerConnected, lobbyId)) { if (ControllerLobby.playerCreatorIdPresentInLobby(MainActivity.idPlayerConnected, lobbyId)) {
btnLaunchQuiz.visibility = View.VISIBLE btnLaunchQuiz.visibility = View.VISIBLE
} else { } else {
btnLaunchQuiz.visibility = View.GONE btnLaunchQuiz.visibility = View.GONE
@ -120,12 +120,12 @@ class ServerDetailsActivity : AppCompatActivity() {
} }
} }
private fun MyBackPressed() { private fun myBackPressed() {
val lobbyId = intent.getIntExtra("lobbyId", -1) val lobbyId = intent.getIntExtra("lobbyId", -1)
ControllerUtiliser.DeleteUtiliserForLobby(MainActivity.idPlayerConnected, lobbyId) ControllerUtiliser.DeleteUtiliserForLobby(MainActivity.idPlayerConnected, lobbyId)
if (ControllerLobby.PlayerCreatorIdPresentInLobby(MainActivity.idPlayerConnected, lobbyId)) { if (ControllerLobby.playerCreatorIdPresentInLobby(MainActivity.idPlayerConnected, lobbyId)) {
val idNextPlayerCreator = ControllerUtiliser.getIdNextPlayerInLobby(lobbyId) val idNextPlayerCreator = ControllerUtiliser.getIdNextPlayerInLobby(lobbyId)
if (idNextPlayerCreator == -1) { if (idNextPlayerCreator == -1) {

@ -42,6 +42,41 @@ class ControllerLobby {
return null return null
} }
fun getIdQuestionsLobby(idLobby: Int): ArrayList<String> {
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/propose/$idLobby/qUOGkWdoPCgbmuqxIC8xiaX0rV1Pw1LoPafkaoHOgszEyD9P2vcOu493xCDZpAqO")
.build()
// API Response
client.newCall(request).execute().use { response ->
if (!response.isSuccessful) throw IOException("Unexpected code $response")
// Gson deserialization
val gson = Gson()
val typeTokenProduct = object : TypeToken<ArrayList<Map<String, String>>>() {}.type
val questionList: ArrayList<Map<String, String>> = gson.fromJson(response.body!!.string(), typeTokenProduct)
// Extracting question IDs
val idList = ArrayList<String>()
for (question in questionList) {
val id = question["idquestion"]
if (id != null) {
idList.add(id)
}
}
return idList
}
}
fun createLobby(lobbyData: MultipartBody.Builder): Int { fun createLobby(lobbyData: MultipartBody.Builder): Int {
try { try {
val policy = StrictMode.ThreadPolicy.Builder().permitAll().build() val policy = StrictMode.ThreadPolicy.Builder().permitAll().build()
@ -74,7 +109,7 @@ class ControllerLobby {
return -1 return -1
} }
fun PlayerCreatorIdPresentInLobby(idPlayer: Int, lobbyId: Int): Boolean { fun playerCreatorIdPresentInLobby(idPlayer: Int, lobbyId: Int): Boolean {
val policy = StrictMode.ThreadPolicy.Builder().permitAll().build() val policy = StrictMode.ThreadPolicy.Builder().permitAll().build()
StrictMode.setThreadPolicy(policy) StrictMode.setThreadPolicy(policy)

@ -0,0 +1,63 @@
package com.example.mathseduc.controllers
import android.os.StrictMode
import android.util.Log
import com.example.mathseduc.models.Lobby
import com.example.mathseduc.models.Question
import com.example.mathseduc.models.Utiliser
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import okhttp3.MultipartBody
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import org.json.JSONObject
import java.io.IOException
class ControllerQuestion {
companion object {
fun getQuestionsForLobby(questionIds: ArrayList<String>): List<Question>? {
val questions = ArrayList<Question>()
for (questionId in questionIds) {
val question = getQuestionById(questionId)
if (question != null) {
questions.add(question)
}
}
return if (questions.isNotEmpty()) questions else null
}
private fun getQuestionById(questionId: String): Question? {
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/questions/$questionId/qUOGkWdoPCgbmuqxIC8xiaX0rV1Pw1LoPafkaoHOgszEyD9P2vcOu493xCDZpAqO")
.build()
// API Response
client.newCall(request).execute().use { response ->
if (!response.isSuccessful) {
// Handle error, log, or throw an exception as needed
return null
}
// Gson deserialization
val gson = Gson()
val typeTokenProduct = object : TypeToken<ArrayList<Question>>() {}.type
val question: ArrayList<Question> = gson.fromJson(response.body!!.string(), typeTokenProduct)
// Assuming the API returns a list with a single question
return if (question.isNotEmpty()) question[0] else null
}
}
}
}

@ -0,0 +1,10 @@
package com.example.mathseduc.models
data class Question(
val id: Int,
val content: String,
val nbfails: Int,
val difficulty: Int,
val idchapter: Int,
val idanswergood: Int,
)
Loading…
Cancel
Save