samedi 4 mars 2017

This "if else" statement don't work in my socket.io/Node.js environment

So I am developing a simple game. The logic flow is when 2 players connect to the room, the game will initialize and emit a 3 seconds countdown event alongside with a button . if one player clicks the button, that player will emit an event making him/her the host of the game.

The button will appear for 3 seconds. If it disappear without being clicked, the server will randomly pick the host.

Whether or not the button is clicked,after 3 seconds all clients will emit a "ready" event.

Here is my code.

if the button is clicked, the "host" event will be emitted by the client,and my server side code is

client.on('host',function(){

   var player = room.getPlayer(client.id);
   player.isHost=true;


 });

Then here is the server side for the "ready" event.

 client.on("ready", function() {
  var player = room.getPlayer(client.id);
  var table = room.getTable(player.tableID);
   if (2>1) {
      for (var i = 0; i < table.players.length; i++) { 

        if (table.players[i].isHost===true){

          console.log("you are the host")
        } else {
          console.log("we are going to randomly pick a host")
          }

       }
     }
 })

Now if no players click the button the terminal will show

we are going to randomly pick a host

we are going to randomly pick a host

we are going to randomly pick a host

we are going to randomly pick a host

Otherwise it will be like

we are going to randomly pick a host

you are the host

we are going to randomly pick a host

you are the host

At this stage only 2 clients are allow for the game so the players.length is 2.It seems like the if/else will be executed same time as the players.length?

Aucun commentaire:

Enregistrer un commentaire