Skip to content

Commit

Permalink
Merge pull request #253 from I3uckwheat/hotfix/remove-played-white-ca…
Browse files Browse the repository at this point in the history
…rds-from-player-hands

Remove a players submitted card when submitted
  • Loading branch information
I3uckwheat authored Apr 24, 2021
2 parents a9d44d6 + 8e80b41 commit f444fc8
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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: {} });
Expand Down
32 changes: 29 additions & 3 deletions frontend/src/contexts/HostContext/HostReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function playerConnected(state, { playerId, playerName }) {
};
}

function updatePlayerCards(state, { selectedCards, playerId }) {
function playerSubmitCards(state, { selectedCards, playerId }) {
const newState = {
...state,
players: {
Expand All @@ -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(
Expand Down Expand Up @@ -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);
Expand All @@ -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':
Expand Down Expand Up @@ -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);

Expand Down
27 changes: 27 additions & 0 deletions frontend/src/contexts/HostContext/HostReducer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit f444fc8

Please sign in to comment.