summaryrefslogtreecommitdiff
path: root/frontend/src/sagas/lobby.ts
blob: 09360b02a0d1ea01893eadc1546f21177bc97e85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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