Skip to content

Commit

Permalink
Refactor : sendReadyHandler의 game관련 로직을 gameController로 이동
Browse files Browse the repository at this point in the history
이유:
- 해당 handler를 제외하고도 다른 곳에서 위의 로직을 사용하는 경우가 발생
  • Loading branch information
younguna committed Nov 28, 2019
1 parent 651fa2d commit 7f09ee6
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 40 deletions.
68 changes: 68 additions & 0 deletions server/socket/gameController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const rooms = require('./rooms');
const io = require('./io');
const { MIN_USER_COUNT } = require('../config');

const assignViewer = socket => {
const room = rooms.getRoomByRoomId(socket.roomId);
const { streamerSocketId } = room;
const socketIds = Object.keys(room.players);
socketIds.forEach(socketId => {
if (socketId !== streamerSocketId) {
io.to(socketId).emit('assignViewer', { streamerSocketId });
}
});
};

const assignStreamer = socket => {
const { streamerSocketId } = rooms.getRoomByRoomId(socket.roomId);
io.to(streamerSocketId).emit('assignStreamer');
};

const startRound = socket => {
const { roomId } = socket;
rooms.setRound(roomId);
io.to(roomId).emit('roundStart', {
currentRound: rooms.getRoomByRoomId(roomId).currentRound,
});
};

const startSet = socket => {
const { roomId } = socket;
rooms.setSet(roomId);
io.to(roomId).emit('setStart', {
currentSet: rooms.getRoomByRoomId(roomId).currentSet,
});
assignStreamer(socket);
assignViewer(socket);
};

const endSet = roomId => {
const room = rooms.getRoomByRoomId(roomId);
const { streamerSocketId } = room;
room.players[streamerSocketId].type = 'viewer';
io.to(roomId).emit('setEnd', { currentSet: room.currentSet });
};

const startGame = socket => {
const { roomId } = socket;
rooms.resetGameProgress(roomId);
rooms.setRoomStatusByRoomId(roomId, 'playing');
io.to(roomId).emit('gameStart');
startRound(socket);
startSet(socket);
};

const isGameContinuable = socket => {
const room = rooms.getRoomByRoomId(socket.roomId);
return Object.keys(room.players).length > MIN_USER_COUNT;
};

module.exports = {
assignStreamer,
assignViewer,
startSet,
startRound,
startGame,
endSet,
isGameContinuable,
};
41 changes: 1 addition & 40 deletions server/socket/handlers/sendReadyHandler.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,6 @@
const rooms = require('../rooms');
const io = require('../io');

const assignViewer = socket => {
const room = rooms.getRoomByRoomId(socket.roomId);
const { streamerSocketId } = room;
const socketIds = Object.keys(room.players);
socketIds.forEach(socketId => {
if (socketId !== streamerSocketId) {
io.to(socketId).emit('assignViewer', { streamerSocketId });
}
});
};

const assignStreamer = socket => {
const { streamerSocketId } = rooms.getRoomByRoomId(socket.roomId);
io.to(streamerSocketId).emit('assignStreamer');
};

const startRound = roomId => {
rooms.setRound(roomId);
io.to(roomId).emit('roundStart', {
currentRound: rooms.getRoomByRoomId(roomId).currentRound,
});
};

const startSet = roomId => {
rooms.setSet(roomId);
io.to(roomId).emit('setStart', {
currentSet: rooms.getRoomByRoomId(roomId).currentSet,
});
};

const startGame = (socket, roomId) => {
rooms.resetGameProgress(roomId);
rooms.setRoomStatusByRoomId(roomId, 'playing');
io.to(roomId).emit('gameStart');
startRound(roomId);
startSet(roomId);
assignStreamer(socket);
assignViewer(socket);
};
const { startGame } = require('../gameController');

const setPlayerReady = (socket, isReady) => {
const room = rooms.findRoomBySocket(socket);
Expand Down

0 comments on commit 7f09ee6

Please sign in to comment.