summaryrefslogtreecommitdiff
path: root/frontend/src/redux/players.js
diff options
context:
space:
mode:
authorJoffrey BION <joffrey.bion@gmail.com>2017-05-11 19:47:49 +0200
committerJoffrey BION <joffrey.bion@gmail.com>2017-05-12 00:13:57 +0200
commitb494d2d141a942b0905ac46a551ff42878f0f081 (patch)
tree18d94a3e86d0749f013075a110d6c43efaa782de /frontend/src/redux/players.js
parentAdd time limit setting (diff)
downloadseven-wonders-b494d2d141a942b0905ac46a551ff42878f0f081.tar.gz
seven-wonders-b494d2d141a942b0905ac46a551ff42878f0f081.tar.bz2
seven-wonders-b494d2d141a942b0905ac46a551ff42878f0f081.zip
First attempt at lobby joining
Diffstat (limited to 'frontend/src/redux/players.js')
-rw-r--r--frontend/src/redux/players.js57
1 files changed, 34 insertions, 23 deletions
diff --git a/frontend/src/redux/players.js b/frontend/src/redux/players.js
index 967d93b6..85b5d042 100644
--- a/frontend/src/redux/players.js
+++ b/frontend/src/redux/players.js
@@ -1,23 +1,26 @@
-import { fromJS, Map } from 'immutable'
+import {fromJS, Map, Set} from 'immutable'
export const types = {
- SET_USERNAME: 'USER/SET_USERNAME',
- SET_USERNAMES: 'USER/SET_USERNAMES',
- CHOOSE_USERNAME: 'USER/CHOOSE_USERNAME'
+ REQUEST_CHOOSE_USERNAME: 'USER/REQUEST_CHOOSE_USERNAME',
+ SET_CURRENT_PLAYER: 'USER/SET_CURRENT_PLAYER',
+ UPDATE_PLAYERS: 'USER/UPDATE_PLAYERS'
}
export const actions = {
- setUsername: (username, displayName, index) => ({
- type: types.SET_USERNAME,
- username,
- index,
- displayName
+ chooseUsername: (username) => ({
+ type: types.REQUEST_CHOOSE_USERNAME,
+ username
+ }),
+ setCurrentPlayer: (player) => ({
+ type: types.SET_CURRENT_PLAYER,
+ player
+ }),
+ updatePlayers: (players) => ({
+ type: types.UPDATE_PLAYERS,
+ players
}),
- setPlayers: (players) => ({ type: types.SET_USERNAMES, players }),
- chooseUsername: (username) => ({ type: types.CHOOSE_USERNAME, username }),
}
-
const initialState = fromJS({
all: {},
current: ''
@@ -25,20 +28,28 @@ const initialState = fromJS({
export default (state = initialState, action) => {
switch (action.type) {
- case types.SET_USERNAME:
- const user = fromJS({
- username: action.username,
- displayName: action.displayName,
- index: action.index,
- })
- return state.setIn(['all', user.get('username')], user).set('current', user.get('username'))
- case types.SET_USERNAMES:
+ 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
}
}
-export const getCurrentPlayerUserName = state => state.get('players').get('current')
-export const getAllPlayers = state => state.get('players').get('all')
-export const getCurrentPlayer = (state) => getAllPlayers(state).get(getCurrentPlayerUserName(state), Map())
+const getState = globalState => globalState.get('players')
+
+function keyIn(...keys) {
+ return (v, k) => Set(keys).has(k)
+}
+
+export const getAllPlayersByUsername = globalState => getState(globalState).get('all')
+export const getAllPlayers = globalState => getAllPlayersByUsername(globalState).toList()
+export const getPlayers = (globalState, usernames) => getAllPlayersByUsername(globalState)
+ .filter(keyIn(usernames))
+ .toList()
+export const getCurrentPlayerUsername = globalState => getState(globalState).get('current')
+export const getCurrentPlayer = globalState => getAllPlayersByUsername(globalState)
+ .get(getCurrentPlayerUsername(globalState), Map())
bgstack15