diff options
Diffstat (limited to 'frontend/src/sagas')
-rw-r--r-- | frontend/src/sagas/home.js | 5 | ||||
-rw-r--r-- | frontend/src/sagas/lobby.js | 74 |
2 files changed, 79 insertions, 0 deletions
diff --git a/frontend/src/sagas/home.js b/frontend/src/sagas/home.js new file mode 100644 index 00000000..24f385c9 --- /dev/null +++ b/frontend/src/sagas/home.js @@ -0,0 +1,5 @@ +function *homeSaga(wsConnection) { + yield console.log('home saga') +} + +export default homeSaga diff --git a/frontend/src/sagas/lobby.js b/frontend/src/sagas/lobby.js new file mode 100644 index 00000000..144d523a --- /dev/null +++ b/frontend/src/sagas/lobby.js @@ -0,0 +1,74 @@ +import { call, put, take } from 'redux-saga/effects' +import { eventChannel } from 'redux-saga' +import { fromJS } from 'immutable' +import { push } from 'react-router-redux' + +import { actions, types } from '../../redux/game' + +function createSocketChannel(socket) { + return eventChannel(emit => { + const makeHandler = (type) => (event) => { + const response = fromJS(JSON.parse(event.body)) + + emit({ + type, + response + }) + } + + const newGameHandler = makeHandler(types.NEW_GAME) + const joinGameHandler = makeHandler(types.JOIN_GAME) + + const newGame = socket.subscribe('/topic/games', newGameHandler) + const joinGame = socket.subscribe('/user/queue/join-game', joinGameHandler) + + const unsubscribe = () => { + newGame.unsubscribe() + joinGame.unsubscribe() + } + + return unsubscribe + }) +} + +export function* watchGames(socketConnection) { + + const { socket } = socketConnection + const socketChannel = createSocketChannel(socket) + + while (true) { + const { type, response } = yield take(socketChannel) + + switch (type) { + case types.NEW_GAME: + yield put(actions.newGame(response)) + break; + case types.JOIN_GAME: + yield put(actions.joinGame(response)) + break; + default: + console.error('Unknown type') + } + } +} + +export function* createGame(socketConnection) { + const { name } = yield take(types.CREATE_GAME) + const { socket } = socketConnection + + socket.send("/app/lobby/create-game", JSON.stringify({ + 'gameName': name, + 'playerName': 'Cesar92' + }), {}) +} + +export function* gameBrowserSaga(socketConnection) { + yield put(push('/lobby')) + + yield [ + call(watchGames, socketConnection), + call(createGame, socketConnection) + ] +} + +export default gameBrowserSaga |