summaryrefslogtreecommitdiff
path: root/frontend/src/redux/currentGame.js
blob: f4174eca587db40796f88273f8f39f03bd7213ab (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
// @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,
  });
}

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) => {
  if (action.type === types.TURN_INFO_RECEIVED) {
    return action.turnInfo;
  } else {
    return state;
  }
};

const tableUpdatesReducer = (state: ApiTable, action: Action) => {
  if (action.type === types.TABLE_UPDATE_RECEIVED) {
    return action.table;
  } else {
    return state;
  }
};

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