diff --git a/frontend/src/components/screens/HostWinnerScreen/HostWinnerScreen.js b/frontend/src/components/screens/HostWinnerScreen/HostWinnerScreen.js index 8781d8f1..8030b4a9 100644 --- a/frontend/src/components/screens/HostWinnerScreen/HostWinnerScreen.js +++ b/frontend/src/components/screens/HostWinnerScreen/HostWinnerScreen.js @@ -133,6 +133,8 @@ function RightPanel() { } function startNextRound(dispatch) { + dispatch({ type: 'REMOVE_SUBMITTED_CARDS_FROM_PLAYER', payload: {} }); + dispatch({ type: 'SET_NEXT_CZAR', payload: {} }); dispatch({ type: 'SELECT_BLACK_CARD', payload: {} }); diff --git a/frontend/src/contexts/HostContext/HostReducer.js b/frontend/src/contexts/HostContext/HostReducer.js index ca5e0970..2c0ed776 100644 --- a/frontend/src/contexts/HostContext/HostReducer.js +++ b/frontend/src/contexts/HostContext/HostReducer.js @@ -38,7 +38,7 @@ function playerConnected(state, { playerId, playerName }) { }; } -function updatePlayerCards(state, { selectedCards, playerId }) { +function playerSubmitCards(state, { selectedCards, playerId }) { const newState = { ...state, players: { @@ -63,6 +63,30 @@ function updatePlayerCards(state, { selectedCards, playerId }) { }; } +function removeSubmittedCards(state) { + const { players, playerIDs } = state; + const newPlayers = playerIDs.reduce((acc, playerId) => { + const player = players[playerId]; + + const newCards = player.cards?.filter( + (card, index) => !player.submittedCards?.includes(index), + ); + + return { + ...acc, + [playerId]: { + ...player, + cards: newCards, + }, + }; + }, {}); + + return { + ...state, + players: newPlayers, + }; +} + function removePlayer(state, { playerId }) { // Removes the value playerId from the original playerIDs array const newPlayerIds = state.playerIDs.filter( @@ -305,7 +329,7 @@ function HostReducer(state, action) { return removePlayer(state, payload); case 'PLAYER_SUBMIT': - return updatePlayerCards(state, payload); + return playerSubmitCards(state, payload); case 'KICK_PLAYER': return removePlayer(state, payload); @@ -317,7 +341,6 @@ function HostReducer(state, action) { return previewWinner(state, payload); case 'SELECT_WINNER': - // TODO: HANDLE PAYLOAD AND TEST return selectWinner(state, payload); case 'SET_LOBBY_ID': @@ -347,6 +370,9 @@ function HostReducer(state, action) { case 'DEAL_WHITE_CARDS': return dealWhiteCards(state); + case 'REMOVE_SUBMITTED_CARDS_FROM_PLAYER': + return removeSubmittedCards(state); + case 'SHUFFLE_JOIN_CODE': return getJoinCode(state); diff --git a/frontend/src/contexts/HostContext/HostReducer.spec.js b/frontend/src/contexts/HostContext/HostReducer.spec.js index 160e703f..2ce2ff75 100644 --- a/frontend/src/contexts/HostContext/HostReducer.spec.js +++ b/frontend/src/contexts/HostContext/HostReducer.spec.js @@ -878,6 +878,33 @@ describe('reducer', () => { }); }); + describe('REMOVE_SUBMITTED_CARDS_FROM_PLAYER', () => { + it('removes the players submitted cards', () => { + const state = { + players: { + bar: { + submittedCards: [], + cards: [{ text: 'test' }, { text: 'test' }, { text: 'test' }], + }, + + baz: { + submittedCards: [0], + cards: [{ text: 'test1' }, { text: 'test2' }, { text: 'test3' }], + }, + }, + + playerIDs: ['bar', 'baz'], + }; + + const result = HostReducer(state, { + type: 'REMOVE_SUBMITTED_CARDS_FROM_PLAYER', + payload: {}, + }); + + expect(result.players.baz.cards).not.toContain({ text: 'test1' }); + }); + }); + describe('SELECT_BLACK_CARD', () => { it('sets a random black card and removes it from the deck', () => { const state = {