MesasActivity.kt 11.7 KB
Newer Older
1 2 3
package com.example.tolucagraphics

import android.annotation.SuppressLint
4
import android.content.Intent
5 6 7
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
8
import android.widget.Toast
9 10
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.tolucagraphics.adapters.JugadoresAdapter
11 12 13 14 15 16
import com.example.tolucagraphics.adapters.MesaAdapter
import com.example.tolucagraphics.datas.CreateTable
import com.example.tolucagraphics.datas.GetRooms
import com.example.tolucagraphics.datas.JoinRoom
import com.example.tolucagraphics.events.*
import com.example.tolucagraphics.models.ModelForTables
17 18 19 20 21 22 23 24 25 26
import com.google.gson.Gson
import kotlinx.android.synthetic.main.activity_mesas_players.*
import okhttp3.Response
import okhttp3.WebSocket
import okhttp3.WebSocketListener

class MesasActivity : AppCompatActivity() {
    var selector = 1
    val gson = Gson()
    val decoJson = Gson()
27 28 29 30 31 32 33 34 35 36
    private val conection = Socket
    var yaEntro = false
    var yaEntroMesaCreada = false
    var tamanioJugadores = 0
    var tamanioMesas = 0
    var listaJugadores = ArrayList<UserElementJoin>()
    var listaMesas = ArrayList<EventCreateMesa>()
    val listaTables = ArrayList<ModelForTables>()
    lateinit var jugadoresAdapter: JugadoresAdapter
    lateinit var mesasAdapter: MesaAdapter
37 38 39 40 41

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

42 43 44 45 46 47 48 49
        conection.webSocketSingle?.close(1000, "Change Activity")
        conection.listener = TestListener()
        conection.conectarconWS()
        conection.webSocketSingle?.send(gson.toJson(GetRooms()))
        conection.webSocketSingle?.send(gson.toJson(JoinRoom()))
        btnCreaMesa.setOnClickListener {
            conection.webSocketSingle?.send(gson.toJson(CreateTable()))
        }
50
        /********************PETICION DE MESAS******************/
51
        val joinRoomJSON = gson.toJson(JoinRoom())
52
        Log.d("request",joinRoomJSON)
53
        conection.webSocketSingle?.send(joinRoomJSON)
54 55 56

        btnCreaMesa.setOnClickListener {
            Log.d("request", "mesnaje a enviar:{\"command\":\"create_room_table\",\"data\":{\"roomId\":\"1\",\"points\":30}} ")
57
            conection.webSocketSingle?.send(gson.toJson(CreateTable()))
58 59 60 61

        }

    }
62 63 64 65 66 67 68 69 70 71
    fun iniciarRecyclerJugadores(){
        jugadoresAdapter = JugadoresAdapter(listaJugadores)
        lista_jugadores.apply {
            layoutManager = LinearLayoutManager(this@MesasActivity)
            adapter = jugadoresAdapter
        }
    }
    fun iniciarRecyclerMesas(){
        mesasAdapter = MesaAdapter(listaMesas){ cadena, join ->
            Toast.makeText(this@MesasActivity, cadena, Toast.LENGTH_LONG).show()
72 73
            val intentJuego = Intent(this@MesasActivity, MesaJugando::class.java)
            //intentJuego.putExtra("join", gson.toJson(join))
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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
            startActivity(intentJuego)
        }
        lista_mesas.apply {
            layoutManager = LinearLayoutManager(this@MesasActivity)
            adapter = mesasAdapter
        }
    }
    fun agregarTableGetInit(entrada : EventGetRoom){
        for (indiceT in entrada.data?.rooms?.get(0)?.tables?.indices!!) {
            listaTables.add(ModelForTables(id = entrada.data.rooms[0].tables?.get(indiceT)?.id, state = entrada.data.rooms[0].tables?.get(indiceT)?.status, users = entrada.data.rooms[0].tables?.get(indiceT)?.positions))
        }

    }
    fun agregarTable(entrada : EventCreateMesa){

        listaTables.add(ModelForTables(id = entrada.data?.table?.id, state = entrada.data?.table?.status, users = entrada.data?.table?.users as List<OwnerClass>))
    }
    fun buscarDuplicados(){
        /*if(list.removeAll { it == item }) {
            println("Lista modificada: ${list}")
        } else {
            println("El elemento no existe!!")
        }    */
        if (tamanioMesas > 1) {
            for (indice in listaMesas.indices) {
                if (listaMesas.removeAll { it == listaMesas[indice] }) {
                    tamanioMesas = listaMesas.size
                    mesasAdapter.notifyItemRemoved(tamanioMesas)
                }
            }
        }
    }
    fun contieneId(lista: ArrayList<EventCreateMesa>, idNuevo : String?):Boolean{
        var contiene = false
        for (indice in lista.indices){
            contiene = lista[indice].data?.table?.id != idNuevo
        }
        return contiene
    }
    fun adaptarEvento(evento : EventCreateMesa) : EventCreateMesa{
        var resultado = EventCreateMesa()
        resultado.command = evento.command
        resultado.type = evento.type
        resultado.data?.user = evento.data?.user
        resultado.data?.room = evento.data?.room
        resultado.data?.eventName = evento.data?.eventName
        resultado.data?.table?.id = evento.data?.table?.id
        resultado.data?.table?.owner = evento.data?.table?.owner
        resultado.data?.table?.points = evento.data?.table?.points
        resultado.data?.table?.privateTable = evento.data?.table?.privateTable
        resultado.data?.table?.roomID = evento.data?.table?.roomID
        resultado.data?.table?.status = evento.data?.table?.status
        resultado.data?.table?.users?.add(evento.data?.table?.users!!.first())
        return resultado
    }
