Merge branch 'master' of https://codefirst.iut.uca.fr/git/jade.van_brabandt/3.01-QCM_MuscuMaths
commit
5b2f5f0d67
@ -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, MainActivity::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()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,102 @@
|
||||
package com.example.mathseduc.controllers
|
||||
|
||||
import android.os.StrictMode
|
||||
import com.example.mathseduc.models.*
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.reflect.TypeToken
|
||||
import okhttp3.FormBody
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import org.mindrot.jbcrypt.BCrypt
|
||||
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<Utiliser>>() {}.type
|
||||
|
||||
val playerInfoList: List<Utiliser> = 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
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package com.example.mathseduc.controllers
|
||||
|
||||
import android.os.StrictMode
|
||||
import android.util.Log
|
||||
import com.example.mathseduc.models.Lobby
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.reflect.TypeToken
|
||||
import okhttp3.MultipartBody
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import java.io.IOException
|
||||
|
||||
class ControllerUtiliser {
|
||||
companion object {
|
||||
|
||||
fun createUtiliserByIdLobby(utiliserData: MultipartBody.Builder): Boolean {
|
||||
try {
|
||||
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/add/utiliser/qUOGkWdoPCgbmuqxIC8xiaX0rV1Pw1LoPafkaoHOgszEyD9P2vcOu493xCDZpAqO")
|
||||
.post(utiliserData.build())
|
||||
.build()
|
||||
|
||||
// API Response
|
||||
client.newCall(request).execute().use { response ->
|
||||
// Vérifier si la création du lobby a réussi
|
||||
return response.isSuccessful
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
// Log en cas d'erreur
|
||||
Log.e("CreateLobby", "Error creating lobby", e)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.example.mathseduc.models
|
||||
|
||||
data class Player(
|
||||
val id: Int,
|
||||
val nickname: String,
|
||||
val password: String
|
||||
)
|
@ -0,0 +1,7 @@
|
||||
package com.example.mathseduc.models
|
||||
|
||||
data class Utiliser (
|
||||
val idlobby: Int,
|
||||
val idplayer: Int,
|
||||
val playertime: Int
|
||||
)
|
@ -1,99 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<Button
|
||||
android:id="@+id/btnReturn"
|
||||
android:layout_width="100dp"
|
||||
android:layout_height="60dp"
|
||||
android:layout_gravity="start"
|
||||
android:layout_marginTop="16dp"
|
||||
android:backgroundTint="@color/grey"
|
||||
android:text="@string/retour"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintHorizontal_bias="0.948"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
tools:ignore="MissingConstraints">
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center_horizontal"
|
||||
android:layout_marginBottom="15dp"
|
||||
android:fontFamily="@font/math_educ_font"
|
||||
android:text="@string/connexion"
|
||||
android:textColor="@color/white"
|
||||
android:textSize="40sp"/>
|
||||
|
||||
<!--<TextView
|
||||
android:id="@+id/errorTextView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginBottom="15dp"
|
||||
android:text=""
|
||||
android:textAlignment="center"
|
||||
android:textColor="@android:color/holo_red_dark"
|
||||
android:textSize="20sp" /> -->
|
||||
|
||||
<EditText
|
||||
android:id="@+id/nickname"
|
||||
android:layout_width="320dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="@drawable/rounded_corner"
|
||||
android:hint="@string/nom_d_utilisateur"
|
||||
android:layout_gravity="center_horizontal"
|
||||
android:paddingStart="8dp"
|
||||
android:inputType="text"
|
||||
android:minHeight="50dp"
|
||||
android:autofillHints=""
|
||||
tools:ignore="RtlSymmetry" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/password"
|
||||
android:layout_width="320dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="15dp"
|
||||
android:background="@drawable/rounded_corner"
|
||||
android:layout_gravity="center_horizontal"
|
||||
android:hint="@string/mot_de_passe"
|
||||
android:inputType="textPassword"
|
||||
android:minHeight="50dp"
|
||||
android:paddingStart="8dp"
|
||||
android:autofillHints=""
|
||||
tools:ignore="RtlSymmetry" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/loginButton"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="55dp"
|
||||
android:layout_gravity="center"
|
||||
android:layout_marginTop="13dp"
|
||||
android:backgroundTint="@color/blue"
|
||||
android:text="@string/se_connecter" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/registerButton"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="55dp"
|
||||
android:layout_gravity="center"
|
||||
android:layout_marginTop="3dp"
|
||||
android:backgroundTint="@color/blue"
|
||||
android:text="@string/s_inscrire" />
|
||||
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
@ -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>
|
@ -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