feat : ajout création d'un lobby en android
continuous-integration/drone/push Build is passing Details

master
Jeremy DUCOURTHIAL 1 year ago
parent e5c118d88b
commit fefae1ebfb

@ -21,5 +21,6 @@
</activity>
<activity android:name=".MultiActivity" />
<activity android:name=".ServerDetailsActivity" />
<activity android:name=".CreateLobbyActivity" />
</application>
</manifest>

@ -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()
}
}
}
}

@ -2,13 +2,13 @@ package com.example.mathseduc
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.ListView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
@ -21,6 +21,8 @@ class MultiActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_multi)
var btnAddLobby = findViewById<Button>(R.id.btnAddLobby)
val serverList = ControllerLobby.getLobbies()
if (serverList != null) {
@ -38,6 +40,11 @@ class MultiActivity : AppCompatActivity() {
} else {
Log.e("MultiActivity", "Error fetching server list")
}
btnAddLobby.setOnClickListener {
val intent = Intent(this, CreateLobbyActivity::class.java)
startActivity(intent)
}
}
class LobbyAdapter(context: Context, lobbies: ArrayList<Lobby>): ArrayAdapter<Lobby>(context, 0, lobbies), View.OnClickListener {

@ -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
}
}
}

@ -1,11 +1,15 @@
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.MediaType.Companion.toMediaTypeOrNull
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONObject
import java.io.IOException
class ControllerLobby {
@ -33,8 +37,41 @@ class ControllerLobby {
return gson.fromJson(response.body!!.string(), typeTokenProduct)
}
return null
}
fun createLobby(lobbyData: JSONObject): Boolean {
try {
// Log avant l'appel API
Log.d("CreateLobby", "Request Data: ${lobbyData.toString()}")
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/lobbies/qUOGkWdoPCgbmuqxIC8xiaX0rV1Pw1LoPafkaoHOgszEyD9P2vcOu493xCDZpAqO")
.post(lobbyData.toString().toRequestBody("application/json".toMediaTypeOrNull()))
.build()
// API Response
client.newCall(request).execute().use { response ->
// Log après la réponse API
Log.d("CreateLobby", "Response Code: ${response.code}")
Log.d("CreateLobby", "Response Body: ${response.body?.string()}")
// 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
}
}
}
}

@ -0,0 +1,6 @@
package com.example.mathseduc.models
data class Chapter(
val id: String,
val name: String,
)

@ -5,7 +5,7 @@ data class Lobby(
val name: String,
val password: String,
val nbplayers: Int,
val idplayercreator: String,
val idplayercreator: Int,
val idchapter: Int,
val difficulty: Int
)

@ -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>

@ -8,4 +8,18 @@
<string name="addLobby">Ajouter un lobby</string>
<string name="lobby">lobby</string>
<string name="nbPlayer">nombre de joueurs</string>
<string name="difficulty_prompt">Selectionne la difficulty</string>
<string name="chapter_prompt">Selectionne un chapter</string>
<string-array name="difficulty_array">
<item>Facile</item>
<item>Moyen</item>
<item>Difficile</item>
</string-array>
<string-array name="chapter_array">
<item>Chapter 1</item>
<item>Chapter 2</item>
<item>Chapter 3</item>
</string-array>
</resources>
Loading…
Cancel
Save