summaryrefslogtreecommitdiff
path: root/sw-ui/src/sagas/lobby.ts
diff options
context:
space:
mode:
authorJoffrey BION <joffrey.bion@gmail.com>2019-05-16 23:48:38 +0200
committerJoffrey BION <joffrey.bion@gmail.com>2019-05-16 23:48:38 +0200
commit2382a452456e4bdef4584e1046925e372624cb79 (patch)
tree0e49b2e5d81facb55fb8b08228abeb218a27d466 /sw-ui/src/sagas/lobby.ts
parentRemove GRADLE_METADATA feature to avoid breaking frontend build (diff)
downloadseven-wonders-2382a452456e4bdef4584e1046925e372624cb79.tar.gz
seven-wonders-2382a452456e4bdef4584e1046925e372624cb79.tar.bz2
seven-wonders-2382a452456e4bdef4584e1046925e372624cb79.zip
Rationalize module names
Diffstat (limited to 'sw-ui/src/sagas/lobby.ts')
-rw-r--r--sw-ui/src/sagas/lobby.ts44
1 files changed, 44 insertions, 0 deletions
diff --git a/sw-ui/src/sagas/lobby.ts b/sw-ui/src/sagas/lobby.ts
new file mode 100644
index 00000000..09360b02
--- /dev/null
+++ b/sw-ui/src/sagas/lobby.ts
@@ -0,0 +1,44 @@
+import { push } from 'react-router-redux';
+import { Channel, eventChannel, SagaIterator } from 'redux-saga';
+import { all, apply, call, put, take } from 'redux-saga/effects';
+import { SevenWondersSession } from '../api/sevenWondersApi';
+import { actions as gameActions, ENTER_LOBBY, REQUEST_START_GAME } from '../redux/actions/lobby';
+
+function* watchLobbyUpdates(session: SevenWondersSession, lobbyId: number): any {
+ const lobbyUpdatesChannel: Channel<Object> = yield eventChannel(session.watchLobbyUpdated(lobbyId));
+ try {
+ while (true) {
+ const lobby = yield take(lobbyUpdatesChannel);
+ yield put(gameActions.updateGames([lobby]));
+ }
+ } finally {
+ yield apply(lobbyUpdatesChannel, lobbyUpdatesChannel.close);
+ }
+}
+
+function* watchGameStart(session: SevenWondersSession, lobbyId: number): any {
+ const gameStartedChannel = yield eventChannel(session.watchGameStarted(lobbyId));
+ try {
+ yield take(gameStartedChannel);
+ yield put(gameActions.enterGame(lobbyId));
+ yield put(push(`/game/${lobbyId}`));
+ } finally {
+ yield apply(gameStartedChannel, gameStartedChannel.close);
+ }
+}
+
+function* startGame(session: SevenWondersSession): SagaIterator {
+ while (true) {
+ yield take(REQUEST_START_GAME);
+ yield apply(session, session.startGame);
+ }
+}
+
+export function* lobbySaga(session: SevenWondersSession): SagaIterator {
+ const { gameId } = yield take(ENTER_LOBBY);
+ yield all([
+ call(watchLobbyUpdates, session, gameId),
+ call(watchGameStart, session, gameId),
+ call(startGame, session)
+ ]);
+}
bgstack15