Merge branch 'master' of https://codefirst.iut.uca.fr/git/jade.van_brabandt/3.01-QCM_MuscuMaths
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
commit
f0483bb479
@ -0,0 +1,71 @@
|
||||
package com.example.mathseduc
|
||||
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.widget.*
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import com.example.mathseduc.controllers.ControllerChapter
|
||||
import com.example.mathseduc.controllers.ControllerLobby
|
||||
import okhttp3.MultipartBody
|
||||
import org.json.JSONObject
|
||||
|
||||
|
||||
class CreateLobbyActivity : AppCompatActivity() {
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_create_lobby)
|
||||
|
||||
var spinnerChapter = findViewById<Spinner>(R.id.spinnerChapter)
|
||||
|
||||
// Fetch chapters from API
|
||||
val chaptersFromApi = ControllerChapter.getChapters()
|
||||
|
||||
// Populate Spinner with fetched chapters
|
||||
if (chaptersFromApi != null) {
|
||||
val chapterNames = chaptersFromApi.map { it.name }.toTypedArray()
|
||||
|
||||
val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, chapterNames)
|
||||
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
|
||||
spinnerChapter.adapter = adapter
|
||||
}
|
||||
|
||||
val buttonCreateLobby = findViewById<Button>(R.id.buttonCreateLobby)
|
||||
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()
|
||||
val difficulty = findViewById<Spinner>(R.id.spinnerDifficulty).selectedItemPosition + 1
|
||||
val selectedChapterPosition = findViewById<Spinner>(R.id.spinnerChapter).selectedItemPosition
|
||||
val idChapter = chaptersFromApi?.getOrNull(selectedChapterPosition)?.id?: -1
|
||||
|
||||
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)
|
||||
|
||||
// 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
|
||||
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()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package com.example.mathseduc.controllers
|
||||
|
||||
import android.os.StrictMode
|
||||
import com.example.mathseduc.models.Chapter
|
||||
import com.example.mathseduc.models.Lobby
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.reflect.TypeToken
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import java.io.IOException
|
||||
|
||||
class ControllerChapter {
|
||||
companion object {
|
||||
fun getChapters(): ArrayList<Chapter>? {
|
||||
|
||||
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/all/chapters/qUOGkWdoPCgbmuqxIC8xiaX0rV1Pw1LoPafkaoHOgszEyD9P2vcOu493xCDZpAqO/1")
|
||||
.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<Chapter>>() {}.type
|
||||
|
||||
// Parse API response and return the list of chapters
|
||||
return gson.fromJson(response.body!!.string(), typeTokenProduct)
|
||||
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
package com.example.mathseduc.models
|
||||
|
||||
data class Chapter(
|
||||
val id: String,
|
||||
val name: String,
|
||||
)
|
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<shape xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<corners android:topLeftRadius="20dp"
|
||||
android:topRightRadius="20dp"/>
|
||||
<solid android:color="@color/grey"/>
|
||||
</shape>
|
@ -0,0 +1,75 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:padding="16dp"
|
||||
android:background="#000000">
|
||||
|
||||
<EditText
|
||||
android:id="@+id/editTextLobbyName"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="Lobby Name"
|
||||
android:background="#FFFFFF"
|
||||
android:textColor="#000000"
|
||||
android:layout_marginBottom="16dp"/>
|
||||
|
||||
<EditText
|
||||
android:id="@+id/editTextPassword"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="Password"
|
||||
android:inputType="textPassword"
|
||||
android:background="#FFFFFF"
|
||||
android:textColor="#000000"
|
||||
android:layout_below="@id/editTextLobbyName"
|
||||
android:layout_marginTop="8dp"
|
||||
android:layout_marginBottom="16dp"/>
|
||||
|
||||
<EditText
|
||||
android:id="@+id/editTextNbPlayers"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="Number of Players"
|
||||
android:inputType="number"
|
||||
android:background="#FFFFFF"
|
||||
android:textColor="#000000"
|
||||
android:layout_below="@id/editTextPassword"
|
||||
android:layout_marginTop="8dp"
|
||||
android:layout_marginBottom="16dp"/>
|
||||
|
||||
<Spinner
|
||||
android:id="@+id/spinnerDifficulty"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@id/editTextNbPlayers"
|
||||
android:layout_marginTop="8dp"
|
||||
android:background="#FFFFFF"
|
||||
android:textColor="#000000"
|
||||
android:entries="@array/difficulty_array"
|
||||
android:prompt="@string/difficulty_prompt"
|
||||
/>
|
||||
|
||||
<Spinner
|
||||
android:id="@+id/spinnerChapter"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@id/spinnerDifficulty"
|
||||
android:layout_marginTop="8dp"
|
||||
android:background="#FFFFFF"
|
||||
android:textColor="#000000"
|
||||
android:entries="@array/chapter_array"
|
||||
android:prompt="@string/chapter_prompt"
|
||||
/>
|
||||
|
||||
<Button
|
||||
android:id="@+id/buttonCreateLobby"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Create Lobby"
|
||||
android:background="#4CAF50"
|
||||
android:textColor="#FFFFFF"
|
||||
android:layout_below="@id/spinnerChapter"
|
||||
android:layout_marginTop="16dp"/>
|
||||
</RelativeLayout>
|
@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal"
|
||||
android:layout_marginTop="5dp"
|
||||
android:background="@drawable/top_rounded_corner"
|
||||
android:padding="16dp">
|
||||
|
||||
<TextView
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:text="@string/lobby"
|
||||
android:textColor="@android:color/white"
|
||||
android:textSize="18sp"
|
||||
android:textStyle="bold" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/nbPlayer"
|
||||
android:textColor="@android:color/white"
|
||||
android:textSize="18sp"
|
||||
android:textStyle="bold" />
|
||||
|
||||
</LinearLayout>
|
Loading…
Reference in new issue