-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor : sendReadyHandler의 game관련 로직을 gameController로 이동
이유: - 해당 handler를 제외하고도 다른 곳에서 위의 로직을 사용하는 경우가 발생
- Loading branch information
Showing
2 changed files
with
69 additions
and
40 deletions.
There are no files selected for viewing
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
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, | ||
}; |
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