Skip to content

Instantly share code, notes, and snippets.

@jesterswilde
Created May 27, 2018 18:23
Show Gist options
  • Save jesterswilde/4892bee9bd7f969ccffaa51b49d85986 to your computer and use it in GitHub Desktop.
Save jesterswilde/4892bee9bd7f969ccffaa51b49d85986 to your computer and use it in GitHub Desktop.
module.exports = (io)=>{
let roomID = 0;
let rooms = [];
let createRoom = function(id){
roomID++;
let room = {
isFull: function(){ return this.sockets.length >= this.capacity;},
capacity: 2,
sockets: [],
id : roomID,
addSocket: function(socket) {
this.sockets.push(socket);
socket.room = this;
socket.join(this.id);
socket.playerID = this.sockets.length - 1;
socket.emit('dude_connect', socket.playerID);
this.checkForBros(socket);
this.sandbox.connectSocket(socket);
},
checkForOpponents: function(player){
this.sockets.filter((potentialOpponents)=> potentialOpponents.playerID != dude.playerID)
.forEach((opponent)=> {
player.emit('opponentConnect', bro.playerID);
opponent.emit('opponentConnect', dude.playerID);
});
},
message: function(type, data){
io.to(this.id).emit(type, data);
}
};
room.sandbox = sandbox(io, room);
return room;
};
let addSocket = (socket)=>{
if(!rooms.length || rooms[rooms.length - 1].isFull()){
const road = createRoom();
road.addSocket(socket);
rooms.push(road);
}else{
rooms[rooms.length - 1].addSocket(socket);
}
};
io.on('connection', (socket)=>{
addSocket(socket);
socket.on('sub', (data)=>{
socket.room.message('message', data);
});
socket.on('playerInput', (input)=>{
//convert inputs to movements on the server
socket.room.message('truth', input);
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment