summaryrefslogtreecommitdiff
path: root/frontend/src/redux/players.js
blob: ce3c305fdbdf2ff840ffd8ee9458f9b10e24ba3d (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
import { List } from 'immutable';
import { Player, PlayerState } from '../models/players';
import type { Action } from './actions/all';
import { types } from './actions/players';

export const playersReducer = (state = new PlayerState(), action: Action) => {
  switch (action.type) {
    case types.SET_CURRENT_PLAYER:
      return state.addPlayer(action.player);
    case types.UPDATE_PLAYERS:
      return state.addPlayers(action.players);
    default:
      return state;
  }
};

const ANONYMOUS = new Player({displayName: '[NOT LOGGED]'});

export function getCurrentPlayer(state): Player {
  const players = state.get('players');
  return getPlayer(players, players.current, ANONYMOUS);
}

export const getPlayer = (players, username, defaultPlayer): ?Player => players.all.get(username, defaultPlayer);
export const getPlayers = (players, usernames): List<Player> => usernames.map(u => getPlayer(players, u, undefined));
bgstack15