|
|
@ -1,7 +1,7 @@
|
|
|
|
package com.example.mathseduc.ui
|
|
|
|
package com.example.mathseduc.ui
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.Intent
|
|
|
|
import android.content.Intent
|
|
|
|
import android.os.Handler
|
|
|
|
|
|
|
|
import android.os.Looper
|
|
|
|
|
|
|
|
import android.util.Log
|
|
|
|
import android.util.Log
|
|
|
|
import android.widget.Toast
|
|
|
|
import android.widget.Toast
|
|
|
|
import androidx.compose.foundation.background
|
|
|
|
import androidx.compose.foundation.background
|
|
|
@ -26,6 +26,8 @@ import androidx.compose.runtime.Composable
|
|
|
|
import androidx.compose.runtime.DisposableEffect
|
|
|
|
import androidx.compose.runtime.DisposableEffect
|
|
|
|
import androidx.compose.runtime.getValue
|
|
|
|
import androidx.compose.runtime.getValue
|
|
|
|
import androidx.compose.runtime.mutableStateOf
|
|
|
|
import androidx.compose.runtime.mutableStateOf
|
|
|
|
|
|
|
|
import androidx.compose.runtime.remember
|
|
|
|
|
|
|
|
import androidx.compose.runtime.rememberCoroutineScope
|
|
|
|
import androidx.compose.runtime.saveable.rememberSaveable
|
|
|
|
import androidx.compose.runtime.saveable.rememberSaveable
|
|
|
|
import androidx.compose.runtime.setValue
|
|
|
|
import androidx.compose.runtime.setValue
|
|
|
|
import androidx.compose.ui.Alignment
|
|
|
|
import androidx.compose.ui.Alignment
|
|
|
@ -42,37 +44,47 @@ import com.example.mathseduc.controllers.ControllerLobby
|
|
|
|
import com.example.mathseduc.controllers.ControllerUtiliser
|
|
|
|
import com.example.mathseduc.controllers.ControllerUtiliser
|
|
|
|
import com.example.mathseduc.models.Lobby
|
|
|
|
import com.example.mathseduc.models.Lobby
|
|
|
|
import com.example.mathseduc.ui.theme.Colors
|
|
|
|
import com.example.mathseduc.ui.theme.Colors
|
|
|
|
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
|
|
|
|
import kotlinx.coroutines.delay
|
|
|
|
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
|
|
|
|
import kotlinx.coroutines.withContext
|
|
|
|
import okhttp3.MultipartBody
|
|
|
|
import okhttp3.MultipartBody
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Composable
|
|
|
|
@Composable
|
|
|
|
fun MultiPage() {
|
|
|
|
fun MultiPage() {
|
|
|
|
val context = LocalContext.current
|
|
|
|
val context = LocalContext.current
|
|
|
|
var selectedItem by rememberSaveable { mutableStateOf<Lobby?>(null) }
|
|
|
|
var selectedItem by rememberSaveable { mutableStateOf<Lobby?>(null) }
|
|
|
|
|
|
|
|
var lobbyList by remember { mutableStateOf<List<Lobby>>(emptyList()) }
|
|
|
|
var lobbyList by rememberSaveable { mutableStateOf(ControllerLobby.getLobbies() ?: emptyList()) }
|
|
|
|
val scope = rememberCoroutineScope()
|
|
|
|
var refreshState by rememberSaveable { mutableStateOf(true) }
|
|
|
|
var refreshState by remember { mutableStateOf(true) }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Fonction pour actualiser la liste des lobbies
|
|
|
|
|
|
|
|
suspend fun refreshLobbyList() {
|
|
|
|
|
|
|
|
val refreshedLobbyList = withContext(Dispatchers.IO) {
|
|
|
|
|
|
|
|
ControllerLobby.getLobbies() ?: emptyList()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
lobbyList = refreshedLobbyList
|
|
|
|
|
|
|
|
Log.e("MainActivity", "Refreshed Lobby List")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DisposableEffect(refreshState) {
|
|
|
|
DisposableEffect(refreshState) {
|
|
|
|
val handler = Handler(Looper.getMainLooper())
|
|
|
|
val refreshRunnable = {
|
|
|
|
val refreshRunnable = object : Runnable {
|
|
|
|
scope.launch(Dispatchers.IO) {
|
|
|
|
override fun run() {
|
|
|
|
try {
|
|
|
|
lobbyList = emptyList()
|
|
|
|
while (refreshState) {
|
|
|
|
lobbyList = ControllerLobby.getLobbies()!!
|
|
|
|
delay(5000)
|
|
|
|
selectedItem = null
|
|
|
|
refreshLobbyList()
|
|
|
|
if (refreshState){
|
|
|
|
}
|
|
|
|
handler.postDelayed(this,3000)
|
|
|
|
} catch (e: Exception) {
|
|
|
|
Log.e("MainActivity", "Refresh Multi")
|
|
|
|
Log.e("MainActivity", "Error refreshing lobbies: ${e.message}")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
refreshRunnable.invoke()
|
|
|
|
handler.post(refreshRunnable)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
onDispose {
|
|
|
|
onDispose {
|
|
|
|
Toast.makeText(context, "Au REVOIR UwU", Toast.LENGTH_SHORT).show()
|
|
|
|
Toast.makeText(context, "Au REVOIR UwU", Toast.LENGTH_SHORT).show()
|
|
|
|
refreshState = false
|
|
|
|
refreshState = false
|
|
|
|
handler.removeCallbacks(refreshRunnable)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -117,7 +129,6 @@ fun MultiPage() {
|
|
|
|
Button(
|
|
|
|
Button(
|
|
|
|
onClick = {
|
|
|
|
onClick = {
|
|
|
|
refreshState = false
|
|
|
|
refreshState = false
|
|
|
|
|
|
|
|
|
|
|
|
val intent = Intent(context, CreateLobbyActivity::class.java)
|
|
|
|
val intent = Intent(context, CreateLobbyActivity::class.java)
|
|
|
|
context.startActivity(intent)
|
|
|
|
context.startActivity(intent)
|
|
|
|
},
|
|
|
|
},
|
|
|
@ -136,26 +147,23 @@ fun MultiPage() {
|
|
|
|
.fillMaxSize()
|
|
|
|
.fillMaxSize()
|
|
|
|
.weight(0.85f)
|
|
|
|
.weight(0.85f)
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
items(lobbyList ?: emptyList()) { lobby ->
|
|
|
|
items(lobbyList) { lobby ->
|
|
|
|
LobbyItem(lobby, selectedItem == lobby) {
|
|
|
|
LobbyItem(lobby, selectedItem == lobby) {
|
|
|
|
selectedItem = it
|
|
|
|
selectedItem = it
|
|
|
|
|
|
|
|
|
|
|
|
refreshState = false
|
|
|
|
refreshState = false
|
|
|
|
|
|
|
|
|
|
|
|
if (ControllerLobby.getNbPlayerInLobby(selectedItem!!.id) < selectedItem!!.nbplayers){
|
|
|
|
if (ControllerLobby.getNbPlayerInLobby(selectedItem!!.id) < selectedItem!!.nbplayers) {
|
|
|
|
val formDataBuilder = MultipartBody.Builder().setType(MultipartBody.FORM)
|
|
|
|
// Créer un Utiliser si le lobby n'est pas plein
|
|
|
|
formDataBuilder.addFormDataPart("idplayer", MainActivity.idPlayerConnected.toString())
|
|
|
|
scope.launch {
|
|
|
|
formDataBuilder.addFormDataPart("idlobby", selectedItem!!.id.toString())
|
|
|
|
createUtiliserForLobby(context, selectedItem!!)
|
|
|
|
formDataBuilder.addFormDataPart("playertime", "0")
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ControllerUtiliser.createUtiliserByIdLobby(formDataBuilder)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val intent = Intent(context, ServerDetailsActivity::class.java)
|
|
|
|
|
|
|
|
intent.putExtra("serverName", selectedItem!!.name)
|
|
|
|
|
|
|
|
intent.putExtra("lobbyId", selectedItem!!.id)
|
|
|
|
|
|
|
|
context.startActivity(intent)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
Toast.makeText(context, "Oh nan, le serveur est déjà plein ! Réessayer plus tard.", Toast.LENGTH_SHORT).show()
|
|
|
|
// Afficher un message Toast si le lobby est plein
|
|
|
|
|
|
|
|
Toast.makeText(
|
|
|
|
|
|
|
|
context,
|
|
|
|
|
|
|
|
"Oh nan, le serveur est déjà plein ! Réessayer plus tard.",
|
|
|
|
|
|
|
|
Toast.LENGTH_SHORT
|
|
|
|
|
|
|
|
).show()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -164,10 +172,26 @@ fun MultiPage() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Fonction pour créer un Utiliser pour le lobby sélectionné
|
|
|
|
|
|
|
|
private suspend fun createUtiliserForLobby(context: Context, lobby: Lobby) {
|
|
|
|
|
|
|
|
withContext(Dispatchers.IO) {
|
|
|
|
|
|
|
|
val formDataBuilder = MultipartBody.Builder().setType(MultipartBody.FORM)
|
|
|
|
|
|
|
|
formDataBuilder.addFormDataPart("idplayer", MainActivity.idPlayerConnected.toString())
|
|
|
|
|
|
|
|
formDataBuilder.addFormDataPart("idlobby", lobby.id.toString())
|
|
|
|
|
|
|
|
formDataBuilder.addFormDataPart("playertime", "0")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ControllerUtiliser.createUtiliserByIdLobby(formDataBuilder)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Naviguer vers l'activité ServerDetails avec les détails du lobby
|
|
|
|
|
|
|
|
val intent = Intent(context, ServerDetailsActivity::class.java)
|
|
|
|
|
|
|
|
intent.putExtra("serverName", lobby.name)
|
|
|
|
|
|
|
|
intent.putExtra("lobbyId", lobby.id)
|
|
|
|
|
|
|
|
context.startActivity(intent)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Composable
|
|
|
|
@Composable
|
|
|
|
fun LobbyItem(lobby: Lobby, isSelected: Boolean, onItemClick: (Lobby) -> Unit) {
|
|
|
|
fun LobbyItem(lobby: Lobby, isSelected: Boolean, onItemClick: (Lobby) -> Unit) {
|
|
|
|
val nbCurrentPlayer = ControllerLobby.getNbPlayerInLobby(lobby.id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Row(
|
|
|
|
Row(
|
|
|
|
modifier = Modifier
|
|
|
|
modifier = Modifier
|
|
|
|
.fillMaxWidth()
|
|
|
|
.fillMaxWidth()
|
|
|
@ -184,7 +208,6 @@ fun LobbyItem(lobby: Lobby, isSelected: Boolean, onItemClick: (Lobby) -> Unit) {
|
|
|
|
fontSize = 18.sp,
|
|
|
|
fontSize = 18.sp,
|
|
|
|
color = Colors.White,
|
|
|
|
color = Colors.White,
|
|
|
|
modifier = Modifier.weight(1f)
|
|
|
|
modifier = Modifier.weight(1f)
|
|
|
|
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
Text(
|
|
|
|
Text(
|
|
|
|
text = "${ControllerLobby.getNbPlayerInLobby(lobby.id)}/${lobby.nbplayers}",
|
|
|
|
text = "${ControllerLobby.getNbPlayerInLobby(lobby.id)}/${lobby.nbplayers}",
|
|
|
@ -205,5 +228,4 @@ fun LobbyItem(lobby: Lobby, isSelected: Boolean, onItemClick: (Lobby) -> Unit) {
|
|
|
|
modifier = Modifier.weight(1f)
|
|
|
|
modifier = Modifier.weight(1f)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|