-
-
Save jesterswilde/4892bee9bd7f969ccffaa51b49d85986 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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