summaryrefslogtreecommitdiff
path: root/frontend/src/sagas/lobby.js
diff options
context:
space:
mode:
authorjbion <joffrey.bion@amadeus.com>2019-02-22 23:09:40 +0100
committerjbion <joffrey.bion@amadeus.com>2019-02-22 23:09:40 +0100
commit27990fcba5a47252df617db1bc98e2276b326e7e (patch)
tree7fba886ae83379ee9d76e6540efae7b0e7ce2a25 /frontend/src/sagas/lobby.js
parentFix websocket value when no message body is received (diff)
downloadseven-wonders-27990fcba5a47252df617db1bc98e2276b326e7e.tar.gz
seven-wonders-27990fcba5a47252df617db1bc98e2276b326e7e.tar.bz2
seven-wonders-27990fcba5a47252df617db1bc98e2276b326e7e.zip
Implement Game start
Diffstat (limited to 'frontend/src/sagas/lobby.js')
-rw-r--r--frontend/src/sagas/lobby.js33
1 files changed, 16 insertions, 17 deletions
diff --git a/frontend/src/sagas/lobby.js b/frontend/src/sagas/lobby.js
index c87f6ad5..b0f52d5c 100644
--- a/frontend/src/sagas/lobby.js
+++ b/frontend/src/sagas/lobby.js
@@ -5,37 +5,31 @@ import type { Channel, SagaIterator } from 'redux-saga';
import { eventChannel } from 'redux-saga';
import { all, apply, call, put, take } from 'redux-saga/effects';
import { SevenWondersSession } from '../api/sevenWondersApi';
-import { actions as gameActions, types } from '../redux/games';
-import { actions as playerActions } from '../redux/players';
+import { actions as gameActions, types } from '../redux/actions/lobby';
+import { actions as playerActions } from '../redux/actions/players';
import { game as gameSchema } from '../schemas/games';
-function getCurrentGameId(): number {
- const path = window.location.pathname;
- return path.split('lobby/')[1];
-}
-
-function* watchLobbyUpdates(session: SevenWondersSession): SagaIterator {
- const currentGameId: number = getCurrentGameId();
- const lobbyUpdatesChannel: Channel = yield eventChannel(session.watchLobbyUpdated(currentGameId));
+function* watchLobbyUpdates(session: SevenWondersSession, lobbyId: number): SagaIterator {
+ const lobbyUpdatesChannel: Channel = yield eventChannel(session.watchLobbyUpdated(lobbyId));
try {
while (true) {
const lobby = yield take(lobbyUpdatesChannel);
const normalized = normalize(lobby, gameSchema);
- yield put(gameActions.updateGames(normalized.entities.games));
+ // players update needs to be first, otherwise the UI cannot find the player in the list
yield put(playerActions.updatePlayers(normalized.entities.players));
+ yield put(gameActions.updateGames(normalized.entities.games));
}
} finally {
yield apply(lobbyUpdatesChannel, lobbyUpdatesChannel.close);
}
}
-function* watchGameStart(session: SevenWondersSession): SagaIterator {
- const currentGameId = getCurrentGameId();
- const gameStartedChannel = yield eventChannel(session.watchGameStarted(currentGameId));
+function* watchGameStart(session: SevenWondersSession, lobbyId: number): SagaIterator {
+ const gameStartedChannel = yield eventChannel(session.watchGameStarted(lobbyId));
try {
yield take(gameStartedChannel);
- yield put(gameActions.enterGame());
- yield put(push('/game'));
+ yield put(gameActions.enterGame(lobbyId));
+ yield put(push(`/game/${lobbyId}`));
} finally {
yield apply(gameStartedChannel, gameStartedChannel.close);
}
@@ -49,5 +43,10 @@ function* startGame(session: SevenWondersSession): SagaIterator {
}
export function* lobbySaga(session: SevenWondersSession): SagaIterator {
- yield all([call(watchLobbyUpdates, session), call(watchGameStart, session), call(startGame, session)]);
+ const { gameId } = yield take(types.ENTER_LOBBY);
+ yield all([
+ call(watchLobbyUpdates, session, gameId),
+ call(watchGameStart, session, gameId),
+ call(startGame, session)
+ ]);
}
bgstack15