summaryrefslogtreecommitdiff
path: root/frontend/src/redux/players.js
blob: 85b579f330b68e0465071193ad6a6373b065512b (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
import PlayerState, { Player } from '../models/players';

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,
  }),
};

export default (state = new PlayerState(), 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;
  }
};

export const getCurrentPlayer = players => players.all.get(players.current, new Player({ displayName: '[ERROR]' }));
export const getPlayer = (players, username) => players.all.get(username);
export const getPlayers = (players, usernames) => usernames.map(u => getPlayer(players, u));
bgstack15