summaryrefslogtreecommitdiff
path: root/frontend/src/sagas
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 /frontend/src/sagas
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 'frontend/src/sagas')
-rw-r--r--frontend/src/sagas/errors.ts36
-rw-r--r--frontend/src/sagas/game.ts81
-rw-r--r--frontend/src/sagas/gameBrowser.ts55
-rw-r--r--frontend/src/sagas/home.ts28
-rw-r--r--frontend/src/sagas/lobby.ts44
5 files changed, 0 insertions, 244 deletions
diff --git a/frontend/src/sagas/errors.ts b/frontend/src/sagas/errors.ts
deleted file mode 100644
index b27dfa95..00000000
--- a/frontend/src/sagas/errors.ts
+++ /dev/null
@@ -1,36 +0,0 @@
-import {Toaster} from '@blueprintjs/core';
-import {Channel, eventChannel} from 'redux-saga';
-import {apply, cancelled, take} from 'redux-saga/effects';
-import {ApiError} from '../api/model';
-import {SevenWondersSession} from '../api/sevenWondersApi';
-
-const ErrorToaster = Toaster.create();
-
-export function* errorHandlingSaga(session: SevenWondersSession): any {
- const errorChannel: Channel<ApiError> = yield eventChannel(session.watchErrors());
- try {
- while (true) {
- const error: ApiError = yield take(errorChannel);
- yield* handleOneError(error);
- }
- } finally {
- if (yield cancelled()) {
- console.log('Error management saga cancelled');
- yield apply(errorChannel, errorChannel.close);
- }
- }
-}
-
-function* handleOneError(err: ApiError): any {
- console.error('Error received on web socket channel', err);
- const msg = buildMsg(err);
- yield apply(ErrorToaster, ErrorToaster.show, [{ intent: 'danger', icon: 'error', message: msg }]);
-}
-
-function buildMsg(err: ApiError): string {
- if (err.details.length > 0) {
- return err.details.map(d => d.message).join('\n');
- } else {
- return err.message;
- }
-}
diff --git a/frontend/src/sagas/game.ts b/frontend/src/sagas/game.ts
deleted file mode 100644
index a60ab2d3..00000000
--- a/frontend/src/sagas/game.ts
+++ /dev/null
@@ -1,81 +0,0 @@
-import { eventChannel, SagaIterator } from 'redux-saga';
-import { apply, call, put, take } from 'redux-saga/effects';
-import { ApiPlayerTurnInfo, ApiPreparedCard, ApiTable } from '../api/model';
-import { SevenWondersSession } from '../api/sevenWondersApi';
-import { actions, REQUEST_PREPARE_MOVE, REQUEST_SAY_READY } from '../redux/actions/game';
-import { ENTER_GAME } 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): SagaIterator {
- while (true) {
- yield take(REQUEST_SAY_READY);
- yield apply(session, session.sayReady);
- }
-}
-
-function* prepareMove(session: SevenWondersSession): SagaIterator {
- while (true) {
- let action = yield take(REQUEST_PREPARE_MOVE);
- yield apply(session, session.prepareMove, [action.move]);
- }
-}
-
-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): SagaIterator {
- const { gameId } = yield take(ENTER_GAME);
- console.log('Entered game!', gameId);
- yield [
- call(watchPlayerReady, session, gameId),
- call(watchTableUpdates, session, gameId),
- call(watchPreparedCards, session, gameId),
- call(sayReady, session),
- call(prepareMove, session),
- call(watchTurnInfo, session)
- ];
-}
diff --git a/frontend/src/sagas/gameBrowser.ts b/frontend/src/sagas/gameBrowser.ts
deleted file mode 100644
index 868ec471..00000000
--- a/frontend/src/sagas/gameBrowser.ts
+++ /dev/null
@@ -1,55 +0,0 @@
-import { push } from 'react-router-redux';
-import { eventChannel, SagaIterator } from 'redux-saga';
-import { all, apply, call, put, take } from 'redux-saga/effects';
-import { ApiLobby } from '../api/model';
-import { SevenWondersSession } from '../api/sevenWondersApi';
-import { actions as gameActions, REQUEST_CREATE_GAME, REQUEST_JOIN_GAME } from '../redux/actions/lobby';
-
-function* watchGames(session: SevenWondersSession): any {
- const gamesChannel = yield eventChannel(session.watchGames());
- try {
- while (true) {
- const gameList = yield take(gamesChannel);
- yield put(gameActions.updateGames(gameList));
- }
- } finally {
- yield apply(gamesChannel, gamesChannel.close);
- }
-}
-
-function* watchLobbyJoined(session: SevenWondersSession): any {
- const joinedLobbyChannel = yield eventChannel(session.watchLobbyJoined());
- try {
- const joinedLobby: ApiLobby = yield take(joinedLobbyChannel);
- yield put(gameActions.updateGames([joinedLobby]));
- yield put(gameActions.enterLobby(joinedLobby.id));
- yield put(push(`/lobby/${joinedLobby.id}`));
- } finally {
- yield apply(joinedLobbyChannel, joinedLobbyChannel.close);
- }
-}
-
-function* createGame(session: SevenWondersSession): SagaIterator {
- while (true) {
- const { gameName } = yield take(REQUEST_CREATE_GAME);
- // $FlowFixMe
- yield apply(session, session.createGame, [gameName]);
- }
-}
-
-function* joinGame(session: SevenWondersSession): SagaIterator {
- while (true) {
- const { gameId } = yield take(REQUEST_JOIN_GAME);
- // $FlowFixMe
- yield apply(session, session.joinGame, [gameId]);
- }
-}
-
-export function* gameBrowserSaga(session: SevenWondersSession): SagaIterator {
- yield all([
- call(watchGames, session),
- call(watchLobbyJoined, session),
- call(createGame, session),
- call(joinGame, session),
- ]);
-}
diff --git a/frontend/src/sagas/home.ts b/frontend/src/sagas/home.ts
deleted file mode 100644
index 585c536e..00000000
--- a/frontend/src/sagas/home.ts
+++ /dev/null
@@ -1,28 +0,0 @@
-import { push } from 'react-router-redux';
-import { eventChannel, SagaIterator } from 'redux-saga';
-import { all, apply, call, put, take } from 'redux-saga/effects';
-import { ApiPlayer } from '../api/model';
-import { SevenWondersSession } from '../api/sevenWondersApi';
-import { actions, REQUEST_CHOOSE_USERNAME } from '../redux/actions/user';
-
-function* sendUsername(session: SevenWondersSession): SagaIterator {
- while (true) {
- const { username } = yield take(REQUEST_CHOOSE_USERNAME);
- // $FlowFixMe
- yield apply(session, session.chooseName, [username]);
- }
-}
-
-function* validateUsername(session: SevenWondersSession): any {
- const usernameChannel = yield eventChannel(session.watchNameChoice());
- while (true) {
- const user: ApiPlayer = yield take(usernameChannel);
- yield put(actions.setCurrentPlayer(user));
- yield apply(usernameChannel, usernameChannel.close);
- yield put(push('/games'));
- }
-}
-
-export function* homeSaga(session: SevenWondersSession): SagaIterator {
- yield all([call(sendUsername, session), call(validateUsername, session)]);
-}
diff --git a/frontend/src/sagas/lobby.ts b/frontend/src/sagas/lobby.ts
deleted file mode 100644
index 09360b02..00000000
--- a/frontend/src/sagas/lobby.ts
+++ /dev/null
@@ -1,44 +0,0 @@
-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