feat : page lobby

androidCompose
Yvan CALATAYUD 1 year ago
parent e5c118d88b
commit 94f7927f17

@ -33,6 +33,7 @@ class MultiActivity : AppCompatActivity() {
listView.setOnItemClickListener { _, _, position, _ ->
val intent = Intent(this, ServerDetailsActivity::class.java)
intent.putExtra("serverName", serverList[position].name)
intent.putExtra("lobbyId", serverList[position].id)
startActivity(intent)
}
} else {

@ -1,11 +1,18 @@
package com.example.mathseduc
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.ListView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.example.mathseduc.controllers.ControllerPlayer
import com.example.mathseduc.models.Player
class ServerDetailsActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_server_details)
@ -16,5 +23,35 @@ class ServerDetailsActivity : AppCompatActivity() {
val serverNameTextView = findViewById<TextView>(R.id.titleServerDetails)
serverNameTextView.text = serverName
val lobbyId = intent.getIntExtra("lobbyId", -1)
val playerIds = ControllerPlayer.getPlayersIdFromLobbyId(lobbyId)
if (playerIds != null) {
val playerList = playerIds.mapNotNull { playerId ->
ControllerPlayer.getPlayerInfoById(playerId.toString())
}
val listViewPlayers = findViewById<ListView>(R.id.listViewPlayers)
val playerAdapter = PlayerAdapter(this, playerList)
listViewPlayers.adapter = playerAdapter
}
}
class PlayerAdapter(context: Context, players: List<Player>) :
ArrayAdapter<Player>(context, 0, players) {
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
var currentPlayerView = convertView
if (currentPlayerView == null) {
currentPlayerView =
LayoutInflater.from(context).inflate(R.layout.list_view_player, parent, false)
}
val playerNameTextView = currentPlayerView!!.findViewById<TextView>(R.id.playerName)
playerNameTextView.text = getItem(position)?.nickname ?: "Unknown"
// You can customize the view further if needed
return currentPlayerView
}
}
}

@ -0,0 +1,65 @@
package com.example.mathseduc.controllers
import android.os.StrictMode
import com.example.mathseduc.models.LobbyInfo
import com.example.mathseduc.models.Player
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import okhttp3.OkHttpClient
import okhttp3.Request
import java.io.IOException
class ControllerPlayer {
companion object {
fun getPlayersIdFromLobbyId(lobbyId: Int): List<Int>? {
val policy = StrictMode.ThreadPolicy.Builder().permitAll().build()
StrictMode.setThreadPolicy(policy)
val client = OkHttpClient()
val request = Request.Builder()
.url("https://trusting-panini.87-106-126-109.plesk.page/api/utiliser/$lobbyId/qUOGkWdoPCgbmuqxIC8xiaX0rV1Pw1LoPafkaoHOgszEyD9P2vcOu493xCDZpAqO")
.build()
client.newCall(request).execute().use { response ->
if (!response.isSuccessful) throw IOException("Unexpected code $response")
val gson = Gson()
val typeTokenProduct = object : TypeToken<List<LobbyInfo>>() {}.type
val playerInfoList: List<LobbyInfo> = gson.fromJson(response.body!!.string(), typeTokenProduct)
// Extract player IDs from PlayerInfo list
val playerIds: List<Int> = playerInfoList.map { it.idplayer }
return playerIds
}
return null
}
fun getPlayerInfoById(playerId: String): Player? {
val policy = StrictMode.ThreadPolicy.Builder().permitAll().build()
StrictMode.setThreadPolicy(policy)
val client = OkHttpClient()
val request = Request.Builder()
.url("https://trusting-panini.87-106-126-109.plesk.page/api/players/$playerId/qUOGkWdoPCgbmuqxIC8xiaX0rV1Pw1LoPafkaoHOgszEyD9P2vcOu493xCDZpAqO")
.build()
client.newCall(request).execute().use { response ->
if (!response.isSuccessful) throw IOException("Unexpected code $response")
val gson = Gson()
val playerInfo: List<Player> = gson.fromJson(response.body!!.string(), object : TypeToken<List<Player>>() {}.type)
// Assuming the API returns a list even for a single player, we take the first item
return playerInfo.firstOrNull()
}
return null
}
}
}

@ -1,7 +1,7 @@
package com.example.mathseduc.models
data class Lobby(
val id: String,
val id: Int,
val name: String,
val password: String,
val nbplayers: Int,

@ -0,0 +1,7 @@
package com.example.mathseduc.models
data class LobbyInfo (
val idlobby: Int,
val idplayer: Int,
val playertime: Int
)

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

@ -2,7 +2,7 @@
<LinearLayout 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:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".MultiActivity">

@ -2,15 +2,29 @@
<LinearLayout 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:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="16dp"
android:paddingTop="16dp"
android:paddingRight="16dp"
android:paddingBottom="16dp"
tools:context=".ServerDetailsActivity">
<TextView
android:id="@+id/titleServerDetails"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="LobbyTitle"
android:textColor="@color/white"
android:text="Bonjour depuis le serveur"
android:textSize="18sp"/>
</LinearLayout>
android:textSize="20dp"
android:paddingBottom="16dp"
android:layout_gravity="center"/>
<ListView
android:id="@+id/listViewPlayers"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/white" />
</LinearLayout>

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp" >
<!-- res/layout/list_view_player.xml -->
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/playerName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:textSize="18sp"
android:textColor="@color/black"/>
</FrameLayout>
Loading…
Cancel
Save