Multiplayer Game Logic/How to add 2nd or additional players

I am having trouble coming up with the proper logic and after a few days, I figured I’d come to the community. This multiplayer has been modified to facilitate a turn based style board game. The problem I’m having is during the initializing of the ‘player’, you will need to add the ‘Other’ player. However, if you’re waiting for this player to join, you’re going to error out while trying to assign that ‘Other’ entity a player that hasn’t joined yet.
I guess what I’m looking for is a Promise, but I’m not sure if it will work that way and not too familiar with working Promises all together.
I’ve already scrapped this project and started another from ground, but I came back to this because I feel that I’m literally 1 piece away from solving this and not having to rewrite this entire game again. Thanks in advance for any suggestions.

https://playcanvas.com/editor/scene/1145109

Server.js

 var server = require('http').createServer();
var options = {
  cors: true
}
var io = require('socket.io')(server, options);
var team1=[];
var team2=[];
var teams = {};
var turn = 0;
var startGame = null;
function Player(id) {
    this.id = id;
    this.team = null;
}

io.sockets.on('connection', function(socket) {
    socket.on ('initialize', function () {
        var id = socket.id;
        var newPlayer = new Player(id);
        var tsize = Object.keys(teams).length;
      console.log(tsize);
        if(tsize == 0){
            teams[id] = newPlayer;
            socket.emit ('playerData', {id: id, teams: "Team1"}); 
            console.log("Player 1 has joined " + id);           
        }
        else if(tsize ==1){
            teams[id] = newPlayer;
            socket.emit ('playerData', {id: id, teams: "Team2"}); 
            console.log("Player 2 has joined " + id); 
        }      
        });     
  
    socket.on ('teamUpdate', function (data) {
        if(data.teams == 'Team1'){
          team1.push(data.id);   
        }
        else{
          team2.push(data.id);    
          io.emit ('listo', {id: data.id}); 
        }
    //console.log(team1,team2)
      }); 
  
    socket.on ('ready', function (data) {
        //console.log(data.msg);
        turn = 1;
      });   
               
    socket.on ('positionUpdate', function (data) {
        console.log(data);
        io.emit ('syncMove', data);
      });    
});
console.log ('Server started');
server.listen(3000);













Hi @Jason_Owens,

Usually what is done in this case is when each player connects to the server, the server will send a player-connected message to everybody else connected.

From there you can check the team size in the player-connected event handler and after it reaches a certain start your gameplay. I think you are mostly doing that already, most likely you are missing sending that specific message and receiving it on the client. So you will move your logic to that event handler, instead of doing it in the initialize event.

1 Like

Ok thanks for the reply. I will try that. I think that’s what I was going for, just wanted a second opinion. Thx again!

Thanks for the help, I was able to use what you said and applied it in my app and it works. But I have another question and maybe you can help me since I’ve been stuck the last few days trying to figure something out with this turn base and raycasting.

The short of it is, I’m able to click on all entities and move how I want them to in the map, but I’d like to limit that to only being able to select your entities based off of socket.id. Something like having raycast.js check if socket.id === turn.id, allow mouseEvent.

However, all attempts I’ve made to have raycast.js call functions from Network.js returns undefined, which makes sense because there’s no data/socket on that call from raycast to any function I’ve been able to produce so far that I can use to check in Raycast. I hope that makes sense in trying to explain what’s going on.
What is the approach I need to take to have raycast acquire the socket.id. or am I going about this wrong. Is there another way I could restrict the raycast movement?

So, there are several ways to approach this. I’d say one quick way is to save the socket id on each player entity upon creating them:

myEntity.sockedID = socketID;

In Javascript you can add custom properties to any object. Then when your raycast method returns the picked entity, you can access it the same way:

if( raycastResult){
   console.log(raycastResult.entity.socketID);
}

Of course this isn’t the most elegant way since it “dirties” an object but it can easily get you going with this.