summaryrefslogtreecommitdiff
path: root/frontend/src/redux/games.js
blob: 2b9a92a4522c9057909e460f38d5ddbb5fbac2cf (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
38
39
import Immutable from 'seamless-immutable'

export const types = {
  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 = {
  updateGames: (games) => ({ type: types.UPDATE_GAMES, games: Immutable(games) }),
  requestJoinGame: (gameId) => ({ type: types.REQUEST_JOIN_GAME, gameId }),
  requestCreateGame: (gameName) => ({ type: types.REQUEST_CREATE_GAME, gameName }),
  enterLobby: (lobby) => ({ type: types.ENTER_LOBBY, lobby: Immutable(lobby) }),
}

const initialState = Immutable.from({
  all: {},
  current: ''
})

export default (state = initialState, action) => {
  switch (action.type) {
    case types.UPDATE_GAMES:
      return Immutable.merge(state, {all: action.games}, {deep: true})
    case types.ENTER_LOBBY:
      return state.set('current', action.lobby.id)
    default:
      return state
  }
}

export const getAllGamesById = state => state.games.all
export const getAllGames = state => {
  let gamesById = getAllGamesById(state)
  return Object.keys(gamesById).map(k => gamesById[k]);
}
export const getGame = (state, id) => getAllGamesById(state)[id]
export const getCurrentGame = state => getGame(state, state.games.current)
bgstack15