Skip to content

Commit

Permalink
#33 - Fix bug with totalCards not being updated if cardsInHand was empty
Browse files Browse the repository at this point in the history
  • Loading branch information
tigerclaw-az committed Oct 11, 2020
1 parent c5671e5 commit 8ce31f0
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions server/routes/modules/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,13 @@ class Player {
return Promise.reject("ERROR: Missing 'id' for player update!");
}

const pl = (await this.findPlayersWithCards(playerQuery))[0];

logger.debug('pl -> ', pl);

if (!isEmpty(data.cardsInHand)) {
if (data.addCards) {
// Get existing cards from player and merge them with the given cards
// prettier-ignore
const pl = await this.findPlayersWithCards(playerQuery);

logger.debug('pl -> ', pl);
cardsToAdd = union(data.cardsInHand, pl[0].cardsInHand);
cardsToAdd = union(data.cardsInHand, pl.cardsInHand);
} else {
cardsToAdd = data.cardsInHand;
}
Expand All @@ -70,12 +69,13 @@ class Player {

if (!isEmpty(cardsToAdd)) {
plUpdates.cardsInHand = cardsToAdd;
plUpdates.totalCards = cardsToAdd.length;
}

plUpdates.totalCards = plUpdates.cardsInHand ? plUpdates.cardsInHand.length : pl.cardsInHand.length;
logger.debug('plUpdates -> ', plUpdates);

try {
// Should not need .populate() here, it will populate ALL players cards
const doc = await this.model.findByIdAndUpdate(playerQuery._id, plUpdates, options).select('+sessionId +cardsInHand')
.populate({
path: 'cardsInHand',
Expand Down

0 comments on commit 8ce31f0

Please sign in to comment.