diff options
Diffstat (limited to 'sw-ui/src/redux')
-rw-r--r-- | sw-ui/src/redux/actions/all.ts | 5 | ||||
-rw-r--r-- | sw-ui/src/redux/actions/game.ts | 32 | ||||
-rw-r--r-- | sw-ui/src/redux/actions/lobby.ts | 32 | ||||
-rw-r--r-- | sw-ui/src/redux/actions/user.ts | 17 | ||||
-rw-r--r-- | sw-ui/src/redux/currentGame.ts | 46 | ||||
-rw-r--r-- | sw-ui/src/redux/games.ts | 56 | ||||
-rw-r--r-- | sw-ui/src/redux/user.ts | 43 |
7 files changed, 0 insertions, 231 deletions
diff --git a/sw-ui/src/redux/actions/all.ts b/sw-ui/src/redux/actions/all.ts deleted file mode 100644 index 57d2a443..00000000 --- a/sw-ui/src/redux/actions/all.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { GameAction } from './game'; -import { LobbyAction } from './lobby'; -import { PlayerAction } from './user'; - -export type Action = PlayerAction | LobbyAction | GameAction diff --git a/sw-ui/src/redux/actions/game.ts b/sw-ui/src/redux/actions/game.ts deleted file mode 100644 index b67ea1dc..00000000 --- a/sw-ui/src/redux/actions/game.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { ApiPlayerMove, ApiPlayerTurnInfo, ApiPreparedCard, ApiTable } from '../../api/model'; - -export const REQUEST_SAY_READY = 'GAME/REQUEST_SAY_READY'; -export const REQUEST_PREPARE_MOVE = 'GAME/REQUEST_PREPARE_MOVE'; -export const PLAYER_READY_RECEIVED = 'GAME/PLAYER_READY_RECEIVED'; -export const TABLE_UPDATE_RECEIVED = 'GAME/TABLE_UPDATE_RECEIVED'; -export const PREPARED_CARD_RECEIVED = 'GAME/PREPARED_CARD_RECEIVED'; -export const TURN_INFO_RECEIVED = 'GAME/TURN_INFO_RECEIVED'; - -export type SayReadyAction = { type: typeof REQUEST_SAY_READY }; -export type PrepareMoveAction = { type: typeof REQUEST_PREPARE_MOVE, move: ApiPlayerMove }; -export type PlayerReadyEvent = { type: typeof PLAYER_READY_RECEIVED, username: string }; -export type TableUpdateEvent = { type: typeof TABLE_UPDATE_RECEIVED, table: ApiTable }; -export type PreparedCardEvent = { type: typeof PREPARED_CARD_RECEIVED, card: ApiPreparedCard }; -export type TurnInfoEvent = { type: typeof TURN_INFO_RECEIVED, turnInfo: ApiPlayerTurnInfo }; - -export type GameAction = - SayReadyAction - | PrepareMoveAction - | PlayerReadyEvent - | TableUpdateEvent - | PreparedCardEvent - | TurnInfoEvent; - -export const actions = { - sayReady: () => ({ type: REQUEST_SAY_READY }), - prepareMove: (move: ApiPlayerMove) => ({ type: REQUEST_PREPARE_MOVE, move }), - receivePlayerReady: (username: string) => ({ type: PLAYER_READY_RECEIVED, username }), - receiveTableUpdate: (table: ApiTable) => ({ type: TABLE_UPDATE_RECEIVED, table }), - receivePreparedCard: (card: ApiPreparedCard) => ({ type: PREPARED_CARD_RECEIVED, card }), - receiveTurnInfo: (turnInfo: ApiPlayerTurnInfo) => ({ type: TURN_INFO_RECEIVED, turnInfo }), -}; diff --git a/sw-ui/src/redux/actions/lobby.ts b/sw-ui/src/redux/actions/lobby.ts deleted file mode 100644 index c121b022..00000000 --- a/sw-ui/src/redux/actions/lobby.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { ApiLobby } from '../../api/model'; - -export const UPDATE_GAMES = 'GAMES/UPDATE_GAMES'; -export const REQUEST_CREATE_GAME = 'GAMES/REQUEST_CREATE_GAME'; -export const REQUEST_JOIN_GAME = 'GAMES/REQUEST_JOIN_GAME'; -export const REQUEST_START_GAME = 'GAMES/REQUEST_START_GAME'; -export const ENTER_LOBBY = 'GAMES/ENTER_LOBBY'; -export const ENTER_GAME = 'GAMES/ENTER_GAME'; - -export type UpdateGamesAction = { type: typeof UPDATE_GAMES, games: ApiLobby[]}; -export type RequestCreateGameAction = { type: typeof REQUEST_CREATE_GAME, gameName: string }; -export type RequestJoinGameAction = { type: typeof REQUEST_JOIN_GAME, gameId: number }; -export type RequestStartGameAction = { type: typeof REQUEST_START_GAME }; -export type EnterLobbyAction = { type: typeof ENTER_LOBBY, gameId: number }; -export type EnterGameAction = { type: typeof ENTER_GAME, gameId: number }; - -export type LobbyAction = - | UpdateGamesAction - | RequestCreateGameAction - | RequestJoinGameAction - | RequestStartGameAction - | EnterLobbyAction - | EnterGameAction; - -export const actions = { - updateGames: (games: ApiLobby[]): UpdateGamesAction => ({ type: UPDATE_GAMES, games }), - requestJoinGame: (gameId: number): RequestJoinGameAction => ({ type: REQUEST_JOIN_GAME, gameId }), - requestCreateGame: (gameName: string): RequestCreateGameAction => ({ type: REQUEST_CREATE_GAME, gameName }), - requestStartGame: (): RequestStartGameAction => ({ type: REQUEST_START_GAME }), - enterLobby: (gameId: number): EnterLobbyAction => ({ type: ENTER_LOBBY, gameId }), - enterGame: (gameId: number): EnterGameAction => ({ type: ENTER_GAME, gameId }), -}; diff --git a/sw-ui/src/redux/actions/user.ts b/sw-ui/src/redux/actions/user.ts deleted file mode 100644 index 29c85707..00000000 --- a/sw-ui/src/redux/actions/user.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { Map } from 'immutable'; -import { ApiPlayer } from '../../api/model'; - -export const REQUEST_CHOOSE_USERNAME = 'USER/REQUEST_CHOOSE_USERNAME'; -export const SET_CURRENT_PLAYER = 'USER/SET_CURRENT_PLAYER'; -export const UPDATE_PLAYERS = 'USER/UPDATE_PLAYERS'; - -export type RequestChooseUsernameAction = { type: typeof REQUEST_CHOOSE_USERNAME, username: string }; -export type SetCurrentPlayerAction = { type: typeof SET_CURRENT_PLAYER, player: ApiPlayer }; -export type UpdatePlayersAction = { type: typeof UPDATE_PLAYERS, players: Map<string, ApiPlayer> }; - -export type PlayerAction = RequestChooseUsernameAction | SetCurrentPlayerAction | UpdatePlayersAction; - -export const actions = { - chooseUsername: (username: string): RequestChooseUsernameAction => ({ type: REQUEST_CHOOSE_USERNAME, username }), - setCurrentPlayer: (player: ApiPlayer): SetCurrentPlayerAction => ({ type: SET_CURRENT_PLAYER, player }), -}; diff --git a/sw-ui/src/redux/currentGame.ts b/sw-ui/src/redux/currentGame.ts deleted file mode 100644 index 5e015d60..00000000 --- a/sw-ui/src/redux/currentGame.ts +++ /dev/null @@ -1,46 +0,0 @@ -import { combineReducers } from 'redux'; -import { ApiPlayerTurnInfo, ApiTable } from '../api/model'; -import { GlobalState } from '../reducers'; -import { Action } from './actions/all'; -import { TABLE_UPDATE_RECEIVED, TURN_INFO_RECEIVED } from './actions/game'; - -export type CurrentGameState = { - turnInfo: ApiPlayerTurnInfo | null; - table: ApiTable | null; -} - -export const EMPTY_CURRENT_GAME: CurrentGameState = { - turnInfo: null, - table: null, -}; - -export function createCurrentGameReducer() { - return combineReducers({ - turnInfo: turnInfoReducer, - table: tableUpdatesReducer, - }); -} - -const turnInfoReducer = (state: ApiPlayerTurnInfo | null = null, action: Action) => { - switch (action.type) { - case TURN_INFO_RECEIVED: - return action.turnInfo; - case TABLE_UPDATE_RECEIVED: - return null; - default: - return state; - } -}; - -const tableUpdatesReducer = (state: ApiTable | null = null, action: Action) => { - switch (action.type) { - case TURN_INFO_RECEIVED: - return action.turnInfo.table; - case TABLE_UPDATE_RECEIVED: - return action.table; - default: - return state; - } -}; - -export const getCurrentTurnInfo = (state: GlobalState): ApiPlayerTurnInfo | null => state.currentGame.turnInfo; diff --git a/sw-ui/src/redux/games.ts b/sw-ui/src/redux/games.ts deleted file mode 100644 index 4df2f1da..00000000 --- a/sw-ui/src/redux/games.ts +++ /dev/null @@ -1,56 +0,0 @@ -import { List, Map } from 'immutable'; -import { combineReducers } from 'redux'; -import { ApiLobby } from '../api/model'; -import { GlobalState } from '../reducers'; -import { Action } from './actions/all'; -import { ENTER_LOBBY, UPDATE_GAMES } from './actions/lobby'; - -export type GamesState = { - all: Map<string, ApiLobby>, - current: string | null -}; - -export const EMPTY_GAMES: GamesState = { - all: Map(), - current: null, -}; - -export const createGamesReducer = () => { - return combineReducers({ - all: allGamesReducer, - current: currentGameIdReducer - }) -}; - -export const allGamesReducer = (state: Map<string, ApiLobby> = Map(), action: Action) => { - switch (action.type) { - case UPDATE_GAMES: - const newGames = mapify(action.games); - return state.merge(newGames); - default: - return state; - } -}; - -function mapify(games: ApiLobby[]): Map<string, ApiLobby> { - let newGames: {[id:string]:ApiLobby} = {}; - games.forEach(g => newGames[`${g.id}`] = g); - return Map(newGames); -} - -export const currentGameIdReducer = (state: string | null = null, action: Action) => { - switch (action.type) { - case ENTER_LOBBY: - return `${action.gameId}`; - default: - return state; - } -}; - -export const getAllGames = (state: GlobalState): List<ApiLobby> => state.games.all.toList(); -export const getCurrentGame = (state: GlobalState): ApiLobby | null => { - if (state.games.current == null) { - return null; - } - return state.games.all.get(state.games.current) || null; -}; diff --git a/sw-ui/src/redux/user.ts b/sw-ui/src/redux/user.ts deleted file mode 100644 index 2cc25cc0..00000000 --- a/sw-ui/src/redux/user.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { ApiPlayer } from '../api/model'; -import { GlobalState } from '../reducers'; -import { Action } from './actions/all'; -import { SET_CURRENT_PLAYER } from './actions/user'; -import { getCurrentGame } from './games'; - -export type User = { - username: string, - displayName: string, -} - -export const currentUserReducer = (state: User | null = null, action: Action) => { - switch (action.type) { - case SET_CURRENT_PLAYER: - return { - username: action.player.username, - displayName: action.player.displayName - }; - default: - return state; - } -}; - -export function getCurrentUser(state: GlobalState): User | null { - return state.currentUser -} - -export function getCurrentPlayer(state: GlobalState): ApiPlayer | null { - if (state.currentUser == null) { - return null; - } - let game = getCurrentGame(state); - if (game == null) { - return null; - } - for (let i = 0; i < game.players.length; i++) { - let player = game.players[i]; - if (player.username === state.currentUser.username) { - return player; - } - } - return null; -} |