MesasActivity.kt 4.21 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
package com.example.tolucagraphics

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.tolucagraphics.adapters.JugadoresAdapter
import com.example.tolucagraphics.datas.Data
import com.example.tolucagraphics.datas.JoinData
import com.example.tolucagraphics.datas.JoinTable
import com.example.tolucagraphics.events.Events
import com.example.tolucagraphics.events.UserElement
import com.google.gson.Gson
import kotlinx.android.synthetic.main.activity_mesas_players.*
import okhttp3.Response
import okhttp3.WebSocket
import okhttp3.WebSocketListener
import okio.ByteString

class MesasActivity : AppCompatActivity() {
    var selector = 1
    val gson = Gson()
    val decoJson = Gson()
    var eventoRecibido = Events()
    private val conection2 = LobbyActivity.Conection
    private lateinit var adapterJugadores : JugadoresAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_mesas_players)

        conection2.webSocketSingle?.close(1000, "Change Activity")
        conection2.listener = TestListener()
        conection2.conectarconWS()

        /********************PETICION DE MESAS******************/
        val joinRoom = JoinData("join_room", Data("1"))
        val joinRoomJSON = gson.toJson(joinRoom)
        Log.d("request",joinRoomJSON)
        conection2.webSocketSingle?.send(joinRoomJSON)

        btnCreaMesa.setOnClickListener {
            Log.d("request", "mesnaje a enviar:{\"command\":\"create_room_table\",\"data\":{\"roomId\":\"1\",\"points\":30}} ")
            conection2.webSocketSingle?.send("{\"command\":\"create_room_table\",\"data\":{\"roomId\":\"1\",\"points\":30}}")

        }

    }
    inner class TestListener : WebSocketListener() {
        override fun onOpen(webSocket: WebSocket, response: Response) {
            //Log.d("request", "connection successfully")
            Log.d("request","vuelto a abrir???")
            //webSocket.close(1000, "finish")
        }
        @SuppressLint("SetTextI18n")
        override fun onMessage(webSocket: WebSocket, text: String) {
            //usernameReceived.text = "cargando Rooms"
            runOnUiThread {
                Log.d("request", "testRecibe : $text")
                //--------eventoRecibido = decoJson.fromJson(text, Events::class.java)
                Log.d("request", "evento Jugadores a EventsClass: $eventoRecibido")
                //------if (eventoRecibido.type == "TRUCO_ROOM_EVENT") {
                if (text.contains("TRUCO_ROOM_EVENT", true)) {
                    eventoRecibido = decoJson.fromJson(text, Events::class.java)
                    Log.d("request", "ENTRO AL ADAPTER!!!")
                    val jugadores:ArrayList<UserElement?> = eventoRecibido.data?.room?.users as ArrayList<UserElement?>
                    Log.d("request", "JUGADORES TIENE ESTO $jugadores")
                    adapterJugadores = JugadoresAdapter(jugadores)
                    lista_jugadores.apply {
                        layoutManager = LinearLayoutManager(this@MesasActivity)
                        adapter = adapterJugadores
                    }
                    //disparaAdapter()
                }else{
                    Log.d("request", "MENSAJE: $text")
                }
            }
        }

        override fun onMessage(webSocket: WebSocket, bytes: ByteString) {
            super.onMessage(webSocket, bytes)
            Log.d("request", "testRecibe : $bytes")
        }

        override fun onClosing(webSocket: WebSocket, code: Int, reason: String) {
            Log.d("request","testCierra : $code / $reason")
        }
        override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {
            Log.d("request","Error : " + t.message +response.toString())
            LobbyActivity.Conection.conectarconWS()
            val joinRoom = JoinData("join_room", Data("1"))
            val joinRoomJSON = gson.toJson(joinRoom)
            Log.d("request",joinRoomJSON)
            LobbyActivity.Conection.webSocketSingle?.send(joinRoomJSON)
        }
    }
}