summaryrefslogtreecommitdiff
path: root/frontend/src/sagas/lobby.js
blob: b0f52d5c74eb1ff86030f5fa627a9868a25b0dff (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
45
46
47
48
49
50
51
52
// @flow
import { normalize } from 'normalizr';
import { push } from 'react-router-redux';
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/actions/lobby';
import { actions as playerActions } from '../redux/actions/players';
import { game as gameSchema } from '../schemas/games';

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);
      // 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, lobbyId: number): SagaIterator {
  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(types.REQUEST_START_GAME);
    yield apply(session, session.startGame, []);
  }
}

export function* lobbySaga(session: SevenWondersSession): SagaIterator {
  const { gameId } = yield take(types.ENTER_LOBBY);
  yield all([
    call(watchLobbyUpdates, session, gameId),
    call(watchGameStart, session, gameId),
    call(startGame, session)
  ]);
}
bgstack15