From ea4c05c15e9a5cbefc9a977839f00b4a0dfc9fa5 Mon Sep 17 00:00:00 2001 From: Araozu Date: Tue, 22 Sep 2020 20:20:23 -0500 Subject: [PATCH] Creadas clases para manejar el juego --- src/EstadoJuego.kt | 7 +++++ src/GestorJuegos.kt | 15 ----------- src/Juego.kt | 62 +++++++++++++++++++++++++-------------------- src/Mano.kt | 3 +++ src/MetodosJuego.kt | 41 ++++++++++++++++++++++++++++++ 5 files changed, 86 insertions(+), 42 deletions(-) create mode 100644 src/EstadoJuego.kt create mode 100644 src/Mano.kt create mode 100644 src/MetodosJuego.kt diff --git a/src/EstadoJuego.kt b/src/EstadoJuego.kt new file mode 100644 index 0000000..7eb52eb --- /dev/null +++ b/src/EstadoJuego.kt @@ -0,0 +1,7 @@ +package dev.araozu + +enum class EstadoJuego { + Espera, + Iniciado, + Terminado +} diff --git a/src/GestorJuegos.kt b/src/GestorJuegos.kt index 046bee9..6184b71 100644 --- a/src/GestorJuegos.kt +++ b/src/GestorJuegos.kt @@ -2,21 +2,6 @@ package dev.araozu import io.ktor.http.cio.websocket.* -data class Juego(val usuarios: ArrayList>) { - - private val cartas: Array = GestorJuegos.generarCartas() - val conexiones: ArrayList = arrayListOf() - - fun agregarConexion(conexion: WebSocketSession) { - conexiones.add(conexion) - } - - fun agregarUsuario(idUsuario: String) { - usuarios.add(Pair(idUsuario, true)) - } - -} - object GestorJuegos { private val todasCartas = arrayOf( diff --git a/src/Juego.kt b/src/Juego.kt index 84951a3..53c730c 100644 --- a/src/Juego.kt +++ b/src/Juego.kt @@ -1,41 +1,49 @@ package dev.araozu -import io.ktor.application.* -import io.ktor.http.* -import io.ktor.request.* -import io.ktor.response.* -import io.ktor.routing.* +import io.ktor.http.cio.websocket.* -data class InfoJuego(val id: String) -data class DataCrearJuego(val idUsuario: String) +class Juego(val usuarios: ArrayList>) { -fun Routing.crearJuego() { + private val cartas: Array = GestorJuegos.generarCartas() + val conexiones: ArrayList = arrayListOf() + private val manos: HashMap = HashMap() + private val dora: ArrayList = arrayListOf() + var estadoJuego = EstadoJuego.Espera + var posCartaActual = 0 - post("/partida") { - val dataUsuario = call.receive() + suspend fun iniciarJuego() { + if (estadoJuego != EstadoJuego.Espera) return - var sigId = GestorJuegos.generarId() - while (GestorJuegos.juegos.containsKey(sigId)) { - sigId = GestorJuegos.generarId() + estadoJuego = EstadoJuego.Iniciado + for (i in posCartaActual until (posCartaActual + 10)) { + dora.add(cartas[i]) + } + posCartaActual += 10 + + for ((idUsuario, _) in usuarios) { + val cartas = ArrayList() + + for (i in posCartaActual until (posCartaActual + 10)) { + cartas.add(cartas[i]) + } + posCartaActual += 10 + + val mano = Mano(cartas) + manos[idUsuario] = mano } - val juego = Juego(arrayListOf()) - GestorJuegos.juegos[sigId] = juego - - call.respondText("{\"id\": \"$sigId\"}", contentType = ContentType.Application.Json) + conexiones.forEach { socket -> + socket.send(Frame.Text("{\"operacion\": \"juego_iniciado\"}")) + } + conexiones.clear() } - post("/partida-join") { - val infoJuego = call.receive() - if (infoJuego.id.length != 6) { - call.respondText("{\"error\": \"ID invalido.\"}", contentType = ContentType.Application.Json) - } + fun agregarConexion(conexion: WebSocketSession) { + conexiones.add(conexion) + } - if (GestorJuegos.juegos.containsKey(infoJuego.id)) { - call.respondText("{\"ok\": true}", contentType = ContentType.Application.Json) - } else { - call.respondText("{\"error\": \"El juego no existe\"}", contentType = ContentType.Application.Json) - } + fun agregarUsuario(idUsuario: String) { + usuarios.add(Pair(idUsuario, true)) } } diff --git a/src/Mano.kt b/src/Mano.kt new file mode 100644 index 0000000..c8821ca --- /dev/null +++ b/src/Mano.kt @@ -0,0 +1,3 @@ +package dev.araozu + +class Mano(cartas: ArrayList) diff --git a/src/MetodosJuego.kt b/src/MetodosJuego.kt new file mode 100644 index 0000000..84951a3 --- /dev/null +++ b/src/MetodosJuego.kt @@ -0,0 +1,41 @@ +package dev.araozu + +import io.ktor.application.* +import io.ktor.http.* +import io.ktor.request.* +import io.ktor.response.* +import io.ktor.routing.* + +data class InfoJuego(val id: String) +data class DataCrearJuego(val idUsuario: String) + +fun Routing.crearJuego() { + + post("/partida") { + val dataUsuario = call.receive() + + var sigId = GestorJuegos.generarId() + while (GestorJuegos.juegos.containsKey(sigId)) { + sigId = GestorJuegos.generarId() + } + + val juego = Juego(arrayListOf()) + GestorJuegos.juegos[sigId] = juego + + call.respondText("{\"id\": \"$sigId\"}", contentType = ContentType.Application.Json) + } + + post("/partida-join") { + val infoJuego = call.receive() + if (infoJuego.id.length != 6) { + call.respondText("{\"error\": \"ID invalido.\"}", contentType = ContentType.Application.Json) + } + + if (GestorJuegos.juegos.containsKey(infoJuego.id)) { + call.respondText("{\"ok\": true}", contentType = ContentType.Application.Json) + } else { + call.respondText("{\"error\": \"El juego no existe\"}", contentType = ContentType.Application.Json) + } + } + +}