diff --git a/app/build.gradle.kts b/app/build.gradle.kts index b804642965d1e8400d19803db8dac013da53168d..e1b5475c93287bc7d72df7e143b080c2c63e8fd8 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -1,6 +1,9 @@ plugins { - alias(libs.plugins.android.application) - alias(libs.plugins.kotlin.android) + id("com.android.application") + id("com.google.gms.google-services") + + id("org.jetbrains.kotlin.android") + } android { @@ -36,6 +39,9 @@ android { } dependencies { + implementation(platform(libs.firebase.bom)) + implementation(libs.firebase.auth) + implementation(libs.firebase.firestore) implementation(libs.androidx.core.ktx) implementation(libs.androidx.appcompat) @@ -45,4 +51,5 @@ dependencies { testImplementation(libs.junit) androidTestImplementation(libs.androidx.junit) androidTestImplementation(libs.androidx.espresso.core) + } \ No newline at end of file diff --git a/app/google-services.json b/app/google-services.json new file mode 100644 index 0000000000000000000000000000000000000000..109d1dfd664338e6c4ce4f8527f194b94b489290 --- /dev/null +++ b/app/google-services.json @@ -0,0 +1,29 @@ +{ + "project_info": { + "project_number": "789583009455", + "project_id": "legado-de-pintia", + "storage_bucket": "legado-de-pintia.firebasestorage.app" + }, + "client": [ + { + "client_info": { + "mobilesdk_app_id": "1:789583009455:android:d0e7031feda3582a53665a", + "android_client_info": { + "package_name": "com.example.ellegadodepintia" + } + }, + "oauth_client": [], + "api_key": [ + { + "current_key": "AIzaSyAikVMefC2ZB7mXHzZm4OUA7Q-ADdOWsAM" + } + ], + "services": { + "appinvite_service": { + "other_platform_oauth_client": [] + } + } + } + ], + "configuration_version": "1" +} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 1f4cfbfa4d0289284474903fa778d081c8b4f0a0..0879419ff1a82035c12a84bf18d72c0588c54f6e 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -20,7 +20,7 @@ <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> - <activity android:name=".RegisterActivity" android:exported="true"/> + <activity android:name=".AuthActivity" android:exported="true"/> <activity android:name=".MapActivity" android:exported="true"/> <activity android:name=".minijuego1.Minijuego1" android:exported="true"/> <activity android:name=".minijuego2.Minijuego2" android:exported="true"/> diff --git a/app/src/main/java/com/example/ellegadodepintia/AuthActivity.kt b/app/src/main/java/com/example/ellegadodepintia/AuthActivity.kt new file mode 100644 index 0000000000000000000000000000000000000000..8735ce17b52b8b2f790f038d86a6bdd091a78a9f --- /dev/null +++ b/app/src/main/java/com/example/ellegadodepintia/AuthActivity.kt @@ -0,0 +1,76 @@ +package com.example.ellegadodepintia + +import android.content.Intent +import android.os.Bundle +import android.widget.Button +import android.widget.TextView +import androidx.appcompat.app.AlertDialog +import androidx.appcompat.app.AppCompatActivity +import com.example.ellegadodepintia.exploradoresDePintia.repositorios.RepositorioJugador +import com.google.firebase.auth.FirebaseAuth + +class AuthActivity : AppCompatActivity() { + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_register) + + + val botonRegistrarse = findViewById<Button>(R.id.buttonRegister) + val textoEmail = findViewById<TextView>(R.id.editTextEmail).text + val textoPassword = findViewById<TextView>(R.id.editTextPassword).text + + botonRegistrarse.setOnClickListener { + if (textoEmail.isNotEmpty() && textoPassword.isNotEmpty()) { + FirebaseAuth.getInstance().createUserWithEmailAndPassword( + textoEmail.toString(), + textoPassword.toString() + ).addOnCompleteListener { + if (it.isSuccessful) { + RepositorioJugador.addJugador(textoEmail.toString()) + RepositorioJugador.cargarJugador(textoEmail.toString()) + mostrarMapActivity() + + } else { + showAlert() + } + } + } + } + + val botonIniciarSesion = findViewById<Button>(R.id.buttonLogIn) + botonIniciarSesion.setOnClickListener { + if (textoEmail.isNotEmpty() && textoPassword.isNotEmpty()) { + FirebaseAuth.getInstance().signInWithEmailAndPassword( + textoEmail.toString(), + textoPassword.toString() + ).addOnCompleteListener { + if (it.isSuccessful) { + RepositorioJugador.cargarJugador(textoEmail.toString()) + mostrarMapActivity() + } else { + showAlert() + } + } + } + } + + } + + private fun showAlert() { + val builder = AlertDialog.Builder(this) + builder.setTitle("Error") + builder.setMessage("Se ha producido un error en la autenticación") + builder.setPositiveButton("Aceptar", null) + val dialog: AlertDialog = builder.create() + dialog.show() + } + + private fun mostrarMapActivity() { + val intent = Intent(this, MapActivity::class.java) + startActivity(intent) + } + + + +} diff --git a/app/src/main/java/com/example/ellegadodepintia/MainActivity.kt b/app/src/main/java/com/example/ellegadodepintia/MainActivity.kt index 9f593d56135a06a9f7b918ab2598bf5105702925..8b33efeef65498bb235bc889c3f244fd76002507 100644 --- a/app/src/main/java/com/example/ellegadodepintia/MainActivity.kt +++ b/app/src/main/java/com/example/ellegadodepintia/MainActivity.kt @@ -12,7 +12,7 @@ class MainActivity : AppCompatActivity() { val signInButton = findViewById<Button>(R.id.signInButton) signInButton.setOnClickListener { - val intent = Intent(this, ListaMinijuegos::class.java) + val intent = Intent(this, AuthActivity::class.java) startActivity(intent) } diff --git a/app/src/main/java/com/example/ellegadodepintia/RegisterActivity.kt b/app/src/main/java/com/example/ellegadodepintia/RegisterActivity.kt deleted file mode 100644 index b165de7e7eb953bc8dabb0a61c835d1d5c642b91..0000000000000000000000000000000000000000 --- a/app/src/main/java/com/example/ellegadodepintia/RegisterActivity.kt +++ /dev/null @@ -1,11 +0,0 @@ -package com.example.ellegadodepintia - -import android.os.Bundle -import androidx.appcompat.app.AppCompatActivity - -class RegisterActivity : AppCompatActivity() { - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - setContentView(R.layout.activity_register) - } -} diff --git a/app/src/main/java/com/example/ellegadodepintia/exploradoresDePintia/model/GameState.kt b/app/src/main/java/com/example/ellegadodepintia/exploradoresDePintia/model/GameState.kt index 40d3c2628189e8b8106cd1115876ac81340340e0..a7fd02f19492e2e6f75bdbbb4474eab7ebf6f26b 100644 --- a/app/src/main/java/com/example/ellegadodepintia/exploradoresDePintia/model/GameState.kt +++ b/app/src/main/java/com/example/ellegadodepintia/exploradoresDePintia/model/GameState.kt @@ -9,7 +9,7 @@ import java.lang.ref.WeakReference object GameState { lateinit var eventoActual: Evento lateinit var fragmentManager : FragmentManager - var jugador = Jugador() + lateinit var jugador: Jugador private var contexto: WeakReference<Context>? = null val context: Activity? @@ -19,6 +19,7 @@ object GameState { contexto = context } + //TODO() Este reset no va fun reset() { jugador = Jugador() contexto = null diff --git a/app/src/main/java/com/example/ellegadodepintia/exploradoresDePintia/model/Jugador.kt b/app/src/main/java/com/example/ellegadodepintia/exploradoresDePintia/model/Jugador.kt index b8682e4f4ff2c0c497a41f222097f66adc68ab5c..e5491a34e7db240be5146e0cd439fbb1a18c3ad4 100644 --- a/app/src/main/java/com/example/ellegadodepintia/exploradoresDePintia/model/Jugador.kt +++ b/app/src/main/java/com/example/ellegadodepintia/exploradoresDePintia/model/Jugador.kt @@ -17,6 +17,7 @@ class Jugador { var equipables = ArrayList<Objeto>(3) var habilidades = ArrayList<Habilidad>(1) var buffos = ArrayList<EfectoBuffo>() + var monedas = 0 init { atributos[Atributo.Vida] = 10 diff --git a/app/src/main/java/com/example/ellegadodepintia/exploradoresDePintia/repositorios/RepositorioJugador.kt b/app/src/main/java/com/example/ellegadodepintia/exploradoresDePintia/repositorios/RepositorioJugador.kt new file mode 100644 index 0000000000000000000000000000000000000000..96304d0095b9271390bff259bfb3a03e30e77cde --- /dev/null +++ b/app/src/main/java/com/example/ellegadodepintia/exploradoresDePintia/repositorios/RepositorioJugador.kt @@ -0,0 +1,31 @@ +package com.example.ellegadodepintia.exploradoresDePintia.repositorios + +import com.example.ellegadodepintia.exploradoresDePintia.model.GameState +import com.example.ellegadodepintia.exploradoresDePintia.model.Jugador +import com.google.firebase.firestore.FirebaseFirestore + +object RepositorioJugador{ + private val db by lazy { FirebaseFirestore.getInstance() } + private lateinit var email: String + + fun cargarJugador(email: String){ + this.email = email + db.collection("users").document(email).get().addOnSuccessListener { + GameState.jugador = Jugador() + GameState.jugador.monedas = (it.get("monedas") as Long).toInt() + } + } + + fun addJugador(email: String){ + db.collection("users").document(email).set( + hashMapOf("monedas" to 0) + ) + } + + fun setMonedas(monedas: Int){ + db.collection("users").document(email).set( + hashMapOf("monedas" to monedas) + ) + } + +} diff --git a/app/src/main/res/layout/activity_register.xml b/app/src/main/res/layout/activity_register.xml index daf4c24b9676f0358113a4e23ef62cdf7d69adb4..a6d7393f8590721fd66e9567201d8e1e878ec991 100644 --- a/app/src/main/res/layout/activity_register.xml +++ b/app/src/main/res/layout/activity_register.xml @@ -22,7 +22,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" - android:text="Registrar Nuevo Usuario" + android:text="Inicio o registro de usuario" android:textColor="@color/pintiaTitleText" android:textSize="24sp" android:textStyle="bold" @@ -54,15 +54,35 @@ android:backgroundTint="@color/pintiaInput" tools:ignore="HardcodedText" /> - <Button - android:id="@+id/buttonRegister" - android:layout_width="match_parent" + <LinearLayout + android:layout_width="wrap_content" android:layout_height="wrap_content" - android:text="Registrarse" - android:layout_marginTop="20dp" - android:backgroundTint="@color/pintiaButton" - android:textColor="@color/pintiaButtonText" - android:textStyle="bold" - tools:ignore="HardcodedText" /> + android:orientation="horizontal"> + + <Button + android:id="@+id/buttonRegister" + android:layout_width="180dp" + android:layout_height="wrap_content" + android:layout_marginTop="20dp" + android:layout_weight="1" + android:backgroundTint="@color/pintiaButton" + android:text="Registrarse" + android:textColor="@color/pintiaButtonText" + android:textStyle="bold" + tools:ignore="ButtonStyle,HardcodedText,InefficientWeight" /> + + <Button + android:id="@+id/buttonLogIn" + android:layout_width="180dp" + android:layout_height="wrap_content" + android:layout_marginStart="20dp" + android:layout_marginTop="20dp" + android:backgroundTint="@color/pintiaButton" + android:text="Iniciar sesión" + android:textColor="@color/pintiaButtonText" + android:textStyle="bold" + tools:ignore="ButtonStyle,HardcodedText" /> + + </LinearLayout> </LinearLayout> diff --git a/build.gradle.kts b/build.gradle.kts index 922f551105e1b8c176391b38c9436d45f172b546..b8e06aa71bcb43e8649b99ffeae9eb91806ae00b 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -2,4 +2,5 @@ plugins { alias(libs.plugins.android.application) apply false alias(libs.plugins.kotlin.android) apply false + alias(libs.plugins.google.services) apply false } \ No newline at end of file diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 4b29045fcc3b37a1ff3e19d9a9a6bcb8199dd932..549facd89db119140ee2000ad961011265dc182b 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,17 +1,20 @@ [versions] agp = "8.7.2" +firebaseBom = "33.6.0" kotlin = "1.9.24" -coreKtx = "1.10.1" +coreKtx = "1.15.0" junit = "4.13.2" -junitVersion = "1.1.5" -espressoCore = "3.5.1" -appcompat = "1.6.1" -material = "1.10.0" -activity = "1.8.0" -constraintlayout = "2.1.4" +junitVersion = "1.2.1" +espressoCore = "3.6.1" +appcompat = "1.7.0" +material = "1.12.0" +activity = "1.9.3" +constraintlayout = "2.2.0" +firestore = "25.1.1" [libraries] androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" } +firebase-bom = { module = "com.google.firebase:firebase-bom", version.ref = "firebaseBom" } junit = { group = "junit", name = "junit", version.ref = "junit" } androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" } androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" } @@ -19,8 +22,10 @@ androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version material = { group = "com.google.android.material", name = "material", version.ref = "material" } androidx-activity = { group = "androidx.activity", name = "activity", version.ref = "activity" } androidx-constraintlayout = { group = "androidx.constraintlayout", name = "constraintlayout", version.ref = "constraintlayout" } - +firebase-auth = { module = "com.google.firebase:firebase-auth-ktx" } +firebase-firestore = { module = "com.google.firebase:firebase-firestore-ktx", version.ref = "firestore" } [plugins] android-application = { id = "com.android.application", version.ref = "agp" } kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" } +google-services = { id = "com.google.gms.google-services", version = "4.4.2" }