diff options
Diffstat (limited to 'frontend/src/sagas/lobby.js')
-rw-r--r-- | frontend/src/sagas/lobby.js | 68 |
1 files changed, 38 insertions, 30 deletions
diff --git a/frontend/src/sagas/lobby.js b/frontend/src/sagas/lobby.js index f002c897..f092fdb7 100644 --- a/frontend/src/sagas/lobby.js +++ b/frontend/src/sagas/lobby.js @@ -1,58 +1,66 @@ -import { call, put, take, apply } from 'redux-saga/effects' -import { createSubscriptionChannel } from '../utils/websocket' -import { push } from 'react-router-redux' +import { call, put, take, apply } from "redux-saga/effects"; +import { createSubscriptionChannel } from "../utils/websocket"; +import { push } from "react-router-redux"; -import { normalize } from 'normalizr' -import { game as gameSchema } from '../schemas/games' +import { normalize } from "normalizr"; +import { game as gameSchema } from "../schemas/games"; -import { actions as gameActions, types } from '../redux/games' -import { actions as playerActions } from '../redux/players' +import { actions as gameActions, types } from "../redux/games"; +import { actions as playerActions } from "../redux/players"; function getCurrentGameId() { - const path = window.location.pathname - return path.split('lobby/')[1] + const path = window.location.pathname; + return path.split("lobby/")[1]; } -function *watchLobbyUpdates({ socket }) { - const currentGameId = getCurrentGameId() - const lobbyUpdatesChannel = yield call(createSubscriptionChannel, socket, `/topic/lobby/${currentGameId}/updated`) +function* watchLobbyUpdates({ socket }) { + const currentGameId = getCurrentGameId(); + const lobbyUpdatesChannel = yield call( + createSubscriptionChannel, + socket, + `/topic/lobby/${currentGameId}/updated` + ); try { while (true) { - const lobby = yield take(lobbyUpdatesChannel) - const normalized = normalize(lobby, gameSchema) - yield put(gameActions.updateGames(normalized.entities.games)) - yield put(playerActions.updatePlayers(normalized.entities.players)) + const lobby = yield take(lobbyUpdatesChannel); + const normalized = normalize(lobby, gameSchema); + yield put(gameActions.updateGames(normalized.entities.games)); + yield put(playerActions.updatePlayers(normalized.entities.players)); } } finally { - yield apply(lobbyUpdatesChannel, lobbyUpdatesChannel.close) + yield apply(lobbyUpdatesChannel, lobbyUpdatesChannel.close); } } -function *watchGameStart({ socket }) { - const currentGameId = getCurrentGameId() - const gameStartedChannel = yield call(createSubscriptionChannel, socket, `/topic/lobby/${currentGameId}/started`) +function* watchGameStart({ socket }) { + const currentGameId = getCurrentGameId(); + const gameStartedChannel = yield call( + createSubscriptionChannel, + socket, + `/topic/lobby/${currentGameId}/started` + ); try { - yield take(gameStartedChannel) - yield put(gameActions.enterGame()) - yield put(push('/game')) + yield take(gameStartedChannel); + yield put(gameActions.enterGame()); + yield put(push("/game")); } finally { - yield apply(gameStartedChannel, gameStartedChannel.close) + yield apply(gameStartedChannel, gameStartedChannel.close); } } -function *startGame({ socket }) { +function* startGame({ socket }) { while (true) { - yield take(types.REQUEST_START_GAME) - yield apply(socket, socket.send, ['/app/lobby/startGame', {}]) + yield take(types.REQUEST_START_GAME); + yield apply(socket, socket.send, ["/app/lobby/startGame", {}]); } } -function *lobbySaga(socketConnection) { +function* lobbySaga(socketConnection) { yield [ call(watchLobbyUpdates, socketConnection), call(watchGameStart, socketConnection), call(startGame, socketConnection) - ] + ]; } -export default lobbySaga +export default lobbySaga; |