parent
e5c118d88b
commit
94f7927f17
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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
|
||||||
|
)
|
@ -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…
Reference in new issue