summaryrefslogtreecommitdiff
path: root/frontend/src/redux/players.js
blob: db9f1416b9648274d177cf49be219dc36fb1667e (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
import Immutable from 'seamless-immutable';

export const types = {
  REQUEST_CHOOSE_USERNAME: 'USER/REQUEST_CHOOSE_USERNAME',
  SET_CURRENT_PLAYER: 'USER/SET_CURRENT_PLAYER',
  UPDATE_PLAYERS: 'USER/UPDATE_PLAYERS',
};

export const actions = {
  chooseUsername: username => ({
    type: types.REQUEST_CHOOSE_USERNAME,
    username,
  }),
  setCurrentPlayer: player => ({
    type: types.SET_CURRENT_PLAYER,
    player,
  }),
  updatePlayers: players => ({
    type: types.UPDATE_PLAYERS,
    players,
  }),
};

const initialState = Immutable.from({
  all: {},
  current: '',
});

export default (state = initialState, action) => {
  switch (action.type) {
    case types.SET_CURRENT_PLAYER:
      const player = action.player;
      const withNewPlayer = state.setIn(['all', player.username], player);
      return Immutable.set(withNewPlayer, 'current', player.username);
    case types.UPDATE_PLAYERS:
      return Immutable.merge(state, { all: action.players }, { deep: true });
    default:
      return state;
  }
};

export const getCurrentPlayer = state =>
  state.players.all && state.players.all[state.players.current];
export const getPlayer = (state, username) => state.players.all[username];
export const getPlayers = (state, usernames) =>
  usernames.map(u => getPlayer(state, u));
bgstack15