summaryrefslogtreecommitdiff
path: root/frontend/src/sagas
diff options
context:
space:
mode:
Diffstat (limited to 'frontend/src/sagas')
-rw-r--r--frontend/src/sagas/gameBrowser.js66
-rw-r--r--frontend/src/sagas/lobby.js74
-rw-r--r--frontend/src/sagas/usernameChoice.js2
3 files changed, 68 insertions, 74 deletions
diff --git a/frontend/src/sagas/gameBrowser.js b/frontend/src/sagas/gameBrowser.js
new file mode 100644
index 00000000..19d52df9
--- /dev/null
+++ b/frontend/src/sagas/gameBrowser.js
@@ -0,0 +1,66 @@
+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 gameBrowserChannel(socket) {
+ return eventChannel(emit => {
+
+ const makeHandler = type => event => {
+ const response = fromJS(JSON.parse(event.body))
+ emit({ type, response })
+ }
+
+ const newGame = socket.subscribe('/topic/games', makeHandler(types.CREATE_OR_UPDATE_GAMES))
+ const joinGame = socket.subscribe('/user/queue/lobby/joined', makeHandler(types.JOIN_GAME))
+
+ const unsubscribe = () => {
+ newGame.unsubscribe()
+ joinGame.unsubscribe()
+ }
+
+ return unsubscribe
+ })
+}
+
+export function* watchGames({ socket }) {
+ const socketChannel = gameBrowserChannel(socket)
+
+ try {
+ while (true) {
+ const { type, response } = yield take(socketChannel)
+
+ switch (type) {
+ case types.CREATE_OR_UPDATE_GAMES:
+ yield put(actions.createOrUpdateGame(response))
+ break;
+ case types.JOIN_GAME:
+ yield put(actions.joinGame(response))
+ socketChannel.close();
+ yield put(push(`/lobby/${response.id}`))
+ break;
+ default:
+ console.error('Unknown type')
+ }
+ }
+ } finally {
+ console.info('gameBrowserChannel closed')
+ }
+}
+
+export function* createGame({ socket }) {
+ const { name } = yield take(types.CREATE_GAME)
+
+ socket.send("/app/lobby/create", JSON.stringify({ gameName: name }), {})
+}
+
+export function* gameBrowserSaga(socketConnection) {
+ yield [
+ call(watchGames, socketConnection),
+ call(createGame, socketConnection)
+ ]
+}
+
+export default gameBrowserSaga
diff --git a/frontend/src/sagas/lobby.js b/frontend/src/sagas/lobby.js
deleted file mode 100644
index 144d523a..00000000
--- a/frontend/src/sagas/lobby.js
+++ /dev/null
@@ -1,74 +0,0 @@
-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
diff --git a/frontend/src/sagas/usernameChoice.js b/frontend/src/sagas/usernameChoice.js
index 60a1a091..d0d0cdea 100644
--- a/frontend/src/sagas/usernameChoice.js
+++ b/frontend/src/sagas/usernameChoice.js
@@ -1,5 +1,6 @@
import { call, take, put } from 'redux-saga/effects'
import { eventChannel } from 'redux-saga'
+import { push } from 'react-router-redux'
import { actions, types } from '../redux/user'
@@ -21,6 +22,7 @@ function *usernameValidation({ socket }) {
const user = yield take(usernameChannel)
yield put(actions.setUsername(user.username, user.displayName, user.index))
usernameChannel.close()
+ yield put(push('/games'))
}
function *sendUsername({ socket }) {
bgstack15