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
51
|
import { fromJS, Map } from '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 = fromJS({
all: {},
current: ''
})
export default (state = initialState, action) => {
switch (action.type) {
case types.SET_CURRENT_PLAYER:
const player = action.player
const username = player.get('username')
return state.setIn(['all', username], player).set('current', username)
case types.UPDATE_PLAYERS:
return state.setIn(['all'], state.get('all').mergeDeep(action.players))
default:
return state
}
}
const getState = globalState => globalState.get('players')
export const getAllPlayersByUsername = globalState => getState(globalState).get('all')
export const getAllPlayers = globalState => getAllPlayersByUsername(globalState).toList()
export const getPlayers = (globalState, usernames) => getAllPlayersByUsername(globalState)
.filter((v, k) => usernames.contains(k))
.toList()
export const getCurrentPlayerUsername = globalState => getState(globalState).get('current')
export const getCurrentPlayer = globalState => getAllPlayersByUsername(globalState)
.get(getCurrentPlayerUsername(globalState), Map())
|