From 94f7927f17b5d337cb277f77e9529186c178d99c Mon Sep 17 00:00:00 2001 From: Yvan Date: Wed, 28 Feb 2024 15:59:14 +0100 Subject: [PATCH] feat : page lobby --- .../com/example/mathseduc/MultiActivity.kt | 1 + .../mathseduc/ServerDetailsActivity.kt | 39 ++++++++++- .../mathseduc/controllers/ControllerPlayer.kt | 65 +++++++++++++++++++ .../com/example/mathseduc/models/Lobby.kt | 2 +- .../com/example/mathseduc/models/LobbyInfo.kt | 7 ++ .../com/example/mathseduc/models/Player.kt | 6 ++ .../src/main/res/layout/activity_multi.xml | 2 +- .../res/layout/activity_server_details.xml | 24 +++++-- .../src/main/res/layout/list_view_player.xml | 18 +++++ 9 files changed, 156 insertions(+), 8 deletions(-) create mode 100644 Android/app/src/main/java/com/example/mathseduc/controllers/ControllerPlayer.kt create mode 100644 Android/app/src/main/java/com/example/mathseduc/models/LobbyInfo.kt create mode 100644 Android/app/src/main/java/com/example/mathseduc/models/Player.kt create mode 100644 Android/app/src/main/res/layout/list_view_player.xml diff --git a/Android/app/src/main/java/com/example/mathseduc/MultiActivity.kt b/Android/app/src/main/java/com/example/mathseduc/MultiActivity.kt index d50a265..cdb3e44 100644 --- a/Android/app/src/main/java/com/example/mathseduc/MultiActivity.kt +++ b/Android/app/src/main/java/com/example/mathseduc/MultiActivity.kt @@ -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 { diff --git a/Android/app/src/main/java/com/example/mathseduc/ServerDetailsActivity.kt b/Android/app/src/main/java/com/example/mathseduc/ServerDetailsActivity.kt index 277f246..d4e6208 100644 --- a/Android/app/src/main/java/com/example/mathseduc/ServerDetailsActivity.kt +++ b/Android/app/src/main/java/com/example/mathseduc/ServerDetailsActivity.kt @@ -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(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(R.id.listViewPlayers) + val playerAdapter = PlayerAdapter(this, playerList) + listViewPlayers.adapter = playerAdapter + } + } + + class PlayerAdapter(context: Context, players: List) : + ArrayAdapter(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(R.id.playerName) + playerNameTextView.text = getItem(position)?.nickname ?: "Unknown" + + // You can customize the view further if needed + + return currentPlayerView + } + } } \ No newline at end of file diff --git a/Android/app/src/main/java/com/example/mathseduc/controllers/ControllerPlayer.kt b/Android/app/src/main/java/com/example/mathseduc/controllers/ControllerPlayer.kt new file mode 100644 index 0000000..f9b80b0 --- /dev/null +++ b/Android/app/src/main/java/com/example/mathseduc/controllers/ControllerPlayer.kt @@ -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? { + 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>() {}.type + + val playerInfoList: List = gson.fromJson(response.body!!.string(), typeTokenProduct) + + // Extract player IDs from PlayerInfo list + val playerIds: List = 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 = gson.fromJson(response.body!!.string(), object : TypeToken>() {}.type) + + // Assuming the API returns a list even for a single player, we take the first item + return playerInfo.firstOrNull() + } + + return null + } + } +} diff --git a/Android/app/src/main/java/com/example/mathseduc/models/Lobby.kt b/Android/app/src/main/java/com/example/mathseduc/models/Lobby.kt index 5a12385..e4bb80c 100644 --- a/Android/app/src/main/java/com/example/mathseduc/models/Lobby.kt +++ b/Android/app/src/main/java/com/example/mathseduc/models/Lobby.kt @@ -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, diff --git a/Android/app/src/main/java/com/example/mathseduc/models/LobbyInfo.kt b/Android/app/src/main/java/com/example/mathseduc/models/LobbyInfo.kt new file mode 100644 index 0000000..6a60f1d --- /dev/null +++ b/Android/app/src/main/java/com/example/mathseduc/models/LobbyInfo.kt @@ -0,0 +1,7 @@ +package com.example.mathseduc.models + +data class LobbyInfo ( + val idlobby: Int, + val idplayer: Int, + val playertime: Int +) diff --git a/Android/app/src/main/java/com/example/mathseduc/models/Player.kt b/Android/app/src/main/java/com/example/mathseduc/models/Player.kt new file mode 100644 index 0000000..b18e749 --- /dev/null +++ b/Android/app/src/main/java/com/example/mathseduc/models/Player.kt @@ -0,0 +1,6 @@ +package com.example.mathseduc.models + +data class Player( + val id: Int, + val nickname: String +) diff --git a/Android/app/src/main/res/layout/activity_multi.xml b/Android/app/src/main/res/layout/activity_multi.xml index 7e3408f..8f6ddc0 100644 --- a/Android/app/src/main/res/layout/activity_multi.xml +++ b/Android/app/src/main/res/layout/activity_multi.xml @@ -2,7 +2,7 @@ diff --git a/Android/app/src/main/res/layout/activity_server_details.xml b/Android/app/src/main/res/layout/activity_server_details.xml index f4b412c..71adb3d 100644 --- a/Android/app/src/main/res/layout/activity_server_details.xml +++ b/Android/app/src/main/res/layout/activity_server_details.xml @@ -2,15 +2,29 @@ - \ No newline at end of file + android:textSize="20dp" + android:paddingBottom="16dp" + android:layout_gravity="center"/> + + + + diff --git a/Android/app/src/main/res/layout/list_view_player.xml b/Android/app/src/main/res/layout/list_view_player.xml new file mode 100644 index 0000000..653eedc --- /dev/null +++ b/Android/app/src/main/res/layout/list_view_player.xml @@ -0,0 +1,18 @@ + + + + + + + + +