feat : ajout création d'un lobby en android
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
e5c118d88b
commit
fefae1ebfb
@ -0,0 +1,64 @@
|
||||
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 com.example.mathseduc.models.Lobby
|
||||
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().toInt()
|
||||
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
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
// Appeler la fonction pour créer le lobby
|
||||
val lobbyCreatedSuccessfully = ControllerLobby.createLobby(lobbyData)
|
||||
|
||||
// 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,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>
|
Loading…
Reference in new issue