Agregado codigo para aceptar solicitudes de iniciar el juego, y establecer las cartas del juego iniciado.

master
Araozu 2020-09-22 21:11:15 -05:00
parent ea4c05c15e
commit 806247ce56
4 changed files with 37 additions and 11 deletions

View File

@ -42,7 +42,7 @@ object GestorJuegos {
}
private suspend fun broadcast(juego: Juego, mensaje: String) {
juego.conexiones.forEach { socket ->
juego.conexiones.forEach { (_, socket) ->
socket.send(Frame.Text(mensaje))
}
}
@ -61,9 +61,18 @@ object GestorJuegos {
str += "{\"idUsuario\": \"$idUsuarioAct\", \"nombreUsuario\": \"$nombreUsuarioAct\"}"
}
str += "]"
juego.agregarConexion(ws)
juego.agregarConexion(idUsuario, ws)
juego.agregarUsuario(idUsuario)
ws.send(Frame.Text("{\"operacion\": \"conexion_exitosa\", \"jugadores\": $str}"))
}
suspend fun iniciarJuego(idJuego: String, ws: WebSocketSession) {
val juego = juegos[idJuego]
if (juego != null) {
juego.iniciarJuego(ws)
} else {
ws.send(Frame.Text("{\"operacion\": \"error\", \"razon\": \"Juego invalido\"}"))
}
}
}

View File

@ -5,15 +5,21 @@ import io.ktor.http.cio.websocket.*
class Juego(val usuarios: ArrayList<Pair<String, Boolean>>) {
private val cartas: Array<Int> = GestorJuegos.generarCartas()
val conexiones: ArrayList<WebSocketSession> = arrayListOf()
val conexiones: HashMap<String, WebSocketSession> = HashMap()
private val manos: HashMap<String, Mano> = HashMap()
private val dora: ArrayList<Int> = arrayListOf()
var estadoJuego = EstadoJuego.Espera
var posCartaActual = 0
var doraDescubiertos = 1
suspend fun iniciarJuego() {
suspend fun iniciarJuego(ws: WebSocketSession) {
if (estadoJuego != EstadoJuego.Espera) return
if (conexiones.size < 4) {
ws.send(Frame.Text("{\"operacion\": \"error\", \"razon\": \"Usuarios insuficientes\"}"))
return
}
estadoJuego = EstadoJuego.Iniciado
for (i in posCartaActual until (posCartaActual + 10)) {
dora.add(cartas[i])
@ -21,25 +27,26 @@ class Juego(val usuarios: ArrayList<Pair<String, Boolean>>) {
posCartaActual += 10
for ((idUsuario, _) in usuarios) {
val cartas = ArrayList<Int>()
val cartasL = arrayListOf<Int>()
for (i in posCartaActual until (posCartaActual + 10)) {
cartas.add(cartas[i])
cartasL.add(cartas[i])
}
posCartaActual += 10
val mano = Mano(cartas)
val mano = Mano(cartasL)
manos[idUsuario] = mano
}
conexiones.forEach { socket ->
conexiones.forEach { (_, socket) ->
socket.send(Frame.Text("{\"operacion\": \"juego_iniciado\"}"))
}
conexiones.clear()
println("Parametros del juego creados!")
}
fun agregarConexion(conexion: WebSocketSession) {
conexiones.add(conexion)
fun agregarConexion(idUsuario: String, conexion: WebSocketSession) {
conexiones[idUsuario] = conexion
}
fun agregarUsuario(idUsuario: String) {

View File

@ -1,3 +1,9 @@
package dev.araozu
class Mano(cartas: ArrayList<Int>)
class Mano(cartas: ArrayList<Int>) {
val allIn: Boolean = false
val cartaSig: Int? = null
val cartasReveladas: ArrayList<ArrayList<Int>> = ArrayList()
}

View File

@ -22,6 +22,10 @@ fun Routing.wsjuego() {
val datos2 = gson.fromJson(datos.datos, ConexionNueva::class.java)
GestorJuegos.conectarASala(datos2.idJuego, datos2.idUsuario, this)
}
"iniciar" -> {
val datos2 = gson.fromJson(datos.datos, ConexionNueva::class.java)
GestorJuegos.iniciarJuego(datos2.idJuego, this)
}
}
}
}