summaryrefslogtreecommitdiff
path: root/frontend/src/redux/currentGame.js
blob: e5659195c8ee4deabee899b5812eccdb3f57a0cc (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
// @flow
import { List } from 'immutable';
import { combineReducers } from 'redux';
import type { ApiPlayerTurnInfo, ApiTable } from '../api/model';
import { CurrentGameState } from '../models/currentGame';
import type { Action } from './actions/all';
import { types } from './actions/game';

export function createCurrentGameReducer() {
  return combineReducers({
    readyUsernames: readyUsernamesReducer,
    turnInfo: turnInfoReducer,
    table: tableUpdatesReducer,
  });
}

const readyUsernamesReducer = (state: List<string> = new List(), action: Action) => {
  if (action.type === types.PLAYER_READY_RECEIVED) {
    return state.push(action.username);
  } else {
    return state;
  }
};

const turnInfoReducer = (state: ApiPlayerTurnInfo | null = null, action: Action) => {
  switch (action.type) {
    case types.TURN_INFO_RECEIVED:
      return action.turnInfo;
    case types.TABLE_UPDATE_RECEIVED:
      return null;
    default:
      return state;
  }
};

const tableUpdatesReducer = (state: ApiTable | null = null, action: Action) => {
  switch (action.type) {
    case types.TURN_INFO_RECEIVED:
      return action.turnInfo.table;
    case types.TABLE_UPDATE_RECEIVED:
      return action.table;
    default:
      return state;
  }
};

export const getCurrentTurnInfo = (state: CurrentGameState): ApiPlayerTurnInfo => state.turnInfo;
bgstack15