summaryrefslogtreecommitdiff
path: root/frontend/src/sagas/game.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/game.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/game.js')
-rw-r--r--frontend/src/sagas/game.js74
1 files changed, 74 insertions, 0 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)
+ ];
+}
bgstack15