Ajout API(fonctionnel)

pull/10/head
Alexis1663 2 years ago
parent bbc903e116
commit 66117fc62e

@ -39,6 +39,9 @@ dependencies {
implementation 'com.google.android.material:material:1.8.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.fragment:fragment:1.5.5'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-moshi:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"

@ -0,0 +1,3 @@
package api
class Data(val users: List<User>)

@ -0,0 +1,9 @@
package api
import retrofit2.Call
import retrofit2.http.GET
interface TetrisAPI {
@GET("users/lists/xp")
fun getAllUsers() : Call<UserResponse>
}

@ -0,0 +1,54 @@
package api
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import okhttp3.OkHttpClient
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
class TetrisClient(okHttpClient: OkHttpClient) {
private val api: TetrisAPI
init {
// Create Gson object with lenient policy
val gson = GsonBuilder()
.setLenient()
.create()
// Create Retrofit instance
val retrofit = Retrofit.Builder()
.baseUrl("https://ch.tetr.io/api/")
.addConverterFactory(GsonConverterFactory.create(gson))
.client(okHttpClient)
.build()
// Create API instance
api = retrofit.create(TetrisAPI::class.java)
}
fun getUsers(callback: (List<User>?, Throwable?) -> Unit) {
api.getAllUsers().enqueue(object : Callback<UserResponse> {
override fun onResponse(call: Call<UserResponse>, response: Response<UserResponse>) {
if (response.isSuccessful) {
// API call successful, parse response body
val users = response.body()?.data?.users
callback(users, null)
} else {
// API call failed, handle error
val error = Exception("API call failed with code ${response.code()}")
callback(null, error)
}
}
override fun onFailure(call: Call<UserResponse>, t: Throwable) {
// API call failed, handle error
callback(null, t)
}
})
}
}

@ -0,0 +1,14 @@
package api
class User(val id: String,
val username: String,
val role: String,
val xp: Double,
val supporter: Boolean,
val verified: Boolean,
val country: String,
val timestamp: String,
val gamesPlayed: Int,
val gamesWon: Int,
val gameTime: Int) {
}

@ -0,0 +1,3 @@
package api
class UserResponse(val success: Boolean, val data: Data)

@ -0,0 +1,82 @@
package but.androidstudio.tetris
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import api.TetrisAPI
import api.TetrisClient
import api.User
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import okhttp3.OkHttpClient
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.io.InputStream
import java.security.KeyStore
import java.security.SecureRandom
import java.security.cert.CertificateFactory
import javax.net.ssl.SSLContext
import javax.net.ssl.TrustManagerFactory
import javax.net.ssl.X509TrustManager
class ClassementFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_classement, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// Read the SSL certificate for the request
val certificateStream : InputStream = resources.openRawResource(R.raw.sni_cloudflaressl_com)
// We create a certificateFactory to extract de data of the certificateInputStream
val certificateFactory = CertificateFactory.getInstance("X.509")
val certificate = certificateFactory.generateCertificate(certificateStream)
//To specified the certificate to used
val keyStore = KeyStore.getInstance(KeyStore.getDefaultType())
keyStore.load(null, null)
keyStore.setCertificateEntry("ca", certificate)
val trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm())
trustManagerFactory.init(keyStore)
//To create a SSL context
val sslContext = SSLContext.getInstance("SSL")
sslContext.init(null, trustManagerFactory.trustManagers, SecureRandom())
// We create a OkHttpClient to use the SSLContext
val okHttpClient: OkHttpClient = OkHttpClient.Builder()
.sslSocketFactory(sslContext.socketFactory, trustManagerFactory.trustManagers[0] as X509TrustManager)
.build()
val client = TetrisClient(okHttpClient)
client.getUsers { users, error ->
if (error != null) {
// Handle error
println("Error: ${error.message}")
} else {
// Handle success
users?.forEach {
println("User: ${it.username}")
}
}
}
}
}

@ -34,5 +34,13 @@ class MainFragment : Fragment() {
.replace(R.id.homeLayout, OptionFragment())
.commit()
}
val buttonClassement: Button = view.findViewById(R.id.buttonClassement)
buttonClassement.setOnClickListener {
val fragmentManager = requireActivity().supportFragmentManager
fragmentManager.beginTransaction()
.replace(R.id.homeLayout, ClassementFragment())
.commit()
}
}
}

@ -3,6 +3,7 @@ package modele
import android.util.Log
import kotlinx.coroutines.delay
import views.ViewsGame
import kotlin.random.Random
class DashBoard(private val width: Int,private val height: Int,private val view: ViewsGame) {
val gridOfGame = Array(this.height) { IntArray(this.width) }

@ -54,25 +54,24 @@ class Shape(val typeShape: TypeShape,var position: Position) {
return sharePosition
}
<<<<<<< HEAD
fun sharePositionUp(shape: Shape): MutableList<Position>{
fun sharePositionUp(shape: Shape): MutableList<Position> {
val sharePosition = mutableListOf<Position>()
for( line in 0..3){
for (line in 0..3) {
for (column in 0..3) {
if ( line != 3 ){
if ( (typeShape.showShape[line][column] == 1) and (typeShape.showShape[line+1][column] == 0)){
sharePosition.add(Position(column,line))
if (line != 3) {
if ((typeShape.showShape[line][column] == 1) and (typeShape.showShape[line + 1][column] == 0)) {
sharePosition.add(Position(column, line))
}
}
if ( (line == 3) and (typeShape.showShape[line][column] == 1) ){
sharePosition.add(Position(column,line))
if ((line == 3) and (typeShape.showShape[line][column] == 1)) {
sharePosition.add(Position(column, line))
}
}
}
return sharePosition
=======
}
fun sharePositionRotationRight(shape: Shape):Array<IntArray>{
var leftmostCol = 4
@ -133,6 +132,5 @@ class Shape(val typeShape: TypeShape,var position: Position) {
}
}
return rotatedMatrix
>>>>>>> Enzo
}
}

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ClassementFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</FrameLayout>

@ -38,6 +38,12 @@
android:layout_height="wrap_content"
android:text="@string/start" />
<Button
android:id="@+id/buttonClassement"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Classement" />
<Button
android:id="@+id/buttonOption"
android:layout_width="match_parent"

Loading…
Cancel
Save