-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(app-board): connect game board to state
- Loading branch information
Showing
13 changed files
with
229 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import firebase from 'firebase/app'; | ||
import 'firebase/firestore'; | ||
import { CardEntity, FirebaseCollection } from '@pipeline/common'; | ||
|
||
export default async function loadCardsForDeck(deckId: string): Promise<CardEntity[]> { | ||
const cards = await firebase.firestore().collection(FirebaseCollection.Cards).where('deckId', '==', deckId).get(); | ||
|
||
return cards.docs.map(d => ({ id: d.id, ...d.data() } as CardEntity)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import firebase from 'firebase/app'; | ||
import 'firebase/firestore'; | ||
import { FirebaseCollection, Game } from '@pipeline/common'; | ||
|
||
export default async function loadGame(gameId: string): Promise<Game> { | ||
const cardDoc = await firebase.firestore().collection(FirebaseCollection.Games).doc(gameId).get(); | ||
|
||
return cardDoc.data() as Game; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 5 additions & 2 deletions
7
packages/game-app/src/gameView/components/DraggableCard/DraggableCard.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
packages/game-app/src/gameView/hooks/useCardEventHandler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { useCallback } from 'react'; | ||
import { GameEvent, GameEventType } from '../types/gameEvents'; | ||
import { useDispatch } from 'react-redux'; | ||
import { actions } from '../slice'; | ||
|
||
export default function useCardEventHandler() { | ||
const dispatch = useDispatch(); | ||
|
||
const onCardEvent = useCallback( | ||
(event: GameEvent) => { | ||
if (event.type === GameEventType.CardMovingEnd) { | ||
const { cardId, target, position } = event; | ||
dispatch(actions.updateCardPosition({ cardId, position, target })); | ||
} | ||
}, | ||
[dispatch], | ||
); | ||
|
||
return { | ||
onCardEvent, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { useDispatch, useSelector } from 'react-redux'; | ||
import { selectors, actions } from '../slice'; | ||
import { useEffect } from 'react'; | ||
|
||
export default function useGameState(currentGame: string) { | ||
const placedCardsIds = useSelector(selectors.getPlacedCards); | ||
const deckCardsIds = useSelector(selectors.getDeckCardsIds); | ||
const selectedGameId = useSelector(selectors.getSelectedGameId); | ||
|
||
const dispatch = useDispatch(); | ||
|
||
useEffect(() => { | ||
if ((!placedCardsIds && !deckCardsIds) || selectedGameId !== currentGame) { | ||
dispatch(actions.loadGame(currentGame)); | ||
} | ||
}, [placedCardsIds, deckCardsIds, dispatch, currentGame, selectedGameId]); | ||
|
||
return { | ||
placedCardsIds: placedCardsIds || [], | ||
deckCardsIds: deckCardsIds || [], | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import { all } from 'redux-saga/effects'; | ||
import loadGards from './loadCards'; | ||
import loadCards from './loadCards'; | ||
import loadGame from './loadGame'; | ||
|
||
export default function* gameSaga() { | ||
yield all([loadGards()]); | ||
yield all([loadCards(), loadGame()]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { call, put, takeEvery } from 'redux-saga/effects'; | ||
import { actions, GameState } from '../slice'; | ||
import { addRequestStatusManagement } from '@pipeline/requests-status'; | ||
import { CardEntity, Game } from '@pipeline/common'; | ||
import loadGame from '../apis/callLoadGame'; | ||
import loadCardsForDeck from '../apis/callLoadCardsForDeck'; | ||
|
||
function* executeLoadGame(action: ReturnType<typeof actions.loadGame>) { | ||
const game: Game = yield call(loadGame, action.payload); | ||
|
||
const cards: CardEntity[] = yield call(loadCardsForDeck, game.deckId); | ||
yield put(actions.saveCards(cards)); | ||
// TODO load actual game state from firestore | ||
const gameState: GameState = { | ||
boardCards: [], | ||
deckCards: cards.map(c => c.id), | ||
cardsState: {}, | ||
}; | ||
yield put(actions.setInitialGameState({ state: gameState, gameId: action.payload })); | ||
} | ||
|
||
export default function* loadGameSaga() { | ||
yield takeEvery(actions.loadGame, addRequestStatusManagement(executeLoadGame, 'game.loadGame')); | ||
} |
Oops, something went wrong.