feat : ajout connexion d'un joueur + obligation d'être connecter pour jouer en multi
continuous-integration/drone/push Build is passing Details

androidCompose
Jeremy DUCOURTHIAL 1 year ago
parent f0483bb479
commit 70bd4fffd8

@ -10,7 +10,7 @@ android {
defaultConfig { defaultConfig {
applicationId "com.example.mathseduc" applicationId "com.example.mathseduc"
minSdk 21 minSdk 21
targetSdk 34 targetSdk 33
versionCode 1 versionCode 1
versionName "1.0" versionName "1.0"
@ -44,4 +44,5 @@ dependencies {
implementation("com.squareup.okhttp3:okhttp:4.10.0") implementation("com.squareup.okhttp3:okhttp:4.10.0")
implementation("com.google.code.gson:gson:2.10.1") implementation("com.google.code.gson:gson:2.10.1")
implementation "org.mindrot:jbcrypt:0.4"
} }

@ -22,5 +22,6 @@
<activity android:name=".MultiActivity" /> <activity android:name=".MultiActivity" />
<activity android:name=".ServerDetailsActivity" /> <activity android:name=".ServerDetailsActivity" />
<activity android:name=".CreateLobbyActivity" /> <activity android:name=".CreateLobbyActivity" />
<activity android:name=".ConnexionPlayerActivity" />
</application> </application>
</manifest> </manifest>

@ -0,0 +1,40 @@
package com.example.mathseduc
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.mathseduc.controllers.ControllerPlayer
import org.mindrot.jbcrypt.BCrypt
class ConnexionPlayerActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_connexion_player)
val buttonLogin = findViewById<Button>(R.id.buttonLogin)
val editTextNickname = findViewById<EditText>(R.id.editTextNickname)
val editTextPassword = findViewById<EditText>(R.id.editTextPassword)
buttonLogin.setOnClickListener {
val nickname = editTextNickname.text.toString()
val password = editTextPassword.text.toString()
val isAuthenticated = ControllerPlayer.authenticateUser(nickname,password)
if (isAuthenticated != -1) {
// Save authentication status for global accessibility
val intent = Intent(this, ServerDetailsActivity::class.java)
startActivity(intent)
MainActivity.idPlayerConnected = isAuthenticated
Toast.makeText(this, "Connexion réussi, bienvenue $nickname !!", Toast.LENGTH_SHORT).show()
} else {
// Show an error toast
Toast.makeText(this, "Connexion échoué. Veuillez réessayer.", Toast.LENGTH_SHORT).show()
}
}
}
}

@ -3,9 +3,13 @@ package com.example.mathseduc
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.widget.Button import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
companion object {
var idPlayerConnected: Int = -1
}
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -13,6 +17,7 @@ class MainActivity : AppCompatActivity() {
var btnSolo = findViewById<Button>(R.id.btnSolo) var btnSolo = findViewById<Button>(R.id.btnSolo)
var btnMulti = findViewById<Button>(R.id.btnMulti) var btnMulti = findViewById<Button>(R.id.btnMulti)
var btnConnexion = findViewById<Button>(R.id.btnConnexion)
btnSolo.setOnClickListener { btnSolo.setOnClickListener {
// Traitement pour le bouton Solo // Traitement pour le bouton Solo
@ -20,7 +25,17 @@ class MainActivity : AppCompatActivity() {
} }
btnMulti.setOnClickListener { btnMulti.setOnClickListener {
val intent = Intent(this, MultiActivity::class.java) if (MainActivity.idPlayerConnected != -1){
val intent = Intent(this, MultiActivity::class.java)
startActivity(intent)
}
else {
Toast.makeText(this, "Vous n'êtes pas connecté", Toast.LENGTH_SHORT).show()
}
}
btnConnexion.setOnClickListener {
val intent = Intent(this, ConnexionPlayerActivity::class.java)
startActivity(intent) startActivity(intent)
} }
} }

@ -1,12 +1,16 @@
package com.example.mathseduc.controllers package com.example.mathseduc.controllers
import android.os.StrictMode import android.os.StrictMode
import com.example.mathseduc.models.Chapter
import com.example.mathseduc.models.Lobby
import com.example.mathseduc.models.LobbyInfo import com.example.mathseduc.models.LobbyInfo
import com.example.mathseduc.models.Player import com.example.mathseduc.models.Player
import com.google.gson.Gson import com.google.gson.Gson
import com.google.gson.reflect.TypeToken import com.google.gson.reflect.TypeToken
import okhttp3.FormBody
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import okhttp3.Request import okhttp3.Request
import org.mindrot.jbcrypt.BCrypt
import java.io.IOException import java.io.IOException
class ControllerPlayer { class ControllerPlayer {
@ -61,5 +65,41 @@ class ControllerPlayer {
return null return null
} }
fun authenticateUser(nickname: String, password: String): Int {
val policy = StrictMode.ThreadPolicy.Builder().permitAll().build()
StrictMode.setThreadPolicy(policy)
// Client HTTP API
val client = OkHttpClient()
// API Access
val request = Request.Builder()
.url("https://trusting-panini.87-106-126-109.plesk.page/api/verif/player/$nickname/qUOGkWdoPCgbmuqxIC8xiaX0rV1Pw1LoPafkaoHOgszEyD9P2vcOu493xCDZpAqO")
.build()
// API Response
client.newCall(request).execute().use { response ->
if (response.isSuccessful) {
val responseData = response.body?.string()
// Use Gson to parse the JSON response
val gson = Gson()
val playerListType = object : TypeToken<List<Player>>() {}.type
val playerList: List<Player> = gson.fromJson(responseData, playerListType)
// Check if the list is not empty
if (playerList.isNotEmpty()) {
val storedPasswordHash = playerList[0].password
// Use BCrypt to check if the provided password matches the stored hash
if(BCrypt.checkpw(password, storedPasswordHash)){
return playerList[0].id
}
}
}
}
return -1
}
} }
} }

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

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:padding="16dp">
<EditText
android:id="@+id/editTextNickname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Nickname"
android:background="#FFFFFF"
android:padding="8dp"
android:textColor="#000000"
android:layout_marginBottom="16dp"/>
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
android:background="#FFFFFF"
android:padding="8dp"
android:textColor="#000000"
android:layout_below="@id/editTextNickname"
android:layout_marginTop="8dp"
android:layout_marginBottom="16dp"/>
<Button
android:id="@+id/buttonLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:background="#4CAF50"
android:textColor="#FFFFFF"
android:layout_below="@id/editTextPassword"
android:layout_marginTop="16dp"/>
</RelativeLayout>
Loading…
Cancel
Save