2020-01-08 19:24:24 +00:00
|
|
|
// TODO const someSocketLogic = require('./middleware/socketssockets/...');
|
2020-01-21 22:33:40 +00:00
|
|
|
const socketIO = require('socket.io');
|
|
|
|
const io = socketIO({ cookie: false });
|
2020-01-08 19:24:24 +00:00
|
|
|
|
2020-01-24 05:25:08 +00:00
|
|
|
const gameQueries = require('./data/queries/game');
|
|
|
|
|
2020-01-21 22:33:40 +00:00
|
|
|
io.on('connection', ()=> {
|
|
|
|
io.emit('connected', {message: 'socket connected'});
|
|
|
|
})
|
2020-01-08 19:24:24 +00:00
|
|
|
|
2020-01-24 08:07:40 +00:00
|
|
|
const enableRoomSocket = (roomId) => {
|
2020-01-21 22:33:40 +00:00
|
|
|
const roomSocket = io.of(roomId);
|
2020-01-24 05:25:08 +00:00
|
|
|
roomSocket.on('connection', (socket) => {
|
2020-01-24 05:41:25 +00:00
|
|
|
|
|
|
|
//! Join Game Request queries db for game, ensures unique player joining
|
2020-01-24 05:25:08 +00:00
|
|
|
socket.on('join_game_request', async data => {
|
|
|
|
const gameRequest = await logJoinGameRequest(data);
|
2020-01-24 05:41:25 +00:00
|
|
|
|
|
|
|
if (gameRequest.err) {
|
|
|
|
roomSocket.emit('join_game_request_error', gameRequest.err);
|
|
|
|
}
|
|
|
|
|
2020-01-24 05:25:08 +00:00
|
|
|
roomSocket.emit('join_game_request', gameRequest);
|
2020-01-24 05:41:25 +00:00
|
|
|
});
|
|
|
|
|
2020-01-21 22:33:40 +00:00
|
|
|
});
|
|
|
|
return roomSocket;
|
|
|
|
}
|
2020-01-08 19:24:24 +00:00
|
|
|
|
2020-01-24 08:07:40 +00:00
|
|
|
const enableGameSocket = (roomId, gameId) => {
|
|
|
|
const gameSocket = io.of(roomId);
|
|
|
|
gameSocket.on('connection', (socket) => {
|
|
|
|
socket.join(gameId);
|
|
|
|
socket.to(gameId).emit(`joined room ${gameId}`)
|
|
|
|
console.log(socket)
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return gameSocket;
|
|
|
|
}
|
|
|
|
|
2020-01-21 22:33:40 +00:00
|
|
|
module.exports = {
|
|
|
|
io,
|
2020-01-24 08:07:40 +00:00
|
|
|
enableRoomSocket,
|
|
|
|
enableGameSocket
|
2020-01-24 05:25:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function logJoinGameRequest (data) {
|
|
|
|
const {user, game} = data;
|
|
|
|
const requestedGame = await gameQueries.findGameById(game.id);
|
2020-01-24 05:41:25 +00:00
|
|
|
|
|
|
|
if (requestedGame.user_black === user.id) {
|
|
|
|
return { err: 'players must be unique' }
|
|
|
|
}
|
|
|
|
|
|
|
|
const requestingUser = {...user};
|
|
|
|
delete requestingUser.email;
|
|
|
|
return { requestingUser, requestedGame }
|
2020-01-08 07:17:10 +00:00
|
|
|
}
|