Creadas clases para manejar el juego

master
Araozu 2020-09-22 20:20:23 -05:00
parent aaa95cc784
commit ea4c05c15e
5 changed files with 86 additions and 42 deletions

7
src/EstadoJuego.kt Normal file
View File

@ -0,0 +1,7 @@
package dev.araozu
enum class EstadoJuego {
Espera,
Iniciado,
Terminado
}

View File

@ -2,21 +2,6 @@ package dev.araozu
import io.ktor.http.cio.websocket.* import io.ktor.http.cio.websocket.*
data class Juego(val usuarios: ArrayList<Pair<String, Boolean>>) {
private val cartas: Array<Int> = GestorJuegos.generarCartas()
val conexiones: ArrayList<WebSocketSession> = arrayListOf()
fun agregarConexion(conexion: WebSocketSession) {
conexiones.add(conexion)
}
fun agregarUsuario(idUsuario: String) {
usuarios.add(Pair(idUsuario, true))
}
}
object GestorJuegos { object GestorJuegos {
private val todasCartas = arrayOf( private val todasCartas = arrayOf(

View File

@ -1,41 +1,49 @@
package dev.araozu package dev.araozu
import io.ktor.application.* import io.ktor.http.cio.websocket.*
import io.ktor.http.*
import io.ktor.request.*
import io.ktor.response.*
import io.ktor.routing.*
data class InfoJuego(val id: String) class Juego(val usuarios: ArrayList<Pair<String, Boolean>>) {
data class DataCrearJuego(val idUsuario: String)
fun Routing.crearJuego() { private val cartas: Array<Int> = GestorJuegos.generarCartas()
val conexiones: ArrayList<WebSocketSession> = arrayListOf()
private val manos: HashMap<String, Mano> = HashMap()
private val dora: ArrayList<Int> = arrayListOf()
var estadoJuego = EstadoJuego.Espera
var posCartaActual = 0
post("/partida") { suspend fun iniciarJuego() {
val dataUsuario = call.receive<DataCrearJuego>() if (estadoJuego != EstadoJuego.Espera) return
var sigId = GestorJuegos.generarId() estadoJuego = EstadoJuego.Iniciado
while (GestorJuegos.juegos.containsKey(sigId)) { for (i in posCartaActual until (posCartaActual + 10)) {
sigId = GestorJuegos.generarId() dora.add(cartas[i])
}
posCartaActual += 10
for ((idUsuario, _) in usuarios) {
val cartas = ArrayList<Int>()
for (i in posCartaActual until (posCartaActual + 10)) {
cartas.add(cartas[i])
}
posCartaActual += 10
val mano = Mano(cartas)
manos[idUsuario] = mano
} }
val juego = Juego(arrayListOf()) conexiones.forEach { socket ->
GestorJuegos.juegos[sigId] = juego socket.send(Frame.Text("{\"operacion\": \"juego_iniciado\"}"))
}
call.respondText("{\"id\": \"$sigId\"}", contentType = ContentType.Application.Json) conexiones.clear()
} }
post("/partida-join") { fun agregarConexion(conexion: WebSocketSession) {
val infoJuego = call.receive<InfoJuego>() conexiones.add(conexion)
if (infoJuego.id.length != 6) {
call.respondText("{\"error\": \"ID invalido.\"}", contentType = ContentType.Application.Json)
} }
if (GestorJuegos.juegos.containsKey(infoJuego.id)) { fun agregarUsuario(idUsuario: String) {
call.respondText("{\"ok\": true}", contentType = ContentType.Application.Json) usuarios.add(Pair(idUsuario, true))
} else {
call.respondText("{\"error\": \"El juego no existe\"}", contentType = ContentType.Application.Json)
}
} }
} }

3
src/Mano.kt Normal file
View File

@ -0,0 +1,3 @@
package dev.araozu
class Mano(cartas: ArrayList<Int>)

41
src/MetodosJuego.kt Normal file
View File

@ -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<DataCrearJuego>()
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<InfoJuego>()
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)
}
}
}