How can i find if a Player has Quit the game from memory to force quit the game or has internet connection lost to wait for reconnection Colyseus

Viewed 108

So I have this game whose server is built on colyseus v0.12.0. I want to find if the player has killed the game instance or He has lost the internet connection.

  1. if he kills the game - other players scoreboard is shown and all players leave the room and it's disposed.
  2. if his phone has internet issue - we try to reconnect for a certain time, if he comes back we resume the game, if he doesn't comes back we force quit the game and show the scoreboard.

Here is the code of onLeave on server.

onLeave(consented) is a boolean parameter if it returns true then that means the player has quit the
game from inside the games quit option in the game. if consented is false then that means either the game instance was killed by the user or it has internet issue. I want to distinguish between this two - " so that i can make other players wait if it is an reconnection issue, or end the game if he has killed the instance of the game."

    async onLeave(client, consented) {
        console.log("===============Leave ================");
        console.log(consented, "----Client Left----", client.sessionId);
        console.log(consented, "---------- Left----", 
        this.state.players[client.sessionId].name);
        console.log(consented, "---------- player----", 
        this.ClientUserName);
        //this.state.players[client.sessionId].connected = false;

        try {
            //     //Reconnection of client
            if (consented) {
                //this.allowReconnection(client,70); 
                console.log("----consented true----", client.sessionId);

                console.log("--------ROOM DISOPOSED------consented",
                  this.ourRoomName);

                if (this.ClientUserName[0] === 
                    this.state.players[client.sessionId].name) {
                    this.playersInRoom = 0;
                    console.log("1111111111111111111111");
                    this.broadcast(
                        {
                            en: "QuitLobby",
                            value: "host quit the lobby",
                        }, { except: client });
                    /*  for (var i = 0; i < this.ClientId.length; i++) {
                      delete this.state.players[this.ClientId[i]];
                      }*/


                    this.disconnect();
                    this.ClientId = [];
                    this.ClientAvatar = [];
                    this.setState(this.state);
                }
                else {
                    this.playersInRoom--;
                    this.playerCount--;
                    console.log(this.users.length)
                    for (let i = 0; i < this.ClientUserName.length; i++) {
                        if (this.ClientUserName[i] === 
                            this.state.players[client.sessionId].name) {
                            this.ClientUserName.splice(i, 1);

                            this.ClientId.splice(i, 1);

                            this.ClientAvatar.splice(i, 1);

                            this.users.splice(i + 1, 1);
                        }
                    }
                    console.log(this.playersInRoom)
                    console.log(this.users.length)


                    this.broadcast({
                        en: 'playerInfo',
                        playerData: {
                            idArr: this.ClientId,
                            nameArr: this.ClientUserName,
                            avatarArr: this.ClientAvatar // added avtar
                        }
                    })

                }

            }
            else {
                await this.allowReconnection(client, 20);
                this.state.players[client.sessionId].connected = true;
                // delete this.state.players[client.sessionId];    //after 
              reconnection the player is not able to see anybody else's card
                this.broadcast({
                    en: 'DisconnectedTemp',
                    value: client.sessionId,
                })

                console.log("----Client Reconnect----", client.sessionId);
                

            }
        } catch (e) {
            delete this.state.players[client.sessionId];
        }

    }
0 Answers
Related