129 130 131 132 133 134 135 136 137 138
    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 {
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
                if (text.contains(KeysWebSocket.commandResponse,true) && text.contains(KeysWebSocket.commandGetRooms)){
                    Log.d("TAG", "GETROOMS -> $text")
                    agregarTableGetInit(gson.fromJson(text, EventGetRoom::class.java))
                    Log.d("TAG", "ListaTables Nueva -> $listaTables")
                    /**********************************************Parte para el Recycler de Jugadores**************************************/
                }else if (text.contains(KeysWebSocket.commandResponse,true) && text.contains(KeysWebSocket.commandJoinRoom)){
                    val eventJoinRoom = gson.fromJson(text, EventJoinRoom::class.java)
                    listaJugadores = eventJoinRoom.data?.room?.users!! as ArrayList<UserElementJoin>
                    tamanioJugadores = listaJugadores.size
                    iniciarRecyclerJugadores()
                }else if(text.contains(KeysWebSocket.trucoRoomEvent,true) && text.contains(KeysWebSocket.roomUserJoined)){
                    if (yaEntro) {
                        val eventTrucoRoom = gson.fromJson(text, EventJoinRoomTRE::class.java)
                        listaJugadores.add(UserElementJoin(user = UserUserJoin(eventTrucoRoom.data?.user!!.id, eventTrucoRoom.data.user.username ), online = true))
                        jugadoresAdapter.notifyItemInserted(tamanioJugadores)
                    }else yaEntro = true
                }else if(text.contains(KeysWebSocket.trucoRoomEvent, true) && text.contains(KeysWebSocket.roomLeftRoom, true)){
                    val eventLeftTrucoRoom = gson.fromJson(text, EventJoinRoomTRE::class.java)
                    listaJugadores.remove(UserElementJoin(user = UserUserJoin(eventLeftTrucoRoom.data?.user!!.id, eventLeftTrucoRoom.data.user.username ), online = true))
                    jugadoresAdapter.notifyItemRemoved(tamanioJugadores)
                    /************************************************Parte para el Recycler de Mesas ****************************************************/
                }else if (text.contains(KeysWebSocket.commandResponse,true) && text.contains(KeysWebSocket.commandCreateRoomTable)){
                    val eventoCreateTable = gson.fromJson(text, EventCreateMesa::class.java)
                    listaMesas.add(eventoCreateTable)
                    tamanioMesas = listaMesas.size
                    if (yaEntroMesaCreada){
                        mesasAdapter.notifyItemInserted(tamanioMesas)
                    }else {
                        iniciarRecyclerMesas()
                        buscarDuplicados()
                        yaEntroMesaCreada = true
                    }
                }else if(text.contains(KeysWebSocket.trucoRoomEvent,true) && text.contains(KeysWebSocket.roomTableCreated,true)){
                    val eventoCreateTable = gson.fromJson(text, EventCreateMesa::class.java)
                    // Users solo tiene el owner size = 1
                    Log.d("TAG", "MesaCreada-> $eventoCreateTable")
                    if (listaMesas.isEmpty()){
                        listaMesas.add(eventoCreateTable)
                        tamanioMesas = listaMesas.size
                        iniciarRecyclerMesas()
                    }else{

                        if (contieneId(listaMesas, eventoCreateTable.data?.table?.id))
                            listaMesas.add(eventoCreateTable)
                        tamanioMesas = listaMesas.size
                        mesasAdapter.notifyItemInserted(tamanioMesas)
                    }
                }else if(text.contains(KeysWebSocket.trucoRoomEvent, true) && text.contains(KeysWebSocket.roomTableUserJoined, true)){
                    var eventoJoined = gson.fromJson(text, EventCreateMesa::class.java)
                    var eventoJoinedAdaptado = adaptarEvento(eventoJoined)
                    for (indice in listaMesas.indices) {
                        if (listaMesas[indice].data?.table?.id == eventoJoinedAdaptado.data?.table?.id ) {
                            listaMesas[indice].data?.table?.users?.get(0)?.id = eventoJoinedAdaptado.data?.user?.id
                            listaMesas[indice].data?.table?.users?.get(0)?.username = eventoJoinedAdaptado.data?.user?.username
                            mesasAdapter.notifyItemChanged(indice)
                        }
                    }
                }else if(text.contains(KeysWebSocket.trucoTableEvent,true)&&text.contains(KeysWebSocket.tablePositionSetted,true)){
                    var eventoJoined = gson.fromJson(text, EventTablePositionSetted::class.java)
                    Log.d("TAG", "se Unio $eventoJoined")
                    Log.d("TAG", "tamanio de la mesa hecha: ${listaMesas.first().data?.table?.users?.size}")
                    Log.d("TAG", "tamanio de la mesa recibida: ${eventoJoined.data?.table?.positions?.size}")
                    for (indice in listaMesas.indices) {
                        if (listaMesas[indice].data?.table?.id == eventoJoined.data?.table?.id ) {
                            Log.d("TAG", "entra para agregar Player!!!")
                            listaMesas[indice].data?.table?.users = eventoJoined.data?.table?.positions as ArrayList<User>
                            //listaMesas[indice].data?.table?.users?.get(eventoJoined.data?.position!!.toInt())?.username = "X"
                            mesasAdapter.notifyItemChanged(tamanioMesas)
                        }
208 209
                    }
                }else{
210
                    Log.d("TAG", "Unidentified-> $text")
211 212 213 214 215 216 217 218 219 220
                }
            }
        }


        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())
221 222
            conection.conectarconWS()
            val joinRoomJSON = gson.toJson(JoinRoom())
223
            Log.d("request",joinRoomJSON)
224
            conection.webSocketSingle?.send(joinRoomJSON)
225 226 227
        }
    }
}