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);