diff options
author | jbion <joffrey.bion@amadeus.com> | 2019-02-22 23:09:40 +0100 |
---|---|---|
committer | jbion <joffrey.bion@amadeus.com> | 2019-02-22 23:09:40 +0100 |
commit | 27990fcba5a47252df617db1bc98e2276b326e7e (patch) | |
tree | 7fba886ae83379ee9d76e6540efae7b0e7ce2a25 /frontend/src/redux | |
parent | Fix websocket value when no message body is received (diff) | |
download | seven-wonders-27990fcba5a47252df617db1bc98e2276b326e7e.tar.gz seven-wonders-27990fcba5a47252df617db1bc98e2276b326e7e.tar.bz2 seven-wonders-27990fcba5a47252df617db1bc98e2276b326e7e.zip |
Implement Game start
Diffstat (limited to 'frontend/src/redux')
-rw-r--r-- | frontend/src/redux/actions/all.js | 5 | ||||
-rw-r--r-- | frontend/src/redux/actions/game.js | 25 | ||||
-rw-r--r-- | frontend/src/redux/actions/lobby.js | 35 | ||||
-rw-r--r-- | frontend/src/redux/actions/players.js | 29 | ||||
-rw-r--r-- | frontend/src/redux/currentGame.js | 33 | ||||
-rw-r--r-- | frontend/src/redux/games.js | 42 | ||||
-rw-r--r-- | frontend/src/redux/players.js | 35 |
7 files changed, 137 insertions, 67 deletions
diff --git a/frontend/src/redux/actions/all.js b/frontend/src/redux/actions/all.js new file mode 100644 index 00000000..45d3ab7a --- /dev/null +++ b/frontend/src/redux/actions/all.js @@ -0,0 +1,5 @@ +import type { GameAction } from './game'; +import type { LobbyAction } from './lobby'; +import type { PlayerAction } from './players'; + +export type Action = PlayerAction | LobbyAction | GameAction diff --git a/frontend/src/redux/actions/game.js b/frontend/src/redux/actions/game.js new file mode 100644 index 00000000..1fe49dfb --- /dev/null +++ b/frontend/src/redux/actions/game.js @@ -0,0 +1,25 @@ +import type { ApiPlayerTurnInfo, ApiPreparedCard, ApiTable } from '../../api/model'; + +export const types = { + REQUEST_SAY_READY: 'GAME/REQUEST_SAY_READY', + PLAYER_READY_RECEIVED: 'GAME/PLAYER_READY_RECEIVED', + TABLE_UPDATE_RECEIVED: 'GAME/TABLE_UPDATE_RECEIVED', + PREPARED_CARD_RECEIVED: 'GAME/PREPARED_CARD_RECEIVED', + TURN_INFO_RECEIVED: 'GAME/TURN_INFO_RECEIVED', +}; + +export type SayReadyAction = { type: 'GAME/REQUEST_SAY_READY' }; +export type PlayerReadyEvent = { type: 'GAME/PLAYER_READY_RECEIVED', username: string }; +export type TableUpdateEvent = { type: 'GAME/TABLE_UPDATE_RECEIVED', table: ApiTable }; +export type PreparedCardEvent = { type: 'GAME/PREPARED_CARD_RECEIVED', card: ApiPreparedCard }; +export type TurnInfoEvent = { type: 'GAME/TURN_INFO_RECEIVED', turnInfo: ApiPlayerTurnInfo }; + +export type GameAction = SayReadyAction | PlayerReadyEvent | TableUpdateEvent | PreparedCardEvent | TurnInfoEvent; + +export const actions = { + sayReady: () => ({ type: types.REQUEST_SAY_READY }), + receivePlayerReady: (username: string) => ({ type: types.PLAYER_READY_RECEIVED, username }), + receiveTableUpdate: (table: ApiTable) => ({ type: types.TABLE_UPDATE_RECEIVED, table }), + receivePreparedCard: (card: ApiPreparedCard) => ({ type: types.PREPARED_CARD_RECEIVED, card }), + receiveTurnInfo: (turnInfo: ApiPlayerTurnInfo) => ({ type: types.TURN_INFO_RECEIVED, turnInfo }), +}; diff --git a/frontend/src/redux/actions/lobby.js b/frontend/src/redux/actions/lobby.js new file mode 100644 index 00000000..b3151a23 --- /dev/null +++ b/frontend/src/redux/actions/lobby.js @@ -0,0 +1,35 @@ +import { fromJS } from 'immutable'; +import type { GameMapType, GameNormalMapType } from '../../models/games'; + +export const types = { + UPDATE_GAMES: 'GAMES/UPDATE_GAMES', + REQUEST_CREATE_GAME: 'GAMES/REQUEST_CREATE_GAME', + REQUEST_JOIN_GAME: 'GAMES/REQUEST_JOIN_GAME', + REQUEST_START_GAME: 'GAMES/REQUEST_START_GAME', + ENTER_LOBBY: 'GAMES/ENTER_LOBBY', + ENTER_GAME: 'GAMES/ENTER_GAME', +}; + +export type UpdateGamesAction = { type: 'GAMES/UPDATE_GAMES', games: GameMapType }; +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' }; +export type EnterLobbyAction = { type: 'GAMES/ENTER_LOBBY', gameId: number }; +export type EnterGameAction = { type: 'GAMES/ENTER_GAME', gameId: number }; + +export type LobbyAction = + | UpdateGamesAction + | RequestCreateGameAction + | RequestJoinGameAction + | RequestStartGameAction + | EnterLobbyAction + | EnterGameAction; + +export const actions = { + updateGames: (games: GameNormalMapType): UpdateGamesAction => ({ type: types.UPDATE_GAMES, games: fromJS(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 }), + enterLobby: (gameId: number): EnterLobbyAction => ({ type: types.ENTER_LOBBY, gameId }), + enterGame: (gameId: number): EnterGameAction => ({ type: types.ENTER_GAME, gameId }), +}; diff --git a/frontend/src/redux/actions/players.js b/frontend/src/redux/actions/players.js new file mode 100644 index 00000000..7df174c4 --- /dev/null +++ b/frontend/src/redux/actions/players.js @@ -0,0 +1,29 @@ +import { Map } from 'immutable'; +import { PlayerShape } from '../../models/players'; + +export const types = { + REQUEST_CHOOSE_USERNAME: 'USER/REQUEST_CHOOSE_USERNAME', + SET_CURRENT_PLAYER: 'USER/SET_CURRENT_PLAYER', + UPDATE_PLAYERS: 'USER/UPDATE_PLAYERS', +}; + +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 PlayerAction = RequestChooseUsernameAction | SetCurrentPlayerAction | UpdatePlayersAction; + +export const actions = { + chooseUsername: (username: string): RequestChooseUsernameAction => ({ + type: types.REQUEST_CHOOSE_USERNAME, + username, + }), + setCurrentPlayer: (player: PlayerShape): SetCurrentPlayerAction => ({ + type: types.SET_CURRENT_PLAYER, + player, + }), + updatePlayers: (players: Map<string, PlayerShape>): UpdatePlayersAction => ({ + type: types.UPDATE_PLAYERS, + players, + }), +}; diff --git a/frontend/src/redux/currentGame.js b/frontend/src/redux/currentGame.js new file mode 100644 index 00000000..cefabb6f --- /dev/null +++ b/frontend/src/redux/currentGame.js @@ -0,0 +1,33 @@ +// @flow +import type { ApiPlayerTurnInfo } from '../api/model'; +import { CurrentGameState } from '../models/currentGame'; +import type { Action } from './actions/all'; +import { types } from './actions/game'; + +export const currentGameReducer = (state: CurrentGameState = new CurrentGameState(), action: Action) => { + switch (action.type) { + case types.REQUEST_SAY_READY: + // TODO handle end of feedback between say ready and ready event received + return state; + case types.PLAYER_READY_RECEIVED: + // const newReadiness = state.playersReadiness.set(action.username, true); + // return { playersReadiness: newReadiness, ...state }; + return state; + case types.TABLE_UPDATE_RECEIVED: + // TODO + return state; + case types.PREPARED_CARD_RECEIVED: + // TODO + return state; + case types.TURN_INFO_RECEIVED: + // TODO find a better way to just update what's needed + const newState = new CurrentGameState(); + newState.turnInfo = action.turnInfo; + newState.playersReadiness = state.playersReadiness; + return newState; + default: + return state; + } +}; + +export const getCurrentTurnInfo = (state: CurrentGameState): ApiPlayerTurnInfo => state.turnInfo; diff --git a/frontend/src/redux/games.js b/frontend/src/redux/games.js index ee8b13df..68571981 100644 --- a/frontend/src/redux/games.js +++ b/frontend/src/redux/games.js @@ -1,48 +1,16 @@ // @flow import type { List, Map } from 'immutable'; -import { fromJS } from 'immutable'; -import type { Game, GameMapType, GameNormalMapType, GameShape } from '../models/games'; +import type { Game } from '../models/games'; import { GamesState } from '../models/games'; +import type { Action } from './actions/all'; +import { types } from './actions/lobby'; -export const types = { - UPDATE_GAMES: 'GAMES/UPDATE_GAMES', - REQUEST_CREATE_GAME: 'GAMES/REQUEST_CREATE_GAME', - REQUEST_JOIN_GAME: 'GAMES/REQUEST_JOIN_GAME', - REQUEST_START_GAME: 'GAMES/REQUEST_START_GAME', - ENTER_LOBBY: 'GAMES/ENTER_LOBBY', - ENTER_GAME: 'GAMES/ENTER_GAME', -}; - -export type UpdateGamesAction = { type: 'GAMES/UPDATE_GAMES', games: GameMapType }; -export type RequestCreateGameAction = { type: 'GAMES/REQUEST_CREATE_GAME', gameName: string }; -export type RequestJoinGameAction = { type: 'GAMES/REQUEST_JOIN_GAME', gameId: string }; -export type RequestStartGameAction = { type: 'GAMES/REQUEST_START_GAME' }; -export type EnterLobbyAction = { type: 'GAMES/ENTER_LOBBY', lobby: GameShape }; -export type EnterGameAction = { type: 'GAMES/ENTER_GAME' }; - -export type GamesAction = - | UpdateGamesAction - | RequestCreateGameAction - | RequestJoinGameAction - | RequestStartGameAction - | EnterLobbyAction - | EnterGameAction; - -export const actions = { - updateGames: (games: GameNormalMapType): UpdateGamesAction => ({ type: types.UPDATE_GAMES, games: fromJS(games) }), - requestJoinGame: (gameId: string): RequestJoinGameAction => ({ type: types.REQUEST_JOIN_GAME, gameId }), - requestCreateGame: (gameName: string): RequestCreateGameAction => ({ type: types.REQUEST_CREATE_GAME, gameName }), - requestStartGame: (): RequestStartGameAction => ({ type: types.REQUEST_START_GAME }), - enterLobby: (lobby: GameShape): EnterLobbyAction => ({ type: types.ENTER_LOBBY, lobby: fromJS(lobby) }), - enterGame: (): EnterGameAction => ({ type: types.ENTER_GAME }), -}; - -export const gamesReducer = (state: GamesState = new GamesState(), action: GamesAction) => { +export const gamesReducer = (state: GamesState = new GamesState(), action: Action) => { switch (action.type) { case types.UPDATE_GAMES: return state.addGames(action.games); case types.ENTER_LOBBY: - return state.set('current', action.lobby.get('id')); + return state.set('current', action.gameId); default: return state; } diff --git a/frontend/src/redux/players.js b/frontend/src/redux/players.js index b9f37c8c..ce3c305f 100644 --- a/frontend/src/redux/players.js +++ b/frontend/src/redux/players.js @@ -1,34 +1,9 @@ -import { Map } from 'immutable'; -import { Player, PlayerShape, PlayerState } from '../models/players'; +import { List } from 'immutable'; +import { Player, PlayerState } from '../models/players'; +import type { Action } from './actions/all'; +import { types } from './actions/players'; -export const types = { - REQUEST_CHOOSE_USERNAME: 'USER/REQUEST_CHOOSE_USERNAME', - SET_CURRENT_PLAYER: 'USER/SET_CURRENT_PLAYER', - UPDATE_PLAYERS: 'USER/UPDATE_PLAYERS', -}; - -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 PlayerAction = RequestChooseUsernameAction | SetCurrentPlayerAction | UpdatePlayersAction; - -export const actions = { - chooseUsername: (username: string): RequestChooseUsernameAction => ({ - type: types.REQUEST_CHOOSE_USERNAME, - username, - }), - setCurrentPlayer: (player: PlayerShape): SetCurrentPlayerAction => ({ - type: types.SET_CURRENT_PLAYER, - player, - }), - updatePlayers: (players: Map<string, PlayerShape>): UpdatePlayersAction => ({ - type: types.UPDATE_PLAYERS, - players, - }), -}; - -export const playersReducer = (state = new PlayerState(), action: PlayerAction) => { +export const playersReducer = (state = new PlayerState(), action: Action) => { switch (action.type) { case types.SET_CURRENT_PLAYER: return state.addPlayer(action.player); |