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
48
49
50
|
import { Map } from 'immutable';
import { Player, PlayerShape, PlayerState } 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 type RequestChooseUsernameAction = { type: types.REQUEST_CHOOSE_USERNAME, username: string };
export type SetCurrentPlayerAction = { type: types.SET_CURRENT_PLAYER, player: PlayerShape };
export type UpdatePlayersAction = { type: types.UPDATE_PLAYERS, players: Map<string, PlayerShape> };
export type PlayerAction = RequestChooseUsernameAction | SetCurrentPlayerAction | UpdatePlayersAction;
export const actions = {
chooseUsername: (username: string): RequestChooseUsernameAction => ({
type: types.REQUEST_CHOOSE_USERNAME,
username,
}),
setCurrentPlayer: (player: PlayerShape): SetCurrentPlayerAction => ({
type: types.SET_CURRENT_PLAYER,
player,
}),
updatePlayers: (players: Map<string, PlayerShape>): UpdatePlayersAction => ({
type: types.UPDATE_PLAYERS,
players,
}),
};
export const playersReducer = (state = new PlayerState(), action: PlayerAction) => {
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));
|