diff options
author | jbion <joffrey.bion@amadeus.com> | 2019-02-27 03:12:55 +0100 |
---|---|---|
committer | jbion <joffrey.bion@amadeus.com> | 2019-02-27 03:12:55 +0100 |
commit | 8d73d21108738754efd07b63ecc7368fd49502fa (patch) | |
tree | 8755dd1aa9c61e9f473fd6787ba8f3595006f0fa /frontend/src/redux | |
parent | Remove unnecessary Jackson annotation on non-DTOs (diff) | |
download | seven-wonders-8d73d21108738754efd07b63ecc7368fd49502fa.tar.gz seven-wonders-8d73d21108738754efd07b63ecc7368fd49502fa.tar.bz2 seven-wonders-8d73d21108738754efd07b63ecc7368fd49502fa.zip |
Simplify state and reducers
Diffstat (limited to 'frontend/src/redux')
-rw-r--r-- | frontend/src/redux/actions/all.js | 2 | ||||
-rw-r--r-- | frontend/src/redux/actions/lobby.js | 7 | ||||
-rw-r--r-- | frontend/src/redux/actions/user.js (renamed from frontend/src/redux/actions/players.js) | 14 | ||||
-rw-r--r-- | frontend/src/redux/currentGame.js | 19 | ||||
-rw-r--r-- | frontend/src/redux/games.js | 40 | ||||
-rw-r--r-- | frontend/src/redux/players.js | 25 | ||||
-rw-r--r-- | frontend/src/redux/user.js | 37 |
7 files changed, 83 insertions, 61 deletions
diff --git a/frontend/src/redux/actions/all.js b/frontend/src/redux/actions/all.js index 45d3ab7a..12522819 100644 --- a/frontend/src/redux/actions/all.js +++ b/frontend/src/redux/actions/all.js @@ -1,5 +1,5 @@ import type { GameAction } from './game'; import type { LobbyAction } from './lobby'; -import type { PlayerAction } from './players'; +import type { PlayerAction } from './user'; export type Action = PlayerAction | LobbyAction | GameAction diff --git a/frontend/src/redux/actions/lobby.js b/frontend/src/redux/actions/lobby.js index b3151a23..8768ec80 100644 --- a/frontend/src/redux/actions/lobby.js +++ b/frontend/src/redux/actions/lobby.js @@ -1,5 +1,4 @@ -import { fromJS } from 'immutable'; -import type { GameMapType, GameNormalMapType } from '../../models/games'; +import type { ApiLobby } from '../../api/model'; export const types = { UPDATE_GAMES: 'GAMES/UPDATE_GAMES', @@ -10,7 +9,7 @@ export const types = { ENTER_GAME: 'GAMES/ENTER_GAME', }; -export type UpdateGamesAction = { type: 'GAMES/UPDATE_GAMES', games: GameMapType }; +export type UpdateGamesAction = { type: 'GAMES/UPDATE_GAMES', games: ApiLobby[]}; export type RequestCreateGameAction = { type: 'GAMES/REQUEST_CREATE_GAME', gameName: string }; export type RequestJoinGameAction = { type: 'GAMES/REQUEST_JOIN_GAME', gameId: number }; export type RequestStartGameAction = { type: 'GAMES/REQUEST_START_GAME' }; @@ -26,7 +25,7 @@ export type LobbyAction = | EnterGameAction; export const actions = { - updateGames: (games: GameNormalMapType): UpdateGamesAction => ({ type: types.UPDATE_GAMES, games: fromJS(games) }), + updateGames: (games: ApiLobby[]): UpdateGamesAction => ({ type: types.UPDATE_GAMES, games }), requestJoinGame: (gameId: number): RequestJoinGameAction => ({ type: types.REQUEST_JOIN_GAME, gameId }), requestCreateGame: (gameName: string): RequestCreateGameAction => ({ type: types.REQUEST_CREATE_GAME, gameName }), requestStartGame: (): RequestStartGameAction => ({ type: types.REQUEST_START_GAME }), diff --git a/frontend/src/redux/actions/players.js b/frontend/src/redux/actions/user.js index 7df174c4..4406230b 100644 --- a/frontend/src/redux/actions/players.js +++ b/frontend/src/redux/actions/user.js @@ -1,5 +1,5 @@ import { Map } from 'immutable'; -import { PlayerShape } from '../../models/players'; +import type { ApiPlayer } from '../../api/model'; export const types = { REQUEST_CHOOSE_USERNAME: 'USER/REQUEST_CHOOSE_USERNAME', @@ -8,8 +8,8 @@ export const types = { }; 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 SetCurrentPlayerAction = { type: types.SET_CURRENT_PLAYER, player: ApiPlayer }; +export type UpdatePlayersAction = { type: types.UPDATE_PLAYERS, players: Map<string, ApiPlayer> }; export type PlayerAction = RequestChooseUsernameAction | SetCurrentPlayerAction | UpdatePlayersAction; @@ -18,12 +18,8 @@ export const actions = { type: types.REQUEST_CHOOSE_USERNAME, username, }), - setCurrentPlayer: (player: PlayerShape): SetCurrentPlayerAction => ({ + setCurrentPlayer: (player: ApiPlayer): SetCurrentPlayerAction => ({ type: types.SET_CURRENT_PLAYER, - player, - }), - updatePlayers: (players: Map<string, PlayerShape>): UpdatePlayersAction => ({ - type: types.UPDATE_PLAYERS, - players, + player: player, }), }; diff --git a/frontend/src/redux/currentGame.js b/frontend/src/redux/currentGame.js index e5659195..e315a1e8 100644 --- a/frontend/src/redux/currentGame.js +++ b/frontend/src/redux/currentGame.js @@ -1,27 +1,22 @@ // @flow -import { List } from 'immutable'; import { combineReducers } from 'redux'; import type { ApiPlayerTurnInfo, ApiTable } from '../api/model'; -import { CurrentGameState } from '../models/currentGame'; +import type { GlobalState } from '../reducers'; import type { Action } from './actions/all'; import { types } from './actions/game'; +export type CurrentGameState = { + turnInfo: ApiPlayerTurnInfo | null; + table: ApiTable | null; +} + export function createCurrentGameReducer() { return combineReducers({ - readyUsernames: readyUsernamesReducer, turnInfo: turnInfoReducer, table: tableUpdatesReducer, }); } -const readyUsernamesReducer = (state: List<string> = new List(), action: Action) => { - if (action.type === types.PLAYER_READY_RECEIVED) { - return state.push(action.username); - } else { - return state; - } -}; - const turnInfoReducer = (state: ApiPlayerTurnInfo | null = null, action: Action) => { switch (action.type) { case types.TURN_INFO_RECEIVED: @@ -44,4 +39,4 @@ const tableUpdatesReducer = (state: ApiTable | null = null, action: Action) => { } }; -export const getCurrentTurnInfo = (state: CurrentGameState): ApiPlayerTurnInfo => state.turnInfo; +export const getCurrentTurnInfo = (state: GlobalState): ApiPlayerTurnInfo => state.currentGame.turnInfo; diff --git a/frontend/src/redux/games.js b/frontend/src/redux/games.js index 68571981..f5543a76 100644 --- a/frontend/src/redux/games.js +++ b/frontend/src/redux/games.js @@ -1,22 +1,42 @@ // @flow -import type { List, Map } from 'immutable'; -import type { Game } from '../models/games'; -import { GamesState } from '../models/games'; +import { List, Map } from 'immutable'; +import { combineReducers } from 'redux'; +import type { ApiLobby } from '../api/model'; +import type { GlobalState } from '../reducers'; import type { Action } from './actions/all'; import { types } from './actions/lobby'; -export const gamesReducer = (state: GamesState = new GamesState(), action: Action) => { +export type GamesState = { + all: Map<string, ApiLobby>, + current: string | void +}; + +export const createGamesReducer = () => { + return combineReducers({ + all: allGamesReducer, + current: currentGameIdReducer + }) +}; + +export const allGamesReducer = (state: Map<string, ApiLobby> = Map(), action: Action) => { switch (action.type) { case types.UPDATE_GAMES: - return state.addGames(action.games); + let newGames = {}; + action.games.forEach(g => newGames[g.id] = g); + return state.merge(Map(newGames)); + default: + return state; + } +}; + +export const currentGameIdReducer = (state: string | void = null, action: Action) => { + switch (action.type) { case types.ENTER_LOBBY: - return state.set('current', action.gameId); + return `${action.gameId}`; default: return state; } }; -export const getAllGamesById = (games: GamesState): Map<string, Game> => games.all; -export const getAllGames = (games: GamesState): List<Game> => getAllGamesById(games).toList(); -export const getGame = (games: GamesState, id: string | number): Game => getAllGamesById(games).get(`${id}`); -export const getCurrentGame = (games: GamesState): Game => getGame(games, games.current); +export const getAllGames = (state: GlobalState): List<ApiLobby> => state.games.all.toList(); +export const getCurrentGame = (state: GlobalState): ApiLobby | null => state.games.all.get(state.games.current); diff --git a/frontend/src/redux/players.js b/frontend/src/redux/players.js deleted file mode 100644 index ce3c305f..00000000 --- a/frontend/src/redux/players.js +++ /dev/null @@ -1,25 +0,0 @@ -import { List } from 'immutable'; -import { Player, PlayerState } from '../models/players'; -import type { Action } from './actions/all'; -import { types } from './actions/players'; - -export const playersReducer = (state = new PlayerState(), action: Action) => { - 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)); diff --git a/frontend/src/redux/user.js b/frontend/src/redux/user.js new file mode 100644 index 00000000..f2247b38 --- /dev/null +++ b/frontend/src/redux/user.js @@ -0,0 +1,37 @@ +import { ApiPlayer } from '../api/model'; +import type { GlobalState } from '../reducers'; +import type { Action } from './actions/all'; +import { types } from './actions/user'; +import { getCurrentGame } from './games'; + +export type User = { + username: string, + displayName: string, +} + +export const currentUserReducer = (state: ?User = null, action: Action) => { + switch (action.type) { + case types.SET_CURRENT_PLAYER: + return { + username: action.player.username, + displayName: action.player.displayName + }; + default: + return state; + } +}; + +export function getCurrentUser(state: GlobalState): ?User { + return state.currentUser +} + +export function getCurrentPlayer(state: GlobalState): ApiPlayer { + let game = getCurrentGame(state); + for (let i = 0; i < game.players.length; i++) { + let player = game.players[i]; + if (player.username === state.currentUser.username) { + return player; + } + } + return null; +} |