summaryrefslogtreecommitdiff
path: root/frontend/src/redux
diff options
context:
space:
mode:
Diffstat (limited to 'frontend/src/redux')
-rw-r--r--frontend/src/redux/games.js38
-rw-r--r--frontend/src/redux/players.js57
2 files changed, 58 insertions, 37 deletions
diff --git a/frontend/src/redux/games.js b/frontend/src/redux/games.js
index 4115323c..5ad6f8d2 100644
--- a/frontend/src/redux/games.js
+++ b/frontend/src/redux/games.js
@@ -1,29 +1,39 @@
-import { Map } from 'immutable'
+import {fromJS} from 'immutable'
export const types = {
- CREATE_OR_UPDATE_GAMES: 'GAME/CREATE_OR_UPDATE_GAMES',
- ENTER_GAME: 'GAME/ENTER_GAME',
- JOIN_GAME: 'GAME/JOIN_GAME',
- CREATE_GAME: 'GAME/CREATE_GAME',
+ UPDATE_GAMES: 'GAME/UPDATE_GAMES',
+ REQUEST_CREATE_GAME: 'GAME/REQUEST_CREATE_GAME',
+ REQUEST_JOIN_GAME: 'GAME/REQUEST_JOIN_GAME',
+ ENTER_LOBBY: 'GAME/ENTER_LOBBY',
}
export const actions = {
- createOrUpdateGame: (games) => ({ type: types.CREATE_OR_UPDATE_GAMES, games }),
- enterGame: (username) => ({ type: types.ENTER_GAME, username }),
- joinGame: (id) => ({ type: types.JOIN_GAME, id }),
- createGame: (name) => ({ type: types.CREATE_GAME, name }),
+ updateGames: (games) => ({ type: types.UPDATE_GAMES, games }),
+ requestJoinGame: (id) => ({ type: types.REQUEST_JOIN_GAME, id }),
+ requestCreateGame: (name) => ({ type: types.REQUEST_CREATE_GAME, name }),
+ enterLobby: (lobby) => ({ type: types.ENTER_LOBBY, lobby }),
}
-
-const initialState = Map({})
+const initialState = fromJS({
+ all: {},
+ current: ''
+})
export default (state = initialState, action) => {
switch (action.type) {
- case types.CREATE_OR_UPDATE_GAMES:
- return state.mergeDeep(action.games)
+ case types.UPDATE_GAMES:
+ return state.setIn(['all'], state.get('all').mergeDeep(action.games))
+ case types.ENTER_LOBBY:
+ return state.set('current', action.lobby.get('id'))
default:
return state
}
}
-export const getAllGames = state => state.get('games')
+const getState = globalState => globalState.get('games')
+
+export const getAllGamesById = globalState => getState(globalState).get('all')
+export const getAllGames = globalState => getAllGamesById(globalState).toList()
+export const getGame = (globalState, id) => getAllGamesById(globalState).get(id)
+export const getCurrentGameId = globalState => getState(globalState).get('current')
+export const getCurrentGame = globalState => getGame(globalState, getCurrentGameId(globalState))
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