summaryrefslogtreecommitdiff
path: root/frontend/src/sagas/lobby.js
diff options
context:
space:
mode:
authorJoffrey BION <joffrey.bion@gmail.com>2017-05-14 18:43:58 +0200
committerJoffrey BION <joffrey.bion@gmail.com>2017-05-14 18:43:58 +0200
commitb6f54ed56121aa5435dd813c952b292b25b56bfb (patch)
treeb4c88476393b031f00e9a1398f4f2a352f530341 /frontend/src/sagas/lobby.js
parentFix typo in error subscription path (diff)
downloadseven-wonders-b6f54ed56121aa5435dd813c952b292b25b56bfb.tar.gz
seven-wonders-b6f54ed56121aa5435dd813c952b292b25b56bfb.tar.bz2
seven-wonders-b6f54ed56121aa5435dd813c952b292b25b56bfb.zip
Add lobby saga
- Fix lobby's player list updates - Fix lobby's player list order - Add 'start game' button (not restricted to owner yet) Resolves: https://github.com/luxons/seven-wonders/issues/7
Diffstat (limited to 'frontend/src/sagas/lobby.js')
-rw-r--r--frontend/src/sagas/lobby.js58
1 files changed, 58 insertions, 0 deletions
diff --git a/frontend/src/sagas/lobby.js b/frontend/src/sagas/lobby.js
new file mode 100644
index 00000000..f002c897
--- /dev/null
+++ b/frontend/src/sagas/lobby.js
@@ -0,0 +1,58 @@
+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 { 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]
+}
+
+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))
+ }
+ } finally {
+ yield apply(lobbyUpdatesChannel, lobbyUpdatesChannel.close)
+ }
+}
+
+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'))
+ } finally {
+ yield apply(gameStartedChannel, gameStartedChannel.close)
+ }
+}
+
+function *startGame({ socket }) {
+ while (true) {
+ yield take(types.REQUEST_START_GAME)
+ yield apply(socket, socket.send, ['/app/lobby/startGame', {}])
+ }
+}
+
+function *lobbySaga(socketConnection) {
+ yield [
+ call(watchLobbyUpdates, socketConnection),
+ call(watchGameStart, socketConnection),
+ call(startGame, socketConnection)
+ ]
+}
+
+export default lobbySaga
bgstack15