From 7f09ee6b87d7310b76d2b8c1900221d273cd225d Mon Sep 17 00:00:00 2001 From: young na Date: Thu, 28 Nov 2019 17:21:09 +0900 Subject: [PATCH] =?UTF-8?q?Refactor=20:=20sendReadyHandler=EC=9D=98=20game?= =?UTF-8?q?=EA=B4=80=EB=A0=A8=20=EB=A1=9C=EC=A7=81=EC=9D=84=20gameControll?= =?UTF-8?q?er=EB=A1=9C=20=EC=9D=B4=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 이유: - 해당 handler를 제외하고도 다른 곳에서 위의 로직을 사용하는 경우가 발생 --- server/socket/gameController.js | 68 ++++++++++++++++++++++ server/socket/handlers/sendReadyHandler.js | 41 +------------ 2 files changed, 69 insertions(+), 40 deletions(-) create mode 100644 server/socket/gameController.js diff --git a/server/socket/gameController.js b/server/socket/gameController.js new file mode 100644 index 00000000..7c759473 --- /dev/null +++ b/server/socket/gameController.js @@ -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, +}; diff --git a/server/socket/handlers/sendReadyHandler.js b/server/socket/handlers/sendReadyHandler.js index 0cadcb43..6df2dcd3 100644 --- a/server/socket/handlers/sendReadyHandler.js +++ b/server/socket/handlers/sendReadyHandler.js @@ -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);