summaryrefslogtreecommitdiff
path: root/frontend/src/sagas
diff options
context:
space:
mode:
Diffstat (limited to 'frontend/src/sagas')
-rw-r--r--frontend/src/sagas/game.js74
-rw-r--r--frontend/src/sagas/gameBrowser.js7
-rw-r--r--frontend/src/sagas/home.js3
-rw-r--r--frontend/src/sagas/lobby.js33
4 files changed, 96 insertions, 21 deletions
diff --git a/frontend/src/sagas/game.js b/frontend/src/sagas/game.js
new file mode 100644
index 00000000..04209e91
--- /dev/null
+++ b/frontend/src/sagas/game.js
@@ -0,0 +1,74 @@
+import { eventChannel } from 'redux-saga';
+import { apply, call, put, take } from 'redux-saga/effects';
+import type { ApiPlayerTurnInfo, ApiPreparedCard, ApiTable } from '../api/model';
+import { SevenWondersSession } from '../api/sevenWondersApi';
+import { actions } from '../redux/actions/game';
+import { types } from '../redux/actions/game';
+import { types as gameTypes } from '../redux/actions/lobby';
+
+function* watchPlayerReady(session: SevenWondersSession, gameId: number) {
+ const channel = yield eventChannel(session.watchPlayerReady(gameId));
+ try {
+ while (true) {
+ const username = yield take(channel);
+ yield put(actions.receivePlayerReady(username));
+ }
+ } finally {
+ yield apply(channel, channel.close);
+ }
+}
+
+function* watchTableUpdates(session: SevenWondersSession, gameId: number) {
+ const channel = yield eventChannel(session.watchTableUpdates(gameId));
+ try {
+ while (true) {
+ const table: ApiTable = yield take(channel);
+ yield put(actions.receiveTableUpdate(table));
+ }
+ } finally {
+ yield apply(channel, channel.close);
+ }
+}
+
+function* watchPreparedCards(session: SevenWondersSession, gameId: number) {
+ const channel = yield eventChannel(session.watchPreparedCards(gameId));
+ try {
+ while (true) {
+ const preparedCard: ApiPreparedCard = yield take(channel);
+ yield put(actions.receivePreparedCard(preparedCard));
+ }
+ } finally {
+ yield apply(channel, channel.close);
+ }
+}
+
+function* sayReady(session: SevenWondersSession) {
+ while (true) {
+ yield take(types.REQUEST_SAY_READY);
+ yield apply(session, session.sayReady);
+ }
+}
+
+function* watchTurnInfo(session: SevenWondersSession) {
+ const channel = yield eventChannel(session.watchTurnInfo());
+ try {
+ while (true) {
+ const turnInfo: ApiPlayerTurnInfo = yield take(channel);
+ yield put(actions.receiveTurnInfo(turnInfo));
+ }
+ } finally {
+ yield apply(channel, channel.close);
+ }
+}
+
+export function* gameSaga(session: SevenWondersSession) {
+ const { gameId } = yield take(gameTypes.ENTER_GAME);
+ console.log('Entered game!', gameId);
+ yield [
+ call(watchPlayerReady, session, gameId),
+ call(watchTableUpdates, session, gameId),
+ call(watchPreparedCards, session, gameId),
+ call(sayReady, session),
+ call(watchTurnInfo, session)
+ ];
+}
diff --git a/frontend/src/sagas/gameBrowser.js b/frontend/src/sagas/gameBrowser.js
index 7cd45667..062603a3 100644
--- a/frontend/src/sagas/gameBrowser.js
+++ b/frontend/src/sagas/gameBrowser.js
@@ -5,8 +5,9 @@ import type { SagaIterator } from 'redux-saga';
import { eventChannel } from 'redux-saga';
import { all, apply, call, put, take } from 'redux-saga/effects';
import type { SevenWondersSession } from '../api/sevenWondersApi';
-import { actions as gameActions, types } from '../redux/games';
-import { actions as playerActions } from '../redux/players';
+import { actions as gameActions } from '../redux/actions/lobby';
+import { types } from '../redux/actions/lobby';
+import { actions as playerActions } from '../redux/actions/players';
import { game as gameSchema, gameList as gameListSchema } from '../schemas/games';
function* watchGames(session: SevenWondersSession): SagaIterator {
@@ -32,7 +33,7 @@ function* watchLobbyJoined(session: SevenWondersSession): SagaIterator {
const gameId = normalized.result;
yield put(playerActions.updatePlayers(normalized.entities.players));
yield put(gameActions.updateGames(normalized.entities.games));
- yield put(gameActions.enterLobby(normalized.entities.games[gameId]));
+ yield put(gameActions.enterLobby(gameId));
yield put(push(`/lobby/${gameId}`));
} finally {
yield apply(joinedLobbyChannel, joinedLobbyChannel.close);
diff --git a/frontend/src/sagas/home.js b/frontend/src/sagas/home.js
index 328102fb..705a7a40 100644
--- a/frontend/src/sagas/home.js
+++ b/frontend/src/sagas/home.js
@@ -5,7 +5,8 @@ import { eventChannel } from 'redux-saga';
import { all, apply, call, put, take } from 'redux-saga/effects';
import type { ApiPlayer } from '../api/model';
import type { SevenWondersSession } from '../api/sevenWondersApi';
-import { actions, types } from '../redux/players';
+import { actions } from '../redux/actions/players';
+import { types } from '../redux/actions/players';
function* sendUsername(session: SevenWondersSession): SagaIterator {
while (true) {
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